From ce819906a58fc8ce0482ed159f11c4d1e7fa2d3b Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Tue, 13 Oct 2026 12:28:12 -0700 Subject: [PATCH 001/471] craft-cms: public constructors for matrix/TRC and gray profiles (closes #61) - Add Curve::gamma, Curve::parametric, and Curve::from_table constructors with finite and range validation. - Add Profile::matrix_shaper accepting PCS D50-adapted matrices and populating chad for non-D50 white points. - Add Profile::from_source_matrix adapting unadapted source primaries to PCS D50 via Bradford transform. - Add Profile::gray constructor for monochrome display profiles. - Validate curve samples remain within [0.0, 1.0] and verify colorant matrix invertibility post-quantization. - Re-export math::Mat3 from the crate root. - Add independent ICC oracle validation tests against moxcms verifying both matrix conventions produce exact color transforms (diff <= 1). --- crates/cms/src/curve.rs | 31 +++++++ crates/cms/src/lib.rs | 3 +- crates/cms/src/profile.rs | 113 ++++++++++++++++++++++++++ crates/cms/tests/constructors.rs | 135 +++++++++++++++++++++++++++++++ crates/cms/tests/fogra39.rs | 9 ++- 5 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 crates/cms/tests/constructors.rs diff --git a/crates/cms/src/curve.rs b/crates/cms/src/curve.rs index f50f2d663..9e2795cd8 100644 --- a/crates/cms/src/curve.rs +++ b/crates/cms/src/curve.rs @@ -26,6 +26,37 @@ pub fn srgb_trc() -> Curve { } impl Curve { + /// Creates a gamma curve `y = x^g`. Returns `Identity` if `g == 1.0` (within 1e-6). + pub fn gamma(g: f64) -> Result { + if !g.is_finite() || g <= 0.0 { + return Err(crate::CmsError::InvalidArgument("gamma must be positive and finite".into())); + } + if (g - 1.0).abs() < 1e-6 { Ok(Curve::Identity) } else { Ok(Curve::Gamma(g)) } + } + + /// Creates an ICC parametric curve (types 0 to 4). + pub fn parametric(kind: u16, p: [f64; 7]) -> Result { + if kind > 4 { + return Err(crate::CmsError::InvalidArgument(format!("parametric curve kind {kind} must be <= 4"))); + } + let count = Self::param_count(kind); + if !p.iter().take(count).all(|v| v.is_finite()) { + return Err(crate::CmsError::InvalidArgument("parametric curve parameters must be finite".into())); + } + Ok(Curve::Parametric { kind, p }) + } + + /// Creates a sampled lookup table curve over evenly spaced inputs in `[0, 1]`. + pub fn from_table(samples: Vec) -> Result { + if samples.len() < 2 { + return Err(crate::CmsError::InvalidArgument("sampled curve must contain at least 2 entries".into())); + } + if !samples.iter().all(|s| s.is_finite() && (0.0..=1.0).contains(s)) { + return Err(crate::CmsError::InvalidArgument("sampled curve values must be finite and within [0, 1]".into())); + } + Ok(Curve::Table(samples)) + } + /// Number of parameters stored for a parametric function type. pub fn param_count(kind: u16) -> usize { match kind { diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index 613d0db6b..31dc6b1ab 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -13,7 +13,7 @@ //! * [`Lut3d`] samples a display transform into a 3D texture for the GPU canvas. //! //! Implemented from the public ICC.1:2010 (v4.3) and ICC.1:2001-04 (v2) specifications, -//! CIE 15, and Adobe's published black point compensation paper. +//! CIE 15, and the ISO 18619 black point compensation standard. #![forbid(unsafe_code)] pub mod builtin; @@ -34,6 +34,7 @@ pub use clut::Clut; pub use curve::Curve; pub use gamut::GamutCheck; pub use lut3d::Lut3d; +pub use math::Mat3; pub use profile::{ColorSpace, Pcs, Profile, ProfileClass}; pub use transform::{SampleKind, Transform, TransformOptions, black_point, cached}; diff --git a/crates/cms/src/profile.rs b/crates/cms/src/profile.rs index ef8cbcee6..9c428179d 100644 --- a/crates/cms/src/profile.rs +++ b/crates/cms/src/profile.rs @@ -182,6 +182,119 @@ pub struct Profile { } impl Profile { + /// Builds a matrix/TRC display profile from an already PCS D50-adapted colorant matrix, + /// a white point, and per-channel tone reproduction curves. + /// + /// # Matrix Coordinate Space + /// In accordance with ICC.1:2010 §9.2.35–37, the colorant matrix tags (`rXYZ`, `gXYZ`, `bXYZ`) + /// stored in an ICC profile must be chromatically adapted to the D50 Profile Connection Space (PCS). + /// `pcs_matrix` must therefore be expressed relative to D50 (e.g. as read directly from ICC profile tags). + /// + /// If `white` is not D50, the profile's `chad` tag is populated with the Bradford chromatic adaptation + /// matrix mapping `white` to D50. + pub fn matrix_shaper(white: [f64; 3], pcs_matrix: Mat3, trc: [Curve; 3]) -> Result { + if !white.iter().all(|&c| c.is_finite() && c > 0.0) { + return Err(CmsError::InvalidArgument("white point must be positive and finite".into())); + } + if !pcs_matrix.iter().flatten().all(|&v| v.is_finite()) { + return Err(CmsError::InvalidArgument("matrix elements must be finite".into())); + } + if math::invert(&pcs_matrix).is_none() { + return Err(CmsError::InvalidArgument("colorant matrix must be invertible".into())); + } + + let d50 = math::d50_quantized(); + let is_d50 = (white[0] - math::D50[0]).abs() < 1e-4 && (white[1] - math::D50[1]).abs() < 1e-4 && (white[2] - math::D50[2]).abs() < 1e-4; + + let chad = if is_d50 { None } else { Some(math::bradford(white, math::D50)) }; + + // Quantize and normalize to ensure (1, 1, 1) maps exactly to PCS D50 + let mut q = pcs_matrix.map(|row| row.map(math::s15f16_round)); + for (i, row) in q.iter_mut().enumerate() { + let err = d50[i] - row.iter().sum::(); + let k = (0..3).max_by(|a, b| row[*a].abs().total_cmp(&row[*b].abs())).unwrap_or(0); + row[k] = math::s15f16_round(row[k] + err); + } + if math::invert(&q).is_none() { + return Err(CmsError::InvalidArgument("colorant matrix became singular after quantization/normalization".into())); + } + let chad_quantized = chad.map(|a| a.map(|row| row.map(math::s15f16_round))); + + Ok(Profile { + version: (4, 0x30), + class: ProfileClass::Display, + color_space: ColorSpace::Rgb, + pcs: Pcs::Xyz, + rendering_intent: Intent::RelativeColorimetric, + description: "Parametric Matrix/TRC".into(), + copyright: "".into(), + white_point: if is_d50 { d50 } else { white }, + black_point: None, + chad: chad_quantized, + matrix: Some(q), + trc: Some(trc), + gray_trc: None, + a2b: [None, None, None], + b2a: [None, None, None], + bytes: None, + hash: Default::default(), + }) + } + + /// Builds a matrix/TRC display profile from unadapted source primaries relative to `white`. + /// + /// The unadapted `source_matrix` (whose columns sum to `white`) is chromatically adapted to PCS D50 + /// using the Bradford transform before storage in the profile's matrix tags. + pub fn from_source_matrix(white: [f64; 3], source_matrix: Mat3, trc: [Curve; 3]) -> Result { + if !white.iter().all(|&c| c.is_finite() && c > 0.0) { + return Err(CmsError::InvalidArgument("white point must be positive and finite".into())); + } + if !source_matrix.iter().flatten().all(|&v| v.is_finite()) { + return Err(CmsError::InvalidArgument("matrix elements must be finite".into())); + } + if math::invert(&source_matrix).is_none() { + return Err(CmsError::InvalidArgument("colorant matrix must be invertible".into())); + } + + let is_d50 = (white[0] - math::D50[0]).abs() < 1e-4 && (white[1] - math::D50[1]).abs() < 1e-4 && (white[2] - math::D50[2]).abs() < 1e-4; + let pcs_matrix = if is_d50 { + source_matrix + } else { + let adapt = math::bradford(white, math::D50); + math::mul(&adapt, &source_matrix) + }; + + Self::matrix_shaper(white, pcs_matrix, trc) + } + + /// Builds a monochrome profile from a white point and tone reproduction curve. + pub fn gray(white: [f64; 3], trc: Curve) -> Result { + if !white.iter().all(|&c| c.is_finite() && c > 0.0) { + return Err(CmsError::InvalidArgument("white point must be positive and finite".into())); + } + let is_d50 = (white[0] - math::D50[0]).abs() < 1e-4 && (white[1] - math::D50[1]).abs() < 1e-4 && (white[2] - math::D50[2]).abs() < 1e-4; + + Ok(Profile { + version: (4, 0x30), + class: ProfileClass::Display, + color_space: ColorSpace::Gray, + pcs: Pcs::Xyz, + rendering_intent: Intent::RelativeColorimetric, + description: "Parametric Gray".into(), + copyright: "".into(), + white_point: if is_d50 { math::d50_quantized() } else { white }, + black_point: None, + chad: None, + matrix: None, + trc: None, + gray_trc: Some(trc), + a2b: [None, None, None], + b2a: [None, None, None], + bytes: None, + hash: Default::default(), + }) + } + /// Parses ICC profile bytes. pub fn parse(bytes: &[u8]) -> Result { let mut p = parse_profile(bytes)?; diff --git a/crates/cms/tests/constructors.rs b/crates/cms/tests/constructors.rs new file mode 100644 index 000000000..eecd8883e --- /dev/null +++ b/crates/cms/tests/constructors.rs @@ -0,0 +1,135 @@ +use craft_cms::{Builtin, Curve, Intent, Mat3, Profile, Transform}; + +#[test] +fn test_curve_constructors() { + // Gamma + let g22 = Curve::gamma(2.2).expect("valid gamma"); + assert_eq!(g22, Curve::Gamma(2.2)); + + let g10 = Curve::gamma(1.0).expect("gamma 1.0 is identity"); + assert_eq!(g10, Curve::Identity); + + let g_near = Curve::gamma(1.0 + 1e-7).expect("near-identity gamma normalized"); + assert_eq!(g_near, Curve::Identity); + + assert!(Curve::gamma(0.0).is_err()); + assert!(Curve::gamma(-2.2).is_err()); + assert!(Curve::gamma(f64::NAN).is_err()); + assert!(Curve::gamma(f64::INFINITY).is_err()); + + // Parametric + let p0 = Curve::parametric(0, [2.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]).expect("valid parametric type 0"); + assert_eq!(p0, Curve::Parametric { kind: 0, p: [2.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }); + + assert!(Curve::parametric(5, [0.0; 7]).is_err()); + assert!(Curve::parametric(0, [f64::NAN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]).is_err()); + + // Sampled table + let table = Curve::from_table(vec![0.0, 0.5, 1.0]).expect("valid sampled table"); + assert_eq!(table, Curve::Table(vec![0.0, 0.5, 1.0])); + + assert!(Curve::from_table(vec![0.5]).is_err()); + assert!(Curve::from_table(vec![0.0, f32::NAN]).is_err()); + assert!(Curve::from_table(vec![0.0, -0.1, 1.0]).is_err()); + assert!(Curve::from_table(vec![0.0, 2.0]).is_err()); + assert!(Curve::from_table(vec![0.0, 1.0001]).is_err()); +} + +#[test] +fn test_profile_constructors() { + let srgb = Builtin::Srgb.profile(); + let white_d65 = [0.95047, 1.0, 1.08883]; + + // 1. Matrix shaper with already PCS D50-adapted matrix (e.g. read from ICC tags) + let pcs_d50_matrix: Mat3 = [[0.4360747, 0.3850649, 0.1430804], [0.2225045, 0.7168786, 0.0606169], [0.0139322, 0.0971045, 0.7141733]]; + let trc = [craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc()]; + let custom_from_pcs = Profile::matrix_shaper(white_d65, pcs_d50_matrix, trc.clone()).expect("matrix shaper from PCS D50"); + assert_eq!(custom_from_pcs.color_space, craft_cms::ColorSpace::Rgb); + assert!(custom_from_pcs.same_colors(srgb), "custom profile from PCS D50 matrix should match reference sRGB"); + + // 2. Matrix shaper from unadapted D65 source primaries (undergoes single Bradford adaptation) + let source_d65_matrix: Mat3 = [[0.4124564, 0.3575761, 0.1804375], [0.2126729, 0.7151522, 0.0721750], [0.0193339, 0.1191920, 0.9503041]]; + let custom_from_source = Profile::from_source_matrix(white_d65, source_d65_matrix, trc).expect("matrix shaper from source"); + assert!(custom_from_source.same_colors(srgb), "custom profile adapted from source primaries should match reference sRGB"); + + // Evaluate transform + let xform = Transform::new(&custom_from_pcs, srgb, Intent::RelativeColorimetric, true).expect("transform from custom RGB to sRGB"); + let mut out = [0.0f32; 3]; + xform.eval(&[0.5, 0.5, 0.5], &mut out).expect("eval"); + assert!((out[0] - 0.5).abs() < 1e-2); + assert!((out[1] - 0.5).abs() < 1e-2); + assert!((out[2] - 0.5).abs() < 1e-2); + + // Invalid matrix shaper inputs + assert!(Profile::matrix_shaper([0.0, 1.0, 1.0], pcs_d50_matrix, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + assert!(Profile::matrix_shaper([f64::NAN, 1.0, 1.0], pcs_d50_matrix, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + let singular: Mat3 = [[0.0; 3]; 3]; + assert!(Profile::matrix_shaper(white_d65, singular, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + + // Near-degenerate matrix that is invertible before quantization but becomes singular after + let d50 = [0.9642, 1.0, 0.8249]; + let near_degenerate: Mat3 = [[d50[0] - 0.000001, 0.000001, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, d50[2]]]; + assert!(Profile::matrix_shaper(d50, near_degenerate, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + + // Gray profile + let gray = Profile::gray(white_d65, Curve::gamma(2.2).expect("curve")).expect("gray profile"); + assert_eq!(gray.color_space, craft_cms::ColorSpace::Gray); + + let gray_to_srgb = Transform::new(&gray, srgb, Intent::RelativeColorimetric, true).expect("transform from gray to sRGB"); + let mut srgb_out = [0.0f32; 3]; + gray_to_srgb.eval(&[0.5], &mut srgb_out).expect("eval"); + assert!((srgb_out[0] - srgb_out[1]).abs() < 1e-3); + assert!((srgb_out[1] - srgb_out[2]).abs() < 1e-3); +} + +#[test] +fn test_matrix_constructors_against_independent_moxcms_oracle() { + use moxcms::{ColorProfile, Layout, TransformOptions as MoxOpts}; + + let srgb = Builtin::Srgb.profile(); + let white_d65 = [0.95047, 1.0, 1.08883]; + + // 1. PCS D50-adapted matrix profile + let pcs_d50_matrix: Mat3 = [[0.4360747, 0.3850649, 0.1430804], [0.2225045, 0.7168786, 0.0606169], [0.0139322, 0.0971045, 0.7141733]]; + let trc = [craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc()]; + let custom_from_pcs = Profile::matrix_shaper(white_d65, pcs_d50_matrix, trc.clone()).expect("matrix shaper from PCS D50"); + + // 2. Unadapted source matrix profile + let source_d65_matrix: Mat3 = [[0.4124564, 0.3575761, 0.1804375], [0.2126729, 0.7151522, 0.0721750], [0.0193339, 0.1191920, 0.9503041]]; + let custom_from_source = Profile::from_source_matrix(white_d65, source_d65_matrix, trc).expect("matrix shaper from source"); + + // Convert both to standard ICC bytes and parse with independent oracle moxcms + let mox_from_pcs = ColorProfile::new_from_slice(&custom_from_pcs.to_bytes()).expect("moxcms parse custom PCS profile"); + let mox_from_source = ColorProfile::new_from_slice(&custom_from_source.to_bytes()).expect("moxcms parse custom source profile"); + let mox_dst_srgb = ColorProfile::new_from_slice(&srgb.to_bytes()).expect("moxcms parse builtin sRGB"); + + let test_pixels: Vec = (0..512u32).flat_map(|i| [(i * 17 % 256) as u8, (i * 53 % 256) as u8, (i * 97 % 256) as u8]).collect(); + + // Verify 1: moxcms transforming custom_from_pcs -> builtin sRGB produces identity (diff <= 1) + let mox_xform_pcs = mox_from_pcs.create_transform_8bit(Layout::Rgb, &mox_dst_srgb, Layout::Rgb, MoxOpts::default()).unwrap(); + let mut mox_out_pcs = vec![0u8; test_pixels.len()]; + mox_xform_pcs.transform(&test_pixels, &mut mox_out_pcs).unwrap(); + let max_diff_pcs = test_pixels.iter().zip(&mox_out_pcs).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max_diff_pcs <= 1, "moxcms PCS-adapted profile -> sRGB max diff was {max_diff_pcs}"); + + // Verify 2: moxcms transforming custom_from_source -> builtin sRGB produces identity (diff <= 1) + let mox_xform_src = mox_from_source.create_transform_8bit(Layout::Rgb, &mox_dst_srgb, Layout::Rgb, MoxOpts::default()).unwrap(); + let mut mox_out_src = vec![0u8; test_pixels.len()]; + mox_xform_src.transform(&test_pixels, &mut mox_out_src).unwrap(); + let max_diff_src = test_pixels.iter().zip(&mox_out_src).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max_diff_src <= 1, "moxcms source-adapted profile -> sRGB max diff was {max_diff_src}"); + + // Verify 3: craft_cms and moxcms agree when converting from custom_from_source to DisplayP3 + let p3 = Builtin::DisplayP3.profile(); + let mox_dst_p3 = ColorProfile::new_from_slice(&p3.to_bytes()).expect("moxcms parse Display P3"); + let mox_xform_p3 = mox_from_source.create_transform_8bit(Layout::Rgb, &mox_dst_p3, Layout::Rgb, MoxOpts::default()).unwrap(); + let mut mox_p3_out = vec![0u8; test_pixels.len()]; + mox_xform_p3.transform(&test_pixels, &mut mox_p3_out).unwrap(); + + let ours_xform_p3 = Transform::new(&custom_from_source, p3, Intent::RelativeColorimetric, false).unwrap(); + let mut ours_p3_out = vec![0u8; test_pixels.len()]; + ours_xform_p3.convert_u8(&test_pixels, 3, &mut ours_p3_out, 3, false).unwrap(); + + let max_p3_diff = ours_p3_out.iter().zip(&mox_p3_out).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max_p3_diff <= 1, "craft_cms vs moxcms custom source profile -> P3 max diff was {max_p3_diff}"); +} diff --git a/crates/cms/tests/fogra39.rs b/crates/cms/tests/fogra39.rs index f7fcdd0ca..f958ffc51 100644 --- a/crates/cms/tests/fogra39.rs +++ b/crates/cms/tests/fogra39.rs @@ -545,8 +545,13 @@ fn stand_in_separations_respect_the_ink_limit() { let t = Transform::new(Builtin::Srgb.profile(), cmyk, intent, true).unwrap(); let mut out = vec![0.0f32; N * N * N * 4]; t.convert_f32(&grid, 3, &mut out, 4, false).unwrap(); - let (i, max) = - out.chunks_exact(4).map(|c| c.iter().sum::()).enumerate().fold((0, 0.0f32), |m, (i, s)| if s > m.1 { (i, s) } else { m }); + let (i, max) = out + .as_chunks::<4>() + .0 + .iter() + .map(|c| c.iter().sum::()) + .enumerate() + .fold((0, 0.0f32), |m, (i, s)| if s > m.1 { (i, s) } else { m }); eprintln!("{name}, {intent:?}: max total ink {:.2} % at sRGB {:?}", max * 100.0, &grid[i * 3..i * 3 + 3]); assert!(max <= LIMIT, "{name}, {intent:?}: total ink {max} at sRGB {:?}", &grid[i * 3..i * 3 + 3]); From c950dacd39ebfe014dff47271d35ca270f858f4d Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Fri, 13 Nov 2026 23:50:23 -0500 Subject: [PATCH 002/471] craft-xlsx: read row heights, column widths and default sizes of a binary sheet BrtRowHdr fills Row's height, custom height, hidden, outline level, collapsed and style (with thickTop, thickBot and ph kept as the XML reader keeps them); BrtColInfo fills SheetPart::columns; BrtWsFmtInfo fills the new typed Features::format (SheetFormat, mirroring sheetFormatPr), which the .xlsx reader now fills too and to_xml writes from the field. A row's ht follows Excel's .xlsx: set by hand, or differing from the default. Column ranges outside the grid are skipped with a warning, column records are capped at 16,384, and heights past 8,192 twips are ignored. Closes #1254. --- crates/xlsx/README.md | 17 +++-- crates/xlsx/src/features.rs | 32 ++++++++- crates/xlsx/src/lib.rs | 4 +- crates/xlsx/src/xlsb.rs | 130 +++++++++++++++++++++++++++++++++--- crates/xlsx/tests/xlsb.rs | 129 ++++++++++++++++++++++++++++++++++- 5 files changed, 291 insertions(+), 21 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..367056ef9 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -36,8 +36,9 @@ were. Part names and relationship ids are kept, so a file read and written witho where it did, and writing it twice gives the same bytes. The calculation chain is dropped (Excel rebuilds it). `XlsxKind` is the workbook part's content type (`.xlsx`, `.xlsm`, `.xltx`, `.xltm`). Typed worksheet features (`SheetPart::features`), each mirroring its element with optional -attributes and the unnamed ones kept: sheet views (panes, selections), print setup (options, -margins, page setup, header and footer, page breaks), data validation (also read from the `x14` +attributes and the unnamed ones kept: sheet views (panes, selections), the default sizes +(`sheetFormatPr`), print setup (options, margins, page setup, header and footer, page breaks), +data validation (also read from the `x14` extension), conditional formats (colour scales, data bars, icon sets, rule extensions kept), hyperlinks (with their URLs), the legacy drawing. Comments (`Comments`, from the comments part) and the note shapes of the VML drawing (`NoteShape`). A sheet that gains comments and has no VML @@ -83,8 +84,11 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the +A binary workbook gives the same `Workbook`, strings, rows (with their heights, outline levels, +hidden flags and styles), columns, default sizes and merged cells. Its styles are not read (each +cell keeps its format index). The descent Excel records for rows and the sheet +(`x14ac:dyDescent`) is in alternate-content blocks, which are skipped. Its cell formulas are +decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the @@ -102,8 +106,9 @@ other workbooks, Python code stored outside the cell, data tables. The XLSB func (40 million records, 4 million cells and as many shared strings, 256 MiB of text, as `craft-xls` allows). `binary_formula_text`: decoded formula text over the workbook (128 MiB); past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its - nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested - extension blocks. + nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 + merged ranges and 16,384 column records per sheet, 64 nested extension blocks. A row taller + than 8,192 twips keeps the default height. - Cells and rows outside the 1,048,576 × 16,384 grid are dropped and counted. SpreadsheetML is read leniently: a damaged optional part is a warning, a damaged worksheet keeps diff --git a/crates/xlsx/src/features.rs b/crates/xlsx/src/features.rs index 17c2c2aab..9bae562b5 100644 --- a/crates/xlsx/src/features.rs +++ b/crates/xlsx/src/features.rs @@ -1,6 +1,7 @@ -//! The worksheet's typed features, each mirroring its SpreadsheetML element: sheet views, print -//! setup, data validation, conditional formatting, hyperlinks (ECMA-376 Part 1, 18.3), and the -//! comments part (18.7) with the legacy note shapes Excel draws comments with. +//! The worksheet's typed features, each mirroring its SpreadsheetML element: sheet views, the +//! sheet's default sizes, print setup, data validation, conditional formatting, hyperlinks +//! (ECMA-376 Part 1, 18.3), and the comments part (18.7) with the legacy note shapes Excel draws +//! comments with. //! //! An attribute the file may leave out is an `Option` (`None`: not written, the schema's //! default applies), so a feature reads and writes back as it was. Attributes a type does not @@ -213,6 +214,27 @@ impl SheetView { } } +// ------------------------------------------------------------------------------- sheet format + +element! { + /// ``: the sheet's default column width and row height, and its outline depth. + pub struct SheetFormat { + /// Characters of the default font's maximum digit width, padding excluded (8 when absent). + base_col_width: u32 = "baseColWidth", + /// Characters, padding included, like [`crate::Column::width`]. + default_col_width: f64 = "defaultColWidth", + /// Points. + default_row_height: f64 = "defaultRowHeight", + custom_height: bool = "customHeight", + /// Rows are hidden unless they say otherwise. + zero_height: bool = "zeroHeight", + thick_top: bool = "thickTop", + thick_bottom: bool = "thickBottom", + outline_level_row: u32 = "outlineLevelRow", + outline_level_col: u32 = "outlineLevelCol", + } +} + // ------------------------------------------------------------------------------- printing element! { @@ -643,6 +665,8 @@ impl Hyperlink { #[derive(Clone, Debug, Default, PartialEq)] pub struct Features { pub views: Vec, + /// `sheetFormatPr`. + pub format: Option, pub print: Print, pub validations: Option, pub conditional_formats: Vec, @@ -660,6 +684,7 @@ impl Features { let attrs_of = |n: &str| root.child(n); Features { views: root.child("sheetViews").map(|v| v.children("sheetView").map(SheetView::read).collect()).unwrap_or_default(), + format: attrs_of("sheetFormatPr").map(SheetFormat::read_attrs), print: Print { options: attrs_of("printOptions").map(PrintOptions::read_attrs), margins: attrs_of("pageMargins").map(PageMargins::read_attrs), @@ -708,6 +733,7 @@ impl Features { let p = &self.print; vec![ ("sheetViews", Vec::from_iter((!self.views.is_empty()).then_some(views))), + ("sheetFormatPr", Vec::from_iter(self.format.as_ref().map(|f| el("sheetFormatPr", &|e| f.write_attrs(e))))), ("conditionalFormatting", self.conditional_formats.iter().map(ConditionalFormatting::to_el).collect()), ("dataValidations", Vec::from_iter(validations)), ("hyperlinks", Vec::from_iter((!self.hyperlinks.is_empty()).then_some(links))), diff --git a/crates/xlsx/src/lib.rs b/crates/xlsx/src/lib.rs index 5a64ae0ab..9105ba886 100644 --- a/crates/xlsx/src/lib.rs +++ b/crates/xlsx/src/lib.rs @@ -75,8 +75,8 @@ pub use document::{Document, DrawingPart, KeptPart, PivotCachePart, PivotTablePa pub use drawing::{Anchor, Drawing, Item, Marker, ObjectKind, Placement}; pub use features::{ Break, CfRule, Cfvo, ColorScale, Comment, Comments, ConditionalFormatting, DataBar, DataValidation, DataValidations, Features, HeaderFooter, - Hyperlink, IconSet, NoteShape, PageMargins, PageSetup, Pane, Print, PrintOptions, Selection, SheetView, Table, TableColumn, TableStyleInfo, - notes_vml, parse_sqref, range_ref, + Hyperlink, IconSet, NoteShape, PageMargins, PageSetup, Pane, Print, PrintOptions, Selection, SheetFormat, SheetView, Table, TableColumn, + TableStyleInfo, notes_vml, parse_sqref, range_ref, }; pub use package::{Package, is_xlsb}; pub use pivot::{ diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..20953cbad 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,7 +1,8 @@ -//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from -//! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an -//! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. -//! Styles are not read. A record that does not parse fails the read. +//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheets (rows, cells, +//! columns, default sizes, merged cells), from their records. Cell formulas are decoded from +//! their tokens ([`formula`]) into the text an `.xlsx` file holds; one that can't be keeps only +//! its saved result, with a warning saying why. Styles are not read. A record that does not parse +//! fails the read. mod expr; mod formula; @@ -13,7 +14,8 @@ use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; -use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, Row, SheetPart}; +use crate::features::SheetFormat; +use crate::sheet::{Cell, CellFormula, CellValue, Column, FormulaKind, Row, SheetPart}; use crate::strings::StringItem; use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; @@ -28,6 +30,10 @@ pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; const MAX_SHEETS: usize = 1024; /// Most merged ranges in one worksheet. const MAX_MERGES: usize = 100_000; +/// Most column records in one worksheet: one per column. +const MAX_COLUMNS: usize = MAX_COLS as usize; +/// The tallest row, in twips (409.6 points). +const MAX_ROW_TWIPS: u16 = 8192; /// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the /// workbook's formulas keep their saved results. const MAX_NAMES: usize = 65_536; @@ -731,7 +737,87 @@ fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &B Ok(value) } -/// A worksheet part: rows (BrtRowHdr) and cells inside BrtBeginSheetData, merged cells after. +/// `BrtRowHdr` after `rw`, up to its column spans: the row's height, flags and style, as the +/// `.xlsx` row Excel writes. `miyRw` is the height of every row: it is `ht` when set by hand +/// (`fUnsynced`) or when it differs from the sheet's default, and past 8192 twips it is ignored. +fn row_header(index: u32, b: &mut Bytes<'_>, default_twips: Option) -> Result { + let ixfe = b.u32()?; + let twips = b.u16()?; + let [borders, flags, phonetic] = [b.u8()?, b.u8()?, b.u8()?]; + let custom = flags & 0x20 != 0; + let height = (twips <= MAX_ROW_TWIPS && (custom || default_twips.is_some_and(|d| d != twips))).then(|| f64::from(twips) / 20.0); + let mut attrs = vec![]; + for (bit, byte, name) in [(1, borders, "thickTop"), (2, borders, "thickBot"), (1, phonetic, "ph")] { + if byte & bit != 0 { + attrs.push((name.to_string(), "1".to_string())); + } + } + Ok(Row { + index, + height, + custom_height: custom && height.is_some(), + hidden: flags & 0x10 != 0, + outline_level: flags & 7, + collapsed: flags & 0x08 != 0, + style: (flags & 0x40 != 0).then_some(ixfe), + cells: vec![], + attrs, + }) +} + +/// `BrtWsFmtInfo`: `sheetFormatPr` as Excel writes it, and the default row height in twips. +/// `dxGCol` is the default width in 256ths of a character (`0xFFFFFFFF`: none set); +/// `cchDefColWidth` is the base width, left out at its default of 8. +fn sheet_format(b: &mut Bytes<'_>) -> Result<(SheetFormat, u16), Error> { + let width = b.u32()?; + let base = b.u16()?; + let twips = b.u16()?; + let flags = b.u16()?; + let rows = b.u8()?; + let cols = b.u8()?; + let set = |bit: u16| (flags & bit != 0).then_some(true); + let level = |n: u8| (n > 0).then_some(u32::from(n.min(7))); + let format = SheetFormat { + base_col_width: (base != 8).then_some(u32::from(base)), + default_col_width: (width != u32::MAX).then(|| f64::from(width) / 256.0), + default_row_height: Some(f64::from(twips) / 20.0), + custom_height: set(1), + zero_height: set(2), + thick_top: set(4), + thick_bottom: set(8), + outline_level_row: level(rows), + outline_level_col: level(cols), + attrs: vec![], + }; + Ok((format, twips)) +} + +/// `BrtColInfo`: `None` for a range outside the grid or backwards. `coldx` is the width in 256ths +/// of a character, which is the `.xlsx` width exactly. +fn column(b: &mut Bytes<'_>) -> Result, Error> { + let first = b.u32()?; + let last = b.u32()?; + let width = b.u32()?; + let style = b.u32()?; + let flags = b.u16()?; + if first > last || last >= MAX_COLS { + return Ok(None); + } + Ok(Some(Column { + first, + last, + width: Some(f64::from(width) / 256.0), + custom_width: flags & 2 != 0, + best_fit: flags & 4 != 0, + hidden: flags & 1 != 0, + outline_level: ((flags >> 8) & 7) as u8, + collapsed: flags & 0x1000 != 0, + style: (style != 0).then_some(style), + })) +} + +/// A worksheet part: the sheet's default sizes (BrtWsFmtInfo) and columns (BrtColInfo) before +/// BrtBeginSheetData, rows (BrtRowHdr) and cells inside it, merged cells after. /// Formulas refer to the workbook's `links`; with none, each keeps its saved result. #[allow(clippy::too_many_arguments)] pub(crate) fn worksheet( @@ -750,6 +836,9 @@ pub(crate) fn worksheet( let mut data_seen = false; let mut row: Option = None; let mut col = None; + // The default row height in twips: a row of another height has `ht` even when it was not set + // by hand (a large font, a thick border), as Excel writes it in `.xlsx`. + let mut default_twips: Option = None; // BrtCellMeta applies to the cell record that follows it (a dynamic-array formula), as `cm` // does in `.xlsx`: a one-based index into the metadata part's cell metadata. Excel may write // the cell's BrtValueMeta (a rich value, such as a spill error) in between. @@ -818,15 +907,14 @@ pub(crate) fn worksheet( if r >= MAX_ROWS || row.as_ref().is_some_and(|previous| r <= previous.index) { return Err(invalid("row is out of bounds or unordered")); } - // ixfe, miyRw and the flags are not read. - b.take(9)?; + let next = row_header(r, &mut b, default_twips)?; let spans = b.u32()?; if spans > 16 { return Err(invalid("too many row spans")); } b.take(spans as usize * 8)?; b.finish()?; - if let Some(previous) = row.replace(Row { index: r, ..Row::default() }) { + if let Some(previous) = row.replace(next) { each(previous)?; } sheet.rows += 1; @@ -885,6 +973,30 @@ pub(crate) fn worksheet( }); col = Some(c); } + 485 => { + if data_seen { + return Err(invalid("sheet format after worksheet data")); + } + let (format, twips) = sheet_format(&mut b)?; + b.finish()?; + default_twips = Some(twips); + sheet.features.format = Some(format); + } + 60 => { + if data_seen { + return Err(invalid("column after worksheet data")); + } + let column = column(&mut b)?; + b.finish()?; + let Some(column) = column else { + warnings.warn("invalid XLSB column ranges were skipped"); + continue; + }; + if sheet.columns.len() >= MAX_COLUMNS { + return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); + } + sheet.columns.push(column); + } 176 => { if data { return Err(invalid("merge inside worksheet data")); diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..0cf2c5c14 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Package, Range, Row, SheetKind, SheetPart, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -930,3 +930,130 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +/// `BrtRowHdr` with its style, height and three flag bytes, and no column spans. +fn row_hdr(r: u32, ixfe: u32, twips: u16, flags: [u8; 3]) -> Vec { + record(0, &[&r.to_le_bytes()[..], &ixfe.to_le_bytes(), &twips.to_le_bytes(), &flags, &0u32.to_le_bytes()].concat()) +} +/// `BrtColInfo`. +fn col_info(first: u32, last: u32, coldx: u32, ixfe: u32, flags: u16) -> Vec { + record(60, &[&first.to_le_bytes()[..], &last.to_le_bytes(), &coldx.to_le_bytes(), &ixfe.to_le_bytes(), &flags.to_le_bytes()].concat()) +} +/// `BrtWsFmtInfo`. +fn ws_fmt_info(dx: u32, base: u16, twips: u16, flags: u16, levels: [u8; 2]) -> Vec { + record(485, &[&dx.to_le_bytes()[..], &base.to_le_bytes(), &twips.to_le_bytes(), &flags.to_le_bytes(), &levels].concat()) +} +/// A worksheet with `head` before its data (the sheet format and columns) and `rows` in it. +fn sized_sheet(head: &[u8], rows: &[u8]) -> Vec { + [&record(129, &[])[..], head, &record(145, &[]), rows, &record(146, &[]), &record(130, &[])].concat() +} +fn sizes_of(bytes: &[u8]) -> Result<(Vec, SheetPart, Vec), Error> { + let mut p = Package::open(bytes, &Limits::default())?; + let wb = p.workbook()?; + let mut rows = vec![]; + let part = p.sheet(&wb.sheets[0], |r| { + rows.push(r); + Ok(()) + })?; + Ok((rows, part, p.warnings().to_vec())) +} +fn xml_sheet(xml: &str) -> (Vec, SheetPart) { + let types = r#""#; + let root = r#""#; + let rels = r#""#; + let book = r#""#; + let bytes = zip(&[ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", root.as_bytes()), + ("xl/workbook.xml", book.as_bytes()), + ("xl/_rels/workbook.xml.rels", rels.as_bytes()), + ("xl/sheet.xml", xml.as_bytes()), + ]); + let (rows, part, _) = sizes_of(&bytes).unwrap(); + (rows, part) +} + +/// The binary sheet below as Excel spells it in `.xlsx`. Excel writes `ht` for a row whose +/// height differs from the default even when it was not set by hand. +const SIZES_XML: &str = r#"1"#; + +fn sizes_bin() -> Vec { + let head = [ + ws_fmt_info(3225, 10, 285, 0x0A, [2, 1]), + record(390, &[]), + col_info(2, 2, 5273, 0, 0x0002), + col_info(3, 5, 0, 0, 0x0103), + col_info(7, 7, 2321, 7, 0), + col_info(8, 8, 6075, 0, 0x0006), + col_info(9, 9, 2321, 0, 0x1000), + col_info(16_383, 16_383, 7833, 0, 0x0002), + record(391, &[]), + ] + .concat(); + let rows = [ + row_hdr(0, 0, 285, [0, 0, 0]), + cell(2, 0, &((1u32 << 2) | 2).to_le_bytes()), + row_hdr(1, 0, 600, [0, 0x20, 0]), + row_hdr(2, 0, 533, [0, 0, 0]), + row_hdr(3, 0, 285, [0, 0x10, 0]), + row_hdr(4, 0, 300, [3, 0, 0]), + row_hdr(5, 0, 285, [0, 2, 0]), + row_hdr(6, 0, 285, [0, 0x11, 0]), + row_hdr(7, 0, 285, [0, 0x08, 0]), + row_hdr(8, 3, 285, [0, 0x40, 0]), + // A style without fGhostDirty is not applied. + row_hdr(9, 5, 285, [0, 0, 0]), + row_hdr(10, 0, 285, [0, 0, 1]), + // Past 8192 twips the height is ignored, set by hand or not. + row_hdr(11, 0, 9000, [0, 0x20, 0]), + ] + .concat(); + sized_sheet(&head, &rows) +} + +#[test] +fn row_heights_and_column_widths_are_read_as_their_xlsx_spelling() { + let (rows, part, _) = sizes_of(&file(&sizes_bin())).unwrap(); + let (xml_rows, xml_part) = xml_sheet(SIZES_XML); + assert_eq!(rows, xml_rows); + assert_eq!(part.columns, xml_part.columns); + assert_eq!(part.features.format, xml_part.features.format); + // A few of the conversions, spelled out. + assert_eq!((rows[1].height, rows[1].custom_height), (Some(30.0), true)); + assert_eq!((rows[2].height, rows[2].custom_height), (Some(26.65), false)); + assert_eq!(rows[8].style, Some(3)); + assert_eq!(part.columns[0].width, Some(5273.0 / 256.0)); + let format = part.features.format.clone().unwrap(); + assert_eq!((format.default_col_width, format.default_row_height), (Some(12.59765625), Some(14.25))); + // Written as SpreadsheetML, the binary sheet has its default sizes and columns. + let written = String::from_utf8(part.to_xml(&rows)).unwrap(); + assert!(written.contains(r#""#), "{written}"); + assert!(written.contains(r#""#), "{written}"); +} + +#[test] +fn damaged_sizes_are_skipped_or_refused() { + // A column range outside the grid, or backwards, is skipped with a warning. + let head = [record(390, &[]), col_info(0, 16_384, 2321, 0, 0), col_info(5, 4, 2321, 0, 0), col_info(1, 1, 512, 0, 2), record(391, &[])].concat(); + let (_, part, warnings) = sizes_of(&file(&sized_sheet(&head, &row(0)))).unwrap(); + assert_eq!(part.columns.len(), 1); + assert_eq!((part.columns[0].first, part.columns[0].width), (1, Some(2.0))); + assert!(warnings.iter().any(|w| w.contains("column ranges")), "{warnings:?}"); + // Without a sheet format, only a height set by hand is a row's `ht`. + let (rows, _, _) = sizes_of(&file(&sized_sheet(&[], &[row_hdr(0, 0, 300, [0, 0, 0]), row_hdr(1, 0, 400, [0, 0x20, 0])].concat()))).unwrap(); + assert_eq!((rows[0].height, rows[1].height), (None, Some(20.0))); + // More column records than columns are refused. + let many = [&record(390, &[])[..], &col_info(0, 0, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); + assert!(matches!(sizes_of(&file(&sized_sheet(&many, &[]))), Err(Error::TooLarge(_)))); + // A sheet format or column after the data, or a record of the wrong size, fails the read. + for (why, sheet) in [ + ("format after data", worksheet(&[], &ws_fmt_info(u32::MAX, 8, 285, 0, [0, 0]))), + ("column after data", worksheet(&[], &col_info(0, 0, 2321, 0, 0))), + ("short column", sized_sheet(&record(60, &[0; 16]), &[])), + ("short format", sized_sheet(&record(485, &[0; 11]), &[])), + ("long row header", sized_sheet(&[], &record(0, &[0; 18]))), + ] { + assert!(matches!(sizes_of(&file(&sheet)), Err(Error::Invalid(_))), "{why}"); + } +} From e7914038f339325f9e51807b6f2cc7fae4585eff Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 09:55:35 -0500 Subject: [PATCH 003/471] craft-xlsx: read the defined names of a binary workbook Each BrtName becomes a DefinedName as Excel writes it in .xlsx: built-in names as _xlnm.Print_Area, sheet scope as localSheetId, the formula decoded with relative references as offsets from A1, the comment, hidden, and a macro's function, vbProcedure or xlm, functionGroupId, shortcutKey, publishToServer and workbookParameter. The hidden names for newer functions and LET parameters are left out, as .xlsx does. Cell formulas using a name that was read now decode instead of keeping their saved results, PtgNameX through the workbook's own link is 'Other Sheet'!Local, and a deleted 3-D reference is Sheet2!#REF!, as in .xlsx. A name whose formula can't be read is left out with a warning, and formulas using it keep their saved results. Closes #1250. --- crates/xlsx/README.md | 9 +- crates/xlsx/src/xlsb.rs | 153 +++++++++++++++++++++++++++++--- crates/xlsx/src/xlsb/expr.rs | 3 + crates/xlsx/src/xlsb/formula.rs | 105 ++++++++++++++++------ crates/xlsx/tests/xlsb.rs | 135 +++++++++++++++++++++++++++- 5 files changed, 360 insertions(+), 45 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..4e90ca830 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -84,12 +84,17 @@ The parts this crate writes use the transitional namespaces; a strict file's kep they were. A binary workbook is not written. A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the +read (each cell keeps its format index). Its defined names are those of `definedNames` in the +`.xlsx` Excel writes: built-in names as `_xlnm.Print_Area`, sheet-scoped names with their +`localSheetId`, relative references as the cell they reach from A1, macro names with their +`function`, `xlm`, `functionGroupId` and `shortcutKey`. Excel's hidden names for newer functions +and LET parameters are left out, as in `.xlsx`; so is a name whose formula can't be read, with a +warning. Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the file keeps the tokens once. A formula whose decoding needs something the reader does not read -keeps only its saved result, with a warning saying why: user defined names, table references, +keeps only its saved result, with a warning saying why: a defined name left out, table references, other workbooks, Python code stored outside the cell, data tables. The XLSB function table (`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..1941ea61e 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,5 +1,5 @@ -//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from -//! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an +//! Binary workbooks ([MS-XLSB]): the workbook part with its defined names, shared strings and +//! worksheet cells, from their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an //! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. //! Styles are not read. A record that does not parse fails the read. @@ -15,7 +15,7 @@ use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, Row, SheetPart}; use crate::strings::StringItem; -use crate::workbook::{SheetEntry, SheetKind, Workbook}; +use crate::workbook::{DefinedName, SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; pub(crate) use formula::Links; use formula::{Book, Mode, Name, Xti}; @@ -266,6 +266,9 @@ pub(crate) fn workbook( records.first(131, warnings)?; let mut wb = Workbook::default(); let mut links = Some(Links::default()); + let mut pending = vec![]; + // Each BrtBundleSh's place in `wb.sheets`, `None` for a skipped one. + let mut bundle_sheets: Vec> = vec![]; let mut bundle = false; let mut bundle_seen = false; while let Some((id, payload)) = records.next(warnings)? { @@ -279,14 +282,15 @@ pub(crate) fn workbook( if wb.sheets.is_empty() { return Err(invalid("no supported worksheets")); } - if links.is_none() { - warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"); + match links.as_mut() { + Some(l) => wb.defined_names = defined_names(pending, &bundle_sheets, l, limits, budget, warnings), + None => warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"), } return Ok((wb, links)); } 39 | 355 | 357 | 358 | 362 | 667 => { if let Some(l) = links.as_mut() - && link_record(id, payload, l).is_err() + && link_record(id, payload, l, &mut pending).is_err() { links = None; } @@ -341,6 +345,7 @@ pub(crate) fn workbook( if let Some(l) = links.as_mut() { l.sheets.push(None); } + bundle_sheets.push(None); continue; }; let rel = rels.by_id(&rel_id).ok_or_else(|| invalid("worksheet relationship is missing"))?; @@ -355,6 +360,7 @@ pub(crate) fn workbook( if let Some(l) = links.as_mut() { l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone())); } + bundle_sheets.push(u32::try_from(wb.sheets.len()).ok()); wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) }); } _ => {} @@ -363,9 +369,110 @@ pub(crate) fn workbook( Err(invalid("missing workbook end record")) } +/// A `BrtName`'s parts that only its [`DefinedName`] needs, until the workbook part is read. +struct PendingName { + /// Its place in [`Links`]'s names. + index: usize, + flags: u32, + /// `chKey`: a macro's shortcut key. + key: u8, + rgce: Vec, + rgcb: Vec, + comment: Option, + menu: Option, + description: Option, +} + +/// The workbook's defined names, as `.xlsx` has them in `definedNames`: every `BrtName` but the +/// hidden ones for newer functions and LET parameters, which Excel doesn't write there. `sheets` +/// maps a name's `itab` (its sheet's place in `BrtBundleSh` order) to [`Workbook::sheets`]. +/// A name whose formula can't be read, or that belongs to a sheet the reader skips, is left out +/// with a warning, and a cell formula using it keeps its saved result. +fn defined_names( + pending: Vec, + sheets: &[Option], + links: &mut Links, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, +) -> Vec { + let mut out = vec![]; + for p in pending { + let Some(name) = links.names.get(p.index) else { continue }; + if name.future || formula::is_param(&name.text) { + continue; + } + let local_sheet_id = match name.scope { + None => None, + Some(itab) => match usize::try_from(itab).ok().and_then(|i| sheets.get(i)) { + Some(Some(i)) => Some(*i), + _ => { + warnings.warn("XLSB: some defined names were left out (names of sheets the reader skips)"); + continue; + } + }, + }; + let formula = match formula::decode(&p.rgce, &p.rgcb, links, Mode::Name) { + Ok(e) => expr::print(&e), + Err(formula::Unsupported(why)) => { + warnings.warn(format!("XLSB: some defined names were left out ({why})")); + continue; + } + }; + if !budget.try_formula_text(formula.len(), limits) { + warnings.warn("XLSB: some defined names were left out (more formula text than the limits allow)"); + continue; + } + let mut attrs = vec![]; + let mut put = |k: &str, v: String| attrs.push((k.to_string(), v)); + // In `CT_DefinedName`'s order. + if let Some(m) = p.menu { + put("customMenu", m); + } + if let Some(d) = p.description { + put("description", d); + } + let (func, vba, proc) = (p.flags & 0x02 != 0, p.flags & 0x04 != 0, p.flags & 0x08 != 0); + if func { + put("function", "1".into()); + } + if vba { + put("vbProcedure", "1".into()); + } else if proc { + put("xlm", "1".into()); + } + let group = (p.flags >> 6) & 0x1FF; + if proc && group != 0 { + put("functionGroupId", group.to_string()); + } + if p.key != 0 { + put("shortcutKey", char::from(p.key).to_string()); + } + if p.flags & (1 << 15) != 0 { + put("publishToServer", "1".into()); + } + if p.flags & (1 << 16) != 0 { + put("workbookParameter", "1".into()); + } + out.push(DefinedName { + name: name.text.clone(), + local_sheet_id, + formula, + comment: p.comment.unwrap_or_default(), + hidden: p.flags & 1 != 0, + attrs, + }); + if let Some(n) = links.names.get_mut(p.index) { + n.read = true; + } + } + out +} + /// A supporting link (`BrtSupSelf`, `BrtSupSame`, `BrtSupAddin`, `BrtSupBookSrc`), the sheet links -/// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`. -fn link_record(id: u16, payload: &[u8], links: &mut Links) -> Result<(), Error> { +/// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`; a name's other parts go to +/// `pending`. +fn link_record(id: u16, payload: &[u8], links: &mut Links, pending: &mut Vec) -> Result<(), Error> { let mut b = Bytes::new(payload); match id { 357 | 358 | 667 => { @@ -391,15 +498,37 @@ fn link_record(id: u16, payload: &[u8], links: &mut Links) -> Result<(), Error> b.finish()?; } _ => { - // BrtName ([MS-XLSB] 2.4.718): flags, chKey, itab, then the name; the formula and the - // rest aren't needed to call a function or tell a name from one. + // BrtName ([MS-XLSB] 2.4.718). Its formula is decoded once the workbook part is read. if links.names.len() >= MAX_NAMES { return Err(invalid("too many defined names")); } let flags = b.u32()?; - b.take(5)?; + let key = b.u8()?; + let itab = b.u32()?; let text = b.text(255)?; - links.names.push(Name { text, future: flags & (1 << 17) != 0 }); + let cce = b.u32()? as usize; + let rgce = b.take(cce)?.to_vec(); + let cb = b.u32()? as usize; + let rgcb = b.take(cb)?.to_vec(); + let comment = b.string(255, true)?; + // A macro's name (fProc) has four more strings: one unused, its custom menu, its + // description and another unused. + let (menu, description) = if flags & 0x08 != 0 { + b.string(32767, true)?; + let menu = b.string(32767, true)?; + let description = b.string(32767, true)?; + b.string(32767, true)?; + (menu, description) + } else { + (None, None) + }; + b.finish()?; + let scope = (itab != u32::MAX).then_some(itab); + // A built-in name (fBuiltin) is stored bare; `.xlsx` spells it `_xlnm.Print_Area`, in + // `definedNames` and in formulas alike. + let text = if flags & 0x20 != 0 { format!("_xlnm.{text}") } else { text }; + pending.push(PendingName { index: links.names.len(), flags, key, rgce, rgcb, comment, menu, description }); + links.names.push(Name { text, future: flags & (1 << 17) != 0, scope, read: false }); } } Ok(()) diff --git a/crates/xlsx/src/xlsb/expr.rs b/crates/xlsx/src/xlsb/expr.rs index 3dd643a6a..17f5086ae 100644 --- a/crates/xlsx/src/xlsb/expr.rs +++ b/crates/xlsx/src/xlsb/expr.rs @@ -24,6 +24,8 @@ pub(crate) enum RefKind { Rows(u32, bool, u32, bool), /// Whole columns `A:C`. Cols(u32, bool, u32, bool), + /// A deleted cell or range on a named sheet: `Sheet2!#REF!`. + Deleted, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -173,6 +175,7 @@ fn push_reference(s: &mut String, r: &Reference) { s.push_str(dollar(*a1)); push_col(s, *c1); } + RefKind::Deleted => s.push_str(CellError::Ref.as_str()), } } diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 327ffcc25..2e7f7cc96 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -4,8 +4,9 @@ //! called through hidden names), so this is its own decoder, with its own function table //! ([`super::ftab`]) and its own copies of craft-xls's depth guards and limits. Anything outside the //! token set it knows is reported as unsupported, and the caller keeps the cell's cached value. -//! So is a formula that needs something the reader doesn't read: a user's defined name (XLSB -//! names are not read yet) or Python code stored outside the cell. +//! So is a formula that needs something the reader doesn't read: a defined name whose own +//! formula could not be read, or Python code stored outside the cell. Defined names' formulas +//! are decoded here too ([`Mode::Name`]). //! //! Names keep their file spelling (`_xlfn.CONCAT`, `_xlpm.x`), as `.xlsx` formula text has them. @@ -92,13 +93,26 @@ pub(super) struct Xti { pub last: i32, } -/// A `BrtName` as far as cell formulas need it. +/// A `BrtName` as far as formulas need it. #[derive(Clone, Debug)] pub(super) struct Name { - /// The name as written (`_xlfn.CONCAT`, `_xlpm.x`, `Rate`). + /// The name as `.xlsx` spells it (`_xlfn.CONCAT`, `_xlpm.x`, `_xlnm.Print_Area`, `Rate`). pub text: String, /// `fFutureFunction`: a hidden name standing for a function newer than the file format. pub future: bool, + /// `itab`: the sheet a sheet-scoped name belongs to, by its place in `BrtBundleSh` order. + pub scope: Option, + /// Its own formula was read, so it is one of the workbook's defined names. + pub read: bool, +} + +impl Name { + /// Whether a formula may use the name: one the formula text itself defines (a newer + /// function, a LET parameter), or a defined name the reader gives. A name's formula may use + /// any other name: its text is the same either way. + fn usable(&self, mode: Mode) -> bool { + self.future || is_param(&self.text) || self.read || mode == Mode::Name + } } /// Workbook-level tables formulas refer to. @@ -114,20 +128,37 @@ pub(crate) struct Links { } /// Whether a name stands for a LET parameter (`_xlpm.x`), which only the formula itself defines. -fn is_param(name: &str) -> bool { +pub(super) fn is_param(name: &str) -> bool { name.get(..6).is_some_and(|p| p.eq_ignore_ascii_case("_xlpm.")) } impl Links { - /// The sheets a 3-D reference names, or `None` for a deleted sheet (the reference is `#REF!`). - fn sheet_sel(&self, ixti: u16) -> Result, Unsupported> { + /// `PtgName`'s and `PtgNameX`'s name: a 1-based index into every `BrtName`. A cell formula + /// using a defined name the reader doesn't give would calculate to `#NAME?`, so it keeps its + /// saved result instead. + fn name(&self, index: u32, mode: Mode) -> Result<&Name, Unsupported> { + let i = usize::try_from(index).ok().and_then(|i| i.checked_sub(1)); + let name = i.and_then(|i| self.names.get(i)).ok_or(Unsupported("reference to a missing defined name"))?; + if !name.usable(mode) { + return Err(Unsupported("defined names whose formula could not be read")); + } + Ok(name) + } + + /// Sheet link `ixti`, which must be through this workbook's own supporting link. + fn this_book(&self, ixti: u16) -> Result<&Xti, Unsupported> { let x = self.xti.get(usize::from(ixti)).ok_or(Unsupported("reference to a missing sheet link"))?; match usize::try_from(x.book).ok().and_then(|b| self.books.get(b)) { - Some(Book::This) => {} - Some(Book::External) => return Err(Unsupported("references to other workbooks (external links)")), - Some(_) => return Err(Unsupported("references through add-in or same-sheet links")), - None => return Err(Unsupported("reference to a missing workbook link")), + Some(Book::This) => Ok(x), + Some(Book::External) => Err(Unsupported("references to other workbooks (external links)")), + Some(_) => Err(Unsupported("references through add-in or same-sheet links")), + None => Err(Unsupported("reference to a missing workbook link")), } + } + + /// The sheets a 3-D reference names, or `None` for a deleted sheet (the reference is `#REF!`). + fn sheet_sel(&self, ixti: u16) -> Result, Unsupported> { + let x = self.this_book(ixti)?; // -1: "the first sheet in the reference cannot be found" (a deleted sheet). if x.first == -1 || x.last == -1 { return Ok(None); @@ -194,10 +225,15 @@ const LAST_COL: u32 = MAX_COLS - 1; /// Where a formula lives: a cell formula's references hold coordinates; a shared formula's /// relative tokens (`PtgRefN`, `PtgAreaN`) hold offsets from the shared formula's first cell. +/// +/// A defined name's relative references are offsets from A1 that wrap around the grid +/// (`RgceLocRel`), in its 3-D references too: `.xlsx` spells them as the cell they reach from A1 +/// (one column left is `Data!XFD1`). #[derive(Clone, Copy, PartialEq)] pub(super) enum Mode { Cell, Shared(CellRef), + Name, } /// RgceLoc ([MS-XLSB] 2.5.98.91): a row and a `ColRelShort` (14-bit column, then the @@ -225,9 +261,15 @@ fn loc(row: u32, col: u16, mode: Mode) -> Result { match mode { Mode::Cell => anchor(row, col), Mode::Shared(base) => relative_anchor(row, col, base), + Mode::Name => relative_anchor(row, col, CellRef::new(0, 0)), } } +/// A 3-D reference's cell: a coordinate, except in a defined name. +fn loc3d(row: u32, col: u16, mode: Mode) -> Result { + if mode == Mode::Name { loc(row, col, mode) } else { anchor(row, col) } +} + /// RgceArea: a range spanning every row (or column) is a whole-column (row) reference. fn area(r0: u32, r1: u32, c0: u16, c1: u16) -> Result { area_of(anchor(r0, c0)?, anchor(r1, c1)?) @@ -380,14 +422,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res } 0x23 => { // PtgName: a 1-based index into every BrtName, hidden ones included. - let i = usize::try_from(t.u32()?).ok().and_then(|i| i.checked_sub(1)); - let name = i.and_then(|i| links.names.get(i)).ok_or(Unsupported("reference to a missing defined name"))?; - // Defined names are not imported from XLSB yet: a cell using one would calculate - // to #NAME?, so it keeps its saved result. Newer functions and LET parameters are - // hidden names that the formula text itself defines. - if !name.future && !is_param(&name.text) { - return Err(Unsupported("defined names")); - } + let name = links.name(t.u32()?, mode)?; push(&mut stack, kids, Expr::Name(name.text.clone()))?; } 0x24 => { @@ -420,36 +455,52 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res t.take(12)?; push(&mut stack, kids, Expr::Error(CellError::Ref))?; } - 0x39 => return Err(Unsupported("names in other workbooks or on other sheets")), + 0x39 => { + // PtgNameX: a name through a supporting link. Through this workbook's own link + // it is a sheet-scoped name used from another sheet, which `.xlsx` spells + // `'Other Sheet'!Local`: Excel's files show it, [MS-XLSB] doesn't say. + let ixti = t.u16()?; + let index = t.u32()?; + links.this_book(ixti)?; + let name = links.name(index, mode)?; + let sheet = name.scope.and_then(|s| links.sheets.get(usize::try_from(s).ok()?).cloned().flatten()); + let sheet = sheet.ok_or(Unsupported("names in other workbooks or on other sheets"))?; + push(&mut stack, kids, Expr::Name(format!("{sheet}!{}", name.text)))?; + } 0x3A => { let ixti = t.u16()?; let (r, c) = (t.u32()?, t.u16()?); - let kind = RefKind::Cell(anchor(r, c)?); + let kind = RefKind::Cell(loc3d(r, c, mode)?); push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?; } 0x3B => { let ixti = t.u16()?; let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); - let kind = area(r0, r1, c0, c1)?; + let kind = area_of(loc3d(r0, c0, mode)?, loc3d(r1, c1, mode)?)?; push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?; } 0x3C | 0x3D => { + // PtgRefErr3d, PtgAreaErr3d: a deleted cell or range, which `.xlsx` spells + // `Sheet2!#REF!` (`#REF!` when the sheet is gone too). The sheet link must still + // be one the reader follows: an external #REF! stays cached. let ixti = t.u16()?; t.take(if id == 0x3C { 6 } else { 12 })?; - // The sheet link must still be one the reader follows: an external #REF! stays cached. - links.sheet_sel(ixti)?; - push(&mut stack, kids, Expr::Error(CellError::Ref))?; + push(&mut stack, kids, reference(links.sheet_sel(ixti)?, RefKind::Deleted))?; } 0x01 => return Err(Unsupported("the shared or array formula form")), 0x02 => return Err(Unsupported("what-if data tables")), 0x18 => return Err(Unsupported("structured table references")), 0x2C => { - let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; + if mode == Mode::Cell { + return Err(Unsupported("relative references in a cell formula")); + } let (r, c) = (t.u32()?, t.u16()?); push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: RefKind::Cell(loc(r, c, mode)?) }))?; } 0x2D => { - let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; + if mode == Mode::Cell { + return Err(Unsupported("relative references in a cell formula")); + } let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); let kind = area_of(loc(r0, c0, mode)?, loc(r1, c1, mode)?)?; push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind }))?; diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..73bd23218 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Package, Range, SheetKind, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -463,6 +463,9 @@ fn name(flags: u32, text: &str) -> Vec { data.extend(wide(text)); data.extend([2, 0, 0, 0, 0x1c, 0x1d, 0, 0, 0, 0]); // Formula `#NAME?`, no extra data. data.extend(u32::MAX.to_le_bytes()); // No comment. + if flags & 0x08 != 0 { + data.extend(u32::MAX.to_le_bytes().repeat(4)); // A macro's four strings, all null. + } record(39, &data) } @@ -543,8 +546,8 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { ), (9, ref3d(3, 0, 0), vec![]), (10, [&[0x25u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &REL.to_le_bytes(), &REL.to_le_bytes(), &[0x41, 20, 0]].concat(), vec![]), - // Kept as their saved results: (11, vec![0x43, 4, 0, 0, 0, 0x1E, 2, 0, 0x05], vec![]), + // Kept as their saved results: (12, ref3d(4, 0, REL), vec![]), (13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]), // Part of a shared formula at A1 that isn't one. ]; @@ -572,7 +575,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { Some("_xlfn.LET(_xlpm.x,1,_xlpm.x+1)"), Some("#REF!"), Some("SQRT(A1:A2)"), - None, + Some("Rate*2"), None, None, // A BrtCellMeta before a cell gives it cell metadata, as `cm` does in .xlsx. @@ -584,7 +587,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&CellRef::new(0, 14), &1)]); // Every formula cell keeps its saved result as its value. assert!((1..=15).all(|c| sheet.value(CellRef::new(0, c)) == Value::Number(0.0))); - for why in ["defined names", "other workbooks", "missing from the file", "structured table references"] { + for why in ["other workbooks", "missing from the file", "structured table references"] { assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); } } @@ -930,3 +933,127 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +// Defined names ([MS-XLSB] 2.4.718): `BrtName` records, and the `definedNames` Excel writes for +// the same names when it saves the workbook as `.xlsx`. + +/// `BrtName`. A macro's name (`fProc`, 0x08) has four more strings, null here. +fn brt_name(flags: u32, key: u8, itab: u32, text: &str, rgce: &[u8], comment: Option<&str>) -> Vec { + let mut data = flags.to_le_bytes().to_vec(); + data.push(key); + data.extend(itab.to_le_bytes()); + data.extend(wide(text)); + data.extend((rgce.len() as u32).to_le_bytes()); + data.extend(rgce); + data.extend(0u32.to_le_bytes()); + data.extend(comment.map_or_else(|| u32::MAX.to_le_bytes().to_vec(), wide)); + if flags & 0x08 != 0 { + data.extend(u32::MAX.to_le_bytes().repeat(4)); + } + record(39, &data) +} +fn ref3d_name(ixti: u16, row: u32, col: u16) -> Vec { + [&[0x3Au8][..], &ixti.to_le_bytes(), &loc(row, col)].concat() +} +const WORKBOOK: u32 = u32::MAX; + +/// `book(false)` with the supporting links of `book_with_links` (sheet links Data, Hidden, +/// Data:Hidden, a deleted sheet, the other workbook, and this workbook as a whole, which +/// `PtgNameX` uses), and `names`. +fn book_with_names(names: &[Vec]) -> Vec { + let plain = book(false); + let end = record(132, &[]); + let mut xti = 6u32.to_le_bytes().to_vec(); + for (book, first, last) in [(0u32, 0i32, 0i32), (0, 1, 1), (0, 0, 1), (0, -1, -1), (1, 0, 0), (0, -2, -2)] { + xti.extend([book.to_le_bytes(), first.to_le_bytes(), last.to_le_bytes()].concat()); + } + let links = [record(353, &[]), record(357, &[]), record(355, &wide("rId9")), record(362, &xti), record(354, &[])]; + [&plain[..plain.len() - end.len()], &links.concat(), &names.concat(), &end].concat() +} + +/// The names below, in record order, as `PtgName` numbers them from 1. +fn names() -> Vec> { + vec![ + // 1: a newer function's hidden name: not a defined name in `.xlsx`. + brt_name(0x0002_000b, 0, WORKBOOK, "_xlfn.CONCAT", &[0x1C, 0x1D], None), + // 2: built-in and hidden, on Data. + brt_name(0x21, 0, 0, "_FilterDatabase", &[&[0x3Bu8, 0, 0][..], &0u32.to_le_bytes(), &9u32.to_le_bytes(), &[0, 0, 2, 0]].concat(), None), + // 3: refers to another workbook: left out. + brt_name(0, 0, WORKBOOK, "External", &ref3d_name(4, 0, 0), None), + // 4: a deleted cell. + brt_name(0, 0, WORKBOOK, "Gone", &[0x3C, 0, 0, 0, 0, 0, 0, 0, 0], None), + brt_name(1, 0, WORKBOOK, "Hid", &[0x1E, 42, 0], None), + // 6, 7: the same name on two sheets. + brt_name(0, 0, 0, "Local", &ref3d_name(0, 1, 1), None), + brt_name(0, 0, 1, "Local", &ref3d_name(1, 0, 1), None), + // 8: an XLM macro with a shortcut key, in the "User Defined" group (14). + brt_name(0x388, b'k', WORKBOOK, "MacroName", &ref3d_name(0, 0, 0), None), + // 9 + brt_name(0, 0, WORKBOOK, "Rate", &[&[0x1Fu8][..], &0.05f64.to_le_bytes()].concat(), Some("a note")), + // 10, 11: relative references are offsets from A1, wrapping around the grid. + brt_name(0, 0, WORKBOOK, "Rel", &ref3d_name(0, 0, 0xFFFF), None), + brt_name(0, 0, WORKBOOK, "RelN", &[&[0x2Cu8][..], &loc(u32::MAX, REL)].concat(), None), + // 12: on a sheet the workbook doesn't have: left out. + brt_name(0, 0, 5, "Skipped", &[0x1E, 1, 0], None), + // 13: a name of a name. + brt_name(0, 0, WORKBOOK, "Twice", &[0x23, 9, 0, 0, 0, 0x1E, 2, 0, 0x05], None), + ] +} + +const NAMES_XML: &str = r#"Data!#REF!Data!$B$2Hidden!$B$1Data!$A$10.05Data!XFD1A1048576Rate*2"#; + +fn xml_names(xml: &str) -> Vec { + let types = r#""#; + let root = r#""#; + let bytes = zip(&[("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", root.as_bytes()), ("xl/workbook.xml", xml.as_bytes())]); + Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names +} + +#[test] +fn defined_names_are_read_as_their_xlsx_spelling() { + let cells: Vec> = vec![ + vec![0x23, 9, 0, 0, 0, 0x1E, 100, 0, 0x05], + vec![0x23, 6, 0, 0, 0], + // PtgNameX through this workbook's own link: a sheet-scoped name used from another sheet. + vec![0x39, 5, 0, 7, 0, 0, 0], + vec![0x23, 2, 0, 0, 0], + vec![0x3C, 1, 0, 0, 0, 0, 0, 0, 0], + // Kept as their saved results: a name left out, a name in another workbook, and a + // workbook name through PtgNameX. + vec![0x23, 3, 0, 0, 0], + vec![0x39, 4, 0, 9, 0, 0, 0], + vec![0x39, 5, 0, 9, 0, 0, 0], + ]; + let mut data = row(0); + for (i, rgce) in cells.iter().enumerate() { + data.extend(formula_cell(i as u32 + 1, rgce, &[])); + } + let bytes = package(&book_with_names(&names()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + assert_eq!(wb.defined_names, xml_names(NAMES_XML)); + let book = read(&bytes).unwrap(); + let got: Vec<_> = (1..=8).map(|c| book.sheets[0].formula(CellRef::new(0, c))).collect(); + let want = [Some("Rate*100"), Some("Local"), Some("Hidden!Local"), Some("_xlnm._FilterDatabase"), Some("Hidden!#REF!"), None, None, None]; + assert_eq!(got, want); + for why in ["defined names whose formula could not be read", "other workbooks", "names in other workbooks or on other sheets"] { + assert!(warned(&book, why), "{why}: {:?}", book.warnings); + } + for why in ["(references to other workbooks (external links))", "(names of sheets the reader skips)"] { + assert!(book.warnings.iter().any(|w| w.contains("defined names were left out") && w.contains(why)), "{why}: {:?}", book.warnings); + } +} + +#[test] +fn a_damaged_name_leaves_names_and_formulas_as_values() { + // A truncated BrtName: the workbook opens with no names, and every formula keeps its value. + let mut list = names(); + let full = brt_name(0, 0, WORKBOOK, "Rate", &[0x1E, 1, 0], None); + list[8] = record(39, &full[2..full.len() - 2]); // The payload without its record header and last two bytes. + let data = [row(0), formula_cell(1, &[0x1E, 1, 0], &[])].concat(); + let bytes = package(&book_with_names(&list), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + assert!(wb.defined_names.is_empty()); + let book = read(&bytes).unwrap(); + assert_eq!(book.sheets[0].formula(CellRef::new(0, 1)), None); + assert!(book.warnings.iter().any(|w| w.contains("names could not be read")), "{:?}", book.warnings); +} From 2a786e27a2826e9a67cfe0af167410409717b0d3 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 11:30:32 -0500 Subject: [PATCH 004/471] craft-xlsx: names on macro sheets, spans over chart sheets, formula-less built-ins A second Excel probe showed three cases where the .xlsb reader left out what .xlsx has: - Macro names point at an XLM macro sheet (Macro1!$A$1), which the sheet links treated as a sheet with no cells, so every such name was left out. Macro sheets have cells; references to them are now named. - A 3-D span over a chart sheet ('Data:After Sheet'!$A$1) was refused. A span is spelled by its ends; a sheet with no cells between them adds nothing, and .xlsx spells it the same. - Excel writes a workbook-level built-in with no formula (Database) beside a sheet's own. .xlsx leaves it out of definedNames, but formulas using it spell _xlnm.Database; they now do instead of keeping their saved results. --- crates/xlsx/README.md | 7 +++-- crates/xlsx/src/xlsb.rs | 13 ++++++++-- crates/xlsx/src/xlsb/formula.rs | 13 ++++------ crates/xlsx/tests/xlsb.rs | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 4e90ca830..3b636a2fa 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -88,8 +88,11 @@ read (each cell keeps its format index). Its defined names are those of `defined `.xlsx` Excel writes: built-in names as `_xlnm.Print_Area`, sheet-scoped names with their `localSheetId`, relative references as the cell they reach from A1, macro names with their `function`, `xlm`, `functionGroupId` and `shortcutKey`. Excel's hidden names for newer functions -and LET parameters are left out, as in `.xlsx`; so is a name whose formula can't be read, with a -warning. Its cell formulas are decoded from their tokens into the +and LET parameters are left out, as in `.xlsx`, and so is the formula-less workbook-level built-in +Excel writes beside a sheet's (`Database`), which formulas still spell `_xlnm.Database`; a name +whose formula can't be read is left out with a warning. A reference may name an XLM macro sheet +(`Macro1!$A$1`, where macro names point), and a 3-D span is spelled by its ends whatever chart +sheets lie between them. Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 1941ea61e..dd52a55b4 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -356,9 +356,10 @@ pub(crate) fn workbook( if let SheetKind::Other(_) = kind { return Err(invalid("unknown sheet relationship type")); } - // Only a worksheet has cells a formula can refer to. + // Only worksheets and macro sheets have cells a formula can refer to (a macro's + // name refers to its macro sheet: `Macro1!$A$1`). if let Some(l) = links.as_mut() { - l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone())); + l.sheets.push(matches!(kind, SheetKind::Worksheet | SheetKind::Macrosheet).then(|| name.clone())); } bundle_sheets.push(u32::try_from(wb.sheets.len()).ok()); wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) }); @@ -412,6 +413,14 @@ fn defined_names( } }, }; + // Excel writes a workbook-level built-in with no formula (`Database`) beside a sheet's + // own; `.xlsx` leaves it out of `definedNames`, and formulas using it spell its name. + if p.rgce.is_empty() { + if let Some(n) = links.names.get_mut(p.index) { + n.read = true; + } + continue; + } let formula = match formula::decode(&p.rgce, &p.rgcb, links, Mode::Name) { Ok(e) => expr::print(&e), Err(formula::Unsupported(why)) => { diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 2e7f7cc96..be6f47bfb 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -118,8 +118,8 @@ impl Name { /// Workbook-level tables formulas refer to. #[derive(Default)] pub(crate) struct Links { - /// Every sheet's name in `BrtBundleSh` order, `None` for sheets the reader skips or that hold - /// no cells (chart, dialog and macro sheets). + /// Every sheet's name in `BrtBundleSh` order, `None` for the sheets that hold no cells (chart, + /// dialog and module sheets). pub(super) sheets: Vec>, /// Every `BrtName`, in order: `PtgName` indexes them from 1. pub(super) names: Vec, @@ -167,14 +167,11 @@ impl Links { let (Ok(first), Ok(last)) = (usize::try_from(x.first), usize::try_from(x.last)) else { return Err(Unsupported("reference to a missing sheet")); }; - // A span takes every sheet between its ends, so a skipped one anywhere in it changes - // what the reference means. + // A span is spelled by its ends. A sheet between them with no cells (a chart sheet) adds + // nothing to it, and `.xlsx` spells the span the same; an end with no cells can't be named. let span = self.sheets.get(first..=last).filter(|s| !s.is_empty()).ok_or(Unsupported("reference to a missing sheet"))?; - if span.iter().any(Option::is_none) { - return Err(Unsupported("references to sheets the reader skips")); - } let (Some(Some(a)), Some(Some(b))) = (span.first(), span.last()) else { - return Err(Unsupported("reference to a missing sheet")); + return Err(Unsupported("references to sheets with no cells")); }; Ok(Some(if first == last { SheetSel::Named(a.clone()) } else { SheetSel::Span(a.clone(), b.clone()) })) } diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 73bd23218..78e2d89b1 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1057,3 +1057,49 @@ fn a_damaged_name_leaves_names_and_formulas_as_values() { assert_eq!(book.sheets[0].formula(CellRef::new(0, 1)), None); assert!(book.warnings.iter().any(|w| w.contains("names could not be read")), "{:?}", book.warnings); } + +#[test] +fn names_on_macro_sheets_spans_over_chart_sheets_and_empty_built_ins() { + // Sheets Data, a chart sheet, an XLM macro sheet, Hidden; sheet links Macro1, Data:Hidden + // (over both) and Data. + let mut book = record(131, &[]); + book.extend(record(153, &[[0u8; 8].as_slice(), &wide("")].concat())); + book.extend(record(143, &[])); + for (id, rel, name) in [(1u32, "one", "Data"), (3, "c", "Chart1"), (4, "m", "Macro1"), (2, "two", "Hidden")] { + book.extend(record(156, &[&0u32.to_le_bytes()[..], &id.to_le_bytes(), &wide(rel), &wide(name)].concat())); + } + book.extend(record(144, &[])); + let mut xti = 3u32.to_le_bytes().to_vec(); + for (first, last) in [(2i32, 2i32), (0, 3), (0, 0)] { + xti.extend([0u32.to_le_bytes(), first.to_le_bytes(), last.to_le_bytes()].concat()); + } + book.extend([record(353, &[]), record(357, &[]), record(362, &xti), record(354, &[])].concat()); + let area = [&[0x3Bu8][..], &2u16.to_le_bytes(), &0u32.to_le_bytes(), &1u32.to_le_bytes(), &0u16.to_le_bytes(), &1u16.to_le_bytes()].concat(); + // A sheet's built-in Database, then the workbook-level one Excel writes with no formula. + book.extend(brt_name(0x20, 0, 0, "Database", &area, None)); + book.extend(brt_name(0x20, 0, WORKBOOK, "Database", &[], None)); + book.extend(brt_name(0x388, b'k', WORKBOOK, "Cmd", &ref3d_name(0, 0, 0), None)); + book.extend(brt_name(0, 0, WORKBOOK, "Span", &ref3d_name(1, 0, 0), None)); + book.extend(record(132, &[])); + let rels = RELS.replace( + "", + r#""#, + ); + let ref3d_cell = |ixti: u16| [&[0x5Au8][..], &ixti.to_le_bytes(), &loc(0, 0)].concat(); + let data = [ + row(0), + formula_cell(1, &[0x23, 2, 0, 0, 0], &[]), + formula_cell(2, &[0x23, 4, 0, 0, 0], &[]), + formula_cell(3, &ref3d_cell(0), &[]), + formula_cell(4, &ref3d_cell(1), &[]), + ] + .concat(); + let bytes = package(&book, &worksheet(&data, &[]), &sst(), ROOT, &rels, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + let xml = r#"Data!$A$1:$B$2Macro1!$A$1Data:Hidden!$A$1"#; + assert_eq!(wb.defined_names, xml_names(xml)); + let read = read(&bytes).unwrap(); + let got: Vec<_> = (1..=4).map(|c| read.sheets[0].formula(CellRef::new(0, c))).collect(); + assert_eq!(got, [Some("_xlnm.Database"), Some("Span"), Some("Macro1!$A$1"), Some("Data:Hidden!$A$1")]); + assert!(read.warnings.iter().all(|w| !w.contains("left out") && !w.contains("saved results")), "{:?}", read.warnings); +} From 4a250888020df77cb7bdac49252c6d79ae15c1cc Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 11:18:42 -0500 Subject: [PATCH 005/471] craft-xlsx: read the tables of a binary workbook Package::tables reads a sheet's tableN.bin parts, through its BrtListParts, into the Table the XML reader builds, as Excel writes the table in .xlsx: BrtBeginList and its columns, the calculated and totals formulas decoded as cell formulas are, an XML map's column properties, the table style, and the AutoFilter and sort state built as the autoFilter and sortState elements. A filter column whose criteria are in an extension (date groups) or on a Boolean value is left out with a warning, as is a sort by stroke or PinYin order. PtgList now decodes to the structured reference .xlsx holds (Sales[Val], Sales[[#Headers],[Key]], escaped column names, Excel's spacing flags), from the workbook's tables, indexed when the workbook part is read. A reference to a table that can't be read, a deleted table or another workbook keeps the cell's saved result. Closes #1251. --- crates/xlsx/README.md | 21 +- crates/xlsx/src/package.rs | 59 ++- crates/xlsx/src/xlsb.rs | 24 +- crates/xlsx/src/xlsb/expr.rs | 3 + crates/xlsx/src/xlsb/formula.rs | 98 +++- crates/xlsx/src/xlsb/table.rs | 770 ++++++++++++++++++++++++++++++++ crates/xlsx/tests/xlsb.rs | 266 ++++++++++- 7 files changed, 1228 insertions(+), 13 deletions(-) create mode 100644 crates/xlsx/src/xlsb/table.rs diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index e03128e81..c99218085 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -91,9 +91,24 @@ the formula, numbers as Excel spells them), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the file keeps the tokens once. A formula whose decoding needs something the reader does not read -keeps only its saved result, with a warning saying why: user defined names, table references, -other workbooks, Python code stored outside the cell, data tables. The XLSB function table -(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. +keeps only its saved result, with a warning saying why: user defined names, other workbooks, +Python code stored outside the cell, data tables. + +A binary workbook's tables (`tableN.bin`, listed by the sheet's `BrtListPart`s) read through +`Package::tables` into the same `Table`, as Excel writes the table in `.xlsx`: the calculated and +totals formulas decoded as cell formulas are, an XML map's column properties, and the AutoFilter and +sort state built as the `autoFilter` and `sortState` elements, with numbers in a filter spelled as +Excel writes them there (17 significant digits; Excel writes a few values shorter, such as `0.1`, +which read back the same). A filter column the reader can't spell is left out with a warning (Excel +keeps date-group filters in an extension the reader skips; a custom filter on a Boolean value has no +`.xlsx` spelling Excel has shown), as is a sort by stroke or PinYin order. The revision ids Excel +writes (`xr:uid`) are in alternate-content blocks, which are skipped. A structured reference in a +formula (`PtgList`) is spelled as `.xlsx` has it (`Sales[Val]`, `Sales[[#Headers],[Key]]`, +`Sales[[#This Row],[A]:[B]]`, with Excel's spacing flags), from the table's id, which is +workbook-wide: the workbook's table parts are indexed when its workbook part is read. A reference to +a table that can't be read, to a deleted table or into another workbook keeps the cell's saved +result. The XLSB function table (`xlsb/ftab.rs`) and the expression printer are this crate's own, +apart from `craft-xls`'s. ## Hostile input diff --git a/crates/xlsx/src/package.rs b/crates/xlsx/src/package.rs index 8f2c5efc6..820132836 100644 --- a/crates/xlsx/src/package.rs +++ b/crates/xlsx/src/package.rs @@ -12,7 +12,7 @@ use crate::pivot::{CacheRecord, PivotCacheDefinition, PivotTableDefinition, read use crate::sheet::{Row, RowReader, SheetPart}; use crate::strings::StringItem; use crate::styles::Styles; -use crate::workbook::{SheetEntry, Workbook}; +use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings, xlsb}; /// Most bytes of `[Content_Types].xml` read from a binary workbook. @@ -149,7 +149,10 @@ impl<'a> Package<'a> { if self.binary.is_some() { let bytes = self.required(&self.workbook_part.clone())?; let rels = self.workbook_rels()?; - let (wb, links) = xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings)?; + let (wb, mut links) = xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings)?; + if let Some(links) = links.as_mut() { + self.index_tables(&wb, links)?; + } self.binary_links = Some(links); return Ok(wb); } @@ -296,10 +299,31 @@ impl<'a> Package<'a> { Ok(Some((part, Drawing::read(&root), rels))) } - /// The tables of a worksheet read by [`Package::sheet`], in the order of its `tableParts`, - /// each with its part name. A table part that does not read is skipped with a warning. + /// The tables of a worksheet read by [`Package::sheet`], in the order of its `tableParts` + /// (its `BrtListPart`s in a binary workbook), each with its part name. A table part that does + /// not read is skipped with a warning. pub fn tables(&mut self, sheet: &SheetPart) -> Result, Error> { let mut out = Vec::new(); + if self.binary.is_some() { + let rels = self.opc.rels(&sheet.part)?; + for id in &sheet.features.table_parts { + let Some(rel) = rels.by_id(id).filter(|r| !r.external && r.kind() == "table") else { + self.warn(format!("XLSB: {}: a table part relationship is missing", sheet.part)); + continue; + }; + let part = rel.target.clone(); + let read = self.binary_part(&part, xlsb::TABLE).and_then(|bytes| { + let links = self.binary_links.as_ref().and_then(Option::as_ref); + xlsb::table(&bytes, links, &self.limits, &self.budget, &mut self.warnings) + }); + match read { + Ok(t) => out.push((part, t)), + Err(Error::TooLarge(m)) => return Err(Error::TooLarge(m)), + Err(e) => self.warn(format!("skipped {part}: {e}")), + } + } + return Ok(out); + } for id in &sheet.features.table_parts { let Some(rel) = sheet.rels.by_id(id).filter(|r| !r.external) else { continue }; let part = rel.target.clone(); @@ -392,6 +416,33 @@ impl<'a> Package<'a> { Ok(root.map(|r| craft_drawingml::theme::read_theme(&mut craft_drawingml::part::NoSource, &r))) } + /// A part of a binary workbook, checked against its content type. + fn binary_part(&mut self, part: &str, content_type: &str) -> Result, Error> { + if let Some(types) = &self.binary { + xlsb::check_type(types, part, content_type)?; + } + self.required(part) + } + + /// Indexes the table parts of a binary workbook's worksheets, for the structured references + /// in its formulas. A table part that does not read is left out with a warning: formulas + /// using it keep their saved results. + fn index_tables(&mut self, wb: &Workbook, links: &mut xlsb::Links) -> Result<(), Error> { + for entry in wb.sheets.iter().filter(|e| e.kind == SheetKind::Worksheet) { + let Some(part) = &entry.part else { continue }; + let rels = self.opc.rels(part)?; + for rel in rels.list.iter().filter(|r| r.kind() == "table" && !r.external) { + let read = self.binary_part(&rel.target, xlsb::TABLE).and_then(|bytes| xlsb::index_table(links, &bytes, &self.limits, &self.budget)); + match read { + Ok(()) => {} + Err(Error::TooLarge(m)) => return Err(Error::TooLarge(m)), + Err(e) => self.warn(format!("XLSB: {} could not be read ({e}); formulas using its table keep their saved results", rel.target)), + } + } + } + Ok(()) + } + fn required(&mut self, part: &str) -> Result, Error> { self.opc.read(part)?.ok_or_else(|| Error::Invalid(format!("missing part {part}"))) } diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 65aab0eb8..13e687d2c 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,11 +1,13 @@ //! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from //! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an //! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. -//! Styles are not read. A record that does not parse fails the read. +//! Table parts are read in `table`. Styles are not read. A record that does not parse fails +//! the read. mod expr; mod formula; mod ftab; +mod table; use std::collections::HashMap; use std::sync::Arc; @@ -19,6 +21,7 @@ use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; pub(crate) use formula::Links; use formula::{Book, Mode, Name, Xti}; +pub(crate) use table::{TABLE, index_table, table}; pub(crate) const MAIN: &str = "application/vnd.ms-excel.main"; const BINARY_MAIN: &str = "application/vnd.ms-excel.sheet.binary.macroEnabled.main"; @@ -28,6 +31,8 @@ pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; const MAX_SHEETS: usize = 1024; /// Most merged ranges in one worksheet. const MAX_MERGES: usize = 100_000; +/// Table parts one worksheet lists at most. +const MAX_TABLE_PARTS: usize = 65_536; /// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the /// workbook's formulas keep their saved results. const MAX_NAMES: usize = 65_536; @@ -172,11 +177,13 @@ struct Records<'a, 'l> { limits: &'l Limits, budget: &'l Budget, blocks: Vec, + /// Future-record and alternate-content blocks skipped so far. + skipped: usize, } impl<'a, 'l> Records<'a, 'l> { fn new(bytes: &'a [u8], limits: &'l Limits, budget: &'l Budget) -> Self { - Self { bytes: Bytes::new(bytes), limits, budget, blocks: vec![] } + Self { bytes: Bytes::new(bytes), limits, budget, blocks: vec![], skipped: 0 } } fn next(&mut self, warnings: &mut Warnings) -> Result, Error> { @@ -219,6 +226,7 @@ impl<'a, 'l> Records<'a, 'l> { return Err(invalid("extension blocks nested too deeply")); } self.blocks.push(id + 1); + self.skipped += 1; warnings.warn("unsupported XLSB future-record and alternate-content blocks were skipped"); } // BrtFRTEnd, BrtACEnd. @@ -886,6 +894,18 @@ pub(crate) fn worksheet( }); col = Some(c); } + // BrtListPart: one of the sheet's tables, by relationship id. + 661 => { + if data { + return Err(invalid("table part inside worksheet data")); + } + let rel = b.text(255)?; + b.finish()?; + if sheet.features.table_parts.len() >= MAX_TABLE_PARTS { + return Err(Error::TooLarge(format!("XLSB exceeds {MAX_TABLE_PARTS} tables on a sheet"))); + } + sheet.features.table_parts.push(rel); + } 176 => { if data { return Err(invalid("merge inside worksheet data")); diff --git a/crates/xlsx/src/xlsb/expr.rs b/crates/xlsx/src/xlsb/expr.rs index 197644a33..96dc8f03a 100644 --- a/crates/xlsx/src/xlsb/expr.rs +++ b/crates/xlsx/src/xlsb/expr.rs @@ -123,6 +123,8 @@ pub(crate) enum Expr { /// Spaces and line breaks typed before and after an expression (`PtgAttrSpace`), which /// `.xlsx` formula text keeps. Spaced(String, Box, String), + /// A structured reference to a table, as written (`Sales[[#Headers],[Key]]`). + Structured(String), Unary(UnOp, Box), Binary(BinOp, Box, Box), Call(String, Vec), @@ -310,6 +312,7 @@ fn write_expr(s: &mut String, e: &Expr) { } None => s.push_str(n), }, + Expr::Structured(t) => s.push_str(t), Expr::Unary(UnOp::Neg, x) => { s.push('-'); write_expr(s, x); diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 3a6960b39..7cc820ff8 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -5,10 +5,12 @@ //! ([`super::ftab`]) and its own copies of craft-xls's depth guards and limits. Anything outside the //! token set it knows is reported as unsupported, and the caller keeps the cell's cached value. //! So is a formula that needs something the reader doesn't read: a user's defined name (XLSB -//! names are not read yet) or Python code stored outside the cell. +//! names are not read yet), a table whose part could not be read, or Python code stored outside +//! the cell. Structured references (`PtgList`) are spelled from the tables in [`Links`]. //! //! Names keep their file spelling (`_xlfn.CONCAT`, `_xlpm.x`), as `.xlsx` formula text has them. +use std::collections::HashMap; use std::sync::Arc; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS}; @@ -154,6 +156,15 @@ pub(crate) struct Links { pub(super) names: Vec, pub(super) books: Vec, pub(super) xti: Vec, + /// Every table by its `idList`, which `PtgList` names: `None` for an id two tables share. + pub(super) tables: HashMap>, +} + +/// A table as structured references need it: its display name and its columns' names. +#[derive(Clone, Debug, PartialEq)] +pub(super) struct ListInfo { + pub name: String, + pub columns: Vec, } /// Whether a name stands for a LET parameter (`_xlpm.x`), which only the formula itself defines. @@ -530,7 +541,13 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res } 0x01 => return Err(Unsupported("the shared or array formula form")), 0x02 => return Err(Unsupported("what-if data tables")), - 0x18 => return Err(Unsupported("structured table references")), + 0x18 => { + if t.u8()? != 0x19 { + return Err(Unsupported("unknown formula token")); + } + let text = list(&mut t, links)?; + push(&mut stack, kids, Expr::Structured(text))?; + } 0x2C => { let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; let (r, c) = (t.u32()?, t.u16()?); @@ -570,6 +587,83 @@ fn bare_function(name: String) -> String { } } +/// `PtgList` ([MS-XLSB] 2.5.98.52), after its two token bytes: a structured reference, spelled +/// as `.xlsx` has it (`Sales[Val]`, `Sales[[#Headers],[Key]]`, `Sales[[#This Row],[A]:[B]]`). +/// The two spacing flags put a space inside the outer brackets and after each comma, as Excel +/// shows the reference. +fn list(t: &mut Bytes<'_>, links: &Links) -> Result { + let ixti = t.u16()?; + let bits = t.u16()?; + let index = t.u32()?; + let (first, last) = (usize::from(t.u16()?), usize::from(t.u16()?)); + if bits & 0x3000 != 0 { + return Err(Unsupported("structured references to deleted tables or to tables in other workbooks")); + } + // The table's sheet link: a table is always in this workbook, on one of its sheets. + links.sheet_sel(ixti)?; + let table = links.tables.get(&index).and_then(Option::as_ref).ok_or(Unsupported("structured references to tables the reader could not read"))?; + let rows: &[&str] = match (bits >> 2) & 0x1F { + 0x00 => &[], + 0x01 => &["#All"], + 0x02 => &["#Headers"], + 0x04 => &["#Data"], + 0x06 => &["#Headers", "#Data"], + 0x08 => &["#Totals"], + 0x0C => &["#Data", "#Totals"], + 0x10 => &["#This Row"], + _ => return Err(Unsupported("unknown structured reference rows")), + }; + let column = |i: usize| table.columns.get(i).map(|c| escape_column(c)).ok_or(Unsupported("structured reference to a missing table column")); + let columns = match bits & 3 { + 0 => None, + 1 => Some((column(first)?, None)), + 2 if first <= last => Some((column(first)?, Some(column(last)?))), + _ => return Err(Unsupported("invalid structured reference columns")), + }; + let mut s = table.name.clone(); + match (rows, &columns) { + ([], None) => s.push_str("[]"), + ([], Some((c, None))) => { + s.push('['); + s.push_str(c); + s.push(']'); + } + ([row], None) => { + s.push('['); + s.push_str(row); + s.push(']'); + } + _ => { + let mut items: Vec = rows.iter().map(|r| format!("[{r}]")).collect(); + match &columns { + Some((a, Some(b))) => items.push(format!("[{a}]:[{b}]")), + Some((a, None)) => items.push(format!("[{a}]")), + None => {} + } + let pad = if bits & 0x80 != 0 { " " } else { "" }; + let comma = if bits & 0x100 != 0 { ", " } else { "," }; + s.push('['); + s.push_str(pad); + s.push_str(&items.join(comma)); + s.push_str(pad); + s.push(']'); + } + } + Ok(s) +} + +/// A column name in a structured reference: `'` escapes `[`, `]`, `#` and `'` (`It''s '['#1']`). +fn escape_column(name: &str) -> String { + let mut s = String::with_capacity(name.len()); + for c in name.chars() { + if matches!(c, '[' | ']' | '#' | '\'') { + s.push('\''); + } + s.push(c); + } + s +} + /// Text written out in the formula: a string, or strings joined with `&` (how Python code /// longer than a string constant is written). fn literal_text(e: &Expr) -> bool { diff --git a/crates/xlsx/src/xlsb/table.rs b/crates/xlsx/src/xlsb/table.rs new file mode 100644 index 000000000..71e8f3d92 --- /dev/null +++ b/crates/xlsx/src/xlsb/table.rs @@ -0,0 +1,770 @@ +//! Table parts ([MS-XLSB] 2.1.7.51, `tableN.bin`) into the [`Table`] a `.xlsx` table part reads +//! as. The table's AutoFilter and sort state are held as `.xlsx` elements, so they are built here +//! as Excel writes them. A filter column the reader can't spell is left out with a warning, as +//! is a sort state; the table keeps the rest. + +use craft_sheet::{CellRef, MAX_COLS, MAX_ROWS, Range}; +use craft_xml::El; + +use super::formula::{self, ListInfo, Mode}; +use super::{Budget, Bytes, Links, Records, expr, invalid}; +use crate::features::{Table, TableColumn, TableStyleInfo}; +use crate::{Error, Limits, Warnings}; + +/// The content type of a table part. +pub(crate) const TABLE: &str = "application/vnd.ms-excel.table"; + +/// `RfX` / `UncheckedRfX` ([MS-XLSB] 2.5.118, 2.5.154): first and last row, first and last column. +fn rfx(b: &mut Bytes<'_>) -> Result { + let (r0, r1, c0, c1) = (b.u32()?, b.u32()?, b.u32()?, b.u32()?); + if r0 > r1 || c0 > c1 || r1 >= MAX_ROWS || c1 >= MAX_COLS { + return Err(invalid("invalid table range")); + } + Ok(Range::new(CellRef::new(r0, c0), CellRef::new(r1, c1))) +} + +/// A `DXFId`, `None` for 0xFFFFFFFF (no differential format). +fn dxf(id: u32) -> Option { + (id != u32::MAX).then_some(id) +} + +/// An `XLNullableWideString` of at most `max` characters, with the empty string as none: Excel +/// writes an empty comment where `.xlsx` has no attribute. +fn text(b: &mut Bytes<'_>, max: u32) -> Result, Error> { + Ok(b.string(max, true)?.filter(|s| !s.is_empty())) +} + +/// What a table part holds of formulas, for the reader's index of tables: its id, display name +/// and column names. +fn list_info(bytes: &[u8], limits: &Limits, budget: &Budget) -> Result<(u32, ListInfo), Error> { + let t = table(bytes, None, limits, budget, &mut Warnings::default())?; + let id = t.id.ok_or_else(|| invalid("table without an id"))?; + let name = t.display_name.or(t.name).ok_or_else(|| invalid("table without a name"))?; + // A column without a name can't be spelled in a structured reference. + let columns = t + .columns + .into_iter() + .map(|c| c.name.filter(|n| !n.is_empty())) + .collect::>() + .ok_or_else(|| invalid("table column without a name"))?; + Ok((id, ListInfo { name, columns })) +} + +/// Adds a table part to `links`, so formulas can name it. Two tables with one id name neither. +pub(crate) fn index_table(links: &mut Links, bytes: &[u8], limits: &Limits, budget: &Budget) -> Result<(), Error> { + let (id, info) = list_info(bytes, limits, budget)?; + links.tables.entry(id).and_modify(|t| *t = None).or_insert(Some(info)); + Ok(()) +} + +/// A table part. `links` decodes its calculated-column and totals-row formulas; without them +/// (or when one can't be decoded) a formula is left out, with a warning. +pub(crate) fn table(bytes: &[u8], links: Option<&Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Result { + let mut records = Records::new(bytes, limits, budget); + let mut table = match records.next(warnings)? { + Some((343, payload)) => begin_list(payload)?, + _ => return Err(invalid("missing table begin record")), + }; + let mut cols_open = false; + let mut cols_seen = false; + let mut column: Option = None; + while let Some((id, payload)) = records.next(warnings)? { + let mut b = Bytes::new(payload); + match id { + 161 if !cols_seen && table.auto_filter.is_none() => { + let range = rfx(&mut b)?; + b.finish()?; + table.auto_filter = Some(auto_filter(range, &mut records, warnings)?); + } + 530 if !cols_seen && table.sort_state.is_none() => table.sort_state = sort_state(payload, &mut records, warnings)?, + 345 if !cols_seen => { + b.u32()?; + b.finish()?; + cols_open = true; + cols_seen = true; + } + 346 if cols_open && column.is_none() => { + b.finish()?; + cols_open = false; + } + 347 if cols_open && column.is_none() => { + if table.columns.len() >= MAX_COLS as usize { + return Err(invalid("too many table columns")); + } + column = Some(begin_list_col(payload)?); + } + 351 | 352 if column.is_some() => { + let f = list_formula(payload, links, limits, budget, warnings)?; + if let Some(c) = column.as_mut() { + *(if id == 351 { &mut c.calculated_formula } else { &mut c.totals_row_formula }) = f; + } + } + 348 => { + b.finish()?; + table.columns.push(column.take().ok_or_else(|| invalid("table column end without a column"))?); + } + 349 => { + let c = column.as_mut().ok_or_else(|| invalid("XML column properties outside a table column"))?; + c.other.push(xml_column(payload)?); + } + 513 if !cols_open && cols_seen => { + let flags = b.u16()?; + let name = b.string(255, true)?; + b.finish()?; + let bit = |n: u16| Some(flags & (1 << n) != 0); + table.style = Some(TableStyleInfo { + name, + show_first_column: bit(0), + show_last_column: bit(1), + show_row_stripes: bit(2), + show_column_stripes: bit(3), + ..TableStyleInfo::default() + }); + } + 344 => { + if cols_open || !cols_seen || column.is_some() { + return Err(invalid("incomplete table")); + } + records.end(payload, warnings)?; + return Ok(table); + } + // The end of an XML map's column; anything else out of place is damage. + 350 => b.finish()?, + _ => return Err(invalid(format!("unexpected record {id} in a table"))), + } + } + Err(invalid("missing table end record")) +} + +/// `BrtBeginList` ([MS-XLSB] 2.4.100). `.xlsx` leaves out what is the default: one header row, +/// no totals row, a totals row that has been shown, a worksheet table. +fn begin_list(payload: &[u8]) -> Result { + let mut b = Bytes::new(payload); + let range = rfx(&mut b)?; + let table_type = match b.u32()? { + 0 => None, + 2 => Some("xml".to_string()), + 3 => Some("queryTable".to_string()), + _ => return Err(invalid("invalid table type")), + }; + let id = b.u32()?; + if id == 0 || id == u32::MAX { + return Err(invalid("invalid table id")); + } + let header = b.u32()?; + let totals = b.u32()?; + let flags = b.u32()?; + // fSingleCell: a single-cell table belongs in its own part type, not here. + if flags & 2 != 0 { + return Err(invalid("single-cell table in a table part")); + } + let dxfs: Vec> = (0..6).map(|_| b.u32().map(dxf)).collect::>()?; + let connection = b.u32()?; + let name = b.string(255, true)?; + let display_name = b.string(255, true)?; + let comment = text(&mut b, 255)?; + let header_row_cell_style = b.string(255, true)?; + let data_cell_style = b.string(255, true)?; + let totals_row_cell_style = b.string(255, true)?; + b.finish()?; + let set = |bit: u32| (flags & bit != 0).then_some(true); + Ok(Table { + id: Some(id), + name, + display_name, + comment, + table_type, + header_row_count: (header != 1).then_some(header), + insert_row: set(4), + insert_row_shift: set(8), + totals_row_count: (totals != 0).then_some(totals), + totals_row_shown: (flags & 1 == 0).then_some(false), + published: set(16), + header_row_dxf_id: dxfs[0], + data_dxf_id: dxfs[1], + totals_row_dxf_id: dxfs[2], + table_border_dxf_id: dxfs[3], + header_row_border_dxf_id: dxfs[4], + totals_row_border_dxf_id: dxfs[5], + header_row_cell_style, + data_cell_style, + totals_row_cell_style, + connection_id: (connection != 0).then_some(connection), + range, + ..Table::default() + }) +} + +/// `BrtBeginListCol` ([MS-XLSB] 2.4.101): `stName` is `.xlsx`'s `uniqueName`, `stCaption` its +/// `name`, and the insert row's format and style are the data's. +fn begin_list_col(payload: &[u8]) -> Result { + let mut b = Bytes::new(payload); + let id = b.u32()?; + let function = match b.u32()? { + 0 => None, + 1 => Some("average"), + 2 => Some("count"), + 3 => Some("countNums"), + 4 => Some("max"), + 5 => Some("min"), + 6 => Some("sum"), + 7 => Some("stdDev"), + 8 => Some("var"), + 9 => Some("custom"), + _ => return Err(invalid("invalid totals row function")), + }; + let header_row_dxf_id = dxf(b.u32()?); + let data_dxf_id = dxf(b.u32()?); + let totals_row_dxf_id = dxf(b.u32()?); + let query = b.u32()?; + let unique_name = b.string(255, true)?; + let name = b.string(255, true)?; + let totals_row_label = b.string(8189, true)?; + let header_row_cell_style = b.string(255, true)?; + let data_cell_style = b.string(255, true)?; + let totals_row_cell_style = b.string(255, true)?; + b.finish()?; + Ok(TableColumn { + id: Some(id), + unique_name, + name, + totals_row_function: function.map(str::to_string), + totals_row_label, + query_table_field_id: (query != 0).then_some(query), + header_row_dxf_id, + data_dxf_id, + totals_row_dxf_id, + header_row_cell_style, + data_cell_style, + totals_row_cell_style, + ..TableColumn::default() + }) +} + +/// `XmlDataType` ([MS-XLSB] 2.5.170) from 1, as `.xlsx` names the XML Schema types. +const XML_TYPES: [&str; 45] = [ + "string", + "normalizedString", + "token", + "byte", + "unsignedByte", + "base64Binary", + "hexBinary", + "integer", + "positiveInteger", + "negativeInteger", + "nonPositiveInteger", + "nonNegativeInteger", + "int", + "unsignedInt", + "long", + "unsignedLong", + "short", + "unsignedShort", + "decimal", + "float", + "double", + "boolean", + "time", + "dateTime", + "duration", + "date", + "gMonth", + "gYear", + "gYearMonth", + "gDay", + "gMonthDay", + "Name", + "QName", + "NCName", + "anyURI", + "language", + "ID", + "IDREF", + "IDREFS", + "ENTITY", + "ENTITIES", + "NOTATION", + "NMTOKEN", + "NMTOKENS", + "anyType", +]; + +/// `BrtBeginListXmlCPr` ([MS-XLSB] 2.4.104): a column's place in an XML map, as `xmlColumnPr`. +/// `fCanBeSingle` (one node for every row) is `.xlsx`'s `denormalized`. +fn xml_column(payload: &[u8]) -> Result { + let mut b = Bytes::new(payload); + let map = b.u32()?; + let flags = b.u32()?; + let kind = b.u32()?; + let xpath = b.text(31_999)?; + b.finish()?; + let kind = kind.checked_sub(1).and_then(|k| XML_TYPES.get(k as usize)).ok_or_else(|| invalid("invalid XML data type"))?; + let mut el = El::new("xmlColumnPr").with_attr("mapId", map).with_attr("xpath", xpath); + if flags & 2 != 0 { + el.set_attr("denormalized", 1); + } + el.set_attr("xmlDataType", kind); + Ok(el) +} + +/// `BrtListCCFmla` / `BrtListTrFmla` ([MS-XLSB] 2.4.706, 2.4.708): a flag byte (`fArray`, bit 1) +/// and a `ListParsedFormula`. Its references hold coordinates, as a cell formula's do. +fn list_formula( + payload: &[u8], + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, +) -> Result, Error> { + let mut b = Bytes::new(payload); + let array = b.u8()? & 2 != 0; + let cce = b.u32()?; + if !(1..=16384).contains(&cce) { + return Err(invalid("invalid formula token length")); + } + let rgce = b.take(cce as usize)?; + let cb = b.u32()?; + let rgcb = b.take(cb as usize)?; + b.finish()?; + let Some(links) = links else { return Ok(None) }; + let text = match formula::decode(rgce, rgcb, links, Mode::Cell) { + Ok(e) => expr::print(&e), + Err(formula::Unsupported(why)) => { + warnings.warn(format!("XLSB: some table column formulas were left out ({why})")); + return Ok(None); + } + }; + if !budget.try_formula_text(text.len(), limits) { + warnings.warn("XLSB: some table column formulas were left out (more formula text than the limits allow)"); + return Ok(None); + } + Ok(Some((text, array))) +} + +/// A number in a filter, as Excel writes it in `.xlsx`, which differs from a formula's: 17 +/// significant digits (`1234.5678901234501`, `1.0000000000000001E-5`), written out from 0.01 up to +/// 10^16 unless that takes more than 17 decimals, and otherwise with an exponent of no fixed +/// width (`1E-4`, `1E+20`). Excel writes a few values with fewer digits (`0.1`, not +/// `0.10000000000000001`); both read back as the same number. +fn filter_number(n: f64) -> String { + if n == 0.0 || !n.is_finite() { + return "0".into(); + } + let sci = format!("{:.16e}", n.abs()); + let Some((mantissa, exp)) = sci.split_once('e') else { return sci }; + let e: i32 = exp.parse().unwrap_or(0); + let digits = mantissa.replace('.', ""); + let digits = digits.trim_end_matches('0'); + let digits = if digits.is_empty() { "0" } else { digits }; + let decimals = i32::try_from(digits.len()).unwrap_or(i32::MAX).saturating_sub(1).saturating_sub(e); + let mut s = String::from(if n < 0.0 { "-" } else { "" }); + if !(-2..=15).contains(&e) || decimals > 17 { + s.push_str(&digits[..1]); + if digits.len() > 1 { + s.push('.'); + s.push_str(&digits[1..]); + } + s.push_str(&format!("E{}{}", if e < 0 { '-' } else { '+' }, e.unsigned_abs())); + } else if e >= 0 { + let whole = e as usize + 1; + if digits.len() <= whole { + s.push_str(digits); + s.push_str(&"0".repeat(whole - digits.len())); + } else { + s.push_str(&digits[..whole]); + s.push('.'); + s.push_str(&digits[whole..]); + } + } else { + s.push_str("0."); + s.push_str(&"0".repeat((-e - 1) as usize)); + s.push_str(digits); + } + s +} + +/// Warns that a table's filter column was left out, and why. +fn left_out(warnings: &mut Warnings, why: &str) { + warnings.warn(format!("XLSB: some table filters were left out ({why})")); +} + +/// The records after `BrtBeginAFilter` ([MS-XLSB] 2.4.8) up to `BrtEndAFilter`, as `autoFilter`. +fn auto_filter(range: Range, records: &mut Records<'_, '_>, warnings: &mut Warnings) -> Result { + let mut el = El::new("autoFilter").with_attr("ref", range.a1()); + while let Some((id, payload)) = records.next(warnings)? { + match id { + 163 => { + if let Some(c) = filter_column(payload, records, warnings)? { + el.push(c); + } + } + 162 => { + Bytes::new(payload).finish()?; + return Ok(el); + } + _ => return Err(invalid(format!("unexpected record {id} in a table filter"))), + } + } + Err(invalid("missing table filter end record")) +} + +/// A `BrtBeginFilterColumn` ([MS-XLSB] 2.4.84) and its criteria, up to `BrtEndFilterColumn`, as +/// `filterColumn`. `None` when its criteria can't be spelled as `.xlsx` has them, or are kept +/// in an extension the reader skips (Excel's date-group filters). +fn filter_column(payload: &[u8], records: &mut Records<'_, '_>, warnings: &mut Warnings) -> Result, Error> { + let mut b = Bytes::new(payload); + let col = b.u32()?; + let flags = b.u16()?; + b.finish()?; + let mut el = El::new("filterColumn").with_attr("colId", col); + if flags & 1 != 0 { + el.set_attr("hiddenButton", 1); + } + if flags & 2 != 0 { + el.set_attr("showButton", 0); + } + let skipped = records.skipped; + let mut keep = true; + while let Some((id, payload)) = records.next(warnings)? { + let mut b = Bytes::new(payload); + match id { + 165 => { + let blank = b.u32()?; + b.u32()?; + b.finish()?; + let mut filters = El::new("filters"); + if blank == 1 { + filters.set_attr("blank", 1); + } + loop { + let (id, payload) = records.next(warnings)?.ok_or_else(|| invalid("missing filter list end record"))?; + let mut b = Bytes::new(payload); + match id { + 167 => { + let val = b.text(255)?; + b.finish()?; + filters.push(El::new("filter").with_attr("val", val)); + } + 175 => filters.push(date_group_item(payload)?), + 166 => { + b.finish()?; + break; + } + _ => return Err(invalid(format!("unexpected record {id} in a filter list"))), + } + } + el.push(filters); + } + 172 => { + let and = b.u32()?; + b.finish()?; + // Excel writes 1 where `.xlsx` has `and="1"` ([MS-XLSB]'s table has them the other way round). + let mut custom = El::new("customFilters"); + if and == 1 { + custom.set_attr("and", 1); + } + loop { + let (id, payload) = records.next(warnings)?.ok_or_else(|| invalid("missing custom filter end record"))?; + match id { + 174 => match custom_filter(payload)? { + Some(f) => custom.push(f), + None => { + left_out(warnings, "custom filters on a Boolean value"); + keep = false; + } + }, + 173 => { + Bytes::new(payload).finish()?; + break; + } + _ => return Err(invalid(format!("unexpected record {id} in custom filters"))), + } + } + el.push(custom); + } + 170 => { + let flags = b.u8()?; + let val = b.f64()?; + let filter_val = b.f64()?; + b.finish()?; + let mut top = El::new("top10"); + if flags & 1 == 0 { + top.set_attr("top", 0); + } + if flags & 2 != 0 { + top.set_attr("percent", 1); + } + top.set_attr("val", filter_number(val)); + if flags & 4 != 0 { + top.set_attr("filterVal", filter_number(filter_val)); + } + el.push(top); + } + 171 => el.push(dynamic_filter(payload)?), + 168 => { + let dxf_id = b.u32()?; + let cell = b.u32()?; + b.finish()?; + let mut color = El::new("colorFilter").with_attr("dxfId", dxf_id); + if cell == 0 { + color.set_attr("cellColor", 0); + } + el.push(color); + } + 169 => { + let set = b.u32()?; + let icon = b.u32()?; + b.finish()?; + // KPINIL: no filter. + if set != u32::MAX { + let name = ICON_SETS.get(set as usize).ok_or_else(|| invalid("invalid filter icon set"))?; + el.push(El::new("iconFilter").with_attr("iconSet", name).with_attr("iconId", icon)); + } + } + 164 => { + b.finish()?; + if records.skipped != skipped { + left_out(warnings, "filters kept in extensions the reader skips, such as date groups"); + return Ok(None); + } + return Ok(keep.then_some(el)); + } + _ => return Err(invalid(format!("unexpected record {id} in a filter column"))), + } + } + Err(invalid("missing filter column end record")) +} + +/// `KPISets` ([MS-XLSB] 2.5.86), as `.xlsx` names them. +const ICON_SETS: [&str; 17] = [ + "3Arrows", + "3ArrowsGray", + "3Flags", + "3TrafficLights1", + "3TrafficLights2", + "3Signs", + "3Symbols", + "3Symbols2", + "4Arrows", + "4ArrowsGray", + "4RedToBlack", + "4Rating", + "4TrafficLights", + "5Arrows", + "5ArrowsGray", + "5Rating", + "5Quarters", +]; + +/// `BrtCustomFilter` ([MS-XLSB] 2.4.348) as `customFilter`; `None` for a Boolean value, whose +/// `.xlsx` spelling no Excel file has shown. A blank or non-blank test is `val=" "`, as Excel +/// writes it. +fn custom_filter(payload: &[u8]) -> Result, Error> { + let mut b = Bytes::new(payload); + let vts = b.u8()?; + let operator = match b.u8()? { + 1 => Some("lessThan"), + 2 => None, + 3 => Some("lessThanOrEqual"), + 4 => Some("greaterThan"), + 5 => Some("notEqual"), + 6 => Some("greaterThanOrEqual"), + _ => return Err(invalid("invalid custom filter operator")), + }; + let union = b.take(8)?; + let val = match vts { + 4 => { + let n = f64::from_le_bytes(union.try_into().map_err(|_| invalid("truncated number"))?); + if !n.is_finite() { + return Err(invalid("nonfinite numeric value")); + } + filter_number(n) + } + 6 => b.text(255)?, + 8 => return Ok(None), + 0x0C | 0x0E => " ".to_string(), + _ => return Err(invalid("invalid custom filter type")), + }; + b.finish()?; + let mut el = El::new("customFilter"); + if let Some(op) = operator { + el.set_attr("operator", op); + } + el.set_attr("val", val); + Ok(Some(el)) +} + +/// `BrtDynamicFilter` ([MS-XLSB] 2.4.362) as `dynamicFilter`: the computed values only once the +/// filter has been applied, and the maximum only for date periods. +fn dynamic_filter(payload: &[u8]) -> Result { + let mut b = Bytes::new(payload); + let cft = b.u32()?; + let applied = b.u8()? & 1 != 0; + let val = b.f64()?; + let max = b.f64()?; + b.finish()?; + const PERIODS: [&str; 16] = [ + "tomorrow", + "today", + "yesterday", + "nextWeek", + "thisWeek", + "lastWeek", + "nextMonth", + "thisMonth", + "lastMonth", + "nextQuarter", + "thisQuarter", + "lastQuarter", + "nextYear", + "thisYear", + "lastYear", + "yearToDate", + ]; + let kind = match cft { + 0 => "null".to_string(), + 1 => "aboveAverage".to_string(), + 2 => "belowAverage".to_string(), + 8..=0x17 => PERIODS[(cft - 8) as usize].to_string(), + 0x18..=0x1B => format!("Q{}", cft - 0x17), + 0x1C..=0x27 => format!("M{}", cft - 0x1B), + _ => return Err(invalid("invalid dynamic filter type")), + }; + let mut el = El::new("dynamicFilter").with_attr("type", kind); + if applied && (1..=0x17).contains(&cft) { + el.set_attr("val", filter_number(val)); + if cft >= 8 { + el.set_attr("maxVal", filter_number(max)); + } + } + Ok(el) +} + +/// `BrtAFilterDateGroupItem` ([MS-XLSB] 2.4.5) as `dateGroupItem`, with the parts of the date +/// its grouping compares. +fn date_group_item(payload: &[u8]) -> Result { + let mut b = Bytes::new(payload); + let year = b.u16()?; + let month = b.u16()?; + let day = b.u32()?; + let (hour, minute, second) = (b.u16()?, b.u16()?, b.u16()?); + b.take(6)?; + let grouping = b.u32()?; + b.finish()?; + const GROUPS: [&str; 6] = ["year", "month", "day", "hour", "minute", "second"]; + let name = GROUPS.get(grouping as usize).ok_or_else(|| invalid("invalid date group"))?; + let mut el = El::new("dateGroupItem").with_attr("year", year); + let parts = [("month", u32::from(month)), ("day", day), ("hour", u32::from(hour)), ("minute", u32::from(minute)), ("second", u32::from(second))]; + for (name, value) in parts.iter().take(grouping as usize) { + el.set_attr(name, value); + } + el.set_attr("dateTimeGrouping", name); + Ok(el) +} + +/// `BrtBeginSortState` ([MS-XLSB] 2.4.227) and its `BrtBeginSortCond`s, up to `BrtEndSortState`, +/// as `sortState`. `None`, with a warning, for a sort by another method than character order, +/// which `.xlsx` names (stroke or PinYin) and the record does not. +fn sort_state(payload: &[u8], records: &mut Records<'_, '_>, warnings: &mut Warnings) -> Result, Error> { + let mut b = Bytes::new(payload); + let flags = b.u16()?; + let range = rfx(&mut b)?; + b.finish()?; + let mut el = El::new("sortState"); + if flags & 1 != 0 { + el.set_attr("columnSort", 1); + } + if flags & 2 != 0 { + el.set_attr("caseSensitive", 1); + } + el.set_attr("ref", range.a1()); + let keep = flags & 4 == 0; + if !keep { + warnings.warn("XLSB: some table sort states were left out (sorts by stroke or PinYin order)"); + } + while let Some((id, payload)) = records.next(warnings)? { + let mut b = Bytes::new(payload); + match id { + 532 => { + let flags = b.u16()?; + let range = rfx(&mut b)?; + let (first, second) = (b.u32()?, b.u32()?); + let list = b.string(32_767, true)?; + b.finish()?; + let mut cond = El::new("sortCondition"); + if flags & 1 != 0 { + cond.set_attr("descending", 1); + } + let on = (flags >> 1) & 0xF; + match on { + 0 => {} + 1 => cond.set_attr("sortBy", "cellColor"), + 2 => cond.set_attr("sortBy", "fontColor"), + 3 => cond.set_attr("sortBy", "icon"), + _ => return Err(invalid("invalid sort condition")), + } + cond.set_attr("ref", range.a1()); + match on { + 0 => { + if let Some(list) = list { + cond.set_attr("customList", list); + } + } + 1 | 2 => cond.set_attr("dxfId", first), + _ => { + let name = ICON_SETS.get(first as usize).ok_or_else(|| invalid("invalid sort icon set"))?; + cond.set_attr("iconSet", name); + cond.set_attr("iconId", second); + } + } + el.push(cond); + } + 533 => b.finish()?, + 531 => { + b.finish()?; + return Ok(keep.then_some(el)); + } + _ => return Err(invalid(format!("unexpected record {id} in a sort state"))), + } + } + Err(invalid("missing sort state end record")) +} + +#[cfg(test)] +mod tests { + use super::filter_number; + + #[test] + fn filter_numbers_are_spelled_as_excel_writes_them() { + // Excel's `.xlsx` text for each value. + let cases = [ + (20.0, "20"), + (23.25, "23.25"), + (46340.0, "46340"), + (-1.5, "-1.5"), + (1e15, "1000000000000000"), + (999_999_999_999_999.0, "999999999999999"), + (1.234_567_890_123_45e17, "1.2345678901234499E+17"), + (1e16, "1E+16"), + (-1e16, "-1E+16"), + (1e20, "1E+20"), + (2.5e300, "2.5000000000000001E+300"), + (1_234.567_890_123_45, "1234.5678901234501"), + (12_345_678_901_234.5, "12345678901234.5"), + (99_999_999_999_999.9, "99999999999999.906"), + (1.1, "1.1000000000000001"), + (0.57, "0.56999999999999995"), + (0.01, "0.01"), + (0.035, "3.5000000000000003E-2"), + (0.001, "1E-3"), + (1e-4, "1E-4"), + (1e-5, "1.0000000000000001E-5"), + (1.5e-10, "1.5E-10"), + (1e-19, "9.9999999999999998E-20"), + ]; + for (n, want) in cases { + assert_eq!(filter_number(n), want, "{n}"); + } + } +} diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 950dc8847..3a0228275 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Package, Range, SheetKind, Table, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -584,7 +584,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&CellRef::new(0, 14), &1)]); // Every formula cell keeps its saved result as its value. assert!((1..=15).all(|c| sheet.value(CellRef::new(0, c)) == Value::Number(0.0))); - for why in ["defined names", "other workbooks", "missing from the file", "structured table references"] { + for why in ["defined names", "other workbooks", "missing from the file", "tables the reader could not read"] { assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); } } @@ -1035,3 +1035,265 @@ fn references_to_macro_sheets_and_spans_over_chart_sheets_are_named() { assert_eq!(got, [Some("Macro1!$A$1"), Some("Data:Hidden!$A$1"), None]); assert!(warned(&wb, "references to sheets with no cells"), "{:?}", wb.warnings); } + +// Tables ([MS-XLSB] 2.1.7.51): table parts assembled from the specification's records, and the +// `` Excel writes for the same table in `.xlsx`. + +fn rfx(r0: u32, r1: u32, c0: u32, c1: u32) -> Vec { + [r0, r1, c0, c1].iter().flat_map(|n| n.to_le_bytes()).collect() +} +/// `BrtBeginList`: a worksheet table with no formats or cell styles, its name as its display name. +fn begin_list(range: &[u8], id: u32, header: u32, totals: u32, flags: u32, name: &str) -> Vec { + begin_list_of(0, range, id, header, totals, flags, name) +} +/// `BrtBeginList` of a table type (`lt`): 0 a worksheet table, 2 an XML table. +fn begin_list_of(lt: u32, range: &[u8], id: u32, header: u32, totals: u32, flags: u32, name: &str) -> Vec { + let mut data = range.to_vec(); + for n in [lt, id, header, totals, flags] { + data.extend(n.to_le_bytes()); + } + data.extend(u32::MAX.to_le_bytes().repeat(6)); + data.extend(0u32.to_le_bytes()); + data.extend([wide(name), wide(name), wide("")].concat()); + data.extend(u32::MAX.to_le_bytes().repeat(3)); + record(343, &data) +} +/// `BrtBeginListCol` with its totals function (`ilta`), data format and totals label. +fn list_col(id: u32, ilta: u32, data_dxf: u32, name: &str, total: Option<&str>) -> Vec { + let mut data: Vec = [id, ilta, u32::MAX, data_dxf, u32::MAX, 0, u32::MAX].iter().flat_map(|n| n.to_le_bytes()).collect(); + data.extend(wide(name)); + data.extend(total.map_or_else(|| u32::MAX.to_le_bytes().to_vec(), wide)); + data.extend(u32::MAX.to_le_bytes().repeat(3)); + record(347, &data) +} +/// `BrtListCCFmla` (351) or `BrtListTrFmla` (352). +fn list_formula(id: u16, rgce: &[u8]) -> Vec { + record(id, &[&[0u8][..], &(rgce.len() as u32).to_le_bytes(), rgce, &0u32.to_le_bytes()].concat()) +} +/// `PtgList`: the flags hold the columns (bits 0-1), the rows (2-6) and the two spacing bits. +fn ptg_list(ixti: u16, flags: u16, list: u32, first: u16, last: u16) -> Vec { + [&[0x18u8, 0x19][..], &ixti.to_le_bytes(), &flags.to_le_bytes(), &list.to_le_bytes(), &first.to_le_bytes(), &last.to_le_bytes()].concat() +} +fn filter_column(col: u32, flags: u16, criteria: &[Vec]) -> Vec { + [record(163, &[&col.to_le_bytes()[..], &flags.to_le_bytes()].concat()), criteria.concat(), record(164, &[])].concat() +} +fn custom_filter(vts: u8, op: u8, union: [u8; 8], text: Option<&str>) -> Vec { + record(174, &[&[vts, op][..], &union, &text.map(wide).unwrap_or_default()].concat()) +} +fn table_style(flags: u16, name: &str) -> Vec { + record(513, &[&flags.to_le_bytes()[..], &wide(name)].concat()) +} +/// The rows of a `PtgList` (bits 2-6). +const fn rows(t: u16) -> u16 { + t << 2 +} + +/// The filters of Sales: a value list with blanks, two custom criteria (OR), a bottom-percent +/// filter applied with its button hidden, and "today" applied. +fn sales_filters() -> Vec> { + vec![ + filter_column(0, 0, &[record(165, &[1, 0, 0, 0, 0, 0, 0, 0]), record(167, &wide("k3")), record(167, &wide("k4")), record(166, &[])]), + filter_column( + 1, + 0, + &[ + record(172, &0u32.to_le_bytes()), + custom_filter(4, 1, 20f64.to_le_bytes(), None), + custom_filter(6, 2, [0; 8], Some("k*")), + record(173, &[]), + ], + ), + filter_column(2, 1, &[record(170, &[&[0b110u8][..], &10f64.to_le_bytes(), &23.25f64.to_le_bytes()].concat())]), + filter_column(3, 0, &[record(171, &[&9u32.to_le_bytes()[..], &[1], &46340f64.to_le_bytes(), &46341f64.to_le_bytes()].concat())]), + ] +} + +/// Sales (B2:E8, id 1) with a totals row, a calculated column, a column name needing escapes, +/// `filters` and a sort. +fn sales(filters: &[Vec]) -> Vec { + [ + begin_list(&rfx(1, 7, 1, 4), 1, 1, 1, 1, "Sales"), + record(161, &rfx(1, 6, 1, 4)), + filters.concat(), + record(162, &[]), + record(530, &[&0u16.to_le_bytes()[..], &rfx(2, 6, 1, 4)].concat()), + record(532, &[&1u16.to_le_bytes()[..], &rfx(1, 7, 1, 1), &[0; 8], &u32::MAX.to_le_bytes()].concat()), + record(533, &[]), + record(531, &[]), + record(345, &4u32.to_le_bytes()), + list_col(1, 0, u32::MAX, "Key", Some("Total")), + record(348, &[]), + list_col(2, 6, u32::MAX, "Val", None), + record(348, &[]), + list_col(3, 0, 2, "It's [#1]", None), + record(348, &[]), + list_col(4, 9, u32::MAX, "Calc", None), + list_formula(351, &[ptg_list(0, 1 | rows(0x10), 1, 1, 1), vec![0x1E, 2, 0, 0x05]].concat()), + list_formula(352, &[ptg_list(0, 1, 1, 3, 3), vec![0x42, 1, 7, 0]].concat()), + record(348, &[]), + record(346, &[]), + table_style(0b101, "TableStyleMedium9"), + record(344, &[]), + ] + .concat() +} + +/// NoHead (H10:I11, id 2): no header row, a totals row never shown, no stripes. +fn no_head() -> Vec { + [ + begin_list(&rfx(9, 10, 7, 8), 2, 0, 0, 0, "NoHead"), + record(345, &2u32.to_le_bytes()), + list_col(1, 0, u32::MAX, "Column1", None), + record(348, &[]), + list_col(2, 0, u32::MAX, "Column2", None), + record(348, &[]), + record(346, &[]), + table_style(0, "TableStyleMedium2"), + record(344, &[]), + ] + .concat() +} + +/// XmlT (K1:L2, id 3): an XML table, its columns mapped to an XML map's elements. +fn xml_table_part() -> Vec { + let col = |id: u32, name: &str, kind: u32, xpath: &str| { + let mut data: Vec = [id, 0, u32::MAX, u32::MAX, u32::MAX, 0].iter().flat_map(|n| n.to_le_bytes()).collect(); + data.extend([wide(name), wide(name)].concat()); + data.extend(u32::MAX.to_le_bytes().repeat(4)); + let map = [&1u32.to_le_bytes()[..], &(if id == 2 { 2u32 } else { 0 }).to_le_bytes(), &kind.to_le_bytes(), &wide(xpath)].concat(); + [record(347, &data), record(349, &map), record(350, &[]), record(348, &[])].concat() + }; + [ + begin_list_of(2, &rfx(0, 1, 10, 11), 3, 1, 0, 0, "XmlT"), + record(345, &2u32.to_le_bytes()), + col(1, "a", 0x0D, "/rows/r/a"), + col(2, "b", 0x01, "/rows/@b"), + record(346, &[]), + table_style(0b100, "TableStyleMedium2"), + record(344, &[]), + ] + .concat() +} + +const XML_TABLE_XML: &str = r#"
"#; +const SALES_XML: &str = r#"Sales[[#This Row],[Val]]*2MAX(Sales[Calc])
"#; +const NO_HEAD_XML: &str = r#"
"#; + +fn xml_table(xml: &str) -> Table { + let root = craft_xml::Options::canonical(&craft_xlsx::ns::CANONICAL).parse(xml.as_bytes()).unwrap(); + Table::read(&root).unwrap() +} + +/// `book_with_links(&xti())` whose sheet one lists `tables` (`BrtListPart`, relationship ids +/// t1, t2, ...) after `data`. +fn book_with_tables(data: &[u8], tables: &[Vec]) -> Vec { + let mut list = vec![record(660, &(tables.len() as u32).to_le_bytes())]; + let mut rels = String::from(r#""#); + let mut types = TYPES.trim_end_matches("").to_string(); + for i in 1..=tables.len() { + list.push(record(661, &wide(&format!("t{i}")))); + rels.push_str(&format!(r#""#)); + types.push_str(&format!(r#""#)); + } + list.push(record(662, &[])); + rels.push_str(""); + types.push_str(""); + let names: Vec = (1..=tables.len()).map(|i| format!("tables/t{i}.bin")).collect(); + let (book, one, two, strings) = (book_with_links(&xti()), worksheet(data, &list.concat()), worksheet(&[], &[]), sst()); + let mut entries: Vec<(&str, &[u8])> = vec![ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", ROOT.as_bytes()), + ("records/book.bin", &book), + ("records/_rels/book.bin.rels", RELS.as_bytes()), + ("data/one.bin", &one), + ("data/_rels/one.bin.rels", rels.as_bytes()), + ("data/two.bin", &two), + ("text/strings.bin", &strings), + ]; + entries.extend(names.iter().map(String::as_str).zip(tables.iter().map(Vec::as_slice))); + zip(&entries) +} + +/// Sheet one's tables through `Package`, and its warnings. +fn tables_of(bytes: &[u8]) -> (Vec, Vec) { + let mut p = Package::open(bytes, &Limits::default()).unwrap(); + let wb = p.workbook().unwrap(); + let part = p.sheet(&wb.sheets[0], |_| Ok(())).unwrap(); + let tables = p.tables(&part).unwrap().into_iter().map(|(_, t)| t).collect(); + (tables, p.warnings().to_vec()) +} + +#[test] +fn tables_are_read_as_their_xlsx_spelling() { + // Spacing flags: 0x80 inside the outer brackets, 0x100 after commas. + let cells: Vec> = vec![ + ptg_list(0, 1, 1, 1, 1), + ptg_list(0, 1 | rows(2), 1, 0, 0), + ptg_list(0, 2 | rows(4), 1, 1, 3), + ptg_list(0, rows(1), 1, 0, 0), + ptg_list(0, 0, 1, 0, 0), + ptg_list(0, 2 | rows(0x10), 1, 1, 3), + ptg_list(0, 1, 1, 2, 2), + ptg_list(0, 1 | rows(4) | 0x180, 1, 1, 1), + ptg_list(0, rows(0x10), 2, 0, 0), + ptg_list(0, 1 | rows(0xC) | 0x100, 1, 0, 0), + ptg_list(0, rows(6), 1, 0, 0), + // Kept as their saved results: a table that isn't there, a deleted table, a column past + // the table's last. + ptg_list(0, 0, 9, 0, 0), + ptg_list(0, 0x1000, 1, 0, 0), + ptg_list(0, 1, 1, 4, 4), + ]; + let mut data = row(0); + for (i, rgce) in cells.iter().enumerate() { + data.extend(formula_cell(i as u32 + 1, rgce, &[])); + } + let bytes = book_with_tables(&data, &[sales(&sales_filters()), no_head(), xml_table_part()]); + let (tables, _) = tables_of(&bytes); + assert_eq!(tables, [xml_table(SALES_XML), xml_table(NO_HEAD_XML), xml_table(XML_TABLE_XML)]); + let wb = read(&bytes).unwrap(); + let got: Vec<_> = (1..=14).map(|c| wb.sheets[0].formula(CellRef::new(0, c))).collect(); + let want = [ + Some("Sales[Val]"), + Some("Sales[[#Headers],[Key]]"), + Some("Sales[[#Data],[Val]:[Calc]]"), + Some("Sales[#All]"), + Some("Sales[]"), + Some("Sales[[#This Row],[Val]:[Calc]]"), + Some("Sales[It''s '['#1']]"), + Some("Sales[ [#Data], [Val] ]"), + Some("NoHead[#This Row]"), + Some("Sales[[#Data], [#Totals], [Key]]"), + Some("Sales[[#Headers],[#Data]]"), + None, + None, + None, + ]; + assert_eq!(got, want); + for why in ["tables the reader could not read", "deleted tables", "missing table column"] { + assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); + } +} + +#[test] +fn table_filters_and_parts_the_reader_cannot_spell_are_left_out() { + // A filter column whose criteria are in a future-record block (where Excel keeps date + // groups), and one on a Boolean value: both left out, the table kept. + let frt = [record(35, &[3, 0, 2, 0]), record(5094, &[0; 32]), record(36, &[])].concat(); + let mut filters = sales_filters(); + filters.push(filter_column(4, 0, &[frt])); + filters.push(filter_column(5, 0, &[record(172, &0u32.to_le_bytes()), custom_filter(8, 2, [1, 0, 0, 0, 0, 0, 0, 0], None), record(173, &[])])); + // A truncated table part: skipped, and formulas using it keep their saved results. + let mut broken = no_head(); + broken.truncate(20); + let data = [row(0), formula_cell(1, &ptg_list(0, 0, 2, 0, 0), &[]), formula_cell(2, &ptg_list(0, 1, 1, 1, 1), &[])].concat(); + let bytes = book_with_tables(&data, &[sales(&filters), broken]); + let (tables, warnings) = tables_of(&bytes); + assert_eq!(tables, [xml_table(SALES_XML)]); + for why in ["filters kept in extensions", "custom filters on a Boolean value", "skipped tables/t2.bin"] { + assert!(warnings.iter().any(|w| w.contains(why)), "{why}: {warnings:?}"); + } + let wb = read(&bytes).unwrap(); + assert_eq!([1, 2].map(|c| wb.sheets[0].formula(CellRef::new(0, c))), [None, Some("Sales[Val]")]); + assert!(warned(&wb, "tables the reader could not read"), "{:?}", wb.warnings); +} From b5eda42a33893ec17143532cef6fc1d97accb9d5 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:21:06 -0500 Subject: [PATCH 006/471] craft-xlsx: VBA function names and macro descriptions in binary workbooks An Excel probe with VBA enabled showed two things the names reader got wrong: - Excel writes a VBA function a cell calls as a BrtName with fFunc, fOB and fProc, no formula, and none of the four strings fProc names otherwise carry. The reader refused it, which dropped every name and left every formula as its saved result. Such a name now ends after its comment; it stays out of the names, as .xlsm lists none, and cells calling it spell it. - The two middle strings of a macro's name are its description and help topic, as .xlsm writes them (description="Function A" help="!0"), not the custom menu and description the specification's labels say. --- crates/xlsx/README.md | 39 ++++++++++++++++----------------- crates/xlsx/src/xlsb.rs | 23 +++++++++++--------- crates/xlsx/tests/xlsb.rs | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index b873c87f6..e79fdbd55 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -83,25 +83,26 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its defined names are those of `definedNames` in the -`.xlsx` Excel writes: built-in names as `_xlnm.Print_Area`, sheet-scoped names with their -`localSheetId`, relative references as the cell they reach from A1, macro names with their -`function`, `xlm`, `functionGroupId` and `shortcutKey`. Excel's hidden names for newer functions -and LET parameters are left out, as in `.xlsx`, and so is the formula-less workbook-level built-in -Excel writes beside a sheet's (`Database`), which formulas still spell `_xlnm.Database`; a name -whose formula can't be read is left out with a warning. A reference may name an XLM macro sheet -(`Macro1!$A$1`, where macro names point), and a 3-D span is spelled by its ends whatever chart -sheets lie between them. Its cell formulas are decoded from their tokens into the text an `.xlsx` -file holds (`_xlfn.` and `_xlpm.` names as written, apart from the four ISO/IEC 29500 functions -`.xlsx` names bare, no implicit `@`, the spaces and line breaks typed in the formula, numbers as -Excel spells them), with -`FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell -metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the -file keeps the tokens once. A formula whose decoding needs something the reader does not read -keeps only its saved result, with a warning saying why: a defined name left out, table references, -other workbooks, Python code stored outside the cell, data tables. The XLSB function table -(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. +A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not read +(each cell keeps its format index). Its defined names are those of `definedNames` in the `.xlsx` +Excel writes: built-in names as `_xlnm.Print_Area`, sheet-scoped names with their `localSheetId`, +relative references as the cell they reach from A1, macro names with their `function`, `xlm` or +`vbProcedure`, `functionGroupId`, `shortcutKey`, `description` and `help`. Excel's hidden names for +newer functions and LET parameters are left out, as in `.xlsx`, and so is the formula-less +workbook-level built-in Excel writes beside a sheet's (`Database`), which formulas still spell +`_xlnm.Database`, and the formula-less name of a VBA function a cell calls (`.xlsm` lists none; the +cell spells it); a name whose formula can't be read is left out with a warning. A reference may name +an XLM macro sheet (`Macro1!$A$1`, where macro names point), and a 3-D span is spelled by its ends +whatever chart sheets lie between them. Its cell formulas are decoded from their tokens into the +text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, apart from the four ISO/IEC +29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks typed in the +formula, numbers as Excel spells them), with `FormulaKind::Shared` or `FormulaKind::Array` and their +ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own +text, decoded at the cell, since the file keeps the tokens once. A formula whose decoding needs +something the reader does not read keeps only its saved result, with a warning saying why: a defined +name left out, table references, other workbooks, Python code stored outside the cell, data tables. +The XLSB function table (`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from +`craft-xls`'s. ## Hostile input diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index dd52a55b4..e6246f4be 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -380,8 +380,8 @@ struct PendingName { rgce: Vec, rgcb: Vec, comment: Option, - menu: Option, description: Option, + help: Option, } /// The workbook's defined names, as `.xlsx` has them in `definedNames`: every `BrtName` but the @@ -435,12 +435,12 @@ fn defined_names( let mut attrs = vec![]; let mut put = |k: &str, v: String| attrs.push((k.to_string(), v)); // In `CT_DefinedName`'s order. - if let Some(m) = p.menu { - put("customMenu", m); - } if let Some(d) = p.description { put("description", d); } + if let Some(h) = p.help { + put("help", h); + } let (func, vba, proc) = (p.flags & 0x02 != 0, p.flags & 0x04 != 0, p.flags & 0x08 != 0); if func { put("function", "1".into()); @@ -520,14 +520,17 @@ fn link_record(id: u16, payload: &[u8], links: &mut Links, pending: &mut VecData!$A$1"#; + assert_eq!(wb.defined_names, xml_names(xml)); +} From eca1698ef11bc0a523dea6da95cdc613b11a405b Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:38:58 -0500 Subject: [PATCH 007/471] craft-xlsx: review fix: skipped column records count toward the column cap A sheet of 16,385 reversed or out-of-grid BrtColInfo ranges was skipped one by one without reaching the cap, since only kept columns were counted. Every column record now counts. --- crates/xlsx/src/xlsb.rs | 9 ++++++--- crates/xlsx/tests/xlsb.rs | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index fdda0d5cb..73755c3dc 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -840,6 +840,8 @@ pub(crate) fn worksheet( let mut sheet = SheetPart::default(); let mut data = false; let mut data_seen = false; + // Every BrtColInfo counts toward the cap, a skipped one too. + let mut column_records = 0usize; let mut row: Option = None; let mut col = None; // The default row height in twips: a row of another height has `ht` even when it was not set @@ -992,15 +994,16 @@ pub(crate) fn worksheet( if data_seen { return Err(invalid("column after worksheet data")); } + if column_records >= MAX_COLUMNS { + return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); + } + column_records += 1; let column = column(&mut b)?; b.finish()?; let Some(column) = column else { warnings.warn("invalid XLSB column ranges were skipped"); continue; }; - if sheet.columns.len() >= MAX_COLUMNS { - return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); - } sheet.columns.push(column); } 176 => { diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index d1e7feafc..4f6886b26 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1046,6 +1046,9 @@ fn damaged_sizes_are_skipped_or_refused() { // More column records than columns are refused. let many = [&record(390, &[])[..], &col_info(0, 0, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); assert!(matches!(sizes_of(&file(&sized_sheet(&many, &[]))), Err(Error::TooLarge(_)))); + // Skipped (reversed) column records count toward that cap too. + let reversed = [&record(390, &[])[..], &col_info(5, 4, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); + assert!(matches!(sizes_of(&file(&sized_sheet(&reversed, &[]))), Err(Error::TooLarge(_)))); // A sheet format or column after the data, or a record of the wrong size, fails the read. for (why, sheet) in [ ("format after data", worksheet(&[], &ws_fmt_info(u32::MAX, 8, 285, 0, [0, 0]))), From 0f944f62d5330c58bfe2cfdeac3d74881ec67748 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:44:03 -0500 Subject: [PATCH 008/471] craft-xlsx: review fixes: names using a left-out name are left out; quoted sheet-scoped calls - A name whose formula uses a name that is left out (another workbook, a skipped sheet, an unreadable formula) was still published, so a cell using it decoded and would calculate to #NAME?. Each name's formula now records the names it uses, and every name reached from a left-out one is left out too, whatever their order: once each, so a long chain costs no more than its uses. A cell using one keeps its saved result. - A sheet-scoped name called as a function through PtgNameX prints its sheet quoted where needed ('My Sheet'!Fn(1)), as a sheet-qualified name does. --- crates/xlsx/README.md | 24 +++++++++---------- crates/xlsx/src/xlsb.rs | 42 +++++++++++++++++++++++++++++++-- crates/xlsx/src/xlsb/expr.rs | 24 ++++++++++++++++++- crates/xlsx/src/xlsb/formula.rs | 10 +++++++- crates/xlsx/tests/xlsb.rs | 22 +++++++++++++++++ 5 files changed, 106 insertions(+), 16 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 75dd8602a..6e6291934 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -91,18 +91,18 @@ relative references as the cell they reach from A1, macro names with their `func newer functions and LET parameters are left out, as in `.xlsx`, and so is the formula-less workbook-level built-in Excel writes beside a sheet's (`Database`), which formulas still spell `_xlnm.Database`, and the formula-less name of a VBA function a cell calls (`.xlsm` lists none; the -cell spells it); a name whose formula can't be read is left out with a warning. A reference may name -an XLM macro sheet (`Macro1!$A$1`, where macro names point), and a 3-D span is spelled by its ends -whatever chart sheets lie between them. Its cell formulas are decoded from their tokens into the -text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, apart from the four ISO/IEC -29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks typed in the -formula, numbers as Excel spells them), with `FormulaKind::Shared` or `FormulaKind::Array` and their -ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own -text, decoded at the cell, since the file keeps the tokens once. A formula whose decoding needs -something the reader does not read keeps only its saved result, with a warning saying why: a defined -name left out, table references, other workbooks, Python code stored outside the cell, data tables. -The XLSB function table (`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from -`craft-xls`'s. +cell spells it); a name whose formula can't be read is left out with a warning, and so is every name +whose formula uses a left-out name. A reference may name an XLM macro sheet (`Macro1!$A$1`, where +macro names point), and a 3-D span is spelled by its ends whatever chart sheets lie between them. +Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and +`_xlpm.` names as written, apart from the four ISO/IEC 29500 functions `.xlsx` names bare, no +implicit `@`, the spaces and line breaks typed in the formula, numbers as Excel spells them), with +`FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell +metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the +file keeps the tokens once. A formula whose decoding needs something the reader does not read keeps +only its saved result, with a warning saying why: a defined name left out, table references, other +workbooks, Python code stored outside the cell, data tables. The XLSB function table +(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. ## Hostile input diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 1eb7cddc1..22976d826 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -397,7 +397,10 @@ fn defined_names( budget: &Budget, warnings: &mut Warnings, ) -> Vec { - let mut out = vec![]; + // Each name's formula, and the names it uses. A name whose formula uses a name that is left + // out is left out too, whatever their order, so a cell using it keeps its saved result. + let mut omitted = vec![false; links.names.len()]; + let mut kept = vec![]; for p in pending { let Some(name) = links.names.get(p.index) else { continue }; if name.future || formula::is_param(&name.text) { @@ -409,6 +412,7 @@ fn defined_names( Some(Some(i)) => Some(*i), _ => { warnings.warn("XLSB: some defined names were left out (names of sheets the reader skips)"); + omit(&mut omitted, p.index); continue; } }, @@ -421,17 +425,46 @@ fn defined_names( } continue; } - let formula = match formula::decode(&p.rgce, &p.rgcb, links, Mode::Name) { + let mut used = vec![]; + let formula = match formula::decode_using(&p.rgce, &p.rgcb, links, Mode::Name, &mut used) { Ok(e) => expr::print(&e), Err(formula::Unsupported(why)) => { warnings.warn(format!("XLSB: some defined names were left out ({why})")); + omit(&mut omitted, p.index); continue; } }; if !budget.try_formula_text(formula.len(), limits) { warnings.warn("XLSB: some defined names were left out (more formula text than the limits allow)"); + omit(&mut omitted, p.index); continue; } + kept.push((p, local_sheet_id, formula, used)); + } + // Who uses each name, then every name reached from a left-out one: once each, so a chain + // of any length costs what its uses do. + let mut users: HashMap> = HashMap::new(); + for (k, (.., used)) in kept.iter().enumerate() { + for &u in used { + users.entry(u).or_default().push(k); + } + } + let mut queue: Vec = (0..omitted.len()).filter(|&i| omitted[i]).collect(); + while let Some(i) = queue.pop() { + for &k in users.get(&i).map_or(&[][..], Vec::as_slice) { + let Some((p, ..)) = kept.get(k) else { continue }; + if omit(&mut omitted, p.index) { + warnings.warn("XLSB: some defined names were left out (names using a name that was left out)"); + queue.push(p.index); + } + } + } + let mut out = vec![]; + for (p, local_sheet_id, formula, _) in kept { + if omitted.get(p.index).copied().unwrap_or(true) { + continue; + } + let Some(name) = links.names.get(p.index) else { continue }; let mut attrs = vec![]; let mut put = |k: &str, v: String| attrs.push((k.to_string(), v)); // In `CT_DefinedName`'s order. @@ -478,6 +511,11 @@ fn defined_names( out } +/// Marks name `i` left out; whether it wasn't already. +fn omit(omitted: &mut [bool], i: usize) -> bool { + omitted.get_mut(i).is_some_and(|o| !std::mem::replace(o, true)) +} + /// A supporting link (`BrtSupSelf`, `BrtSupSame`, `BrtSupAddin`, `BrtSupBookSrc`), the sheet links /// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`; a name's other parts go to /// `pending`. diff --git a/crates/xlsx/src/xlsb/expr.rs b/crates/xlsx/src/xlsb/expr.rs index eee43a557..4c5efe4f7 100644 --- a/crates/xlsx/src/xlsb/expr.rs +++ b/crates/xlsx/src/xlsb/expr.rs @@ -283,6 +283,20 @@ fn write_args(s: &mut String, args: &[Expr], sep: char) { } } +/// A function's name; a sheet-scoped one (`My Sheet!Fn`) is quoted as a sheet-qualified name is. +/// Kept out of [`write_expr`], whose frame every nesting level of a formula repeats. +#[inline(never)] +fn push_function_name(s: &mut String, name: &str) { + match sheet_split(name) { + Some((sheet, name)) => { + s.push_str("e_sheet(sheet)); + s.push('!'); + s.push_str(name); + } + None => s.push_str(name), + } +} + fn write_expr(s: &mut String, e: &Expr) { match e { Expr::Number(n) => s.push_str(&number(*n)), @@ -331,7 +345,7 @@ fn write_expr(s: &mut String, e: &Expr) { write_expr(s, b); } Expr::Call(name, args) => { - s.push_str(name); + push_function_name(s, name); s.push('('); write_args(s, args, ','); s.push(')'); @@ -377,4 +391,12 @@ mod tests { assert_eq!(number_to_text(1e100), "1E+100"); assert!(number(f64::MAX).parse::().is_ok_and(f64::is_finite)); } + + #[test] + fn a_sheet_scoped_function_name_is_quoted() { + let call = |name: &str| print(&Expr::Call(name.into(), vec![Expr::Number(1.0)])); + assert_eq!(call("My Sheet!Fn"), "'My Sheet'!Fn(1)"); + assert_eq!(call("Data!Fn"), "Data!Fn(1)"); + assert_eq!(call("SUM"), "SUM(1)"); + } } diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 2c5712d33..46f7b5acd 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -338,6 +338,11 @@ fn reference(sheet: Option, kind: RefKind) -> Expr { /// represented. A shared formula's tokens are decoded once for each of its cells /// (`Mode::Shared` with that cell), whose relative references are offsets from it. pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Result { + decode_using(rgce, rgcb, links, mode, &mut Vec::new()) +} + +/// [`decode`], adding to `used` the 0-based index of every defined name the formula uses. +pub(super) fn decode_using(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode, used: &mut Vec) -> Result { if rgce.len() > MAX_RGCE { return Err(Unsupported("formula too long")); } @@ -512,7 +517,9 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res } 0x23 => { // PtgName: a 1-based index into every BrtName, hidden ones included. - let name = links.name(t.u32()?, mode)?; + let index = t.u32()?; + let name = links.name(index, mode)?; + used.extend(usize::try_from(index).ok().and_then(|i| i.checked_sub(1))); push(&mut stack, kids, Expr::Name(name.text.clone()))?; } 0x24 => { @@ -553,6 +560,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res let index = t.u32()?; links.this_book(ixti)?; let name = links.name(index, mode)?; + used.extend(usize::try_from(index).ok().and_then(|i| i.checked_sub(1))); let sheet = name.scope.and_then(|s| links.sheets.get(usize::try_from(s).ok()?).cloned().flatten()); let sheet = sheet.ok_or(Unsupported("names in other workbooks or on other sheets"))?; push(&mut stack, kids, Expr::Name(format!("{sheet}!{}", name.text)))?; diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 6502c1e5d..4d8f12d75 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1280,3 +1280,25 @@ fn a_spent_formula_allowance_skips_decoding() { assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings); } + +#[test] +fn a_name_using_a_left_out_name_is_left_out_too() { + // 14 Early = Later (a later name), 15 Alias = External (left out: another workbook), + // 16 Later = Alias, 17 Fine = Rate. Early, Alias and Later are left out whatever their order, + // and cells using them keep their saved results; Fine is read. + let mut list = names(); + for (text, index) in [("Early", 16u32), ("Alias", 3), ("Later", 15), ("Fine", 9)] { + list.push(brt_name(0, 0, WORKBOOK, text, &[&[0x23u8][..], &index.to_le_bytes()].concat(), None)); + } + let cell = |i: u32| [&[0x23u8][..], &i.to_le_bytes()].concat(); + let data = + [row(0), formula_cell(1, &cell(14), &[]), formula_cell(2, &cell(15), &[]), formula_cell(3, &cell(16), &[]), formula_cell(4, &cell(17), &[])] + .concat(); + let bytes = package(&book_with_names(&list), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + let xml = NAMES_XML.replace("", r#"Rate"#); + assert_eq!(wb.defined_names, xml_names(&xml)); + let book = read(&bytes).unwrap(); + assert_eq!((1..=4).map(|c| book.sheets[0].formula(CellRef::new(0, c))).collect::>(), [None, None, None, Some("Fine")]); + assert!(book.warnings.iter().any(|w| w.contains("names using a name that was left out")), "{:?}", book.warnings); +} From 76ddfc37493022c9127095f8fa20a23429ba59a6 Mon Sep 17 00:00:00 2001 From: Alex Chambers Date: Sat, 14 Nov 2026 21:57:59 +0100 Subject: [PATCH 009/471] craft-heif: items reads the primary image's metadata; add scan and exif_tiff Port the container reader of storytold/lightcraft#1208 into `items`: - `read` follows `pitm`, `cdsc` and the `dimg` inputs to return the primary image's EXIF and XMP, then items tied to no image. A thumbnail's or depth map's metadata is no longer the photo's. - `scan` returns every EXIF and XMP item, with each item's bytes or why they can't be read, and `Scan::complete` is false when the item catalog can't be read in full. An app that must prove a file has no location refuses when unsure. - `exif_tiff` finds the TIFF stream with or without a correct offset field or an `Exif\0\0` prefix. - `iloc` is walked once; the module denies indexing and unchecked arithmetic; boxes, items and gathered bytes are bounded. The tests of #1055 and lightcraft#1208 come along, adapted to assert the returned bytes and the scan's completeness. --- crates/heif/Cargo.toml | 3 + crates/heif/src/items.rs | 1044 +++++++++++++++++----------- crates/heif/src/items/tests.rs | 1173 ++++++++++++++++++++++++++++++++ 3 files changed, 1806 insertions(+), 414 deletions(-) create mode 100644 crates/heif/src/items/tests.rs diff --git a/crates/heif/Cargo.toml b/crates/heif/Cargo.toml index a5734e611..9cd59453f 100644 --- a/crates/heif/Cargo.toml +++ b/crates/heif/Cargo.toml @@ -19,6 +19,9 @@ heic-rs = { version = "=0.1.4", default-features = false, features = ["std"], op heic-rs = { version = "=0.1.4", default-features = false, features = ["std", "parallel"], optional = true } rayon = { workspace = true, optional = true } +[dev-dependencies] +proptest = { workspace = true } + [features] default = ["decode"] # The decoder (probe, decode, decode_thumbnail, read_metadata): heic-rs for the container and HEVC. diff --git a/crates/heif/src/items.rs b/crates/heif/src/items.rs index 7d4ffe0dc..d00dbdf06 100644 --- a/crates/heif/src/items.rs +++ b/crates/heif/src/items.rs @@ -1,223 +1,652 @@ //! HEIF / HEIC / AVIF metadata from the box structure alone (feature `metadata`): the `Exif` and -//! XMP items of the ISO base media file container (box structure from ISO/IEC 14496-12, item model -//! from ISO/IEC 23008-12). It needs no HEVC decoder and no heic-rs, so a build that only catalogues +//! XMP items of the image file format built on the ISO base media file format (ISO/IEC 23008-12 +//! on ISO/IEC 14496-12). It needs no HEVC decoder and no heic-rs, so a build that only catalogues //! photos can read their capture time, camera and lens. //! -//! The `meta` box holds an item information box (`iinf`, one `infe` per item) naming each item's -//! type, and an item location box (`iloc`) saying where its bytes are: in the file, or in the -//! `idat` box next to it. The `Exif` item is a four-byte offset followed by a TIFF stream; the XMP -//! packet is a `mime` item whose content type is `application/rdf+xml`. The first such item counts -//! (the decoder's [`crate::read_metadata`] takes the primary image's instead). Moved from LightCraft's -//! `lightcraft-meta`. +//! - `ftyp` names a HEIF or AVIF brand ([`is_heif`]). +//! - The top-level `meta` box lists the items (`iinf`, one `infe` each), says where each item's +//! bytes are (`iloc`: extents in the file, construction method 0, or in the `meta` box's own +//! `idat`, method 1), which item is the photo (`pitm`) and how items relate (`iref`). +//! - EXIF is an item of type `Exif`: a 4-byte big-endian offset to the TIFF header, then the +//! payload (ISO/IEC 23008-12 annex A). iPhones write offset 6 and an `Exif\0\0` prefix; libheif +//! and avif-serialize write offset 0 and the TIFF stream ([`exif_tiff`]). +//! - XMP is an item of type `mime` with content type `application/rdf+xml`. +//! - A `cdsc` ("content describes") reference ties a metadata item to the image it describes. The +//! photo's items are those tied to the primary image or to an image it is made from (its `dimg` +//! inputs: a grid's tiles, the picture an `iden` turns, an `iovl` overlay's or a `tmap` tone +//! map's inputs), else those tied to no image (they describe the file). An item tied only to +//! other images (a thumbnail, a depth map, another photo of a collection) is not this photo's. +//! +//! [`read`] returns the photo's own EXIF and XMP, as the decoder's [`crate::read_metadata`] does. +//! [`scan`] returns every EXIF and XMP item instead, whichever image it describes, and says when +//! it can't be sure it saw them all ([`Scan::complete`], [`Unreadable`]). An app that must prove a +//! file holds no location before copying it out (export's "remove location") refuses when unsure. +//! +//! The ICC profile is left to the decoder, which reads it for rendering. Every size and offset is +//! checked against the buffer; the boxes read, the items considered and the bytes gathered are +//! bounded, and `iloc` is walked once. Moved from LightCraft's `lightcraft-meta`. +#![deny(clippy::indexing_slicing, clippy::arithmetic_side_effects, clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use std::collections::{HashMap, HashSet}; use crate::{Error, SEQUENCE}; -/// The Exif and XMP blocks found. -#[derive(Default)] -struct Embedded { - exif: Option>, - xmp: Option, -} +/// The `ftyp` brands of HEIF still images and image sequences, and of AVIF. +const IMAGE_BRANDS: [&[u8; 4]; 12] = [b"mif1", b"msf1", b"heic", b"heix", b"hevc", b"hevx", b"heim", b"heis", b"hevm", b"hevs", b"avif", b"avis"]; +/// Brands of other formats in the same container (Canon CR3, JPEG XL), which win when listed too. +const OTHER_BRANDS: [&[u8; 4]; 2] = [b"crx ", b"jxl "]; -/// The first `Exif` item's TIFF stream (without HEIF's offset header) and the first XMP packet of a -/// HEIF file, as [`crate::read_metadata`] returns them. An error when the file is not an ISO base -/// media still image ([`is_heif`]), or has no `meta` box (an image sequence); an item that can't be -/// located is left out rather than reported. -pub fn read(bytes: &[u8]) -> Result<(Option>, Option), Error> { - if !is_heif(bytes) { - return Err(Error::Malformed("not a HEIF file".into())); - } - let mut budget = MAX_BOXES; - if !boxes(bytes, 0, bytes.len(), &mut budget).iter().any(|b| &b.kind == b"meta") { - return Err(Error::Unsupported(SEQUENCE)); - } - let e = metadata(bytes); - Ok((e.exif, e.xmp)) -} - -/// At most this many boxes are walked in one file. -pub const MAX_BOXES: usize = 4096; - -/// Brands that declare a still image in the ISO base media container. -const IMAGE_BRANDS: [&[u8]; 12] = [b"heic", b"heix", b"hevc", b"hevx", b"heim", b"heis", b"hevm", b"hevs", b"mif1", b"msf1", b"avif", b"avis"]; - -/// Longest `ftyp` box this reads brands from. +/// How far into the `ftyp` box brands are looked for. pub const MAX_FTYP: usize = 4096; -/// Most items one file's `iinf` / `iloc` describes (an iPhone HEIC has a handful). -pub const MAX_ITEMS: usize = 1024; -/// Most extents one item's bytes are split into. -pub const MAX_EXTENTS: u16 = 1024; -/// Largest metadata item this returns; a real photo's Exif and XMP are a few hundred kilobytes. -pub const MAX_ITEM: usize = 32 << 20; +/// Boxes read in one file. An iPhone photo has about a hundred items, a large grid a few hundred. +pub const MAX_BOXES: usize = 1 << 16; +/// Metadata items [`read`] considers for the photo's own EXIF and XMP (a real file has one or two). +pub const MAX_CANDIDATES: usize = 64; +/// Metadata items [`scan`] returns; a file with more is not [`Scan::complete`]. +pub const MAX_SCANNED: usize = 1024; +/// The most bytes one metadata item may gather (EXIF and XMP are kilobytes). +pub const MAX_ITEM: usize = 16 << 20; +/// The most bytes [`scan`] gathers over all items. +pub const MAX_SCANNED_BYTES: usize = 64 << 20; -/// Does this file declare an ISO base media still image (HEIF / HEIC / AVIF)? +/// Does the `ftyp` box name a HEIF or AVIF brand (and not CR3 or JPEG XL)? pub fn is_heif(bytes: &[u8]) -> bool { if bytes.get(4..8) != Some(b"ftyp".as_slice()) { return false; } - let Some(size) = be32(bytes, 0).map(|v| v as usize) else { - return false; - }; - let end = size.min(bytes.len()).min(MAX_FTYP); - let brand = |at: usize| bytes.get(at..at.saturating_add(4)).is_some_and(|b| IMAGE_BRANDS.contains(&b)); - // the major brand, then (after the minor version) the compatible brands - brand(8) || (16..end).step_by(4).any(brand) + // major brand at 8, minor version at 12, compatible brands from 16 to the end of the box + let size = be32(bytes, 0).and_then(|s| usize::try_from(s).ok()).unwrap_or(0); + let end = size.clamp(12, MAX_FTYP).min(bytes.len()); + let compatible = (16..end).step_by(4).filter_map(|at| bytes.get(at..at.checked_add(4).filter(|&e| e <= end)?)); + let mut heif = false; + for brand in bytes.get(8..12).into_iter().chain(compatible) { + if OTHER_BRANDS.iter().any(|b| b.as_slice() == brand) { + return false; + } + heif |= IMAGE_BRANDS.iter().any(|b| b.as_slice() == brand); + } + heif } -/// The Exif and XMP blocks of a HEIF file. Anything malformed is left out rather than reported: -/// a file whose items can't be located simply has no metadata, as before this reader existed. -fn metadata(bytes: &[u8]) -> Embedded { - let mut out = Embedded::default(); - let mut budget = MAX_BOXES; - let Some(meta) = boxes(bytes, 0, bytes.len(), &mut budget).into_iter().find(|b| &b.kind == b"meta") else { - return out; +/// The photo's EXIF (its TIFF stream, see [`exif_tiff`]) and XMP packet, as +/// [`crate::read_metadata`] returns them: the primary image's items first, then those tied to no +/// image; the first that reads well wins. An error when the file is not HEIF ([`is_heif`]) or has +/// no `meta` box (an image sequence); an item that can't be read is left out rather than reported. +pub fn read(bytes: &[u8]) -> Result<(Option>, Option), Error> { + let c = Container::read(bytes, MAX_CANDIDATES).ok_or_else(not_heif)?; + if !c.has_meta { + return Err(Error::Unsupported(SEQUENCE)); + } + let ranked = |kind: Kind| { + let mut order: Vec<(u8, usize)> = + c.items.iter().enumerate().filter(|(_, it)| it.kind == kind).filter_map(|(i, it)| Some((it.rank(c.primary.is_some())?, i))).collect(); + order.sort_by_key(|&(rank, _)| rank); + order.into_iter().filter_map(|(_, i)| match c.gather(i, MAX_ITEM) { + Gathered::Bytes(b) => Some(b), + _ => None, + }) }; - // `meta` is a FullBox: a version and flags before its children - let (mut items, mut locs, mut idat) = (Vec::new(), Vec::new(), None); - for b in boxes(bytes, meta.body.saturating_add(4), meta.end, &mut budget) { - match &b.kind { - b"iinf" => items = item_infos(bytes, &b, &mut budget), - b"iloc" => locs = item_locations(bytes.get(b.body..b.end).unwrap_or_default()), - b"idat" => idat = bytes.get(b.body..b.end), - _ => {} + let exif = ranked(Kind::Exif).find_map(|item| exif_tiff(&item).map(<[u8]>::to_vec)); + let xmp = ranked(Kind::Xmp).next().map(|item| String::from_utf8_lossy(&item).trim_end_matches('\0').to_string()); + Ok((exif, xmp)) +} + +/// Every EXIF and XMP item of a HEIF file, whichever image it describes, in `iinf` order. An error +/// only when the file is not HEIF ([`is_heif`]); a file without a `meta` box has no items. +/// +/// The scan says when it may be missing something: [`Scan::complete`] is false when the item +/// catalog can't be read in full, and an item whose bytes can't be read comes back as +/// [`Unreadable`]. An item a complete `iloc` doesn't place is left out: it has no bytes. +pub fn scan(bytes: &[u8]) -> Result, Error> { + scan_within(bytes, MAX_SCANNED_BYTES) +} + +/// [`scan`], gathering at most `budget` bytes over all items. +fn scan_within(bytes: &[u8], budget: usize) -> Result, Error> { + let container = Container::read(bytes, MAX_SCANNED).ok_or_else(not_heif)?; + Ok(Scan { container, next: 0, budget }) +} + +fn not_heif() -> Error { + Error::Malformed("not a HEIF file".into()) +} + +/// The metadata items of a HEIF file, from [`scan`]: an iterator over [`Item`]s. +pub struct Scan<'a> { + container: Container<'a>, + next: usize, + /// Bytes still to gather, of [`MAX_SCANNED_BYTES`]. + budget: usize, +} + +impl Scan<'_> { + /// Whether the iterator returns every metadata item in the file: the boxes up to and through + /// the item catalog were read to their ends, there was one `meta` box with one `iinf`, every + /// catalog entry was understood, and there were at most [`MAX_SCANNED`] metadata items. A + /// complete file without a `meta` box is complete, with no items. + pub fn complete(&self) -> bool { + self.container.catalog_complete && !self.container.more + } +} + +impl Iterator for Scan<'_> { + type Item = Item; + + fn next(&mut self) -> Option { + loop { + let i = self.next; + let (id, kind) = self.container.items.get(i).map(|c| (c.id, c.kind))?; + self.next = i.saturating_add(1); + let data = match self.container.gather(i, MAX_ITEM.min(self.budget)) { + Gathered::Bytes(b) => { + self.budget = self.budget.saturating_sub(b.len()); + Ok(b) + } + Gathered::NotPlaced => continue, + Gathered::Unreadable => Err(Unreadable::Placement), + Gathered::TooLarge => Err(Unreadable::TooLarge), + }; + return Some(Item { id, kind, data }); } } - for (id, kind, content_type) in items { - let want_exif = &kind == b"Exif" && out.exif.is_none(); - let want_xmp = &kind == b"mime" && content_type == "application/rdf+xml" && out.xmp.is_none(); - if !want_exif && !want_xmp { - continue; - } - let Some(loc) = locs.iter().find(|l| l.id == id) else { continue }; - let Some(block) = item_bytes(bytes, idat, loc) else { continue }; - if want_exif { - // ISO/IEC 23008-12 A.2.1: a four-byte offset from the end of itself to the TIFF header - let skip = be32(&block, 0).and_then(|v| usize::try_from(v).ok()).and_then(|v| v.checked_add(4)); - out.exif = skip.and_then(|at| block.get(at..)).filter(|b| !b.is_empty()).map(<[u8]>::to_vec); +} + +/// One metadata item from [`scan`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Item { + /// Its item id. + pub id: u32, + pub kind: Kind, + /// Its bytes as stored (for EXIF, with the offset field: see [`exif_tiff`]), or why there are + /// none. + pub data: Result, Unreadable>, +} + +/// What a metadata item holds. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Kind { + /// An `Exif` item. + Exif, + /// An XMP packet: a `mime` item of type `application/rdf+xml`. + Xmp, + /// An XMP packet with a content encoding (compressed). Its bytes are returned as stored; this + /// crate doesn't decode them. + EncodedXmp, +} + +/// Why a metadata item's bytes can't be read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Unreadable { + /// `iloc` is missing or damaged before the item's entry, or the entry points outside the file + /// (or `idat`), into another file, or builds the item from other items (method 2). + Placement, + /// More than [`MAX_ITEM`] bytes, or more than [`scan`] has left of [`MAX_SCANNED_BYTES`]. + TooLarge, +} + +/// The TIFF stream of an `Exif` item: past its 4-byte offset field and the bytes that offset +/// skips. An item whose offset is wrong, or that has no offset field, is read too when an +/// `Exif\0\0` prefix or the TIFF header itself shows where the stream starts. `None` when no TIFF +/// header is found. +pub fn exif_tiff(item: &[u8]) -> Option<&[u8]> { + let tiff = |b: &&[u8]| b.starts_with(b"II*\0") || b.starts_with(b"MM\0*"); + let stated = be32(item, 0).and_then(|n| usize::try_from(n).ok()?.checked_add(4)).and_then(|at| item.get(at..)); + let after_field = item.get(4..).map(strip_exif_header); + [stated, after_field, Some(strip_exif_header(item))].into_iter().flatten().find(tiff) +} + +/// `b` without a leading `Exif\0` and the byte after it (the JPEG APP1 prefix), if it has one. +fn strip_exif_header(b: &[u8]) -> &[u8] { + match b.get(..5) { + Some(b"Exif\0") => b.get(6..).unwrap_or(b), + _ => b, + } +} + +/// A metadata item and the images it describes. +#[derive(Debug)] +struct Candidate { + id: u32, + kind: Kind, + /// Tied to some image by a `cdsc` reference. + describes: bool, + /// Tied to the primary image or an image it is made from. + describes_primary: bool, +} + +impl Candidate { + /// 0 for the photo's own, 1 for one tied to no image (or any, when the file names no primary + /// image that exists); `None` for one that describes other images only. + fn rank(&self, has_primary: bool) -> Option { + if self.describes_primary { + Some(0) + } else if !self.describes || !has_primary { + Some(1) } else { - out.xmp = Some(String::from_utf8_lossy(&block).trim_end_matches('\0').to_string()); + None } } - out } -/// `iinf` → one `(item_ID, item_type, content_type)` per `infe` entry. Entries of version 0 and 1 -/// name no item type and are skipped; HEIF files use version 2 or 3. -fn item_infos(bytes: &[u8], iinf: &BoxRef, budget: &mut usize) -> Vec<(u32, [u8; 4], String)> { - // FullBox version and flags, then a 16- or 32-bit entry count before the `infe` boxes - let version = *bytes.get(iinf.body).unwrap_or(&0); - let entries_at = iinf.body.saturating_add(if version == 0 { 6 } else { 8 }); - let mut out = Vec::new(); - for b in boxes(bytes, entries_at, iinf.end, budget) { - if &b.kind != b"infe" || out.len() >= MAX_ITEMS { - continue; +/// What the `meta` box says about the metadata items, and where their bytes are. +struct Container<'a> { + file: &'a [u8], + /// A top-level `meta` box was found. + has_meta: bool, + idat: Option<&'a [u8]>, + iloc: Option>, + /// The primary image, when `pitm` names an item `iinf` lists. + primary: Option, + /// The EXIF and XMP items, in `iinf` order (the first `cap` of them). + items: Vec, + /// More metadata items than `cap`. + more: bool, + /// Discovery read the whole item catalog, including each entry's type. + catalog_complete: bool, + /// Where each of `items` is (from one walk of `iloc`). + places: Vec>, + /// A complete `iloc` walk established that any unplaced item has no entry. + iloc_complete: bool, +} + +impl<'a> Container<'a> { + /// `None` when `bytes` is not HEIF; no items when there is no `meta` box. + fn read(bytes: &'a [u8], cap: usize) -> Option> { + if !is_heif(bytes) { + return None; } - let Some(e) = bytes.get(b.body..b.end) else { continue }; - let (id, kind_at) = match *e.first().unwrap_or(&0) { - 2 => (be16(e, 4).map(u32::from), 8), - 3 => (be32(e, 4), 10), + let mut c = Container { + file: bytes, + has_meta: false, + idat: None, + iloc: None, + primary: None, + items: Vec::new(), + more: false, + catalog_complete: false, + places: Vec::new(), + iloc_complete: false, + }; + let mut budget = MAX_BOXES; + let top = boxes(bytes, 0, bytes.len(), &mut budget); + c.catalog_complete = top.last().is_some_and(|b| b.end == bytes.len()); + // A complete file without an item `meta` has no item catalog (e.g. a sequence). + let Some(meta) = top.iter().find(|b| &b.kind == b"meta") else { return Some(c) }; + c.has_meta = true; + // `meta` is a full box: version and flags, then the boxes inside it. + let first = meta.body.saturating_add(4); + let inside = boxes(bytes, first, meta.end, &mut budget); + c.catalog_complete &= version(bytes, meta) == Some(0) + && first <= meta.end + && inside.last().map_or(first, |b| b.end) == meta.end + && top.iter().filter(|b| &b.kind == b"meta").count() == 1 + && inside.iter().filter(|b| &b.kind == b"iinf").count() == 1; + let child = |kind: &[u8; 4]| inside.iter().find(|b| &b.kind == kind); + let payload = |kind: &[u8; 4]| child(kind).and_then(|b| bytes.get(b.body..b.end)); + let mut ids = Vec::new(); + if let Some(iinf) = child(b"iinf") { + let catalog = item_infos(bytes, iinf, cap, &mut budget); + ids = catalog.ids; + c.items = catalog.items; + c.more = catalog.more; + c.catalog_complete &= catalog.complete; + } + c.primary = payload(b"pitm").and_then(primary_item).filter(|p| ids.binary_search(p).is_ok()); + if let Some(iref) = child(b"iref") { + link(bytes, iref, c.primary, &mut c.items, &mut budget); + } + c.idat = payload(b"idat"); + c.iloc = payload(b"iloc").and_then(Iloc::parse); + (c.places, c.iloc_complete) = match &c.iloc { + Some(iloc) => iloc.locate(&c.items), + None => (Vec::new(), false), + }; + Some(c) + } + + /// The bytes of `items[i]`, at most `limit` of them. + fn gather(&self, i: usize, limit: usize) -> Gathered { + match (&self.iloc, self.places.get(i)) { + (Some(iloc), Some(Some(place))) => iloc.gather(self.file, self.idat, place, limit), + _ if self.iloc_complete => Gathered::NotPlaced, + _ => Gathered::Unreadable, + } + } +} + +/// An item's bytes, or why there are none. +#[derive(Debug)] +enum Gathered { + Bytes(Vec), + /// A complete `iloc` walk found no entry for the item. + NotPlaced, + /// Placement unresolved, outside the data, in another file, or built from other items (method 2). + Unreadable, + /// More than the limit. + TooLarge, +} + +/// A full box's version: the first byte of its own payload (`None` for an empty box). +fn version(bytes: &[u8], b: &BoxRef) -> Option { + bytes.get(b.body..b.end)?.first().copied() +} + +/// `pitm`: the primary item's id (16 bits in version 0, else 32). +fn primary_item(pitm: &[u8]) -> Option { + match pitm.first()? { + 0 => be16(pitm, 4).map(u32::from), + _ => be32(pitm, 4), + } +} + +/// What `iinf` lists. Best-effort discovery is kept even when the catalog cannot establish that +/// all items were read. +#[derive(Default)] +struct ItemCatalog { + /// Every item id listed, sorted. + ids: Vec, + items: Vec, + more: bool, + complete: bool, +} + +/// `iinf` → its item ids and its metadata items (at most `cap`). The catalog is complete when the +/// table version is known, its count matches its entries, the entries fill the box, and each is +/// an `infe` this reader understands. +fn item_infos(bytes: &[u8], iinf: &BoxRef, cap: usize, budget: &mut usize) -> ItemCatalog { + let mut catalog = ItemCatalog::default(); + let Some(data) = bytes.get(iinf.body..iinf.end) else { return catalog }; + // Only the supported versions establish the count and where the entries start. + let (count, header) = match version(bytes, iinf) { + Some(0) => (be16(data, 4).map(u32::from), 6), + Some(1) => (be32(data, 4), 8), + _ => return catalog, + }; + let Some(count) = count else { return catalog }; + let first = iinf.body.saturating_add(header); + let entries = boxes(bytes, first, iinf.end, budget); + catalog.complete = entries.last().map_or(first, |b| b.end) == iinf.end && u32::try_from(entries.len()).ok() == Some(count); + for b in &entries { + let info = bytes.get(b.body..b.end).filter(|_| &b.kind == b"infe").and_then(item_info); + let Some((id, kind)) = info else { + catalog.complete = false; + continue; + }; + catalog.ids.push(id); + match kind { + Some(_) if catalog.items.len() >= cap => catalog.more = true, + Some(kind) => catalog.items.push(Candidate { id, kind, describes: false, describes_primary: false }), + None => {} + } + } + catalog.ids.sort_unstable(); + catalog.ids.dedup(); + catalog +} + +/// An `infe` entry's item id, and its kind when it is an unprotected `Exif` or XMP item (`mime`, +/// `application/rdf+xml`). `None` when the entry can't be read to its last field. +fn item_info(infe: &[u8]) -> Option<(u32, Option)> { + let version = *infe.first()?; + // version and flags, the item id (32 bits in version 3), the protection index + let (id, at) = match version { + 0..=2 => (u32::from(be16(infe, 4)?), 6usize), + 3 => (be32(infe, 4)?, 8usize), + _ => return None, + }; + if be16(infe, at)? != 0 { + return Some((id, None)); // protected (encrypted) + } + let at = at.checked_add(2)?; + // versions 0 and 1 have no item type and describe MIME items; 2 and 3 name the type + let (kind, at) = if version >= 2 { (infe.get(at..at.checked_add(4)?)?, at.checked_add(4)?) } else { (b"mime".as_slice(), at) }; + let (_name, at) = string(infe, at)?; + let kind = match kind { + b"Exif" => Some(Kind::Exif), + b"mime" => { + let (content_type, at) = string(infe, at)?; + // Content encoding may be absent, but a present string must terminate. + let encoding = if at == infe.len() { b"".as_slice() } else { string(infe, at)?.0 }; + match (content_type.trim_ascii().eq_ignore_ascii_case(b"application/rdf+xml"), encoding.trim_ascii().is_empty()) { + (true, true) => Some(Kind::Xmp), + (true, false) => Some(Kind::EncodedXmp), + (false, _) => None, + } + } + b"uri " => { + string(infe, at)?; + None + } + _ => None, + }; + Some((id, kind)) +} + +/// The NUL-terminated string at `at` and where the next field starts. +fn string(b: &[u8], at: usize) -> Option<(&[u8], usize)> { + let rest = b.get(at..)?; + let n = rest.iter().position(|&c| c == 0)?; + Some((rest.get(..n)?, at.checked_add(n)?.checked_add(1)?)) +} + +/// Mark the candidates that `cdsc` references tie to an image, and those tied to the primary image +/// or one it is made from (`dimg`, followed to any depth; each image once, so loops end). +fn link(bytes: &[u8], iref: &BoxRef, primary: Option, items: &mut [Candidate], budget: &mut usize) { + // full box; item ids are 16 bits in version 0, 32 in version 1 + let width = match version(bytes, iref) { + Some(0) => 2usize, + Some(1) => 4, + _ => return, + }; + let index: HashMap = items.iter().enumerate().rev().map(|(i, c)| (c.id, i)).collect(); + let mut inputs: HashMap> = HashMap::new(); + let mut described: Vec<(usize, Vec)> = Vec::new(); + for r in boxes(bytes, iref.body.saturating_add(4), iref.end, budget) { + let cdsc = match &r.kind { + b"cdsc" => true, + b"dimg" => false, _ => continue, }; - let (Some(id), Some(kind)) = (id, e.get(kind_at..kind_at + 4)) else { continue }; - let mut kind4 = [0u8; 4]; - kind4.copy_from_slice(kind); - // the item name, then (for a `mime` item) the content type, both NUL-terminated - let mut strings = e.get(kind_at + 4..).unwrap_or_default().split(|&c| c == 0).skip(1); - let content_type = String::from_utf8_lossy(strings.next().unwrap_or_default()).into_owned(); - out.push((id, kind4, content_type)); + let Some(body) = bytes.get(r.body..r.end) else { continue }; + let Some(from) = id_at(body, 0, width) else { continue }; + let item = index.get(&from).copied(); + if cdsc && item.is_none() { + continue; + } + let count = be16(body, width).unwrap_or(0); + let mut to = Vec::new(); + let mut at = width.saturating_add(2); + for _ in 0..count { + let Some(id) = id_at(body, at, width) else { break }; + to.push(id); + at = at.saturating_add(width); + } + match item { + Some(i) if cdsc => described.push((i, to)), + _ => inputs.entry(from).or_default().extend(to), + } + } + // the primary image and every image it is made from + let mut photo = HashSet::new(); + let mut todo: Vec = primary.into_iter().collect(); + while let Some(id) = todo.pop() { + if photo.insert(id) { + todo.extend(inputs.get(&id).into_iter().flatten().filter(|i| !photo.contains(*i))); + } + } + for (i, to) in described { + if let Some(item) = items.get_mut(i) + && !to.is_empty() + { + item.describes = true; + item.describes_primary |= to.iter().any(|t| photo.contains(t)); + } } - out } -/// Where one item's bytes are. -struct Loc { - id: u32, - /// 0 = offsets into the file, 1 = into the `idat` box, 2 = another item (not read). - construction: u8, +fn id_at(b: &[u8], at: usize, width: usize) -> Option { + if width == 2 { be16(b, at).map(u32::from) } else { be32(b, at) } +} + +/// `iloc`: its field sizes, read once. +struct Iloc<'a> { + data: &'a [u8], + version: u8, + offset_size: usize, + length_size: usize, + base_size: usize, + index_size: usize, +} + +/// Where an item is: how it is built, and its extents in `iloc`. +#[derive(Clone, Copy, Debug)] +struct Place { + method: u16, + data_reference: u16, base: u64, - /// `(offset, length)` per extent, in order. - extents: Vec<(u64, u64)>, + /// Where its first extent starts in `iloc`. + extents_at: usize, + extents: usize, } -/// `iloc` (payload after the box header) → one [`Loc`] per entry, stopping at the first -/// unreadable one. -fn item_locations(p: &[u8]) -> Vec { - let version = *p.first().unwrap_or(&0); - let (Some(sizes), Some(base_index)) = (p.get(4), p.get(5)) else { return Vec::new() }; - let (offset_size, length_size) = ((sizes >> 4) as usize, (sizes & 0xf) as usize); - let (base_size, index_size) = ((base_index >> 4) as usize, if version > 0 { (base_index & 0xf) as usize } else { 0 }); - let (count, mut at) = match version { - 0 | 1 => (be16(p, 6).map(usize::from), 8), - 2 => (be32(p, 6).and_then(|v| usize::try_from(v).ok()), 10), - _ => return Vec::new(), - }; - let mut out = Vec::new(); - for _ in 0..count.unwrap_or(0).min(MAX_ITEMS) { - let (id, after_id) = match version { - 2 => (be32(p, at), at + 4), - _ => (be16(p, at).map(u32::from), at + 2), - }; - // version 1 and 2 put the construction method in the low nibble of a reserved 16-bit field - let construction = if version == 0 { Some(0) } else { be16(p, after_id).map(|v| (v & 0xf) as u8) }; - let after_method = after_id + if version == 0 { 0 } else { 2 }; - // a data reference index of 0 means this file; an external reference has no bytes here - let external = be16(p, after_method).is_some_and(|v| v != 0); - let base = uint(p, after_method + 2, base_size); - let extent_at = after_method + 2 + base_size; - let (Some(id), Some(construction), Some(base), Some(extent_count)) = (id, construction, base, be16(p, extent_at)) else { - break; - }; - let stride = index_size + offset_size + length_size; - let mut extents = Vec::new(); - let mut e = extent_at + 2; - for _ in 0..extent_count.min(MAX_EXTENTS) { - let (Some(offset), Some(length)) = (uint(p, e + index_size, offset_size), uint(p, e + index_size + offset_size, length_size)) else { - return out; - }; - extents.push((offset, length)); - e += stride; +impl<'a> Iloc<'a> { + fn parse(data: &'a [u8]) -> Option> { + let version = *data.first()?; + if version > 2 { + return None; } - at = e; - if !external { - out.push(Loc { id, construction, base, extents }); + // version and flags, then four 4-bit field sizes: offset, length, base offset, extent index + let (sizes, more) = (*data.get(4)?, *data.get(5)?); + let (offset_size, length_size) = (usize::from(sizes >> 4), usize::from(sizes & 15)); + let base_size = usize::from(more >> 4); + let index_size = if version == 0 { 0 } else { usize::from(more & 15) }; // reserved in version 0 + if [offset_size, length_size, base_size, index_size].into_iter().any(|n| !matches!(n, 0 | 4 | 8)) { + return None; } + Some(Iloc { data, version, offset_size, length_size, base_size, index_size }) + } + + /// One walk over the entries: where each of `items` is (the first entry for an id wins). It + /// stops once all are placed, or where the entries stop making sense. The boolean is true + /// only if the walk exhausted the entries, so an absent placement is known to be absent. + fn locate(&self, items: &[Candidate]) -> (Vec>, bool) { + let mut places = vec![None; items.len()]; + if items.is_empty() { + return (places, false); + } + let wanted: HashMap = items.iter().enumerate().rev().map(|(i, c)| (c.id, i)).collect(); + let mut left = wanted.len(); + let complete = self.walk(|id, place| { + if let Some(&i) = wanted.get(&id) + && let Some(slot @ None) = places.get_mut(i) + { + *slot = Some(place); + left = left.saturating_sub(1); + } + left > 0 + }); + (places, complete.unwrap_or(false)) + } + + /// Call `f` with each entry's item id and place. `Some(true)` means every entry was read, + /// `Some(false)` means `f` stopped the walk, and `None` means an entry ran past the box. + fn walk(&self, mut f: impl FnMut(u32, Place) -> bool) -> Option { + let (version, data) = (self.version, self.data); + let id_size = if version == 2 { 4 } else { 2 }; + let (count, mut at) = if version == 2 { (be32(data, 6)?, 10usize) } else { (u32::from(be16(data, 6)?), 8usize) }; + let extent_size = self.index_size.checked_add(self.offset_size)?.checked_add(self.length_size)?; + // (every entry takes at least six bytes, so a count larger than the box ends at its end) + for _ in 0..count { + iloc_entry_visited(); + let id = uint(data, at, id_size)?; + at = at.checked_add(id_size)?; + let method = if version == 0 { + 0 + } else { + let m = be16(data, at)? & 15; + at = at.checked_add(2)?; + m + }; + let data_reference = be16(data, at)?; + let base = uint(data, at.checked_add(2)?, self.base_size)?; + at = at.checked_add(2)?.checked_add(self.base_size)?; + let extents = usize::from(be16(data, at)?); + at = at.checked_add(2)?; + let place = Place { method, data_reference, base, extents_at: at, extents }; + at = at.checked_add(extents.checked_mul(extent_size)?)?; + if at > data.len() { + return None; + } + if let Ok(id) = u32::try_from(id) + && !f(id, place) + { + return Some(false); + } + } + Some(true) + } + + /// An item's bytes, its extents in order from the file (construction method 0) or from the + /// `meta` box's `idat` (method 1), at most `limit` of them. + fn gather(&self, file: &[u8], idat: Option<&[u8]>, place: &Place, limit: usize) -> Gathered { + // data reference 0 is this file + let source = match (place.data_reference, place.method, idat) { + (0, 0, _) => file, + (0, 1, Some(idat)) => idat, + _ => return Gathered::Unreadable, + }; + let mut out = Vec::new(); + let mut at = place.extents_at; + for _ in 0..place.extents { + let Some((piece, next)) = self.extent(source, place.base, at) else { return Gathered::Unreadable }; + at = next; + if out.len().saturating_add(piece.len()) > limit { + return Gathered::TooLarge; + } + out.extend_from_slice(piece); + } + Gathered::Bytes(out) + } + + /// The extent at `at`: its bytes in `source` (a length of 0 runs to the end) and where the + /// next extent starts. + fn extent<'s>(&self, source: &'s [u8], base: u64, at: usize) -> Option<(&'s [u8], usize)> { + let at = at.checked_add(self.index_size)?; + let offset = uint(self.data, at, self.offset_size)?; + let at = at.checked_add(self.offset_size)?; + let length = uint(self.data, at, self.length_size)?; + let at = at.checked_add(self.length_size)?; + let rest = source.get(usize::try_from(base.checked_add(offset)?).ok()?..)?; + let piece = if length == 0 { rest } else { rest.get(..usize::try_from(length).ok()?)? }; + Some((piece, at)) } - out } -/// A big-endian unsigned integer of `size` bytes; `size` 0 is the value 0, and the item boxes -/// allow no other width than 4 or 8. +#[cfg(test)] +thread_local! { + /// `iloc` entries walked on this thread, for the tests that bound the work. + static ILOC_ENTRIES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +fn iloc_entry_visited() { + #[cfg(test)] + ILOC_ENTRIES.with(|n| n.set(n.get().saturating_add(1))); +} + +/// A big-endian unsigned field of `size` bytes (0 reads as 0). fn uint(b: &[u8], at: usize, size: usize) -> Option { match size { 0 => Some(0), + 2 => be16(b, at).map(u64::from), 4 => be32(b, at).map(u64::from), 8 => be64(b, at), _ => None, } } -/// An item's bytes, its extents concatenated. `None` when they don't fit the file (or the `idat` -/// box), when the item is built by referencing another one, or when it is implausibly large. -fn item_bytes(bytes: &[u8], idat: Option<&[u8]>, loc: &Loc) -> Option> { - let src = match loc.construction { - 0 => bytes, - 1 => idat?, - _ => return None, - }; - let mut out = Vec::new(); - for &(offset, length) in &loc.extents { - let at = loc.base.checked_add(offset)?; - let (start, end) = (usize::try_from(at).ok()?, usize::try_from(at.checked_add(length)?).ok()?); - let extent = src.get(start..end)?; - if out.len().checked_add(extent.len())? > MAX_ITEM { - return None; - } - out.extend_from_slice(extent); - } - (!out.is_empty()).then_some(out) -} - -/// A box: its type and where its payload ends. +/// A box: its type, where its payload starts and where it ends. struct BoxRef { kind: [u8; 4], /// Start of the payload (after the header and, for `uuid`, the 16-byte uuid). @@ -225,33 +654,33 @@ struct BoxRef { end: usize, } -/// The boxes directly inside `[from, to)`; stops at the first malformed header. +/// The boxes directly inside `[from, to)`, at most `budget` of them (shared over the calls of one +/// read); stops at the first malformed header. fn boxes(bytes: &[u8], from: usize, to: usize, budget: &mut usize) -> Vec { let mut out = Vec::new(); - let mut at = from; let to = to.min(bytes.len()); - while at.saturating_add(8) <= to && *budget > 0 { - *budget -= 1; - let (Some(size), Some(kind)) = (be32(bytes, at), bytes.get(at + 4..at + 8)) else { break }; - let mut kind4 = [0u8; 4]; - kind4.copy_from_slice(kind); - let (len, mut hdr) = match size { - 0 => (to - at, 8), - 1 => match be64(bytes, at + 8).and_then(|v| usize::try_from(v).ok()) { - Some(l) => (l, 16), + let mut at = from; + while *budget > 0 { + let Some(header_end) = at.checked_add(8).filter(|&e| e <= to) else { break }; + *budget = budget.saturating_sub(1); + let (Some(size), Some(kind)) = (be32(bytes, at), bytes.get(at.saturating_add(4)..header_end)) else { break }; + let Ok(kind) = <[u8; 4]>::try_from(kind) else { break }; + let (len, header) = match size { + 0 => (to.saturating_sub(at), 8usize), + 1 => match be64(bytes, header_end).and_then(|v| usize::try_from(v).ok()) { + Some(len) => (len, 16), None => break, }, - n => (n as usize, 8), + n => match usize::try_from(n) { + Ok(len) => (len, 8), + Err(_) => break, + }, }; - let Some(end) = at.checked_add(len).filter(|&e| e <= to && len >= hdr) else { break }; - if &kind4 == b"uuid" { - // the 16-byte extended type is part of the header - if at + hdr + 16 > end { - break; - } - hdr += 16; - } - out.push(BoxRef { kind: kind4, body: at + hdr, end }); + // the 16-byte extended type of a `uuid` box is part of its header + let header = if &kind == b"uuid" { header.saturating_add(16) } else { header }; + let Some(end) = at.checked_add(len).filter(|&e| e <= to && len >= header) else { break }; + let Some(body) = at.checked_add(header) else { break }; + out.push(BoxRef { kind, body, end }); at = end; } out @@ -259,231 +688,18 @@ fn boxes(bytes: &[u8], from: usize, to: usize, budget: &mut usize) -> Vec Option { - b.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]])) + b.get(at..at.checked_add(2)?)?.try_into().ok().map(u16::from_be_bytes) } + /// A big-endian `u32` at `at`, or `None` when the buffer is shorter than that. fn be32(b: &[u8], at: usize) -> Option { - b.get(at..at.checked_add(4)?).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])) + b.get(at..at.checked_add(4)?)?.try_into().ok().map(u32::from_be_bytes) } + /// A big-endian `u64` at `at`, or `None` when the buffer is shorter than that. fn be64(b: &[u8], at: usize) -> Option { - let s = b.get(at..at.checked_add(8)?)?; - let mut a = [0u8; 8]; - a.copy_from_slice(s); - Some(u64::from_be_bytes(a)) + b.get(at..at.checked_add(8)?)?.try_into().ok().map(u64::from_be_bytes) } #[cfg(test)] -mod tests { - use super::*; - - /// One box: its own size, its four-letter kind, then its payload. - fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec { - let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec(); - v.extend_from_slice(kind); - v.extend_from_slice(payload); - v - } - - /// A FullBox: a box whose payload begins with a version byte and three flag bytes. - fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec { - bx(kind, &[&[version, 0, 0, 0][..], payload].concat()) - } - - struct Item<'a> { - kind: &'a [u8; 4], - content_type: &'a str, - data: Vec, - } - - /// A HEIC-shaped file holding `items`: `ftyp`, a `meta` box with one `infe` per item, and the - /// item bytes in `idat` (`in_idat`, construction method 1) or in `mdat` (method 0). - fn heic(items: &[Item<'_>], iloc_version: u8, in_idat: bool) -> Vec { - let mut iinf = (items.len() as u16).to_be_bytes().to_vec(); - for (i, it) in items.iter().enumerate() { - let mut e = vec![2u8, 0, 0, 0]; // version 2, flags - e.extend_from_slice(&(i as u16 + 1).to_be_bytes()); - e.extend_from_slice(&[0, 0]); // protection index - e.extend_from_slice(it.kind); - e.extend_from_slice(b"an item\0"); - if it.kind == b"mime" { - e.extend_from_slice(it.content_type.as_bytes()); - e.push(0); - } - iinf.extend_from_slice(&bx(b"infe", &e)); - } - let iinf = full_box(b"iinf", 0, &iinf); - let payload: Vec = items.iter().flat_map(|i| i.data.iter().copied()).collect(); - let idat = if in_idat { bx(b"idat", &payload) } else { Vec::new() }; - let iloc = |base: u32| { - // four-byte offsets and lengths, no base offset and no extent index - let mut l = vec![0x44, 0x00]; - match iloc_version { - 2 => l.extend_from_slice(&(items.len() as u32).to_be_bytes()), - _ => l.extend_from_slice(&(items.len() as u16).to_be_bytes()), - } - let mut at = base; - for (i, it) in items.iter().enumerate() { - match iloc_version { - 2 => l.extend_from_slice(&(i as u32 + 1).to_be_bytes()), - _ => l.extend_from_slice(&(i as u16 + 1).to_be_bytes()), - } - if iloc_version > 0 { - l.extend_from_slice(&[0, u8::from(in_idat)]); // construction method - } - l.extend_from_slice(&[0, 0]); // data reference index: this file - l.extend_from_slice(&[0, 1]); // one extent - l.extend_from_slice(&at.to_be_bytes()); - l.extend_from_slice(&(it.data.len() as u32).to_be_bytes()); - at += it.data.len() as u32; - } - full_box(b"iloc", iloc_version, &l) - }; - let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); - let meta = |base| full_box(b"meta", 0, &[iloc(base), iinf.clone(), idat.clone()].concat()); - if in_idat { - return [ftyp, meta(0)].concat(); - } - let base = (ftyp.len() + meta(0).len() + 8) as u32; - [ftyp, meta(base), bx(b"mdat", &payload)].concat() - } - - const TIFF: &[u8] = b"MM\0\x2a\0\0\0\x08\0\0\0\0\0\0"; - - /// An `Exif` item's payload: the offset from the end of this field to the TIFF header, then - /// `skip` bytes of padding, then the TIFF stream. - fn exif_item(tiff: &[u8], skip: usize) -> Vec { - [&(skip as u32).to_be_bytes()[..], &vec![0u8; skip], tiff].concat() - } - - fn exif_only(tiff: &[u8]) -> Vec> { - vec![Item { kind: b"Exif", content_type: "", data: exif_item(tiff, 0) }] - } - - /// Every item location box the format allows: counts and ids are 16-bit before version 2 and - /// 32-bit in it, and the bytes live in `mdat` or in `idat` next to the location box itself. - #[test] - fn items_are_located_in_every_iloc_version() { - for (version, in_idat) in [(0, false), (1, false), (1, true), (2, false), (2, true)] { - let file = heic(&exif_only(TIFF), version, in_idat); - assert_eq!(read(&file).unwrap(), (Some(TIFF.to_vec()), None), "iloc v{version}, idat {in_idat}"); - } - } - - /// The XMP packet is the `mime` item that declares `application/rdf+xml`, not any other one. - #[test] - fn the_xmp_packet_is_the_rdf_mime_item() { - let items = vec![ - Item { kind: b"mime", content_type: "text/plain", data: b"not the packet".to_vec() }, - Item { kind: b"mime", content_type: "application/rdf+xml", data: b"\0".to_vec() }, - Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 0) }, - ]; - assert_eq!(read(&heic(&items, 1, false)).unwrap(), (Some(TIFF.to_vec()), Some("".into()))); - } - - /// The `Exif` item's leading offset may point past padding, not only straight at the TIFF header. - #[test] - fn the_exif_items_tiff_header_offset_is_honoured() { - let items = vec![Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 6) }]; - assert_eq!(read(&heic(&items, 1, false)).unwrap().0.as_deref(), Some(TIFF)); - } - - /// Which `ftyp` brands count as a still image in this container, major or compatible. - #[test] - fn brands() { - for brand in ["heic", "heix", "mif1", "avif", "hevc", "msf1"] { - let file = bx(b"ftyp", format!("{brand}\0\0\0\0mif1").as_bytes()); - assert!(is_heif(&file), "{brand}"); - } - assert!(is_heif(&bx(b"ftyp", b"mp41\0\0\0\0isomheic"))); - assert!(!is_heif(&bx(b"ftyp", b"crx \0\0\0\0crx isom"))); - assert!(!is_heif(&bx(b"ftyp", b"jxl \0\0\0\0jxl "))); - assert!(!is_heif(b"\xff\xd8\xff\xe1 a jpeg")); - assert!(!is_heif(b"")); - // a compatible brand past MAX_FTYP bytes is not read - let mut long = b"mp41\0\0\0\0".to_vec(); - long.resize(MAX_FTYP, b'x'); - long.extend_from_slice(b"heic"); - assert!(!is_heif(&bx(b"ftyp", &long))); - } - - /// Not a HEIF file, an image sequence (no `meta`), garbage: errors as the decoder reports them. - #[test] - fn what_is_not_a_still_image_is_an_error() { - assert!(matches!(read(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"), Err(Error::Unsupported(m)) if m.contains("sequence"))); - for bytes in [&b""[..], b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", &[0xFF; 64]] { - assert!(read(bytes).is_err()); - } - } - - /// A truncated or scrambled file never panics, and lies about sizes find nothing. - #[test] - fn broken_files_never_panic() { - let good = heic( - &[ - Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 0) }, - Item { kind: b"mime", content_type: "application/rdf+xml", data: b"".to_vec() }, - ], - 1, - false, - ); - for cut in 0..good.len() { - let _ = read(&good[..cut]); - } - for at in 0..good.len() { - let mut bad = good.clone(); - bad[at] ^= 0xff; - let _ = read(&bad); - } - let mut lying = good.clone(); - if let Some(at) = lying.windows(4).position(|w| w == b"iloc") { - lying.splice(at + 8..at + 12, u32::MAX.to_be_bytes()); - } - assert_eq!(read(&lying).unwrap().0, None); - } - - /// Past MAX_ITEMS `infe` entries the rest are not read; an item larger than MAX_ITEM is left out. - #[test] - fn item_limits() { - let mut items: Vec> = (0..MAX_ITEMS).map(|_| Item { kind: b"mime", content_type: "text/plain", data: vec![b'x'] }).collect(); - items.push(Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 0) }); - assert_eq!(read(&heic(&items, 2, false)).unwrap().0, None, "the Exif item is entry {}", MAX_ITEMS + 1); - items.remove(0); - assert_eq!(read(&heic(&items, 2, false)).unwrap().0.as_deref(), Some(TIFF)); - let big = vec![Item { kind: b"Exif", content_type: "", data: exif_item(&vec![0; MAX_ITEM], 0) }]; - assert_eq!(read(&heic(&big, 1, true)).unwrap().0, None); - } - - /// The box walk stops after MAX_BOXES boxes: a `meta` box after that many is not found. - #[test] - fn box_limit() { - let mut file = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); - for _ in 0..MAX_BOXES { - file.extend(bx(b"free", &[])); - } - file.extend(heic(&exif_only(TIFF), 1, true).split_off(bx(b"ftyp", b"heic\0\0\0\0mif1heic").len())); - assert!(matches!(read(&file), Err(Error::Unsupported(_)))); - } - - /// The container writer of this crate's tests: the reader agrees with the layout an encoder writes. - #[test] - fn a_synthetic_heif_file_carries_its_exif() { - let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); - spec.exif = Some(TIFF.to_vec()); - assert_eq!(read(&crate::testdata::build(&spec)).unwrap(), (Some(TIFF.to_vec()), None)); - assert_eq!(read(&crate::testdata::build(&crate::testdata::Spec::new(16, 16, &|_, _| [0; 3]))).unwrap(), (None, None)); - } - - /// With the decoder built too, both readers give the same answer on files with one Exif item. - #[cfg(feature = "decode")] - #[test] - fn agrees_with_the_decoders_read_metadata() { - let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); - spec.exif = Some(TIFF.to_vec()); - for file in [crate::testdata::build(&spec), crate::testdata::build(&crate::testdata::Spec::new(8, 8, &|_, _| [0; 3]))] { - assert_eq!(read(&file), crate::read_metadata(&file)); - } - let sequence = b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"; - assert_eq!(read(sequence), crate::read_metadata(sequence)); - } -} +mod tests; diff --git a/crates/heif/src/items/tests.rs b/crates/heif/src/items/tests.rs new file mode 100644 index 000000000..49cc9d789 --- /dev/null +++ b/crates/heif/src/items/tests.rs @@ -0,0 +1,1173 @@ +#![allow(clippy::arithmetic_side_effects)] + +use super::*; + +/// One box: its own size, its four-letter kind, then its payload. +fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec { + let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec(); + v.extend_from_slice(kind); + v.extend_from_slice(payload); + v +} + +/// A FullBox: a box whose payload begins with a version byte and three flag bytes. +fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec { + bx(kind, &[&[version, 0, 0, 0][..], payload].concat()) +} + +/// A big-endian field of `size` bytes. +fn put(v: &mut Vec, x: u64, size: u8) { + match size { + 0 => assert_eq!(x, 0, "a value needs a field"), + 2 => v.extend_from_slice(&(x as u16).to_be_bytes()), + 4 => v.extend_from_slice(&(x as u32).to_be_bytes()), + 8 => v.extend_from_slice(&x.to_be_bytes()), + _ => panic!("no {size}-byte fields"), + } +} + +const TIFF: &[u8] = b"MM\0\x2a\0\0\0\x08\0\0\0\0\0\0"; + +/// The 100-byte EXIF of LightCraft's issue sweep `synthetic-metadata.heic`: Make "Synthetic", +/// Model "Issue sweep camera", DateTime 2026:10:12 12:34:56, big-endian. +const SWEEP_TIFF: [u8; 100] = [ + 0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x01, 0x0f, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, // + 0x00, 0x32, 0x01, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x3c, 0x01, 0x32, 0x00, 0x02, 0x00, 0x00, // + 0x00, 0x14, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x53, 0x79, 0x6e, 0x74, 0x68, 0x65, 0x74, 0x69, 0x63, 0x00, // + 0x49, 0x73, 0x73, 0x75, 0x65, 0x20, 0x73, 0x77, 0x65, 0x65, 0x70, 0x20, 0x63, 0x61, 0x6d, 0x65, 0x72, 0x61, 0x00, 0x00, // + 0x32, 0x30, 0x32, 0x36, 0x3a, 0x31, 0x30, 0x3a, 0x31, 0x32, 0x20, 0x31, 0x32, 0x3a, 0x33, 0x34, 0x3a, 0x35, 0x36, 0x00, +]; + +/// A TIFF stream as far as this reader looks: a big-endian TIFF header, then `marker`. +fn tiff_with(marker: &str) -> Vec { + [b"MM\0*\0\0\0\x08".as_slice(), marker.as_bytes()].concat() +} + +/// An XMP packet holding `title`. +fn xmp_with(title: &str) -> String { + format!("{title}") +} + +/// An EXIF item as HEIF stores it: the 4-byte offset, `skip` (which the offset jumps), the TIFF. +fn exif_item(skip: &[u8], tiff: &[u8]) -> Vec { + [&(skip.len() as u32).to_be_bytes()[..], skip, tiff].concat() +} + +/// The photo's EXIF, as [`read`] gives it. +fn exif(f: &[u8]) -> Option> { + read(f).ok()?.0 +} + +/// The photo's XMP packet, as [`read`] gives it. +fn xmp(f: &[u8]) -> Option { + read(f).ok()?.1 +} + +/// Every metadata item [`scan`] finds, when it is sure it saw them all: `None` when the catalog +/// is incomplete or an item can't be read. +fn sure(f: &[u8]) -> Option)>> { + let s = scan(f).ok()?; + if !s.complete() { + return None; + } + s.map(|it| it.data.ok().map(|d| (it.id, it.kind, d))).collect() +} + +// -- The single-`meta` builder of the first version of this reader -------------------------------- + +struct Entry<'a> { + kind: &'a [u8; 4], + content_type: &'a str, + data: Vec, +} + +/// A HEIC-shaped file holding `items`: `ftyp`, a `meta` box with one `infe` per item, and the +/// item bytes in `idat` (`in_idat`, construction method 1) or in `mdat` (method 0). No `pitm` and +/// no `iref`: every item describes the file. +fn heic(items: &[Entry<'_>], iloc_version: u8, in_idat: bool) -> Vec { + let mut iinf = (items.len() as u16).to_be_bytes().to_vec(); + for (i, it) in items.iter().enumerate() { + let mut e = vec![2u8, 0, 0, 0]; // version 2, flags + e.extend_from_slice(&(i as u16 + 1).to_be_bytes()); + e.extend_from_slice(&[0, 0]); // protection index + e.extend_from_slice(it.kind); + e.extend_from_slice(b"an item\0"); + if it.kind == b"mime" { + e.extend_from_slice(it.content_type.as_bytes()); + e.push(0); + } + iinf.extend_from_slice(&bx(b"infe", &e)); + } + let iinf = full_box(b"iinf", 0, &iinf); + let payload: Vec = items.iter().flat_map(|i| i.data.iter().copied()).collect(); + let idat = if in_idat { bx(b"idat", &payload) } else { Vec::new() }; + let iloc = |base: u32| { + // four-byte offsets and lengths, no base offset and no extent index + let mut l = vec![0x44, 0x00]; + match iloc_version { + 2 => l.extend_from_slice(&(items.len() as u32).to_be_bytes()), + _ => l.extend_from_slice(&(items.len() as u16).to_be_bytes()), + } + let mut at = base; + for (i, it) in items.iter().enumerate() { + match iloc_version { + 2 => l.extend_from_slice(&(i as u32 + 1).to_be_bytes()), + _ => l.extend_from_slice(&(i as u16 + 1).to_be_bytes()), + } + if iloc_version > 0 { + l.extend_from_slice(&[0, u8::from(in_idat)]); // construction method + } + l.extend_from_slice(&[0, 0]); // data reference index: this file + l.extend_from_slice(&[0, 1]); // one extent + l.extend_from_slice(&at.to_be_bytes()); + l.extend_from_slice(&(it.data.len() as u32).to_be_bytes()); + at += it.data.len() as u32; + } + full_box(b"iloc", iloc_version, &l) + }; + let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + let meta = |base| full_box(b"meta", 0, &[iloc(base), iinf.clone(), idat.clone()].concat()); + if in_idat { + return [ftyp, meta(0)].concat(); + } + let base = (ftyp.len() + meta(0).len() + 8) as u32; + [ftyp, meta(base), bx(b"mdat", &payload)].concat() +} + +/// An `Exif` item's payload: the offset from the end of this field to the TIFF header, then +/// `skip` bytes of padding, then the TIFF stream. +fn exif_padded(tiff: &[u8], skip: usize) -> Vec { + [&(skip as u32).to_be_bytes()[..], &vec![0u8; skip], tiff].concat() +} + +fn exif_only(tiff: &[u8]) -> Vec> { + vec![Entry { kind: b"Exif", content_type: "", data: exif_padded(tiff, 0) }] +} + +/// Every item location box the format allows: counts and ids are 16-bit before version 2 and +/// 32-bit in it, and the bytes live in `mdat` or in `idat` next to the location box itself. +#[test] +fn items_are_located_in_every_iloc_version() { + for (version, in_idat) in [(0, false), (1, false), (1, true), (2, false), (2, true)] { + let file = heic(&exif_only(TIFF), version, in_idat); + assert_eq!(read(&file).unwrap(), (Some(TIFF.to_vec()), None), "iloc v{version}, idat {in_idat}"); + } +} + +/// The XMP packet is the `mime` item that declares `application/rdf+xml`, not any other one. +#[test] +fn the_xmp_packet_is_the_rdf_mime_item() { + let items = vec![ + Entry { kind: b"mime", content_type: "text/plain", data: b"not the packet".to_vec() }, + Entry { kind: b"mime", content_type: "application/rdf+xml", data: b"\0".to_vec() }, + Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }, + ]; + assert_eq!(read(&heic(&items, 1, false)).unwrap(), (Some(TIFF.to_vec()), Some("".into()))); +} + +/// The `Exif` item's leading offset may point past padding, not only straight at the TIFF header. +#[test] +fn the_exif_items_tiff_header_offset_is_honoured() { + let items = vec![Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 6) }]; + assert_eq!(read(&heic(&items, 1, false)).unwrap().0.as_deref(), Some(TIFF)); +} + +/// Which `ftyp` brands count as a still image in this container, major or compatible. +#[test] +fn brands() { + for brand in ["heic", "heix", "mif1", "avif", "hevc", "msf1"] { + let file = bx(b"ftyp", format!("{brand}\0\0\0\0mif1").as_bytes()); + assert!(is_heif(&file), "{brand}"); + } + assert!(is_heif(&bx(b"ftyp", b"mp41\0\0\0\0isomheic"))); + assert!(!is_heif(&bx(b"ftyp", b"crx \0\0\0\0crx isom"))); + assert!(!is_heif(&bx(b"ftyp", b"jxl \0\0\0\0jxl "))); + assert!(!is_heif(b"\xff\xd8\xff\xe1 a jpeg")); + assert!(!is_heif(b"")); + // a compatible brand past MAX_FTYP bytes is not read + let mut long = b"mp41\0\0\0\0".to_vec(); + long.resize(MAX_FTYP, b'x'); + long.extend_from_slice(b"heic"); + assert!(!is_heif(&bx(b"ftyp", &long))); +} + +/// Not a HEIF file, an image sequence (no `meta`), garbage: errors as the decoder reports them. +#[test] +fn what_is_not_a_still_image_is_an_error() { + assert!(matches!(read(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"), Err(Error::Unsupported(m)) if m.contains("sequence"))); + for bytes in [&b""[..], b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", &[0xFF; 64]] { + assert!(read(bytes).is_err()); + } + assert!(scan(&[0xFF; 64]).is_err(), "only a file that isn't HEIF is an error to scan"); +} + +/// A truncated or scrambled file never panics, and lies about sizes find nothing. +#[test] +fn broken_files_never_panic() { + let good = heic( + &[ + Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }, + Entry { kind: b"mime", content_type: "application/rdf+xml", data: b"".to_vec() }, + ], + 1, + false, + ); + for cut in 0..good.len() { + let _ = read(&good[..cut]); + let _ = scan(&good[..cut]).map(Iterator::count); + } + for at in 0..good.len() { + let mut bad = good.clone(); + bad[at] ^= 0xff; + let _ = read(&bad); + let _ = scan(&bad).map(Iterator::count); + } + let mut lying = good.clone(); + if let Some(at) = lying.windows(4).position(|w| w == b"iloc") { + lying.splice(at + 8..at + 12, u32::MAX.to_be_bytes()); + } + assert_eq!(read(&lying).unwrap().0, None); + assert_eq!(sure(&lying), None, "an item `iloc` can't place is unreadable, not absent"); +} + +/// [`read`] considers the first MAX_CANDIDATES metadata items; an item larger than MAX_ITEM is +/// left out by [`read`] and too large for [`scan`]. +#[test] +fn item_limits() { + let mut items: Vec> = + (0..MAX_CANDIDATES).map(|_| Entry { kind: b"Exif", content_type: "", data: exif_padded(b"not TIFF", 0) }).collect(); + items.push(Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }); + assert_eq!(read(&heic(&items, 2, false)).unwrap().0, None, "the TIFF is metadata item {}", MAX_CANDIDATES + 1); + items.remove(0); + assert_eq!(read(&heic(&items, 2, false)).unwrap().0.as_deref(), Some(TIFF)); + let big = heic(&[Entry { kind: b"Exif", content_type: "", data: exif_padded(&vec![0; MAX_ITEM], 0) }], 1, true); + assert_eq!(read(&big).unwrap().0, None); + let found: Vec = scan(&big).unwrap().collect(); + assert_eq!(found, [Item { id: 1, kind: Kind::Exif, data: Err(Unreadable::TooLarge) }]); +} + +/// The scan gathers at most its byte budget over all items; past it an item is too large. +#[test] +fn the_scan_byte_budget() { + let items = [ + Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }, + Entry { kind: b"mime", content_type: "application/rdf+xml", data: b"".to_vec() }, + ]; + let f = heic(&items, 1, true); + let first = TIFF.len() + 4; + let found: Vec = scan_within(&f, first + 5).unwrap().collect(); + assert_eq!(found[0].data.as_deref().map(<[u8]>::len), Ok(first)); + assert_eq!(found[1].data, Err(Unreadable::TooLarge)); + let found: Vec = scan_within(&f, first + 12).unwrap().collect(); + assert_eq!(found[1].data.as_deref(), Ok(b"".as_slice())); +} + +/// The box walk stops after MAX_BOXES boxes: a `meta` box after that many is not found. +#[test] +fn box_limit() { + let mut file = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + for _ in 0..MAX_BOXES { + file.extend(bx(b"free", &[])); + } + file.extend(heic(&exif_only(TIFF), 1, true).split_off(bx(b"ftyp", b"heic\0\0\0\0mif1heic").len())); + assert!(matches!(read(&file), Err(Error::Unsupported(_)))); + assert!(!scan(&file).unwrap().complete()); +} + +/// The container writer of this crate's tests: the reader agrees with the layout an encoder writes. +#[test] +fn a_synthetic_heif_file_carries_its_exif() { + let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); + spec.exif = Some(TIFF.to_vec()); + let file = crate::testdata::build(&spec); + assert_eq!(read(&file).unwrap(), (Some(TIFF.to_vec()), None)); + let mut exif = 0u32.to_be_bytes().to_vec(); + exif.extend_from_slice(TIFF); + assert!(matches!(sure(&file).as_deref(), Some([(_, Kind::Exif, data)]) if *data == exif)); + assert_eq!(read(&crate::testdata::build(&crate::testdata::Spec::new(16, 16, &|_, _| [0; 3]))).unwrap(), (None, None)); +} + +/// With the decoder built too, both readers give the same answer, also on files with a +/// thumbnail and with several items. +#[cfg(feature = "decode")] +#[test] +fn agrees_with_the_decoders_read_metadata() { + let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); + spec.exif = Some(TIFF.to_vec()); + let mut thumbnailed = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); + thumbnailed.exif = Some(TIFF.to_vec()); + thumbnailed.thumbnail = Some(&|_, y| [y as u16 * 4, 0, 0]); + let files = [ + crate::testdata::build(&spec), + crate::testdata::build(&thumbnailed), + crate::testdata::build(&crate::testdata::Spec::new(8, 8, &|_, _| [0; 3])), + ]; + for file in files { + assert_eq!(read(&file), crate::read_metadata(&file)); + } + let sequence = b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"; + assert_eq!(read(sequence), crate::read_metadata(sequence)); +} + +// -- A builder with a primary image, references and every `iloc` layout ---------------------------- + +#[derive(Clone, Copy, PartialEq)] +enum Store { + File, + Idat, +} + +struct Part { + id: u32, + kind: &'static [u8; 4], + /// `mime` items: content type and content encoding. + mime: (&'static str, &'static str), + data: Vec, + store: Store, + /// Extents the data is split into. + extents: usize, +} + +/// A HEIF file for the tests: `ftyp`, `meta` (`hdlr`, `pitm`, `iinf`, `iref`, `iloc`, `idat`) +/// and `mdat`. +struct Heif { + ftyp: Vec, + primary: u32, + items: Vec, + /// `iref` references: type, from, to. + refs: Vec<(&'static [u8; 4], u32, Vec)>, + /// `iloc` version and its offset, length, base-offset and index field sizes. + iloc: (u8, [u8; 4]), + /// 32-bit item ids: `infe` version 3, `pitm` and `iref` version 1 (`iloc` must be version 2). + wide: bool, +} + +impl Heif { + /// A primary HEVC picture, item 1 (its bytes don't matter here). + fn new() -> Heif { + let picture = Part { id: 1, kind: b"hvc1", mime: ("", ""), data: b"coded picture".to_vec(), store: Store::File, extents: 1 }; + Heif { ftyp: b"heic\0\0\0\0mif1heic".to_vec(), primary: 1, items: vec![picture], refs: Vec::new(), iloc: (1, [4, 4, 0, 0]), wide: false } + } + + fn item(mut self, id: u32, kind: &'static [u8; 4], data: Vec, store: Store, extents: usize) -> Heif { + self.items.push(Part { id, kind, mime: ("", ""), data, store, extents }); + self + } + + fn xmp(mut self, id: u32, mime: (&'static str, &'static str), data: Vec) -> Heif { + self.items.push(Part { id, kind: b"mime", mime, data, store: Store::File, extents: 1 }); + self + } + + fn cdsc(self, from: u32, to: &[u32]) -> Heif { + self.iref(b"cdsc", from, to) + } + + fn iref(mut self, kind: &'static [u8; 4], from: u32, to: &[u32]) -> Heif { + self.refs.push((kind, from, to.to_vec())); + self + } + + fn meta(&self, mdat_at: usize) -> Vec { + let id = |v: &mut Vec, id: u32| put(v, u64::from(id), if self.wide { 4 } else { 2 }); + let mut hdlr = vec![0u8; 4]; + hdlr.extend_from_slice(b"pict"); + hdlr.extend_from_slice(&[0; 13]); + let mut pitm = Vec::new(); + id(&mut pitm, self.primary); + let mut iinf = Vec::new(); + put(&mut iinf, self.items.len() as u64, if self.wide { 4 } else { 2 }); + for it in &self.items { + let mut e = Vec::new(); + id(&mut e, it.id); + e.extend_from_slice(&[0, 0]); // not protected + e.extend_from_slice(it.kind); + e.push(0); // no name + if it.kind == b"mime" { + e.extend_from_slice(it.mime.0.as_bytes()); + e.push(0); + e.extend_from_slice(it.mime.1.as_bytes()); + e.push(0); + } + iinf.extend(full_box(b"infe", if self.wide { 3 } else { 2 }, &e)); + } + let mut iref = Vec::new(); + for (kind, from, to) in &self.refs { + let mut r = Vec::new(); + id(&mut r, *from); + put(&mut r, to.len() as u64, 2); + to.iter().for_each(|t| id(&mut r, *t)); + iref.extend(bx(kind, &r)); + } + let (version, [o, l, b, i]) = self.iloc; + let mut iloc = vec![o << 4 | l, b << 4 | i]; + put(&mut iloc, self.items.len() as u64, if version == 2 { 4 } else { 2 }); + let (mut in_file, mut in_idat) = (mdat_at as u64, 0u64); + let mut idat = Vec::new(); + for it in &self.items { + put(&mut iloc, u64::from(it.id), if version == 2 { 4 } else { 2 }); + let at = match it.store { + Store::File => &mut in_file, + Store::Idat => &mut in_idat, + }; + if version > 0 { + put(&mut iloc, if it.store == Store::Idat { 1 } else { 0 }, 2); + } + put(&mut iloc, 0, 2); // this file + let base = if b > 0 { *at } else { 0 }; + put(&mut iloc, base, b); + let chunk = it.data.len().div_ceil(it.extents.max(1)).max(1); + let pieces: Vec<&[u8]> = it.data.chunks(chunk).collect(); + put(&mut iloc, pieces.len() as u64, 2); + for piece in pieces { + if version > 0 { + put(&mut iloc, 0, i); + } + put(&mut iloc, *at - base, o); + put(&mut iloc, piece.len() as u64, l); + *at += piece.len() as u64; + } + if it.store == Store::Idat { + idat.extend_from_slice(&it.data); + } + } + let ref_version = if self.wide { 1 } else { 0 }; + let boxes = [ + full_box(b"hdlr", 0, &hdlr), + full_box(b"pitm", ref_version, &pitm), + full_box(b"iinf", ref_version, &iinf), + full_box(b"iref", ref_version, &iref), + full_box(b"iloc", version, &iloc), + bx(b"idat", &idat), + ]; + full_box(b"meta", 0, &boxes.concat()) + } + + fn build(&self) -> Vec { + let ftyp = bx(b"ftyp", &self.ftyp); + let mdat_at = ftyp.len() + self.meta(0).len() + 8; + let mdat: Vec = self.items.iter().filter(|i| i.store == Store::File).flat_map(|i| i.data.iter().copied()).collect(); + [ftyp, self.meta(mdat_at), bx(b"mdat", &mdat)].concat() + } +} + +/// One item's bytes, as [`read`] gathers them: its place from `iloc`, then its extents. +fn item_bytes(file: &[u8], iloc: &[u8], idat: Option<&[u8]>, id: u32) -> Option> { + let iloc = Iloc::parse(iloc)?; + let mut place = None; + iloc.walk(|i, p| { + if i == id { + place = Some(p); + } + place.is_none() + })?; + match iloc.gather(file, idat, &place?, MAX_ITEM) { + Gathered::Bytes(b) => Some(b), + _ => None, + } +} + +/// LightCraft's issue sweep case, laid out as its HEIF test builder writes it: `iloc` version 1, +/// the EXIF item (offset 0, then the TIFF) tied to the primary image by `cdsc`. +#[test] +fn the_issue_sweep_heic_gives_its_exif() { + let f = Heif::new().item(8, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(8, &[1]).build(); + assert_eq!(exif(&f).as_deref(), Some(SWEEP_TIFF.as_slice())); +} + +/// The 4-byte offset in front of the TIFF stream: iPhones write 6 and `Exif\0\0`, libheif and +/// avif-serialize 0. Writers that left the field out, or got it wrong, are read when the stream +/// is plain to see; anything else is no EXIF. +#[test] +fn exif_items_with_and_without_their_offset() { + let t = &SWEEP_TIFF[..]; + let tiff = Some(t); + assert_eq!(exif_tiff(&exif_item(b"Exif\0\0", t)), tiff, "iPhone"); + assert_eq!(exif_tiff(&exif_item(&[], t)), tiff, "libheif, avif-serialize"); + assert_eq!(exif_tiff(&exif_item(b"\0\0padding", t)), tiff, "any bytes the offset skips"); + assert_eq!(exif_tiff(&[b"\0\0\0\0Exif\0\0".as_slice(), t].concat()), tiff, "offset 0 though a prefix follows"); + assert_eq!(exif_tiff(&[&1000u32.to_be_bytes()[..], t].concat()), tiff, "offset past the end"); + assert_eq!(exif_tiff(&[b"Exif\0\0".as_slice(), t].concat()), tiff, "no offset field, JPEG style"); + assert_eq!(exif_tiff(t), tiff, "no offset field at all"); + assert_eq!(exif_tiff(&exif_item(&[], b"not a TIFF stream")), None); + assert_eq!(exif_tiff(&[0, 0]), None); + assert_eq!(exif_tiff(&[]), None); + assert_eq!(exif_tiff(b"Exif\0"), None, "a prefix cut short"); + // and through a whole file, the iPhone way + let f = Heif::new().item(2, b"Exif", exif_item(b"Exif\0\0", t), Store::File, 1).cdsc(2, &[1]).build(); + assert_eq!(exif(&f).as_deref(), tiff); +} + +/// Items in several extents, in the file or the `meta` box's `idat`, with every `iloc` version +/// and field size. +#[test] +fn item_extents_and_construction_methods() { + let packet = xmp_with("Harbour at dusk"); + let rdf = ("application/rdf+xml", ""); + // EXIF in `idat` (method 1), XMP in the file in three extents, a 32-bit base offset + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::Idat, 1).cdsc(2, &[1]).xmp(3, rdf, packet.clone().into_bytes()); + h.items[2].extents = 3; + h.iloc = (1, [8, 4, 4, 0]); + let f = h.cdsc(3, &[1]).build(); + assert_eq!(read(&f).unwrap(), (Some(SWEEP_TIFF.to_vec()), Some(packet.clone()))); + // version 2: 32-bit item ids, an extent index, 64-bit lengths; EXIF in two extents + let mut h = Heif::new().item(70_000, b"Exif", exif_item(b"Exif\0\0", &SWEEP_TIFF), Store::File, 2).cdsc(70_000, &[70_001]); + (h.primary, h.items[0].id, h.wide, h.iloc) = (70_001, 70_001, true, (2, [4, 8, 0, 4])); + assert_eq!(exif(&h.build()).as_deref(), Some(SWEEP_TIFF.as_slice())); + // version 0: no construction method, file offsets only + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(2, &[1]); + h.iloc = (0, [4, 4, 0, 0]); + assert_eq!(exif(&h.build()).as_deref(), Some(SWEEP_TIFF.as_slice())); + // an XMP item in `idat` + let mut h = Heif::new().xmp(5, rdf, packet.clone().into_bytes()); + h.items[1].store = Store::Idat; + assert_eq!(xmp(&h.build()), Some(packet)); +} + +/// `iloc` entries the builder doesn't write: a length of 0 (to the end of the data), an item +/// in another file, an item built from other items, unknown versions and field sizes. +#[test] +fn iloc_entries_that_are_read_or_refused() { + let file = b"0123456789".as_slice(); + let idat = b"abcdefgh".as_slice(); + // version 1, offset and length 4 bytes, no base or index; one item with one extent + let iloc = |version: u8, sizes: [u8; 2], method: u16, data_ref: u16, offset: u32, length: u32| { + let mut l = vec![version, 0, 0, 0]; + l.extend_from_slice(&sizes); + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&7u16.to_be_bytes()); + if version > 0 { + l.extend_from_slice(&method.to_be_bytes()); + } + l.extend_from_slice(&data_ref.to_be_bytes()); + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&offset.to_be_bytes()); + l.extend_from_slice(&length.to_be_bytes()); + l + }; + let get = |l: &[u8]| item_bytes(file, l, Some(idat), 7); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 2, 3)).as_deref(), Some(b"234".as_slice())); + assert_eq!(get(&iloc(1, [0x44, 0], 1, 0, 2, 3)).as_deref(), Some(b"cde".as_slice()), "from idat"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 6, 0)).as_deref(), Some(b"6789".as_slice()), "length 0: to the end of the file"); + assert_eq!(get(&iloc(1, [0x44, 0], 1, 0, 5, 0)).as_deref(), Some(b"fgh".as_slice()), "length 0: to the end of idat"); + assert_eq!(item_bytes(file, &iloc(1, [0x44, 0], 1, 0, 0, 1), None, 7), None, "method 1 without idat"); + assert_eq!(get(&iloc(1, [0x44, 0], 2, 0, 0, 1)), None, "method 2 (from other items)"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 1, 0, 1)), None, "in another file"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 8, 3)), None, "past the end"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 11, 0)), None, "starts past the end"); + assert_eq!(get(&iloc(1, [0x22, 0], 0, 0, 0, 1)), None, "2-byte fields are not in the standard"); + assert_eq!(get(&iloc(3, [0x44, 0], 0, 0, 0, 1)), None, "unknown version"); + assert_eq!(item_bytes(file, &iloc(1, [0x44, 0], 0, 0, 0, 1), None, 8), None, "no such item"); + // a base offset near u64::MAX plus an extent offset overflows: refused, not wrapped + let mut l = vec![1, 0, 0, 0, 0x44, 0x80]; + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&7u16.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); // method 0, data reference 0 + l.extend_from_slice(&u64::MAX.to_be_bytes()); + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&1u32.to_be_bytes()); + l.extend_from_slice(&1u32.to_be_bytes()); + assert_eq!(get(&l), None); +} + +/// The primary image's metadata is read; metadata tied only to another image is not. +#[test] +fn the_primary_images_metadata_is_read() { + let thumb = |h: Heif| h.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1); + // a thumbnail's EXIF comes first in the file; the primary image's is the one read + let f = thumb(Heif::new()) + .item(10, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Primary")), Store::File, 1) + .cdsc(10, &[2]) + .cdsc(11, &[1]) + .build(); + assert_eq!(exif(&f), Some(tiff_with("Primary"))); + // EXIF tied only to another image is not this photo's + let f = thumb(Heif::new()).item(10, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1).cdsc(10, &[2]).build(); + assert_eq!(exif(&f), None); + // tied to no image: it describes the file; tied to both: read + let f = Heif::new().item(10, b"Exif", exif_item(&[], &tiff_with("Whole file")), Store::File, 1).build(); + assert_eq!(exif(&f), Some(tiff_with("Whole file"))); + let f = thumb(Heif::new()).item(10, b"Exif", exif_item(&[], &tiff_with("Both")), Store::File, 1).cdsc(10, &[2, 1]).build(); + assert_eq!(exif(&f), Some(tiff_with("Both"))); + // a primary image's EXIF that can't be read gives way to one tied to no image + let f = Heif::new() + .item(10, b"Exif", exif_item(&[], b"damaged"), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Fallback")), Store::File, 1) + .cdsc(10, &[1]) + .build(); + assert_eq!(exif(&f), Some(tiff_with("Fallback"))); + // XMP: only uncompressed `application/rdf+xml` items, the primary image's first + let f = thumb(Heif::new()) + .xmp(20, ("image/jpeg", ""), b"\xff\xd8 not XMP".to_vec()) + .xmp(21, ("application/rdf+xml", "deflate"), b"compressed".to_vec()) + .xmp(22, ("application/rdf+xml", ""), xmp_with("Thumbnail").into_bytes()) + .xmp(23, (" Application/RDF+XML ", ""), xmp_with("Primary").into_bytes()) + .cdsc(22, &[2]) + .cdsc(23, &[1]) + .build(); + assert_eq!(xmp(&f), Some(xmp_with("Primary"))); + // 32-bit ids in `pitm`, `iinf` and `iref` + let mut h = Heif::new().item(70_000, b"Exif", exif_item(&[], &tiff_with("Wide")), Store::File, 1).cdsc(70_000, &[70_001]); + (h.primary, h.items[0].id, h.wide, h.iloc.0) = (70_001, 70_001, true, 2); + assert_eq!(exif(&h.build()), Some(tiff_with("Wide"))); +} + +/// EXIF tied to an image the primary is derived from (`dimg`: an `iden` turn, an `iovl` overlay, +/// a `tmap` tone map, a grid) is the primary image's, through any depth and despite loops. So is +/// EXIF in a file whose `pitm` names no item. A thumbnail's EXIF still isn't. +#[test] +fn metadata_of_the_images_a_primary_is_made_from_is_read() { + let base = |h: Heif| { + let mut h = h + .item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1) + .item(3, b"hvc1", b"coded picture".to_vec(), Store::File, 1) + .item(10, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Input")), Store::File, 1) + .cdsc(10, &[2]) + .cdsc(11, &[3]); + h.items[0].kind = b"iden"; + h + }; + // the primary (1) turns item 3 + let f = base(Heif::new()).iref(b"dimg", 1, &[3]).build(); + assert_eq!(exif(&f), Some(tiff_with("Input"))); + // two levels (a tone map of a grid of 3), and a loop back to the primary + let f = + base(Heif::new()).item(4, b"grid", Vec::new(), Store::File, 1).iref(b"dimg", 1, &[4]).iref(b"dimg", 4, &[3]).iref(b"dimg", 3, &[1]).build(); + assert_eq!(exif(&f), Some(tiff_with("Input"))); + // `pitm` names an item that doesn't exist: there is no primary image, so tied items count + let mut h = Heif::new().item(11, b"Exif", exif_item(&[], &tiff_with("Orphan")), Store::File, 1).cdsc(11, &[1]); + h.primary = 99; + assert_eq!(exif(&h.build()), Some(tiff_with("Orphan"))); + // the thumbnail is not an input of the primary: without the `dimg`, nothing is the photo's + assert_eq!(exif(&base(Heif::new()).build()), None); +} + +/// HEIF, HEIC and AVIF brands are read; other files built of the same boxes are not HEIF. +#[test] +fn heif_and_avif_brands() { + let with = |ftyp: &[u8]| { + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(2, &[1]); + h.ftyp = ftyp.to_vec(); + h.build() + }; + for ftyp in [&b"heic\0\0\0\0mif1heic"[..], b"mif1\0\0\0\0mif1", b"heix\0\0\0\0mif1heix", b"avif\0\0\0\0avifmif1miaf", b"msf1\0\0\0\0msf1hevc"] { + assert!(is_heif(&with(ftyp)), "{}", String::from_utf8_lossy(ftyp)); + assert_eq!(exif(&with(ftyp)).as_deref(), Some(SWEEP_TIFF.as_slice()), "{}", String::from_utf8_lossy(ftyp)); + } + // MP4 and QuickTime video, JPEG XL, Canon CR3 + for ftyp in [&b"isom\0\0\x02\0isomiso2mp41"[..], b"qt \0\0\0\0qt ", b"jxl \0\0\0\0jxl ", b"crx \0\0\0\x01crx isom", b"mif1\0\0\0\0crx "] { + assert!(!is_heif(&with(ftyp)), "{}", String::from_utf8_lossy(ftyp)); + assert!(read(&with(ftyp)).is_err() && scan(&with(ftyp)).is_err()); + } + assert!(!is_heif(b"\0\0\0\x0cftyp"), "no brand"); + assert!(!is_heif(&[0xff, 0xd8, 0xff, 0xe0, 0, 0x10, b'J', b'F', b'I', b'F', 0, 1]), "JPEG"); + // a HEIF file without a `meta` box has no metadata + let bare = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + assert!(matches!(read(&bare), Err(Error::Unsupported(_)))); + assert_eq!(sure(&bare), Some(Vec::new())); +} + +/// Damaged structure: an out-of-range box, an `iinf` or `iref` that claims more than it holds, +/// extents that overlap to gather more than [`MAX_ITEM`], an item count far past the box. +#[test] +fn malformed_bounds_are_refused() { + let good = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(2, &[1]).build(); + // the `meta` box says it is longer than the file: nothing is read + let mut f = good.clone(); + let at = u32::from_be_bytes(f[0..4].try_into().unwrap()) as usize; // `meta` follows `ftyp` + assert_eq!(&f[at + 4..at + 8], b"meta"); + f[at..at + 4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(exif(&f), None); + assert_eq!(sure(&f), None); + // an extent past the end of the file + let mut f = good.clone(); + f.truncate(f.len() - 1); + assert_eq!(exif(&f), None); + // 65,535 extents over the same 64 KiB: refused at MAX_ITEM, quickly + let file = vec![0u8; 64 << 10]; + let mut l = vec![1, 0, 0, 0, 0x44, 0]; + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&7u16.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); + l.extend_from_slice(&u16::MAX.to_be_bytes()); + for _ in 0..u16::MAX { + l.extend_from_slice(&0u32.to_be_bytes()); + l.extend_from_slice(&(file.len() as u32).to_be_bytes()); + } + assert_eq!(item_bytes(&file, &l, None, 7), None); + // an item count of 2^32 - 1 in a box holding one entry + let mut l = vec![2, 0, 0, 0, 0x44, 0]; + l.extend_from_slice(&u32::MAX.to_be_bytes()); + l.extend_from_slice(&1u32.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0, 0, 0]); + assert_eq!(item_bytes(&file, &l, None, 7), None); + // a `cdsc` claiming 65,535 targets with none there, an `infe` cut short + let mut iref = vec![0, 0, 0, 0]; + iref.extend(bx(b"cdsc", &[0, 2, 0xff, 0xff])); + let iref = bx(b"iref", &iref); + let mut items = vec![Candidate { id: 2, kind: Kind::Exif, describes: false, describes_primary: false }]; + let mut budget = MAX_BOXES; + let b = boxes(&iref, 0, iref.len(), &mut budget); + link(&iref, &b[0], Some(1), &mut items, &mut budget); + assert!(!items[0].describes); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 0, b'E', b'x']), None, "cut short: the item type is unknown"); + assert_eq!(item_info(&[4, 0, 0, 0, 0, 2, 0, 0, b'E', b'x', b'i', b'f', 0]), None, "unknown infe version"); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x', b'i', b'f', 0]), Some((2, None)), "protected"); + // a full box's version is its own first byte, never the next box's + let two = [bx(b"iref", &[]), bx(b"free", &[1, 2, 3])].concat(); + let mut budget = MAX_BOXES; + let b = boxes(&two, 0, two.len(), &mut budget); + assert_eq!((version(&two, &b[0]), version(&two, &b[1])), (None, Some(1))); +} + +/// Box headers that lie: a 64-bit size, a `uuid` box too short for its extended type, a size +/// smaller than the header, a size of 0 (to the end). +#[test] +fn box_headers() { + let mut large = 1u32.to_be_bytes().to_vec(); + large.extend_from_slice(b"free"); + large.extend_from_slice(&20u64.to_be_bytes()); + large.extend_from_slice(b"abcd"); + let mut budget = MAX_BOXES; + let b = boxes(&large, 0, large.len(), &mut budget); + assert_eq!((b.len(), b[0].body, b[0].end), (1, 16, 20)); + large[8..16].copy_from_slice(&u64::MAX.to_be_bytes()); + assert!(boxes(&large, 0, large.len(), &mut budget).is_empty(), "a 64-bit size past the end"); + let short_uuid = bx(b"uuid", &[0; 8]); + assert!(boxes(&short_uuid, 0, short_uuid.len(), &mut budget).is_empty()); + let uuid = bx(b"uuid", &[0; 20]); + assert_eq!(boxes(&uuid, 0, uuid.len(), &mut budget)[0].body, 24); + assert!(boxes(&[0, 0, 0, 4, b'f', b'r', b'e', b'e'], 0, 8, &mut budget).is_empty(), "smaller than its header"); + let to_end = [&[0u8, 0, 0, 0][..], b"mdat", b"payload"].concat(); + assert_eq!(boxes(&to_end, 0, to_end.len(), &mut budget)[0].end, to_end.len()); + assert!(boxes(&to_end, 9, 4, &mut budget).is_empty(), "an empty range"); + let mut none = 0; + assert!(boxes(&to_end, 0, to_end.len(), &mut none).is_empty(), "no budget left"); +} + +/// Every cut-short or damaged file is read without a panic. +#[test] +fn truncated_and_damaged_files_never_panic() { + let mut h = Heif::new() + .item(2, b"Exif", exif_item(b"Exif\0\0", &SWEEP_TIFF), Store::Idat, 2) + .xmp(3, ("application/rdf+xml", ""), xmp_with("t").into_bytes()) + .cdsc(2, &[1]) + .cdsc(3, &[1]); + h.items[2].extents = 3; + h.iloc = (1, [4, 8, 4, 4]); + let f = h.build(); + assert!(matches!(read(&f), Ok((Some(_), Some(_))))); + for n in 0..f.len() { + let _ = read(&f[..n]); + let _ = scan(&f[..n]).map(Iterator::count); + } + for i in 0..f.len() { + for v in [0x00, 0xff, 0x7f, 0x01] { + let mut g = f.clone(); + g[i] = v; + let _ = read(&g); + let _ = scan(&g).map(Iterator::count); + } + } +} + +/// [`scan`] returns every EXIF and XMP item, not only the photo's own first EXIF: a thumbnail's +/// EXIF and XMP, the primary image's second EXIF item, a compressed XMP item. +#[test] +fn the_scan_returns_every_metadata_item() { + let plain = tiff_with("Primary"); + let thumb = |h: Heif| h.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1); + let ids = |f: &[u8]| sure(f).map(|items| items.into_iter().map(|(id, kind, _)| (id, kind)).collect::>()); + // the thumbnail's EXIF + let f = thumb(Heif::new()) + .item(10, b"Exif", exif_item(&[], &plain), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1) + .cdsc(10, &[1]) + .cdsc(11, &[2]) + .build(); + assert_eq!(exif(&f), Some(plain.clone()), "the photo's own EXIF"); + let all = sure(&f).unwrap(); + assert_eq!(all[1], (11, Kind::Exif, exif_item(&[], &tiff_with("Thumbnail")))); + // the primary image's second EXIF item + let f = Heif::new() + .item(10, b"Exif", exif_item(&[], &plain), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Second")), Store::File, 1) + .cdsc(10, &[1]) + .cdsc(11, &[1]) + .build(); + assert_eq!(exif(&f), Some(plain.clone())); + assert_eq!(ids(&f), Some(vec![(10, Kind::Exif), (11, Kind::Exif)])); + // a thumbnail's XMP (with place names, say) is not the photo's, but the scan has it + let f = thumb(Heif::new()) + .item(10, b"Exif", exif_item(&[], &plain), Store::File, 1) + .cdsc(10, &[1]) + .xmp(20, ("application/rdf+xml", ""), xmp_with("Thumbnail").into_bytes()) + .cdsc(20, &[2]) + .build(); + assert_eq!(xmp(&f), None, "the thumbnail's packet is not the primary image's"); + assert_eq!(sure(&f).unwrap()[1], (20, Kind::Xmp, xmp_with("Thumbnail").into_bytes())); + // a compressed XMP item comes back as stored, for the caller to judge + let f = Heif::new().xmp(20, ("application/rdf+xml", "deflate"), b"compressed".to_vec()).build(); + assert_eq!(sure(&f), Some(vec![(20, Kind::EncodedXmp, b"compressed".to_vec())])); + // an EXIF item cut off by the end of the file, one larger than the reader takes, more items + // than it looks at: not sure + let mut f = Heif::new().item(10, b"Exif", exif_item(&[], &plain), Store::File, 1).build(); + f.truncate(f.len() - 1); + assert_eq!(sure(&f), None, "cut off"); + let f = Heif::new().item(10, b"Exif", exif_item(&[], &vec![0; MAX_ITEM]), Store::File, 1).build(); + assert_eq!(scan(&f).unwrap().next().map(|it| it.data), Some(Err(Unreadable::TooLarge)), "too large"); + let mut h = Heif::new(); + for id in 0..(MAX_SCANNED as u32 + 1) { + h = h.item(10 + id, b"Exif", exif_item(&[], &plain), Store::File, 1); + } + let f = h.build(); + assert!(!scan(&f).unwrap().complete(), "too many items"); + assert_eq!(scan(&f).unwrap().count(), MAX_SCANNED); + assert_eq!(exif(&f), Some(plain), "read still finds the photo's EXIF"); +} + +/// Replace only the catalog; the real metadata remains in `idat` with unchanged `iloc`. +fn with_iinf(h: &Heif, iinf: Option<&[u8]>) -> Vec { + assert!(h.items.iter().filter(|it| it.kind == b"Exif" || it.kind == b"mime").all(|it| it.store == Store::Idat)); + let meta = h.meta(0); + let mut budget = MAX_BOXES; + let children: Vec = boxes(&meta, 12, meta.len(), &mut budget) + .iter() + .flat_map(|b| if &b.kind == b"iinf" { iinf.map(|data| bx(b"iinf", data)).unwrap_or_default() } else { bx(&b.kind, &meta[b.body..b.end]) }) + .collect(); + [bx(b"ftyp", &h.ftyp), full_box(b"meta", 0, &children)].concat() +} + +fn catalog(version: u8, count: u32, entries: &[u8]) -> Vec { + let mut data = vec![version, 0, 0, 0]; + put(&mut data, u64::from(count), if version == 0 { 2 } else { 4 }); + data.extend_from_slice(entries); + data +} + +fn catalog_entry(version: u8, id: u32, kind: &[u8; 4], fields: &[u8]) -> Vec { + let mut data = vec![version, 0, 0, 0]; + put(&mut data, u64::from(id), if version == 3 { 4 } else { 2 }); + data.extend_from_slice(&[0, 0]); + if version >= 2 { + data.extend_from_slice(kind); + } + data.extend_from_slice(fields); + data +} + +/// An `infe` cut anywhere, even one that keeps the id and part of the type, makes the scan unsure. +#[test] +fn item_catalog_truncated_type_is_uncertain() { + let tiff = tiff_with("Located"); + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff), Store::Idat, 1); + for version in [2, 3] { + let entry = catalog_entry(version, 2, b"Exif", b"\0"); + let intact = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry)))); + assert_eq!(exif(&intact), Some(tiff.clone()), "infe v{version}"); + assert_eq!(sure(&intact), Some(vec![(2, Kind::Exif, exif_item(&[], &tiff))]), "infe v{version}"); + for end in 0..entry.len() { + let damaged = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry[..end])))); + assert_eq!(sure(&damaged), None, "infe v{version}, cut at {end}"); + } + } +} + +/// An XMP entry cut in its fields or strings makes the scan unsure, in every `infe` and `iinf` version. +#[test] +fn item_catalog_truncated_fields_and_strings_are_uncertain() { + let packet = xmp_with("Located").into_bytes(); + let mut h = Heif::new().xmp(2, ("application/rdf+xml", ""), packet.clone()); + h.items[1].store = Store::Idat; + for version in 0..=3 { + // The optional content encoding is absent; name and content type are terminated. + let entry = catalog_entry(version, 2, b"mime", b"name\0application/rdf+xml\0"); + for table_version in [0, 1] { + let intact = with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry)))); + assert_eq!(sure(&intact), Some(vec![(2, Kind::Xmp, packet.clone())]), "infe v{version}, iinf v{table_version}"); + for end in 0..entry.len() { + let damaged = with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry[..end])))); + assert_eq!(sure(&damaged), None, "infe v{version}, iinf v{table_version}, cut at {end}"); + } + } + } +} + +/// A missing or cut catalog, unknown versions, unknown entry boxes, a count that doesn't match +/// the entries and unterminated strings all make the scan unsure. +#[test] +fn item_catalog_incomplete_walks_counts_and_versions_are_uncertain() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + let entry = catalog_entry(2, 2, b"Exif", b"\0"); + let intact = catalog(0, 1, &bx(b"infe", &entry)); + assert!(sure(&with_iinf(&h, Some(&intact))).is_some()); + let mut cases = vec![("missing catalog".to_string(), None)]; + for end in 0..intact.len() { + cases.push((format!("catalog cut at {end}"), Some(intact[..end].to_vec()))); + } + cases.push(("unsupported table version".into(), Some(catalog(2, 1, &bx(b"infe", &entry))))); + let mut unsupported = entry.clone(); + unsupported[0] = 4; + cases.push(("unsupported entry version".into(), Some(catalog(0, 1, &bx(b"infe", &unsupported))))); + cases.push(("unknown entry box".into(), Some(catalog(0, 1, &bx(b"free", &entry))))); + // A valid image entry cannot explain a declared but absent second entry. + let image = bx(b"infe", &catalog_entry(2, 1, b"hvc1", b"\0")); + for version in [0, 1] { + for count in [0, 2] { + cases.push((format!("iinf v{version}, count {count}"), Some(catalog(version, count, &image)))); + } + } + for (kind, fields) in [(b"hvc1", b"unterminated".as_slice()), (b"uri ", b"name\0unterminated"), (b"mime", b"name\0image/jpeg\0gzip")] { + cases.push((format!("unterminated {kind:?} field"), Some(catalog(0, 1, &bx(b"infe", &catalog_entry(2, 2, kind, fields)))))); + } + for (label, data) in cases { + assert_eq!(sure(&with_iinf(&h, data.as_deref())), None, "{label}"); + } +} + +/// An unreadable catalog entry doesn't stop [`read`] from returning the EXIF it could read. +#[test] +fn item_catalog_uncertainty_keeps_readable_exif() { + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Readable")), Store::Idat, 1).xmp( + 3, + ("application/rdf+xml", ""), + xmp_with("Located").into_bytes(), + ); + h.items[2].store = Store::Idat; + assert!(sure(&h.build()).is_some()); + let exif_entry = bx(b"infe", &catalog_entry(2, 2, b"Exif", b"\0")); + let broken = bx(b"infe", &catalog_entry(2, 3, b"mime", b"name\0application/rdf+")); + for entries in [[exif_entry.clone(), broken.clone()], [broken, exif_entry]] { + let f = with_iinf(&h, Some(&catalog(0, 2, &entries.concat()))); + assert_eq!(exif(&f), Some(tiff_with("Readable"))); + assert_eq!(sure(&f), None, "unreadable XMP after or before readable EXIF"); + } +} + +/// Catalogs without metadata items, and protected items, leave a complete, empty scan. +#[test] +fn item_catalog_benign_entries_and_empty_tables_are_complete() { + let h = Heif::new(); + assert_eq!(sure(&h.build()), Some(Vec::new()), "image only"); + for table_version in [0, 1] { + assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 0, &[])))), Some(Vec::new()), "empty catalog"); + for version in 0..=3 { + for fields in [b"\0image/jpeg\0".as_slice(), b"name\0image/jpeg\0\0", b"name\0image/jpeg\0gzip\0"] { + let entry = catalog_entry(version, 1, b"mime", fields); + assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry))))), Some(Vec::new())); + } + } + for kind in [b"hvc1", b"av01", b"zzzz", b"uri "] { + let fields = if kind == b"uri " { b"name\0https://example.invalid/item\0".as_slice() } else { b"name\0" }; + let entry = catalog_entry(2, 1, kind, fields); + assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry))))), Some(Vec::new())); + } + } + // A protected (encrypted) item is not a metadata item this reader can read. + let mut entry = catalog_entry(2, 2, b"Exif", b"\0"); + entry[7] = 1; + let h = h.item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + assert_eq!(sure(&with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry))))), Some(Vec::new())); + // Complete top-level files without an item meta box have no item catalog to inspect. + assert_eq!(sure(&bx(b"ftyp", b"msf1\0\0\0\0msf1")), Some(Vec::new())); +} + +/// Running out of box budget, or a walk that stops before the end of its box, never makes a +/// scan complete. +#[test] +fn item_catalog_box_budget_and_enclosing_walks_cannot_clear_uncertainty() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + let f = h.build(); + let ftyp = bx(b"ftyp", &h.ftyp); + let mut top_cut = f.clone(); + top_cut[ftyp.len()..ftyp.len() + 4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(sure(&top_cut), None, "meta box cut short"); + let hidden = [ftyp.clone(), bx(b"free", &[]).repeat(MAX_BOXES), h.meta(0)].concat(); + assert_eq!(sure(&hidden), None, "top-level budget hides meta"); + let hidden = [ftyp.clone(), full_box(b"meta", 0, &[bx(b"free", &[]).repeat(MAX_BOXES), h.meta(0)[12..].to_vec()].concat())].concat(); + assert_eq!(sure(&hidden), None, "meta budget hides catalog"); + let image = bx(b"infe", &catalog_entry(2, 1, b"hvc1", b"\0")); + let data = catalog(1, MAX_BOXES as u32, &image.repeat(MAX_BOXES)); + assert_eq!(sure(&with_iinf(&h, Some(&data))), None, "catalog budget"); + let hidden = [ftyp, full_box(b"meta", 0, &[vec![0; 3], h.meta(0)[12..].to_vec()].concat())].concat(); + assert_eq!(sure(&hidden), None, "incomplete meta child walk"); +} + +/// Replace `iloc` in a builder fixture. Metadata stays in `idat`, so changing the box sizes +/// cannot change its offsets. The coded picture is not used by these metadata tests. +fn with_iloc(h: &Heif, iloc: Option<&[u8]>) -> Vec { + assert!(h.items.iter().filter(|it| it.kind == b"Exif" || it.kind == b"mime").all(|it| it.store == Store::Idat)); + let meta = h.meta(0); + let mut budget = MAX_BOXES; + let children: Vec = boxes(&meta, 12, meta.len(), &mut budget) + .iter() + .flat_map(|b| if &b.kind == b"iloc" { iloc.map(|data| bx(b"iloc", data)).unwrap_or_default() } else { bx(&b.kind, &meta[b.body..b.end]) }) + .collect(); + [bx(b"ftyp", &h.ftyp), full_box(b"meta", 0, &children)].concat() +} + +/// The `iloc` bytes of a builder fixture. +fn iloc_of(f: &[u8]) -> Vec { + Container::read(f, MAX_SCANNED).unwrap().iloc.unwrap().data.to_vec() +} + +/// An `iloc` cut after a readable EXIF entry leaves the later XMP unresolved: [`read`] still +/// returns the EXIF, and the scan reports the XMP as unreadable. +#[test] +fn iloc_truncation_after_readable_exif_leaves_xmp_unreadable() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("First")), Store::Idat, 1); + assert!(sure(&h.build()).is_some(), "readable EXIF alone"); + let mut h = h.xmp(3, ("application/rdf+xml", ""), xmp_with("Located").into_bytes()); + h.items[2].store = Store::Idat; + let f = h.build(); + assert_eq!(sure(&f).map(|v| v.len()), Some(2), "the intact XMP"); + let iloc = iloc_of(&f); + let cut = with_iloc(&h, Some(&iloc[..iloc.len() - 1])); + assert_eq!(exif(&cut), Some(tiff_with("First")), "the first metadata item still reads"); + let found: Vec = scan(&cut).unwrap().collect(); + assert_eq!(found[1], Item { id: 3, kind: Kind::Xmp, data: Err(Unreadable::Placement) }, "the unresolved XMP isn't skipped"); +} + +/// A missing, cut or unsupported `iloc` leaves every metadata item unreadable. +#[test] +fn iloc_unresolved_metadata_is_unreadable() { + for kind in [b"Exif", b"mime"] { + let mut h = Heif::new().item(2, kind, b"unread metadata".to_vec(), Store::Idat, 1); + h.items[1].mime = ("application/rdf+xml", ""); + let iloc = iloc_of(&h.build()); + assert_eq!(sure(&with_iloc(&h, None)), None, "missing iloc: {kind:?}"); + // Every cut leaves the metadata entry unresolved, including cuts in its last extent. + for end in 0..iloc.len() { + assert_eq!(sure(&with_iloc(&h, Some(&iloc[..end]))), None, "{kind:?}: iloc cut at {end}"); + } + // Unsupported version or any one of the four field sizes. + for (at, value) in [(0, 3), (4, 0x24), (4, 0x42), (5, 0x20), (5, 0x02)] { + let mut bad = iloc.clone(); + bad[at] = value; + assert_eq!(sure(&with_iloc(&h, Some(&bad))), None, "{kind:?}: field {at} = {value}"); + } + } +} + +/// A partial `iloc` walk that placed one item doesn't make another, unplaced item absent. +#[test] +fn iloc_partial_walk_does_not_clear_unresolved_metadata() { + let h = Heif::new().item(2, b"Exif", Vec::new(), Store::Idat, 1).item(3, b"Exif", b"unread metadata".to_vec(), Store::Idat, 1); + let iloc = iloc_of(&h.build()); + // Picture: 16 bytes; empty EXIF: 8 bytes; final EXIF: 16 bytes. Keep the first EXIF + // intact while cutting the second entry at every byte, including its extent fields. + for end in 32..iloc.len() { + let cut = with_iloc(&h, Some(&iloc[..end])); + let partial = Container::read(&cut, MAX_SCANNED).unwrap(); + assert!(matches!(partial.gather(0, MAX_ITEM), Gathered::Bytes(b) if b.is_empty())); + assert_eq!(sure(&cut), None, "iloc cut at {end}"); + } +} + +/// A complete `iloc` that places no entry for an item, or one with no extents, is sure: the +/// item has no bytes. +#[test] +fn iloc_complete_walk_allows_unplaced_and_empty_items() { + let h = Heif::new().item(2, b"Exif", Vec::new(), Store::Idat, 1); + assert_eq!(sure(&h.build()), Some(vec![(2, Kind::Exif, Vec::new())]), "placed EXIF with no extents"); + // A well-formed empty table, or a complete table placing only a different item. + for version in 0..=2 { + let mut iloc = vec![version, 0, 0, 0, 0, 0]; + put(&mut iloc, 0, if version == 2 { 4 } else { 2 }); + assert_eq!(sure(&with_iloc(&h, Some(&iloc))), Some(Vec::new()), "empty iloc v{version}"); + let count_at = iloc.len() - 1; + iloc[count_at] = 1; + put(&mut iloc, 9, if version == 2 { 4 } else { 2 }); + if version > 0 { + put(&mut iloc, 0, 2); // construction method + } + iloc.extend_from_slice(&[0, 0, 0, 0]); // this file, no extents + assert_eq!(sure(&with_iloc(&h, Some(&iloc))), Some(Vec::new()), "absent item in complete iloc v{version}"); + } + // No metadata candidates: there is nothing to resolve, even without a usable iloc. + assert_eq!(sure(&Heif::new().build()), Some(Vec::new())); + for iloc in [None, Some([3, 0, 0, 0, 0, 0].as_slice())] { + ILOC_ENTRIES.with(|n| n.set(0)); + assert_eq!(sure(&with_iloc(&Heif::new(), iloc)), Some(Vec::new())); + assert_eq!(ILOC_ENTRIES.with(std::cell::Cell::get), 0); + } +} + +/// The `iloc` walk ends once every metadata item is placed, before a damaged later entry. +#[test] +fn iloc_stops_after_all_metadata_is_placed() { + let h = Heif::new().item(2, b"Exif", Vec::new(), Store::Idat, 1).item(3, b"hvc1", vec![1], Store::File, 1); + let iloc = iloc_of(&h.build()); + // The last picture entry is truncated. The only metadata entry is already complete. + let cut = with_iloc(&h, Some(&iloc[..32])); + ILOC_ENTRIES.with(|n| n.set(0)); + assert_eq!(sure(&cut), Some(vec![(2, Kind::Exif, Vec::new())])); + assert_eq!(ILOC_ENTRIES.with(std::cell::Cell::get), 2, "stop after the picture and the EXIF entry"); +} + +/// A placed item whose extents leave its data, or pass the limit, can't be read. +#[test] +fn iloc_resolved_extents_still_obey_bounds_and_budget() { + let h = Heif::new().item(2, b"Exif", b"abcd".to_vec(), Store::Idat, 1); + let f = h.build(); + let c = Container::read(&f, MAX_SCANNED).unwrap(); + assert!(matches!(c.gather(0, 4), Gathered::Bytes(b) if b == b"abcd")); + assert!(matches!(c.gather(0, 3), Gathered::TooLarge)); + let iloc = iloc_of(&f); + for (offset, length) in [(0u32, 5u32), (5, 0), (u32::MAX, 1)] { + let mut bad = iloc.clone(); + let end = bad.len(); + bad[end - 8..end - 4].copy_from_slice(&offset.to_be_bytes()); + bad[end - 4..].copy_from_slice(&length.to_be_bytes()); + let f = with_iloc(&h, Some(&bad)); + let c = Container::read(&f, MAX_SCANNED).unwrap(); + assert!(matches!(c.gather(0, MAX_ITEM), Gathered::Unreadable)); + assert_eq!(sure(&f), None); + } +} + +/// `iloc` is walked once, however many metadata items there are to place (one walk per item +/// took over a second on a 48 MB `iloc` with 64 items). +#[test] +fn iloc_is_walked_once() { + // 64 metadata items, none placed by `iloc`, and 20,000 entries for other items + let mut iinf = vec![0u8, 0, 0, 0]; + iinf.extend_from_slice(&64u16.to_be_bytes()); + for id in 0..64u16 { + let mut e = vec![2u8, 0, 0, 0]; + e.extend_from_slice(&(1000 + id).to_be_bytes()); + e.extend_from_slice(&[0, 0]); + e.extend_from_slice(if id % 2 == 0 { b"Exif\0" } else { b"mime\0application/rdf+xml\0" }); + iinf.extend(bx(b"infe", &e)); + } + let entries = 20_000usize; + let mut iloc = vec![2u8, 0, 0, 0, 0, 0]; + iloc.extend_from_slice(&(entries as u32).to_be_bytes()); + for _ in 0..entries { + iloc.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]); // item 1, method 0, this file, no extents + } + let meta = full_box(b"meta", 0, &[bx(b"iinf", &iinf), bx(b"iloc", &iloc)].concat()); + let f = [bx(b"ftyp", b"heic\0\0\0\0mif1heic"), meta].concat(); + let walked = |run: &dyn Fn(&[u8])| { + ILOC_ENTRIES.with(|n| n.set(0)); + run(&f); + ILOC_ENTRIES.with(std::cell::Cell::get) + }; + let n = walked(&|f| assert_eq!(read(f).unwrap(), (None, None))); + assert!(n <= entries, "read walked {n} entries of {entries}"); + let n = walked(&|f| assert_eq!(sure(f), Some(Vec::new()))); + assert!(n <= entries, "scan walked {n} entries of {entries}"); +} + +proptest::proptest! { + /// Arbitrary bytes behind a HEIF `ftyp`, or inside its `meta` box, never panic. + #[test] + fn arbitrary_boxes_never_panic(body in proptest::collection::vec(proptest::prelude::any::(), 0..512)) { + for f in [[bx(b"ftyp", b"heic\0\0\0\0mif1heic"), body.clone()].concat(), [bx(b"ftyp", b"heic\0\0\0\0mif1heic"), full_box(b"meta", 0, &body)].concat()] { + let _ = read(&f); + let _ = scan(&f).map(Iterator::count); + } + } +} From 402e686c7a002a8b686afa5b4ea34c13d3fd14b8 Mon Sep 17 00:00:00 2001 From: Alex Chambers Date: Sat, 14 Nov 2026 21:57:59 +0100 Subject: [PATCH 010/471] craft-heif: document items::read, scan and exif_tiff --- crates/heif/README.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/heif/README.md b/crates/heif/README.md index d5adf6fdd..6842dcc0d 100644 --- a/crates/heif/README.md +++ b/crates/heif/README.md @@ -36,11 +36,24 @@ Errors are `Error::Unsupported` (image sequences, overlays, 4:2:2/4:4:4, …), ` `Error::Malformed`. It never panics: every call into heic-rs runs under `catch_unwind`, and a panic inside it (PhotoCraft's fuzzing found two in 0.1.1) becomes `Error::Malformed`. -Features: `decode` (default) is the decoder above. `metadata` adds `craft_heif::items::read`: the -first `Exif` item and XMP packet from the box structure alone (`iinf`, `iloc`, `idat`; at most 4096 -boxes, 1024 items, 1024 extents per item, 32 MiB per item), without heic-rs. A build with +Features: `decode` (default) is the decoder above. `metadata` adds `craft_heif::items`, which reads +the `Exif` and XMP items from the box structure alone (`pitm`, `iinf`, `iref`, `iloc`, `idat`), +without heic-rs: + +- `items::read` returns the photo's own EXIF and XMP: the items `cdsc` ties to the primary image or + to an image it is made from (`dimg`: grid tiles, `iden`, `iovl`, `tmap`), else those tied to no + image. A thumbnail's or depth map's metadata is not the photo's. +- `items::scan` returns every EXIF and XMP item, whichever image it describes, and says when it + can't be sure it saw them all: `Scan::complete` is false for an incomplete or damaged item + catalog, and an item whose bytes can't be read comes back as `Unreadable`. An app that must + prove a file has no location (before an export that removes it) refuses when unsure. +- `items::exif_tiff` finds the TIFF stream in an `Exif` item, with or without a correct offset + field or an `Exif\0\0` prefix. + +The reader walks at most 65,536 boxes and walks `iloc` once. It gathers at most 16 MiB per item, +and a scan gathers at most 64 MiB in all. A build with `default-features = false, features = ["metadata"]` has no heic-rs dependency, and its -`read_metadata` is that reader. With both features, a test checks that the two readers agree. +`read_metadata` is `items::read`. With both features, a test checks that the two readers agree. Feature `testdata` exposes `craft_heif::testdata`: lossless synthetic HEIF files (PCM-coded 32 × 32 HEVC pictures, grids, every property above) for the tests of the crates above. From 2caa7deba14a39465de8fc5a24cceabd3fc71275 Mon Sep 17 00:00:00 2001 From: Alex Chambers Date: Sat, 14 Nov 2026 23:28:51 +0100 Subject: [PATCH 011/471] craft-heif: record the proptest dev-dependency in Cargo.lock --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 52b28f0bd..34837eec4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1785,6 +1785,7 @@ name = "craft-heif" version = "0.1.0" dependencies = [ "heic-rs", + "proptest", "rayon", ] From 7ad164ff8a8938965774fc2581e6545404839b6a Mon Sep 17 00:00:00 2001 From: Alex Chambers Date: Sun, 15 Nov 2026 00:26:42 +0100 Subject: [PATCH 012/471] craft-heif: refuse a too-large item before copying any of it `Iloc::gather` copied extents until it passed its limit and then returned `TooLarge`, and `scan` charges its byte budget only for items it returns. So 1,024 items, each naming one 64 KiB region 257 times, copied 16 GiB from a 2 MiB file, and `read` up to 1 GiB. Check every extent and sum the lengths first, and copy only when the total fits. The new test counts the bytes copied over the full 1,024-gather scan and `read`'s 64 candidates; it copied 16 GiB before. --- crates/heif/src/items.rs | 39 ++++++++++++++++++----- crates/heif/src/items/tests.rs | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/crates/heif/src/items.rs b/crates/heif/src/items.rs index d00dbdf06..2d86b3027 100644 --- a/crates/heif/src/items.rs +++ b/crates/heif/src/items.rs @@ -589,7 +589,9 @@ impl<'a> Iloc<'a> { } /// An item's bytes, its extents in order from the file (construction method 0) or from the - /// `meta` box's `idat` (method 1), at most `limit` of them. + /// `meta` box's `idat` (method 1), at most `limit` of them. Every extent is checked and the + /// lengths summed before anything is copied, so an item that is too large (say, the same + /// region named by thousands of extents) costs no copying. fn gather(&self, file: &[u8], idat: Option<&[u8]>, place: &Place, limit: usize) -> Gathered { // data reference 0 is this file let source = match (place.data_reference, place.method, idat) { @@ -597,14 +599,25 @@ impl<'a> Iloc<'a> { (0, 1, Some(idat)) => idat, _ => return Gathered::Unreadable, }; - let mut out = Vec::new(); - let mut at = place.extents_at; - for _ in 0..place.extents { - let Some((piece, next)) = self.extent(source, place.base, at) else { return Gathered::Unreadable }; - at = next; - if out.len().saturating_add(piece.len()) > limit { + let pieces = || { + let mut at = place.extents_at; + (0..place.extents).map(move |_| { + let (piece, next) = self.extent(source, place.base, at)?; + at = next; + Some(piece) + }) + }; + let mut total = 0usize; + for piece in pieces() { + let Some(piece) = piece else { return Gathered::Unreadable }; + total = total.saturating_add(piece.len()); + if total > limit { return Gathered::TooLarge; } + } + let mut out = Vec::with_capacity(total); + for piece in pieces().flatten() { + bytes_copied(piece.len()); out.extend_from_slice(piece); } Gathered::Bytes(out) @@ -635,6 +648,18 @@ fn iloc_entry_visited() { ILOC_ENTRIES.with(|n| n.set(n.get().saturating_add(1))); } +#[cfg(test)] +thread_local! { + /// Item bytes copied on this thread, for the tests that bound the work. + static COPIED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg_attr(not(test), allow(unused_variables))] +fn bytes_copied(n: usize) { + #[cfg(test)] + COPIED.with(|c| c.set(c.get().saturating_add(n))); +} + /// A big-endian unsigned field of `size` bytes (0 reads as 0). fn uint(b: &[u8], at: usize, size: usize) -> Option { match size { diff --git a/crates/heif/src/items/tests.rs b/crates/heif/src/items/tests.rs index 49cc9d789..6f73ff6c1 100644 --- a/crates/heif/src/items/tests.rs +++ b/crates/heif/src/items/tests.rs @@ -1128,6 +1128,63 @@ fn iloc_resolved_extents_still_obey_bounds_and_budget() { } } +/// A file whose MAX_SCANNED `Exif` items each name one 64 KiB region 257 times: every item is +/// too large, and finding that out copies nothing, for [`scan`] (all 1,024 gathers) and for +/// [`read`] (its MAX_CANDIDATES). Before, each gather copied MAX_ITEM bytes before giving up, +/// about 16 GiB for the scan of a 2 MiB file. +#[test] +fn too_large_items_are_refused_before_copying() { + const REGION: u32 = 64 << 10; + const REPEATS: u16 = 257; + let count = MAX_SCANNED as u16; + let mut iinf = count.to_be_bytes().to_vec(); + for id in 1..=count { + let mut e = vec![2u8, 0, 0, 0]; + e.extend_from_slice(&id.to_be_bytes()); + e.extend_from_slice(&[0, 0]); + e.extend_from_slice(b"Exif\0"); + iinf.extend(bx(b"infe", &e)); + } + let iloc = |region_at: u32| { + let mut l = vec![0x44, 0x00]; + l.extend_from_slice(&count.to_be_bytes()); + for id in 1..=count { + l.extend_from_slice(&id.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); // method 0, this file + l.extend_from_slice(&REPEATS.to_be_bytes()); + for _ in 0..REPEATS { + l.extend_from_slice(®ion_at.to_be_bytes()); + l.extend_from_slice(®ION.to_be_bytes()); + } + } + full_box(b"iloc", 1, &l) + }; + let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + let meta = |region_at| full_box(b"meta", 0, &[full_box(b"iinf", 0, &iinf), iloc(region_at)].concat()); + let region_at = (ftyp.len() + meta(0).len() + 8) as u32; + let f = [ftyp, meta(region_at), bx(b"mdat", &vec![0; REGION as usize])].concat(); + + COPIED.with(|c| c.set(0)); + let s = scan(&f).unwrap(); + assert!(s.complete()); + let found: Vec = s.collect(); + assert_eq!(found.len(), MAX_SCANNED); + assert!(found.iter().all(|it| it.data == Err(Unreadable::TooLarge))); + assert_eq!(COPIED.with(std::cell::Cell::get), 0, "scan copied bytes of items it refused"); + + COPIED.with(|c| c.set(0)); + assert_eq!(read(&f).unwrap(), (None, None)); + assert_eq!(COPIED.with(std::cell::Cell::get), 0, "read copied bytes of items it refused"); + + // an item that fits is copied once, extent by extent + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::Idat, 3); + h.iloc = (1, [4, 4, 4, 0]); + let f = h.build(); + COPIED.with(|c| c.set(0)); + assert_eq!(sure(&f), Some(vec![(2, Kind::Exif, exif_item(&[], &SWEEP_TIFF))])); + assert_eq!(COPIED.with(std::cell::Cell::get), SWEEP_TIFF.len() + 4); +} + /// `iloc` is walked once, however many metadata items there are to place (one walk per item /// took over a second on a 48 MB `iloc` with 64 items). #[test] From ed70d09162f4f932876536cd4bfc0905fc070605 Mon Sep 17 00:00:00 2001 From: Alex Chambers Date: Sun, 15 Nov 2026 00:28:28 +0100 Subject: [PATCH 013/471] craft-heif: a protected Exif or XMP item is unreadable, not absent A nonzero protection index made `item_info` return before it read the item type, so a protected `Exif` or XMP item vanished: `scan` was complete and empty, as if the file held no metadata. The protection index doesn't hide the type, so read it, and return the item from `scan` as `Unreadable::Protected`. `read` still leaves it out. --- crates/heif/src/items.rs | 29 ++++++++++++++++++++--------- crates/heif/src/items/tests.rs | 33 +++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/crates/heif/src/items.rs b/crates/heif/src/items.rs index 2d86b3027..e0b2d2c93 100644 --- a/crates/heif/src/items.rs +++ b/crates/heif/src/items.rs @@ -145,6 +145,7 @@ impl Iterator for Scan<'_> { Gathered::NotPlaced => continue, Gathered::Unreadable => Err(Unreadable::Placement), Gathered::TooLarge => Err(Unreadable::TooLarge), + Gathered::Protected => Err(Unreadable::Protected), }; return Some(Item { id, kind, data }); } @@ -184,6 +185,9 @@ pub enum Unreadable { Placement, /// More than [`MAX_ITEM`] bytes, or more than [`scan`] has left of [`MAX_SCANNED_BYTES`]. TooLarge, + /// The item is protected (encrypted): its `infe` entry names a protection scheme, so its + /// stored bytes are not the metadata in the clear. + Protected, } /// The TIFF stream of an `Exif` item: past its 4-byte offset field and the bytes that offset @@ -214,6 +218,8 @@ struct Candidate { describes: bool, /// Tied to the primary image or an image it is made from. describes_primary: bool, + /// Protected (encrypted): there, but not readable. + protected: bool, } impl Candidate { @@ -308,6 +314,9 @@ impl<'a> Container<'a> { /// The bytes of `items[i]`, at most `limit` of them. fn gather(&self, i: usize, limit: usize) -> Gathered { + if self.items.get(i).is_some_and(|c| c.protected) { + return Gathered::Protected; + } match (&self.iloc, self.places.get(i)) { (Some(iloc), Some(Some(place))) => iloc.gather(self.file, self.idat, place, limit), _ if self.iloc_complete => Gathered::NotPlaced, @@ -326,6 +335,8 @@ enum Gathered { Unreadable, /// More than the limit. TooLarge, + /// A protected (encrypted) item. + Protected, } /// A full box's version: the first byte of its own payload (`None` for an empty box). @@ -370,14 +381,14 @@ fn item_infos(bytes: &[u8], iinf: &BoxRef, cap: usize, budget: &mut usize) -> It catalog.complete = entries.last().map_or(first, |b| b.end) == iinf.end && u32::try_from(entries.len()).ok() == Some(count); for b in &entries { let info = bytes.get(b.body..b.end).filter(|_| &b.kind == b"infe").and_then(item_info); - let Some((id, kind)) = info else { + let Some((id, kind, protected)) = info else { catalog.complete = false; continue; }; catalog.ids.push(id); match kind { Some(_) if catalog.items.len() >= cap => catalog.more = true, - Some(kind) => catalog.items.push(Candidate { id, kind, describes: false, describes_primary: false }), + Some(kind) => catalog.items.push(Candidate { id, kind, describes: false, describes_primary: false, protected }), None => {} } } @@ -386,9 +397,11 @@ fn item_infos(bytes: &[u8], iinf: &BoxRef, cap: usize, budget: &mut usize) -> It catalog } -/// An `infe` entry's item id, and its kind when it is an unprotected `Exif` or XMP item (`mime`, -/// `application/rdf+xml`). `None` when the entry can't be read to its last field. -fn item_info(infe: &[u8]) -> Option<(u32, Option)> { +/// An `infe` entry's item id, its kind when it is an `Exif` or XMP item (`mime`, +/// `application/rdf+xml`), and whether it is protected (encrypted). A protected entry still names +/// its type in the clear, so protected metadata is found too. `None` when the entry can't be read +/// to its last field. +fn item_info(infe: &[u8]) -> Option<(u32, Option, bool)> { let version = *infe.first()?; // version and flags, the item id (32 bits in version 3), the protection index let (id, at) = match version { @@ -396,9 +409,7 @@ fn item_info(infe: &[u8]) -> Option<(u32, Option)> { 3 => (be32(infe, 4)?, 8usize), _ => return None, }; - if be16(infe, at)? != 0 { - return Some((id, None)); // protected (encrypted) - } + let protected = be16(infe, at)? != 0; let at = at.checked_add(2)?; // versions 0 and 1 have no item type and describe MIME items; 2 and 3 name the type let (kind, at) = if version >= 2 { (infe.get(at..at.checked_add(4)?)?, at.checked_add(4)?) } else { (b"mime".as_slice(), at) }; @@ -421,7 +432,7 @@ fn item_info(infe: &[u8]) -> Option<(u32, Option)> { } _ => None, }; - Some((id, kind)) + Some((id, kind, protected)) } /// The NUL-terminated string at `at` and where the next field starts. diff --git a/crates/heif/src/items/tests.rs b/crates/heif/src/items/tests.rs index 6f73ff6c1..35a193613 100644 --- a/crates/heif/src/items/tests.rs +++ b/crates/heif/src/items/tests.rs @@ -707,14 +707,15 @@ fn malformed_bounds_are_refused() { let mut iref = vec![0, 0, 0, 0]; iref.extend(bx(b"cdsc", &[0, 2, 0xff, 0xff])); let iref = bx(b"iref", &iref); - let mut items = vec![Candidate { id: 2, kind: Kind::Exif, describes: false, describes_primary: false }]; + let mut items = vec![Candidate { id: 2, kind: Kind::Exif, describes: false, describes_primary: false, protected: false }]; let mut budget = MAX_BOXES; let b = boxes(&iref, 0, iref.len(), &mut budget); link(&iref, &b[0], Some(1), &mut items, &mut budget); assert!(!items[0].describes); assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 0, b'E', b'x']), None, "cut short: the item type is unknown"); assert_eq!(item_info(&[4, 0, 0, 0, 0, 2, 0, 0, b'E', b'x', b'i', b'f', 0]), None, "unknown infe version"); - assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x', b'i', b'f', 0]), Some((2, None)), "protected"); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x', b'i', b'f', 0]), Some((2, Some(Kind::Exif), true)), "protected"); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x']), None, "protected and cut short"); // a full box's version is its own first byte, never the next box's let two = [bx(b"iref", &[]), bx(b"free", &[1, 2, 3])].concat(); let mut budget = MAX_BOXES; @@ -946,7 +947,7 @@ fn item_catalog_uncertainty_keeps_readable_exif() { } } -/// Catalogs without metadata items, and protected items, leave a complete, empty scan. +/// Catalogs without metadata items leave a complete, empty scan. #[test] fn item_catalog_benign_entries_and_empty_tables_are_complete() { let h = Heif::new(); @@ -965,15 +966,35 @@ fn item_catalog_benign_entries_and_empty_tables_are_complete() { assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry))))), Some(Vec::new())); } } - // A protected (encrypted) item is not a metadata item this reader can read. - let mut entry = catalog_entry(2, 2, b"Exif", b"\0"); + // A protected (encrypted) image item is no metadata item. + let mut entry = catalog_entry(2, 1, b"hvc1", b"\0"); entry[7] = 1; - let h = h.item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); assert_eq!(sure(&with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry))))), Some(Vec::new())); // Complete top-level files without an item meta box have no item catalog to inspect. assert_eq!(sure(&bx(b"ftyp", b"msf1\0\0\0\0msf1")), Some(Vec::new())); } +/// A protected (encrypted) `Exif` or XMP item is in the scan, as unreadable: the file holds +/// metadata that can't be checked. [`read`] leaves it out. +#[test] +fn protected_metadata_is_unreadable_not_absent() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + for (kind, fields, expected) in [(b"Exif", b"\0".as_slice(), Kind::Exif), (b"mime", b"name\0application/rdf+xml\0", Kind::Xmp)] { + for version in [2, 3] { + let mut entry = catalog_entry(version, 2, kind, fields); + let protection_at = if version == 3 { 9 } else { 7 }; + entry[protection_at] = 1; + let f = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry)))); + let s = scan(&f).unwrap(); + assert!(s.complete(), "{kind:?} v{version}"); + let found: Vec = s.collect(); + assert_eq!(found, [Item { id: 2, kind: expected, data: Err(Unreadable::Protected) }], "{kind:?} v{version}"); + assert_eq!(sure(&f), None, "{kind:?} v{version}"); + assert_eq!(read(&f).unwrap(), (None, None), "{kind:?} v{version}"); + } + } +} + /// Running out of box budget, or a walk that stops before the end of its box, never makes a /// scan complete. #[test] From 2e3ff2162bbf0ee24dc225fc54df19eb4e539547 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 11:44:48 +0100 Subject: [PATCH 014/471] craft-image-codecs: SGI image reader and writer Adds Format::Sgi: 8- and 16-bit gray, gray+alpha, RGB and RGBA, verbatim and run-length encoded on read, run-length encoded on write. Palette-mapped files are reported as unsupported. Closes #1559 --- crates/image-codecs/README.md | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/sgi.rs | 345 ++++++++++++++++++++ crates/image-codecs/src/format.rs | 14 +- crates/image-codecs/src/lib.rs | 4 +- crates/image-codecs/tests/it/detect_caps.rs | 17 + 6 files changed, 379 insertions(+), 3 deletions(-) create mode 100644 crates/image-codecs/src/codecs/sgi.rs diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 6e9e11617..135853490 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,6 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| SGI (`.sgi`, `.rgb`, `.bw`) | yes | yes (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 0288ae22b..6f070a290 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -17,6 +17,7 @@ pub(crate) mod jxl; pub(crate) mod pcx; pub(crate) mod png; pub(crate) mod pnm; +pub(crate) mod sgi; pub(crate) mod tga; pub(crate) mod tiff; pub(crate) mod tiff_exif; diff --git a/crates/image-codecs/src/codecs/sgi.rs b/crates/image-codecs/src/codecs/sgi.rs new file mode 100644 index 000000000..1e31b5f0a --- /dev/null +++ b/crates/image-codecs/src/codecs/sgi.rs @@ -0,0 +1,345 @@ +//! SGI image files (`.sgi`, `.rgb`, `.rgba`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and +//! RGBA, verbatim or run-length encoded. +//! +//! Read and write follow the published SGI image file format description: a 512-byte big-endian +//! header, then one plane per channel with the rows stored bottom to top. Palette-mapped files +//! (colormap ID other than 0) are rejected. The writer uses the run-length encoding. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Sgi; +const HEADER: usize = 512; +const MAGIC: u16 = 474; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, reason) +} + +fn be16(b: &[u8], i: usize) -> u16 { + u16::from_be_bytes([b[i], b[i + 1]]) +} + +fn be32(b: &[u8], i: usize) -> u32 { + u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) +} + +/// The fields of the header that decoding needs, already checked for plausibility. +struct Header { + rle: bool, + wide: bool, + width: u32, + height: u32, + planes: usize, +} + +/// Parse the 512-byte header. `None` when the bytes are not a plausible SGI header. +fn header(b: &[u8]) -> Option
{ + if b.len() < HEADER || be16(b, 0) != MAGIC { + return None; + } + let (storage, bpc, dimension) = (b[2], b[3], be16(b, 4)); + let (width, height, planes) = (u32::from(be16(b, 6)), u32::from(be16(b, 8)), usize::from(be16(b, 10))); + if storage > 1 || !(1..=2).contains(&bpc) || !(1..=3).contains(&dimension) { + return None; + } + // A one-dimensional image has one row, a two-dimensional one a single plane. + let (height, planes) = match dimension { + 1 => (1, 1), + 2 => (height, 1), + _ => (height, planes), + }; + if width == 0 || height == 0 || !(1..=4).contains(&planes) { + return None; + } + Some(Header { rle: storage == 1, wide: bpc == 2, width, height, planes }) +} + +/// Whether `b` starts with a plausible SGI header (the magic number plus consistent fields). +pub(crate) fn looks_like_sgi(b: &[u8]) -> bool { + header(b).is_some() +} + +fn layout_of(planes: usize) -> ChannelLayout { + match planes { + 1 => ChannelLayout::Gray, + 2 => ChannelLayout::GrayA, + 3 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + } +} + +/// Expand one run-length encoded row into exactly `out.len()` samples. +fn read_rle_row(src: &[u8], wide: bool, out: &mut [u16]) -> Result<(), CodecError> { + let step = if wide { 2 } else { 1 }; + let sample = |pos: usize| -> Result { + let s = src.get(pos..pos + step).ok_or_else(|| bad("truncated RLE row"))?; + Ok(if wide { u16::from_be_bytes([s[0], s[1]]) } else { u16::from(s[0]) }) + }; + let (mut pos, mut n) = (0usize, 0usize); + while n < out.len() { + let code = sample(pos)?; + pos += step; + let count = usize::from(code & 0x7f); + if count == 0 { + return Err(bad("RLE row ends before the image width")); + } + if count > out.len() - n { + return Err(bad("RLE packet runs past the end of the row")); + } + if code & 0x80 != 0 { + for slot in &mut out[n..n + count] { + *slot = sample(pos)?; + pos += step; + } + } else { + let value = sample(pos)?; + pos += step; + out[n..n + count].fill(value); + } + n += count; + } + Ok(()) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + let h = header(b).ok_or_else(|| bad("invalid SGI header"))?; + if be32(b, 104) != 0 { + return Err(CodecError::unsupported(F, "palette-mapped SGI files are not supported")); + } + let layout = layout_of(h.planes); + let sample = if h.wide { SampleType::U16 } else { SampleType::U8 }; + limits.check(h.width, h.height, layout, sample)?; + let (w, ht, planes) = (h.width as usize, h.height as usize, h.planes); + let rows = ht.checked_mul(planes).ok_or_else(|| bad("image size overflow"))?; + let bps = if h.wide { 2 } else { 1 }; + let total = w.checked_mul(rows).ok_or_else(|| bad("image size overflow"))?; + // Reject inputs that cannot hold the declared pixels before allocating anything for them. + let table = if h.rle { rows.checked_mul(8).ok_or_else(|| bad("image size overflow"))? } else { 0 }; + let min_len = if h.rle { + // Every row needs at least a packet and a terminator. + HEADER + table + rows * bps + } else { + HEADER + total.checked_mul(bps).ok_or_else(|| bad("image size overflow"))? + }; + if b.len() < min_len { + return Err(bad("the file is too short for the declared image")); + } + let mut out = Vec::new(); + out.try_reserve_exact(total).map_err(|_| bad("pixel buffer is too large"))?; + out.resize(total, 0u16); + let mut row = vec![0u16; w]; + for z in 0..planes { + for y in 0..ht { + let index = y + z * ht; + if h.rle { + let start = be32(b, HEADER + index * 4) as usize; + let len = be32(b, HEADER + rows * 4 + index * 4) as usize; + let end = start.checked_add(len).filter(|&e| e <= b.len() && start >= HEADER).ok_or_else(|| bad("RLE row lies outside the file"))?; + read_rle_row(&b[start..end], h.wide, &mut row)?; + } else { + let start = HEADER + index * w * bps; + let s = &b[start..start + w * bps]; + for (x, v) in row.iter_mut().enumerate() { + *v = if h.wide { u16::from_be_bytes([s[2 * x], s[2 * x + 1]]) } else { u16::from(s[x]) }; + } + } + // Rows are stored bottom to top; planes are separate. + let top_y = ht - 1 - y; + for (x, &v) in row.iter().enumerate() { + out[(top_y * w + x) * planes + z] = v; + } + } + } + if h.wide { + Image::from_u16(h.width, h.height, layout, &out) + } else { + Image::from_u8(h.width, h.height, layout, out.iter().map(|&v| v as u8).collect()) + } +} + +/// Append one run-length encoded row (one packet list and a terminator). +fn write_rle_row(row: &[u16], wide: bool, out: &mut Vec) { + let put = |out: &mut Vec, v: u16| { + if wide { + out.extend_from_slice(&v.to_be_bytes()); + } else { + out.push(v as u8); + } + }; + let mut i = 0; + while i < row.len() { + let mut run = 1; + while i + run < row.len() && run < 127 && row[i + run] == row[i] { + run += 1; + } + if run >= 2 { + put(out, run as u16); + put(out, row[i]); + i += run; + } else { + // A literal packet extends until the next run of two or more. + let mut end = i + 1; + while end < row.len() && end - i < 127 && !(end + 1 < row.len() && row[end] == row[end + 1]) { + end += 1; + } + put(out, 0x80 | (end - i) as u16); + for &v in &row[i..end] { + put(out, v); + } + i = end; + } + } + put(out, 0); +} + +/// Writes a run-length encoded SGI file: 8 or 16 bits per channel, gray, gray + alpha, RGB or RGBA. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + let wide = match img.sample_type() { + SampleType::U8 => false, + SampleType::U16 => true, + s => return Err(enc(format!("SGI stores 8 or 16 bits per channel, not {s:?}"))), + }; + let planes = img.layout().channels(); + if !matches!(img.layout(), ChannelLayout::Gray | ChannelLayout::GrayA | ChannelLayout::Rgb | ChannelLayout::Rgba) { + return Err(enc(format!("no SGI layout for {:?}", img.layout()))); + } + let (w, ht) = img.dimensions(); + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(ht)) else { + return Err(enc(format!("dimensions exceed 65535x65535: {w}x{ht}"))); + }; + let (w, ht) = (w as usize, ht as usize); + let bps = if wide { 2 } else { 1 }; + let data = img.data(); + if data.len() != w * ht * planes * bps { + return Err(enc("the image has the wrong number of samples".into())); + } + let sample_at = |i: usize| -> u16 { if wide { u16::from_ne_bytes([data[2 * i], data[2 * i + 1]]) } else { u16::from(data[i]) } }; + let rows = ht * planes; + let table = HEADER; + let mut starts = Vec::with_capacity(rows); + let mut lens = Vec::with_capacity(rows); + let mut body = Vec::new(); + let mut row = vec![0u16; w]; + let base = u32::try_from(HEADER + rows * 8).map_err(|_| enc("image too large".into()))?; + for z in 0..planes { + for y in 0..ht { + let src_y = ht - 1 - y; // stored bottom to top + for (x, v) in row.iter_mut().enumerate() { + *v = sample_at((src_y * w + x) * planes + z); + } + let before = body.len(); + write_rle_row(&row, wide, &mut body); + let start = u32::try_from(base as usize + before).map_err(|_| enc("image too large".into()))?; + starts.push(start); + lens.push((body.len() - before) as u32); + } + } + let mut out = Vec::with_capacity(table + rows * 8 + body.len()); + out.extend_from_slice(&MAGIC.to_be_bytes()); + out.push(1); // RLE + out.push(bps as u8); + out.extend_from_slice(&(if planes > 1 { 3u16 } else { 2u16 }).to_be_bytes()); + out.extend_from_slice(&w16.to_be_bytes()); + out.extend_from_slice(&h16.to_be_bytes()); + out.extend_from_slice(&(planes as u16).to_be_bytes()); + out.extend_from_slice(&0u32.to_be_bytes()); // pixmin + out.extend_from_slice(&(if wide { 65535u32 } else { 255 }).to_be_bytes()); // pixmax + out.resize(HEADER, 0); // dummy, image name, colormap 0, padding + for s in &starts { + out.extend_from_slice(&s.to_be_bytes()); + } + for l in &lens { + out.extend_from_slice(&l.to_be_bytes()); + } + out.extend_from_slice(&body); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn gradient(w: u32, h: u32, layout: ChannelLayout) -> Image { + let ch = layout.channels(); + let data = (0..w as usize * h as usize * ch).map(|i| ((i * 7 + i / 5) % 251) as u8).collect(); + Image::from_u8(w, h, layout, data).unwrap() + } + + #[test] + fn rle_round_trips_every_layout_in_8_bits() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = gradient(37, 11, layout); + let plan = Plan { layout, sample: SampleType::U8 }; + let bytes = encode(&img, plan).unwrap(); + assert!(looks_like_sgi(&bytes)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.layout(), layout); + assert_eq!(back.data(), img.data()); + } + } + + #[test] + fn rle_round_trips_16_bits_with_long_runs() { + let (w, h) = (300u32, 4u32); + let samples: Vec = (0..w * h * 3).map(|i| if (i / 3) % 200 < 150 { 4242 } else { (i * 31) as u16 }).collect(); + let img = Image::from_u16(w, h, ChannelLayout::Rgb, &samples).unwrap(); + let bytes = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U16 }).unwrap(); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.sample_type(), SampleType::U16); + assert_eq!(back.data(), img.data()); + } + + /// A hand-built verbatim file: two rows, one plane, stored bottom to top. + #[test] + fn verbatim_files_read_bottom_to_top() { + let mut f = vec![0u8; HEADER]; + f[0..2].copy_from_slice(&MAGIC.to_be_bytes()); + f[2] = 0; // verbatim + f[3] = 1; + f[4..6].copy_from_slice(&2u16.to_be_bytes()); + f[6..8].copy_from_slice(&3u16.to_be_bytes()); // x + f[8..10].copy_from_slice(&2u16.to_be_bytes()); // y + f[10..12].copy_from_slice(&1u16.to_be_bytes()); // z + f.extend_from_slice(&[10, 11, 12, 20, 21, 22]); // bottom row first + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &[20, 21, 22, 10, 11, 12]); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + let img = gradient(16, 8, ChannelLayout::Rgb); + let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap(); + // Every truncation fails cleanly. + for len in 0..good.len() { + let _ = decode(&good[..len], &Limits::default()); + } + // A row table pointing outside the file, and a packet that overruns its row. + let mut bad_table = good.clone(); + bad_table[HEADER..HEADER + 4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&bad_table, &Limits::default()).is_err()); + let mut overrun = good.clone(); + let start = be32(&overrun, HEADER) as usize; + overrun[start] = 127; // a run longer than the 16-pixel row + assert!(decode(&overrun, &Limits::default()).is_err()); + // A huge declared size is stopped by the limits or the length check, before allocating. + let mut huge = good; + huge[6..8].copy_from_slice(&u16::MAX.to_be_bytes()); + huge[8..10].copy_from_slice(&u16::MAX.to_be_bytes()); + assert!(decode(&huge, &Limits::default()).is_err()); + } + + #[test] + fn palette_mapped_files_are_unsupported() { + let img = gradient(4, 4, ChannelLayout::Gray); + let mut f = encode(&img, Plan { layout: ChannelLayout::Gray, sample: SampleType::U8 }).unwrap(); + f[104..108].copy_from_slice(&1u32.to_be_bytes()); + assert!(matches!(decode(&f, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index ecaf945cd..d0d938bb7 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -44,6 +44,8 @@ pub enum Format { Iff, /// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Jp2, + /// SGI image files (`.sgi`, `.rgb`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and RGBA. + Sgi, } /// What a format can hold **and** what this crate reads/writes for it. @@ -113,7 +115,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L:: impl Format { /// Every format known to the crate (enabled or not). - pub const ALL: [Format; 24] = [ + pub const ALL: [Format; 25] = [ Format::Png, Format::Jpeg, Format::Jp2, @@ -138,6 +140,7 @@ impl Format { Format::Cineon, Format::Dicom, Format::Iff, + Format::Sgi, ]; pub fn caps(self) -> FormatCaps { @@ -176,6 +179,7 @@ impl Format { Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", Format::Jp2 => "JPEG 2000", + Format::Sgi => "SGI", } } @@ -207,6 +211,7 @@ impl Format { Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], + Format::Sgi => &["sgi", "rgb", "rgba", "bw"], } } @@ -236,13 +241,14 @@ impl Format { Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", Format::Jp2 => "image/jp2", + Format::Sgi => "image/x-sgi", } } /// Maximum encodable dimensions, if the format has a hard limit. pub fn max_dimensions(self) -> Option<(u32, u32)> { match self { - Format::Jpeg | Format::Gif | Format::Iff => Some((65535, 65535)), + Format::Jpeg | Format::Gif | Format::Iff | Format::Sgi => Some((65535, 65535)), Format::WebP => Some((16384, 16384)), Format::Ico => Some((256, 256)), _ => None, @@ -310,6 +316,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::Sgi => FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base }, Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -388,6 +395,9 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { return Some(Format::Xpm); } + if crate::codecs::sgi::looks_like_sgi(b) { + return Some(Format::Sgi); + } if crate::codecs::pcx::looks_like_pcx(b) { return Some(Format::Pcx); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 21130e392..cad63019d 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -62,7 +62,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, sgi, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -101,6 +101,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), + Format::Sgi => sgi::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), @@ -457,6 +458,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), + Format::Sgi => sgi::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index 640772c83..d7d448388 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + Format::Sgi, ]; for f in expected { assert!(caps(f).read && caps(f).write, "{f:?}"); @@ -218,6 +219,22 @@ fn detect_magic_iff() { assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); } +#[test] +fn detect_magic_sgi() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let img = Image::from_u8(5, 3, ChannelLayout::Rgba, (0..60).collect()).unwrap(); + let bytes = encode(&img, Format::Sgi, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(Format::Sgi)); + assert_eq!(from_extension("scan.RGB"), Some(Format::Sgi)); + assert_eq!(from_extension("scan.bw"), Some(Format::Sgi)); + assert_eq!(decode(&bytes).unwrap().data(), img.data()); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Sgi)); + assert_eq!(Format::Sgi.max_dimensions(), Some((65535, 65535))); + // The magic number alone, without a full header, is not SGI. + assert_eq!(detect(&[0x01, 0xda, 0, 1, 0, 2]), None); + assert!(decode_as(Format::Sgi, &[0x01, 0xda]).is_err()); +} + #[test] fn detect_magic_avif() { assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); From 5628ad359d8f5bf9778f53edba9b9989b4b61d09 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 11:49:53 +0100 Subject: [PATCH 015/471] craft-image-codecs: Sun Raster image reader and writer Adds Format::SunRaster: 1-, 8- (gray or colour-mapped), 24- and 32-bit files, uncompressed or byte-encoded (RLE) on read, uncompressed on write. Closes #1560 --- crates/image-codecs/README.md | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/sunraster.rs | 237 ++++++++++++++++++++ crates/image-codecs/src/format.rs | 12 +- crates/image-codecs/src/lib.rs | 4 +- crates/image-codecs/tests/it/detect_caps.rs | 14 ++ 6 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 crates/image-codecs/src/codecs/sunraster.rs diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 6e9e11617..f9f5f53e6 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,6 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| Sun Raster (`.ras`) | yes (incl. RLE and colour maps) | yes (uncompressed) | U8 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 0288ae22b..aecf95bbd 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -17,6 +17,7 @@ pub(crate) mod jxl; pub(crate) mod pcx; pub(crate) mod png; pub(crate) mod pnm; +pub(crate) mod sunraster; pub(crate) mod tga; pub(crate) mod tiff; pub(crate) mod tiff_exif; diff --git a/crates/image-codecs/src/codecs/sunraster.rs b/crates/image-codecs/src/codecs/sunraster.rs new file mode 100644 index 000000000..84d60f145 --- /dev/null +++ b/crates/image-codecs/src/codecs/sunraster.rs @@ -0,0 +1,237 @@ +//! Sun Raster files (`.ras`, `.sun`): 1-bit, 8-bit (gray or colour-mapped), 24-bit RGB and 32-bit +//! RGBA, uncompressed or byte-encoded (RLE) on read; uncompressed on write. +//! +//! Follows the published rasterfile format: a 32-byte big-endian header, an optional colour map, +//! then rows padded to an even number of bytes. In the "standard" type (1) 24- and 32-bit pixels +//! are stored blue-first; in the "RGB" type (3) red-first. Other types (TIFF, IFF, experimental) +//! are reported as unsupported. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::SunRaster; +const HEADER: usize = 32; +const MAGIC: u32 = 0x59a6_6a95; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, reason) +} + +fn be32(b: &[u8], i: usize) -> u32 { + u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) +} + +/// Whether `b` starts with a Sun rasterfile header. +pub(crate) fn looks_like_sunraster(b: &[u8]) -> bool { + b.len() >= HEADER && be32(b, 0) == MAGIC && matches!(be32(b, 12), 1 | 8 | 24 | 32) +} + +/// Expand byte-encoded data (`0x80` escapes a run) until `want` bytes are produced. +fn unrle(src: &[u8], want: usize) -> Result, CodecError> { + let mut out = Vec::new(); + out.try_reserve_exact(want).map_err(|_| bad("pixel buffer is too large"))?; + let mut i = 0; + while out.len() < want { + let b = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + if b != 0x80 { + out.push(b); + continue; + } + let n = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + if n == 0 { + out.push(0x80); + continue; + } + let v = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + let count = usize::from(n) + 1; + if count > want - out.len() { + return Err(bad("RLE run passes the end of the image")); + } + out.resize(out.len() + count, v); + } + Ok(out) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_sunraster(b) { + return Err(bad("invalid Sun raster header")); + } + let (w, h, depth, kind, map_type, map_len) = (be32(b, 4), be32(b, 8), be32(b, 12) as usize, be32(b, 20), be32(b, 24), be32(b, 28) as usize); + if !matches!(kind, 0..=3) { + return Err(CodecError::unsupported(F, format!("Sun raster type {kind} is not supported"))); + } + if map_type > 2 { + return Err(CodecError::unsupported(F, format!("Sun raster map type {map_type} is not supported"))); + } + let layout = match depth { + 1 | 8 if map_type != 1 => ChannelLayout::Gray, + 1 | 8 | 24 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + }; + limits.check(w, h, layout, SampleType::U8)?; + let (wu, hu) = (w as usize, h as usize); + let stride = wu.checked_mul(depth).and_then(|n| n.checked_add(15)).ok_or_else(|| bad("image size overflow"))? / 16 * 2; + let want = stride.checked_mul(hu).ok_or_else(|| bad("image size overflow"))?; + let data_at = HEADER.checked_add(map_len).ok_or_else(|| bad("colour map size overflow"))?; + let map = b.get(HEADER..data_at).ok_or_else(|| bad("truncated colour map"))?; + let rest = &b[data_at..]; + let raw; + let rows: &[u8] = if kind == 2 { + raw = unrle(rest, want)?; + &raw + } else { + rest.get(..want).ok_or_else(|| bad("the file is too short for the declared image"))? + }; + let channels = layout.channels(); + let mut out = Vec::new(); + out.try_reserve_exact(wu * hu * channels).map_err(|_| bad("pixel buffer is too large"))?; + let entries = map_len / 3; + for y in 0..hu { + let row = &rows[y * stride..(y + 1) * stride]; + for x in 0..wu { + match depth { + 1 => { + // A set bit is black. + let bit = (row[x / 8] >> (7 - x % 8)) & 1; + out.push(if bit == 1 { 0 } else { 255 }); + } + 8 if map_type == 1 => { + let i = usize::from(row[x]); + if i >= entries { + return Err(bad("pixel index is outside the colour map")); + } + out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]); + } + 8 => out.push(row[x]), + 24 => { + let p = &row[3 * x..3 * x + 3]; + out.extend_from_slice(&if kind == 3 { [p[0], p[1], p[2]] } else { [p[2], p[1], p[0]] }); + } + _ => { + let p = &row[4 * x..4 * x + 4]; + out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], p[0]] }); + } + } + } + } + Image::from_u8(w, h, layout, out) +} + +/// Writes an uncompressed rasterfile: 8-bit gray, 24-bit RGB or 32-bit RGBA, in the RGB-ordered type. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + if img.sample_type() != SampleType::U8 { + return Err(enc(format!("Sun raster stores 8 bits per channel, not {:?}", img.sample_type()))); + } + let depth: usize = match img.layout() { + ChannelLayout::Gray => 8, + ChannelLayout::Rgb => 24, + ChannelLayout::Rgba => 32, + l => return Err(enc(format!("no Sun raster layout for {l:?}"))), + }; + let (w, h) = img.dimensions(); + let (wu, hu) = (w as usize, h as usize); + let bytes = depth / 8; + let stride = (wu * bytes).next_multiple_of(2); + let len = stride.checked_mul(hu).and_then(|n| u32::try_from(n).ok()).ok_or_else(|| enc("image too large".into()))?; + let data = img.data(); + if data.len() != wu * hu * bytes { + return Err(enc("the image has the wrong number of samples".into())); + } + let mut out = Vec::with_capacity(HEADER + len as usize); + for v in [MAGIC, w, h, depth as u32, len, 3, 0, 0] { + out.extend_from_slice(&v.to_be_bytes()); + } + for y in 0..hu { + let row = &data[y * wu * bytes..(y + 1) * wu * bytes]; + if depth == 32 { + // Stored alpha first. + for p in row.as_chunks::<4>().0 { + out.extend_from_slice(&[p[3], p[0], p[1], p[2]]); + } + } else { + out.extend_from_slice(row); + } + out.resize(HEADER + (y + 1) * stride, 0); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pattern(w: u32, h: u32, layout: ChannelLayout) -> Image { + let n = w as usize * h as usize * layout.channels(); + Image::from_u8(w, h, layout, (0..n).map(|i| ((i * 13 + i / 3) % 253) as u8).collect()).unwrap() + } + + #[test] + fn round_trips_gray_rgb_and_rgba_with_odd_widths() { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = pattern(13, 7, layout); + let bytes = encode(&img, Plan { layout, sample: SampleType::U8 }).unwrap(); + assert!(looks_like_sunraster(&bytes)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!((back.layout(), back.data()), (layout, img.data())); + } + } + + fn file(depth: u32, kind: u32, map: &[u8], map_type: u32, w: u32, h: u32, data: &[u8]) -> Vec { + let mut f = Vec::new(); + for v in [MAGIC, w, h, depth, data.len() as u32, kind, map_type, map.len() as u32] { + f.extend_from_slice(&v.to_be_bytes()); + } + f.extend_from_slice(map); + f.extend_from_slice(data); + f + } + + #[test] + fn standard_type_is_blue_first_and_rle_expands() { + let f = file(24, 1, &[], 0, 1, 1, &[1, 2, 3, 0]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[3, 2, 1]); + // 0x80 0x00 is a literal 0x80; 0x80 n v is n + 1 copies of v. The 2x2 image has stride 2. + let f = file(8, 2, &[], 0, 2, 2, &[0x80, 0x00, 0x80, 0x00, 0x80, 0x01, 7]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0x80, 0x80, 7, 7]); + // A run that passes the end of the image is an error. + assert!(decode(&file(8, 2, &[], 0, 2, 2, &[0x80, 0x09, 7]), &Limits::default()).is_err()); + } + + #[test] + fn colour_mapped_and_one_bit_files() { + // Two entries: red plane, green plane, blue plane. + let map = [10, 20, 30, 40, 50, 60]; + let f = file(8, 1, &map, 1, 2, 1, &[0, 1]); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10, 30, 50, 20, 40, 60][..])); + let f = file(8, 1, &map, 1, 1, 1, &[5, 0]); + assert!(decode(&f, &Limits::default()).is_err()); + let f = file(1, 1, &[], 0, 3, 1, &[0b1010_0000, 0]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0, 255, 0]); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + let img = pattern(9, 5, ChannelLayout::Rgb); + let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap(); + for len in 0..good.len() { + let _ = decode(&good[..len], &Limits::default()); + } + let mut huge = good.clone(); + huge[4..8].copy_from_slice(&u32::MAX.to_be_bytes()); + huge[8..12].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&huge, &Limits::default()).is_err()); + let mut bad_map = good; + bad_map[28..32].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&bad_map, &Limits::default()).is_err()); + assert!(matches!(decode(&file(24, 4, &[], 0, 1, 1, &[0; 4]), &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index ecaf945cd..d4075cc8e 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -44,6 +44,8 @@ pub enum Format { Iff, /// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Jp2, + /// Sun Raster (`.ras`): 1-, 8-, 24- and 32-bit, uncompressed or RLE. + SunRaster, } /// What a format can hold **and** what this crate reads/writes for it. @@ -113,7 +115,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L:: impl Format { /// Every format known to the crate (enabled or not). - pub const ALL: [Format; 24] = [ + pub const ALL: [Format; 25] = [ Format::Png, Format::Jpeg, Format::Jp2, @@ -138,6 +140,7 @@ impl Format { Format::Cineon, Format::Dicom, Format::Iff, + Format::SunRaster, ]; pub fn caps(self) -> FormatCaps { @@ -176,6 +179,7 @@ impl Format { Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", Format::Jp2 => "JPEG 2000", + Format::SunRaster => "Sun Raster", } } @@ -207,6 +211,7 @@ impl Format { Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], + Format::SunRaster => &["ras", "sun"], } } @@ -236,6 +241,7 @@ impl Format { Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", Format::Jp2 => "image/jp2", + Format::SunRaster => "image/x-sun-raster", } } @@ -310,6 +316,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::SunRaster => FormatCaps { layouts: &[L::Gray, L::Rgb, L::Rgba], alpha: true, ..base }, Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -388,6 +395,9 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { return Some(Format::Xpm); } + if crate::codecs::sunraster::looks_like_sunraster(b) { + return Some(Format::SunRaster); + } if crate::codecs::pcx::looks_like_pcx(b) { return Some(Format::Pcx); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 21130e392..ab235d2ab 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -62,7 +62,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, sunraster, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -101,6 +101,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), + Format::SunRaster => sunraster::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), @@ -457,6 +458,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), + Format::SunRaster => sunraster::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index 640772c83..17fb20442 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + Format::SunRaster, ]; for f in expected { assert!(caps(f).read && caps(f).write, "{f:?}"); @@ -218,6 +219,19 @@ fn detect_magic_iff() { assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); } +#[test] +fn detect_magic_sun_raster() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let img = Image::from_u8(5, 3, ChannelLayout::Rgb, (0..45).collect()).unwrap(); + let bytes = encode(&img, Format::SunRaster, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(Format::SunRaster)); + assert_eq!(from_extension("scan.RAS"), Some(Format::SunRaster)); + assert_eq!(decode(&bytes).unwrap().data(), img.data()); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::SunRaster)); + assert_eq!(detect(&[0x59, 0xa6, 0x6a, 0x95]), None); + assert!(decode_as(Format::SunRaster, &[0x59, 0xa6, 0x6a, 0x95]).is_err()); +} + #[test] fn detect_magic_avif() { assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); From 3bd1794251a5cfdc72c99e2fa9d63c28ab827ba7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 13:24:04 +0100 Subject: [PATCH 016/471] DWG: keep field formats when saving an older drawing as DWG 2007 or later acadrust reads a pre-2007 DXF FIELD's format only into Field::format, but its DWG writer writes that for versions before 2007 only and value.format from 2007 on. Copy it over before writing. Co-Authored-By: Claude Sonnet 5.5 --- crates/dwg/src/lib.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 05c1a673c..216ed49a0 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -703,6 +703,23 @@ mod native { } } + /// A FIELD's format string between the two places acadrust 0.6.6 keeps it. Its DXF reader + /// puts a pre-2007 field's format (group 4) only in `Field::format`, and its DWG writer + /// writes that one only for versions before 2007; from 2007 it writes `value.format`. A field + /// read from an older drawing and written as DWG 2007 or later would lose its format + /// (`dd/MM/yyyy`, `%lu2`). The child values hold no format of their own in the old DXF. + fn field_formats(doc: &mut acadrust::CadDocument) { + if doc.version < acadrust::DxfVersion::AC1021 { + return; + } + for o in doc.objects.values_mut() { + let acadrust::objects::ObjectType::Field(f) = o else { continue }; + if f.value.format.is_empty() && !f.format.is_empty() { + f.value.format = f.format.clone(); + } + } + } + /// The DWG dimension flags byte. Its bit 1 is DXF group 70's bit 32 (ODA DWG specification, /// common dimension data), which acadrust keeps no copy of from DXF: it is set here as /// CADCraft's DXF writer always sets it (#1094). Bit 0, the opposite of group 70's bit 128, @@ -1238,6 +1255,7 @@ mod native { }; other_layout_owners(&mut doc); table_style_rows(&mut doc, true); + field_formats(&mut doc); dimension_flags(&mut doc); let mut unwritten = drop_unwritable(&mut doc); for (kind, n) in proxies { @@ -2103,4 +2121,43 @@ mod tests { assert_eq!(frames, [vec![0xDE, 0xAD, 0xBE, 0xEF]], "{version}"); } } + + /// A FIELD read from a pre-2007 DXF keeps its format (group 4) only in `Field::format`, which + /// acadrust's DWG writer writes just for versions before 2007: saved as DWG 2007 or later the + /// format (`dd/MM/yyyy`) was lost. A 2007+ field with its own format is written as it is. + #[cfg(not(no_dwg_bridge))] + #[test] + fn field_formats_survive_saving_an_older_drawing_as_dwg_2007_or_later() { + let formats = |dwg: Vec| -> Vec { + let back = acadrust::DwgReader::from_stream(std::io::Cursor::new(dwg)).read().unwrap(); + back.objects + .values() + .filter_map(|o| match o { + acadrust::objects::ObjectType::Field(f) => Some(f.value.format.clone()), + _ => None, + }) + .collect() + }; + for source in ["AC1015", "AC1018"] { + let dxf = format!( + " 0\nSECTION\n 2\nHEADER\n 9\n$ACADVER\n 1\n{source}\n 0\nENDSEC\n 0\nSECTION\n 2\nCLASSES\n 0\nCLASS\n 1\nFIELD\n 2\nAcDbField\n 3\nObjectDBX Classes\n 90\n0\n 91\n0\n 0\nENDSEC\n 0\nSECTION\n 2\nOBJECTS\n 0\nFIELD\n 5\nA1F\n330\n1F\n 1\nAcVar\n 2\n\\AcVar Date\n 4\ndd/MM/yyyy\n 90\n0\n 97\n0\n 91\n0\n 92\n0\n 94\n0\n 95\n0\n 96\n0\n300\n\n 0\nENDSEC\n 0\nEOF\n" + ); + assert_eq!(formats(dxf_to_dwg_as(dxf.as_bytes(), "AC1032").unwrap()), ["dd/MM/yyyy"], "{source}"); + } + let mut doc = acadrust::CadDocument::new(); + doc.classes.add_or_update(acadrust::classes::DxfClass::new("FIELD", "AcDbField")); + let handle = doc.allocate_handle(); + let field = acadrust::objects::Field { + handle, + owner: doc.header.named_objects_dict_handle, + evaluator_id: "AcVar".into(), + code: "\\AcVar Date".into(), + value: acadrust::entities::CellValue { format: "%lu2".into(), ..Default::default() }, + ..Default::default() + }; + doc.objects.insert(handle, acadrust::objects::ObjectType::Field(field)); + let mut dxf = Vec::new(); + acadrust::DxfWriter::new(&doc).write_to_writer(&mut dxf).unwrap(); + assert_eq!(formats(dxf_to_dwg_as(&dxf, "AC1032").unwrap()), ["%lu2"]); + } } From 0dba1ed64ebe764ffea4a97d801d65a9e708431e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 15:03:42 +0100 Subject: [PATCH 017/471] DWG: keep a field's format when the DXF is read as a newer kept version dxf_to_dwg_kept_as declares the kept version before reading, and acadrust 0.6.6 reads a FIELD's group 4 format only below AC1021, so an older drawing's own field lost its format. Collect the group 4 formats by handle before reading and put them back into an empty Field::format; field_formats then copies them into value.format for 2007+. Carried-over fields keep their own value.format. Co-Authored-By: Claude Opus 5.5 --- crates/dwg/src/lib.rs | 150 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 216ed49a0..2d173b325 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -708,7 +708,19 @@ mod native { /// writes that one only for versions before 2007; from 2007 it writes `value.format`. A field /// read from an older drawing and written as DWG 2007 or later would lose its format /// (`dd/MM/yyyy`, `%lu2`). The child values hold no format of their own in the old DXF. - fn field_formats(doc: &mut acadrust::CadDocument) { + /// + /// `legacy`: the group 4 formats of a DXF that was read as a newer version than it declares + /// ([`legacy_field_formats`]), which the reader then skipped. They go back into an empty + /// `Field::format` first, as the reader puts them there under the DXF's own version, so a + /// DWG before 2007 keeps them too. + fn field_formats(doc: &mut acadrust::CadDocument, legacy: &[(acadrust::Handle, String)]) { + for (handle, format) in legacy { + if let Some(acadrust::objects::ObjectType::Field(f)) = doc.objects.get_mut(handle) + && f.format.is_empty() + { + f.format.clone_from(format); + } + } if doc.version < acadrust::DxfVersion::AC1021 { return; } @@ -920,8 +932,9 @@ mod native { /// read from a file of version `kept` ("AC1032"), and acadrust reads several of those /// differently by version: read as R2000, an AC1032 file's mental ray render settings came out /// a class version too high, for which the ODA rejects the whole DWG, and a field lost its - /// cached value. So the DXF is read as `kept` declares. A `kept` version that is unknown, or - /// not newer than the DXF's own, changes nothing. + /// cached value. So the DXF is read as `kept` declares; the formats of the DXF's own pre-2007 + /// fields, which the reader skips then, are restored ([`legacy_field_formats`]). A `kept` + /// version that is unknown, or not newer than the DXF's own, changes nothing. pub fn dxf_to_dwg_kept_as(dxf: &[u8], version: &str, kept: &str) -> Result, String> { dxf_to_dwg_kept_noted(dxf, version, kept).map(|(dwg, _)| dwg) } @@ -987,6 +1000,35 @@ mod native { Some(out) } + /// The format (group 4) of each FIELD record in an ASCII DXF, by handle. acadrust 0.6.6 reads + /// it only from a DXF that declares a version before 2007 (AC1021), where it follows the + /// field code or the object handles, before group 91 (`read_field_object_dxf`). When + /// [`declare_version`] made an older DXF read as 2007 or later, a field the DXF wrote itself + /// would lose its format; a field carried over from a 2007+ file has none and keeps its own + /// `value.format`. + fn legacy_field_formats(dxf: &[u8]) -> Vec<(acadrust::Handle, String)> { + let Some(pairs) = super::proxy_transport::text_pairs(dxf) else { return Vec::new() }; + let mut formats = Vec::new(); + let (mut in_field, mut handle, mut format, mut past) = (false, None, None, false); + for pair in pairs { + let Some((code, value)) = pair else { break }; + match code { + 0 => { + if let (Some(h), Some(f)) = (handle.take(), format.take()) { + formats.push((h, f)); + } + in_field = value == "FIELD" || value == "ACDBFIELD"; + (handle, format, past) = (None, None, false); + } + 5 if in_field && handle.is_none() => handle = u64::from_str_radix(value.trim(), 16).ok().map(acadrust::Handle::new), + 4 if in_field && !past && format.is_none() => format = Some(value), + 91 if in_field => past = true, + _ => {} + } + } + formats + } + /// The byte offset and the trimmed value of header variable `name` in an ASCII DXF: the value /// of the group that follows the `9` group naming it. The file is read as (group code, /// value) line pairs, in one pass with no buffering, up to the first `0`/`ENDSEC`, the end @@ -1215,19 +1257,23 @@ mod native { )); } // Decoding a code page to UTF-8 can grow the text, so the limit applies again. - let data = match kept.and_then(|k| declare_version(dxf, k)) { - Some(d) if d.len() > Limits::DEFAULT.max_dxf_bytes => { + let (data, legacy) = match kept.and_then(|k| declare_version(dxf, k).map(|d| (k, d))) { + Some((_, d)) if d.len() > Limits::DEFAULT.max_dxf_bytes => { return Err(format!( "DXF: the input is {} as UTF-8, larger than the {} accepted for DWG conversion", mb(d.len() as u64), mb(Limits::DEFAULT.max_dxf_bytes as u64) )); } - Some(d) => d, - None => dxf.to_vec(), + // Read as 2007 or later, the DXF's own fields lose their formats: restored after. + Some((k, d)) => (d, k >= acadrust::DxfVersion::AC1021), + None => (dxf.to_vec(), false), }; - let (classes, source) = guarded(AssertUnwindSafe(|| (dxf_class_names(&data), super::proxy_transport::source_classes(&data)))) - .map_err(|_| "the DXF proxy class conversion failed".to_string())?; + let (classes, source, legacy) = guarded(AssertUnwindSafe(|| { + let legacy = if legacy { legacy_field_formats(&data) } else { Vec::new() }; + (dxf_class_names(&data), super::proxy_transport::source_classes(&data), legacy) + })) + .map_err(|_| "the DXF proxy class conversion failed".to_string())?; let mut doc = std::panic::catch_unwind(move || acadrust::DxfReader::from_reader(Cursor::new(data)).and_then(|r| r.read())) .map_err(|_| "the DXF→DWG conversion failed".to_string())? .map_err(|e| format!("DXF: {e}"))?; @@ -1255,7 +1301,7 @@ mod native { }; other_layout_owners(&mut doc); table_style_rows(&mut doc, true); - field_formats(&mut doc); + field_formats(&mut doc, &legacy); dimension_flags(&mut doc); let mut unwritten = drop_unwritable(&mut doc); for (kind, n) in proxies { @@ -2160,4 +2206,88 @@ mod tests { acadrust::DxfWriter::new(&doc).write_to_writer(&mut dxf).unwrap(); assert_eq!(formats(dxf_to_dwg_as(&dxf, "AC1032").unwrap()), ["%lu2"]); } + + /// A FIELD record as an AC1015/AC1018 DXF writes it, its format `dd/MM/yyyy` in group 4. + #[cfg(not(no_dwg_bridge))] + fn legacy_field(handle: acadrust::Handle, owner: acadrust::Handle) -> String { + format!( + " 0\r\nFIELD\r\n 5\r\n{handle:X}\r\n330\r\n{owner:X}\r\n 1\r\nAcVar\r\n 2\r\n\\AcVar Date\r\n 4\r\ndd/MM/yyyy\r\n 90\r\n0\r\n 97\r\n0\r\n 91\r\n0\r\n 92\r\n0\r\n 94\r\n0\r\n 95\r\n0\r\n 96\r\n0\r\n300\r\n\r\n" + ) + } + + /// The FIELD formats of a DWG by handle: `value.format` from 2007, `Field::format` before. + #[cfg(not(no_dwg_bridge))] + fn dwg_field_formats(dwg: Vec) -> Vec<(u64, String)> { + let back = acadrust::DwgReader::from_stream(std::io::Cursor::new(dwg)).read().unwrap(); + let mut formats: Vec<(u64, String)> = back + .objects + .values() + .filter_map(|o| match o { + acadrust::objects::ObjectType::Field(f) => Some((f.handle.into(), f.pre2007_format().to_string())), + _ => None, + }) + .collect(); + formats.sort(); + formats + } + + /// CADCraft's R2000 DXF of a drawing with fields declares AC1015 (or the drawing's AC1018) + /// and is read as the `kept` version, for which acadrust skips group 4: an older drawing's own + /// field lost its format (`dd/MM/yyyy`) in the DWG (#1580 review). + #[cfg(not(no_dwg_bridge))] + #[test] + fn field_formats_survive_a_kept_version_newer_than_the_drawing() { + for source in ["AC1015", "AC1018"] { + for kept in ["AC1024", "AC1032"] { + let owner = acadrust::Handle::new(0x1F); + let legacy = legacy_field(acadrust::Handle::new(0xA1F), owner); + let dxf = format!( + " 0\r\nSECTION\r\n 2\r\nHEADER\r\n 9\r\n$ACADVER\r\n 1\r\n{source}\r\n 0\r\nENDSEC\r\n 0\r\nSECTION\r\n 2\r\nCLASSES\r\n 0\r\nCLASS\r\n 1\r\nFIELD\r\n 2\r\nAcDbField\r\n 3\r\nObjectDBX Classes\r\n 90\r\n0\r\n 91\r\n0\r\n 0\r\nENDSEC\r\n 0\r\nSECTION\r\n 2\r\nOBJECTS\r\n{legacy} 0\r\nENDSEC\r\n 0\r\nEOF\r\n" + ); + for version in ["AC1032", "AC1018"] { + let got = dwg_field_formats(dxf_to_dwg_kept_as(dxf.as_bytes(), version, kept).unwrap()); + assert_eq!(got, [(0xA1F, "dd/MM/yyyy".to_string())], "{source} kept {kept} as {version}"); + } + } + } + } + + /// The same DXF with a field carried over from a 2007+ file next to the drawing's own: the + /// carried-over one keeps its own `value.format` (`%lu2`), the drawing's own its group 4 + /// format (`dd/MM/yyyy`). + #[cfg(not(no_dwg_bridge))] + #[test] + fn field_formats_survive_a_kept_version_with_carried_over_and_own_fields() { + for source in ["AC1015", "AC1018"] { + for kept in ["AC1024", "AC1032"] { + // The carried-over field as acadrust writes 2007+ DXF, the drawing's own field in + // the old layout, under the older version. + let mut doc = acadrust::CadDocument::new(); + doc.classes.add_or_update(acadrust::classes::DxfClass::new("FIELD", "AcDbField")); + let (carried, own) = (doc.allocate_handle(), doc.allocate_handle()); + let field = acadrust::objects::Field { + handle: carried, + owner: doc.header.named_objects_dict_handle, + evaluator_id: "AcVar".into(), + code: "\\AcVar Date".into(), + value: acadrust::entities::CellValue { format: "%lu2".into(), ..Default::default() }, + ..Default::default() + }; + doc.objects.insert(carried, acadrust::objects::ObjectType::Field(field)); + let mut dxf = Vec::new(); + acadrust::DxfWriter::new(&doc).write_to_writer(&mut dxf).unwrap(); + let dxf = String::from_utf8(dxf).unwrap(); + let dxf = dxf.replacen("$ACADVER\r\n 1\r\nAC1032\r\n", &format!("$ACADVER\r\n 1\r\n{source}\r\n"), 1); + let at = dxf.find(" 0\r\nFIELD\r\n").unwrap(); + let dxf = format!("{}{}{}", &dxf[..at], legacy_field(own, doc.header.named_objects_dict_handle), &dxf[at..]); + assert!(dxf.contains(&format!("$ACADVER\r\n 1\r\n{source}\r\n"))); + for version in ["AC1032", "AC1018"] { + let got = dwg_field_formats(dxf_to_dwg_kept_as(dxf.as_bytes(), version, kept).unwrap()); + let mut want = vec![(carried.into(), "%lu2".to_string()), (own.into(), "dd/MM/yyyy".to_string())]; + want.sort(); + assert_eq!(got, want, "mixed {source} kept {kept} as {version}"); + } + } + } + } } From 1985afe943f2088e56697948638d3286260e6db9 Mon Sep 17 00:00:00 2001 From: nuclearspike Date: Sun, 15 Nov 2026 09:40:41 -0500 Subject: [PATCH 018/471] tga: port metadata payload boundaries and regressions from FilmCraft #1932 Source head e7842f76f887005c460c64a8ab51d59d086e6f27; adapt original decoder guard and synthetic regressions to shared Picture/Error API. --- crates/tga/src/lib.rs | 118 ++++++++- crates/tga/src/metadata_tests.rs | 424 +++++++++++++++++++++++++++++++ 2 files changed, 536 insertions(+), 6 deletions(-) create mode 100644 crates/tga/src/metadata_tests.rs diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index ac9d21497..88f4efe6c 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -18,6 +18,8 @@ //! Input is treated as hostile: the picture size is capped by [`Limits`], and a run-length file //! must be long enough to hold the pixels its header declares before anything is allocated. +#[cfg(test)] +mod metadata_tests; #[cfg(test)] mod tests; @@ -180,6 +182,105 @@ impl Stored { } } +fn metadata_offset(body: &[u8], offset: usize, first_pixel: usize) -> Option { + // Optional metadata cannot replace the header, image ID, or colour map. + (offset >= first_pixel && offset < body.len()).then_some(offset) +} + +/// The start of every developer-directory payload with a nonzero length, from the directory at +/// `directory` in `body` (none when that offset holds no readable directory). +fn developer_payload_starts(body: &[u8], directory: usize) -> Vec { + let Some(count) = + body.get(directory..directory.saturating_add(2)).and_then(|v| v.first_chunk::<2>()).map(|v| usize::from(u16::from_le_bytes(*v))) + else { + return Vec::new(); + }; + let Some(fields) = body.get(directory.saturating_add(2)..) else { return Vec::new() }; + fields + .chunks(10) + .take(count) + .filter_map(|field| { + let start = field.get(2..6).and_then(|v| v.first_chunk::<4>()).map(|v| u32::from_le_bytes(*v) as usize)?; + let length = field.get(6..10).and_then(|v| v.first_chunk::<4>()).map(|v| u32::from_le_bytes(*v))?; + (length != 0).then_some(start) + }) + .collect() +} + +fn image_data_end(bytes: &[u8], first_pixel: usize) -> Result { + if !bytes.ends_with(SIGNATURE) { + return Ok(bytes.len()); + } + // A TGA 2.0 footer cannot begin inside the mandatory header. A legacy image + // may end with the same signature as pixel bytes. + let Some(footer) = bytes.len().checked_sub(SIGNATURE.len() + 8).filter(|&at| at >= HEADER) else { + return Ok(bytes.len()); + }; + let offsets = bytes.get(footer..footer + 8).and_then(|b| b.first_chunk::<8>()).ok_or_else(|| Error::Malformed("truncated footer"))?; + let extension = offsets.first_chunk::<4>().map(|v| u32::from_le_bytes(*v) as usize).ok_or_else(|| Error::Malformed("truncated footer"))?; + let directory = offsets + .get(4..) + .and_then(|v| v.first_chunk::<4>()) + .map(|v| u32::from_le_bytes(*v) as usize) + .ok_or_else(|| Error::Malformed("truncated footer"))?; + let body = bytes.get(..footer).ok_or_else(|| Error::Malformed("truncated footer"))?; + let mut end = footer; + + if let Some(offset) = metadata_offset(body, extension, first_pixel) { + // An in-body reference is a conservative image boundary, even if it overlaps + // required pixels. Those bytes cannot be distinguished from a truncated image + // whose missing pixels are supplied by metadata. + end = end.min(offset); + // TGA 2.0 extension fields at byte offsets 482, 486 and 490 point to the colour + // correction, postage-stamp and scan-line tables. The record's first two bytes give + // its size (495 in TGA 2.0); a shorter record has no such fields, and a record never + // reaches into a developer directory that follows it, so those bytes are not read as + // table offsets. Tables may precede the record. + let declared = + body.get(offset..offset.saturating_add(2)).and_then(|v| v.first_chunk::<2>()).map_or(0, |v| usize::from(u16::from_le_bytes(*v))); + let mut record_end = offset.saturating_add(declared.min(495)); + // The developer directory and its payloads may sit anywhere after the pixels, a payload + // before its directory included; whichever starts first after the extension ends it. + // A directory offset of zero means there is none: the header is not read as one. + let payloads = metadata_offset(body, directory, first_pixel).map(|d| developer_payload_starts(body, d)).unwrap_or_default(); + for next in std::iter::once(directory).chain(payloads) { + if next > offset { + record_end = record_end.min(next); + } + } + if let Some(area) = body.get(offset..record_end.min(body.len())) { + for field in [482, 486, 490] { + if let Some(value) = area.get(field..field + 4).and_then(|v| v.first_chunk::<4>()) + && let Some(table) = metadata_offset(body, u32::from_le_bytes(*value) as usize, first_pixel) + { + end = end.min(table); + } + } + } + } + + if let Some(offset) = metadata_offset(body, directory, first_pixel) { + end = end.min(offset); + if let Some(count) = + body.get(offset..offset.saturating_add(2)).and_then(|v| v.first_chunk::<2>()).map(|v| usize::from(u16::from_le_bytes(*v))) + && let Some(fields) = body.get(offset.saturating_add(2)..) + { + // A corrupt count or final entry does not hide earlier payload offsets. Each + // entry has a 2-byte tag followed by a 4-byte offset and 4-byte length. + for field in fields.chunks(10).take(count) { + if let Some(value) = field.get(2..6).and_then(|v| v.first_chunk::<4>()) + && let Some(length) = field.get(6..10).and_then(|v| v.first_chunk::<4>()) + && u32::from_le_bytes(*length) != 0 + && let Some(payload) = metadata_offset(body, u32::from_le_bytes(*value) as usize, first_pixel) + { + end = end.min(payload); + } + } + } + } + Ok(end) +} + /// Decodes a Targa file to straight-alpha RGBA8, top row first. pub fn decode(bytes: &[u8], limits: &Limits) -> Result { let err = Error::Malformed; @@ -208,13 +309,18 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { } else { None }; - let mut at = HEADER + id_len; - if let Some(l) = map_entry { - let end = at + map_len * l.bytes(); - let map = bytes.get(at..end).ok_or(err("truncated colour map"))?; - palette = map.chunks_exact(l.bytes()).map(|e| l.rgba(e)).collect(); - at = end; + let map_start = HEADER + id_len; + let first_pixel = match map_entry { + Some(layout) => map_start.checked_add(map_len * layout.bytes()).ok_or(err("colour map"))?, + None => map_start, + }; + let end = image_data_end(bytes, first_pixel)?; + let bytes = bytes.get(..end).ok_or(err("truncated image data"))?; + if let Some(layout) = map_entry { + let map = bytes.get(map_start..first_pixel).ok_or(err("truncated colour map"))?; + palette = map.chunks_exact(layout.bytes()).map(|entry| layout.rgba(entry)).collect(); } + let at = first_pixel; let (stored_as, mapped) = match kind & 7 { 1 => { if map_type != 1 || !matches!(depth, 8 | 16) { diff --git a/crates/tga/src/metadata_tests.rs b/crates/tga/src/metadata_tests.rs new file mode 100644 index 000000000..a5c6c0b12 --- /dev/null +++ b/crates/tga/src/metadata_tests.rs @@ -0,0 +1,424 @@ +//! Metadata-boundary regressions ported from FilmCraft #1932 (nuclearspike). +use super::*; +fn header(kind: u8, w: u16, h: u16, depth: u8, descriptor: u8) -> Vec { + let mut b = vec![0, 0, kind, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + b.extend_from_slice(&w.to_le_bytes()); + b.extend_from_slice(&h.to_le_bytes()); + b.extend_from_slice(&[depth, descriptor]); + b +} +fn dec(bytes: &[u8]) -> Result { + decode(bytes, &Limits::default()) +} +#[test] +fn legacy_pixels_that_end_in_signature_are_not_a_footer_before_header() { + for prefix in [0usize, 7] { + let width = (prefix + SIGNATURE.len()) as u16; + let mut file = header(3, width, 1, 8, 0x20); + file.extend(vec![0; prefix]); + file.extend_from_slice(SIGNATURE); + let image = dec(&file).unwrap(); + assert_eq!((image.width, image.height), (u32::from(width), 1)); + let pixels: Vec = std::iter::repeat_n(0, prefix).chain(SIGNATURE.iter().copied()).flat_map(|gray| [gray, gray, gray, 255]).collect(); + assert_eq!(image.rgba, pixels); + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, pixels); + } +} + +#[test] +fn short_extension_followed_by_a_developer_payload_keeps_the_pixels() { + // a 482-byte extension whose size field still says 495, then a developer payload whose + // first bytes land where a full extension keeps its colour-correction offset, then the + // directory that points back at that payload + let mut file = header(2, 1, 1, 24, 0x20); + file.extend_from_slice(&[10, 20, 30]); + let extension = file.len() as u32; + let mut area = vec![0; 482]; + area[..2].copy_from_slice(&495u16.to_le_bytes()); + file.extend_from_slice(&area); + let payload = file.len() as u32; + file.extend_from_slice(&18u32.to_le_bytes()); + file.extend_from_slice(&[0; 14]); + let directory = file.len() as u32; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&0u16.to_le_bytes()); + file.extend_from_slice(&payload.to_le_bytes()); + file.extend_from_slice(&18u32.to_le_bytes()); + file.extend_from_slice(&extension.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + let source = dec(&file).expect("the image decodes"); + assert_eq!((source.width, source.height), (1, 1)); + assert_eq!(source.rgba, vec![30, 20, 10, 255]); +} + +#[test] +fn a_footer_without_a_directory_does_not_read_the_header_as_one() { + // no developer directory (offset 0). Read as one, the header's three-byte image ID + // length is an entry count, and the third "entry" is the first words of a colour + // correction table that sits right after a short pixel area; the extension points at + // that table, so the image is incomplete and must stay so. + let mut file = header(2, 2, 1, 24, 0x20); + file[0] = 3; + file.extend_from_slice(&[7, 8, 9]); + file.extend_from_slice(&[0, 0, 255]); + let table = file.len() as u32; + let mut words = vec![0; 2048]; + words[..4].copy_from_slice(&2074u32.to_le_bytes()); + words[4..8].copy_from_slice(&1u32.to_le_bytes()); + file.extend_from_slice(&words); + let extension = file.len() as u32; + let mut area = vec![0; 495]; + area[..2].copy_from_slice(&495u16.to_le_bytes()); + area[482..486].copy_from_slice(&table.to_le_bytes()); + file.extend_from_slice(&area); + file.extend_from_slice(&extension.to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + let decoded = dec(&file); + assert!(decoded.is_err(), "the second pixel is missing"); +} + +#[test] +fn short_extension_followed_by_a_developer_directory_keeps_the_pixels() { + // a complete 1x1 image, a 482-byte extension (its size says so) and right after it a + // developer directory whose entry's length lands where a full extension keeps its + // scan-line table offset; reading it as one would move the image end into the header + let mut file = header(2, 1, 1, 24, 0x20); + file.extend_from_slice(&[10, 20, 30]); + let extension = file.len() as u32; + let mut area = vec![0; 482]; + area[..2].copy_from_slice(&482u16.to_le_bytes()); + file.extend_from_slice(&area); + let directory = file.len() as u32; + let payload = directory + 2 + 10; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&0u16.to_le_bytes()); + file.extend_from_slice(&payload.to_le_bytes()); + file.extend_from_slice(&18u32.to_le_bytes()); + file.extend_from_slice(&[0; 18]); + file.extend_from_slice(&extension.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + let source = dec(&file).expect("the image decodes"); + assert_eq!((source.width, source.height), (1, 1)); +} + +#[test] +fn importing_truncated_pixels_does_not_consume_tga_footer() { + for (kind, data) in [(2, vec![0, 0, 255]), (10, vec![0x80, 0, 0, 255])] { + for metadata in [None, Some(0usize), Some(1)] { + let mut file = header(kind, 2, 1, 24, 0x20); + file.extend_from_slice(&data); // only the first of two pixels + let mut offsets = [0u8; 8]; + if let Some(field) = metadata { + let offset = file.len() as u32; + offsets[field * 4..field * 4 + 4].copy_from_slice(&offset.to_le_bytes()); + let mut area = vec![0; 495]; + if field == 0 { + area[..2].copy_from_slice(&495u16.to_le_bytes()); + } + file.extend_from_slice(&area); // optional metadata is not pixel data + } + file.extend_from_slice(&offsets); + file.extend_from_slice(SIGNATURE); + let result = dec(&file); + assert!(matches!(result, Err(_)), "kind {kind}, metadata {metadata:?}: metadata is not a second pixel"); + } + } + for kind in [1, 9] { + let mut file = header(kind, 1, 1, 8, 0x20); + file[1] = 1; + file[5] = 1; + file[7] = 24; + file.extend_from_slice(&[0, 0]); // missing third palette byte and the index + file.extend_from_slice(&[0; 8]); + file.extend_from_slice(SIGNATURE); + let result = dec(&file); + assert!(matches!(result, Err(_)), "kind {kind}: the footer is not colour-map data"); + } +} + +#[test] +fn complete_tga_footer_and_metadata_do_not_change_pixels() { + for metadata in [None, Some(0usize), Some(1)] { + let mut file = header(2, 1, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255]); + let mut offsets = [0u8; 8]; + if let Some(field) = metadata { + let offset = file.len() as u32; + offsets[field * 4..field * 4 + 4].copy_from_slice(&offset.to_le_bytes()); + let mut area = vec![0; 495]; + if field == 0 { + area[..2].copy_from_slice(&495u16.to_le_bytes()); + } + file.extend_from_slice(&area); + } + file.extend_from_slice(&offsets); + file.extend_from_slice(SIGNATURE); + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255]); + } +} + +fn preceding_metadata(complete: bool, metadata: usize) -> Vec { + let mut file = header(2, if complete { 1 } else { 2 }, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255]); + let payload_offset = file.len() as u32; + let mut offsets = [0u8; 8]; + if metadata == 0 { + file.extend_from_slice(&[0, 255, 0]); + let directory_offset = file.len() as u32; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&payload_offset.to_le_bytes()); + file.extend_from_slice(&3u32.to_le_bytes()); + offsets[4..].copy_from_slice(&directory_offset.to_le_bytes()); + } else { + match metadata { + 1 => file.extend_from_slice(&[0; 2048]), + 2 => file.extend_from_slice(&[1, 1, 0, 255, 0]), + 3 => file.extend_from_slice(&18u32.to_le_bytes()), + _ => unreachable!(), + } + let extension_offset = file.len() as u32; + let mut extension = [0; 495]; + extension[..2].copy_from_slice(&495u16.to_le_bytes()); + let field = [0, 482, 486, 490][metadata]; + extension[field..field + 4].copy_from_slice(&payload_offset.to_le_bytes()); + file.extend_from_slice(&extension); + offsets[..4].copy_from_slice(&extension_offset.to_le_bytes()); + } + file.extend_from_slice(&offsets); + file.extend_from_slice(SIGNATURE); + file +} + +#[test] +fn preceding_metadata_cannot_supply_missing_pixels() { + let mut accepted = Vec::new(); + for metadata in 0..4 { + let result = dec(&preceding_metadata(false, metadata)); + if !matches!(result, Err(_)) { + accepted.push(metadata); + } + } + assert!(accepted.is_empty(), "metadata payloads supplied missing pixels: {accepted:?}"); +} + +#[test] +fn complete_pixels_before_metadata_keep_their_colour() { + for metadata in 0..4 { + let source = dec(&preceding_metadata(true, metadata)).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255], "metadata {metadata}"); + } + let mut file = preceding_metadata(true, 1); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + file[extension..extension + 2].copy_from_slice(&496u16.to_le_bytes()); + file.insert(footer, 0); + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255], "larger complete extensions preserve pixels"); +} + +#[test] +fn malformed_optional_metadata_keeps_complete_pixels_readable() { + let accept = |file: Vec| { + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255]); + }; + for field in [0, 4] { + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + file[footer + field..footer + field + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + accept(file); + } + for metadata in 1..4 { + let mut file = preceding_metadata(true, metadata); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + let field = [0, 482, 486, 490][metadata]; + file[extension + field..extension + field + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + accept(file); + } + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file.drain(directory + 2..footer); + accept(file); + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory + 8..directory + 12].copy_from_slice(&u32::MAX.to_le_bytes()); + accept(file); + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); + accept(file); + // A zero-length entry cannot mark a payload boundary, even if its offset overlaps pixels. + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory + 4..directory + 8].copy_from_slice(&19u32.to_le_bytes()); + file[directory + 8..directory + 12].copy_from_slice(&0u32.to_le_bytes()); + accept(file); + let mut file = preceding_metadata(true, 2); + file[21..23].copy_from_slice(&[255, 255]); + accept(file); + let mut file = preceding_metadata(true, 2); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + file[extension..extension + 2].copy_from_slice(&494u16.to_le_bytes()); + accept(file); + let mut file = preceding_metadata(true, 2); + let footer = file.len() - SIGNATURE.len() - 8; + file.remove(footer - 1); // extension is physically short, but its postage offset remains + accept(file); +} + +#[test] +fn malformed_metadata_offsets_inside_id_or_palette_keep_complete_pixels() { + let mut file = header(1, 1, 1, 8, 0x20); + file[0] = 4; // image ID length + file[1] = 1; // colour map present + file[5..7].copy_from_slice(&1u16.to_le_bytes()); + file[7] = 24; // one BGR palette entry + file.extend_from_slice(&[7, 8, 9, 10]); // image ID + file.extend_from_slice(&[0, 0, 255]); // red palette entry + file.push(0); // palette index + for (field, offset) in [(0, HEADER + 1), (4, HEADER + 4 + 1)] { + let mut candidate = file.clone(); + let mut footer_offsets = [0u8; 8]; + footer_offsets[field..field + 4].copy_from_slice(&(offset as u32).to_le_bytes()); + candidate.extend_from_slice(&footer_offsets); + candidate.extend_from_slice(SIGNATURE); + let image = dec(&candidate).unwrap(); + assert_eq!(image.rgba, [255, 0, 0, 255], "footer field {field}"); + } +} + +#[test] +fn malformed_optional_metadata_cannot_supply_missing_pixels() { + let mut file = preceding_metadata(false, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory + 8..directory + 12].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(matches!(dec(&file), Err(_))); + + // The declared entry count exceeds the bytes before the footer, but the first entry's + // payload still precedes the directory and cannot be used as a second pixel. + let mut file = preceding_metadata(false, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); + assert!(matches!(dec(&file), Err(_))); + + let mut file = preceding_metadata(false, 2); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + file[extension..extension + 2].copy_from_slice(&494u16.to_le_bytes()); + assert!(matches!(dec(&file), Err(_))); + let mut file = preceding_metadata(false, 2); + let footer = file.len() - SIGNATURE.len() - 8; + file.remove(footer - 1); + assert!(matches!(dec(&file), Err(_))); +} + +#[test] +fn valid_developer_directory_with_overlapping_payload_rejects_pixels() { + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); + let mut extra = [0u8; 10]; + extra[..2].copy_from_slice(&2u16.to_le_bytes()); + extra[2..6].copy_from_slice(&(HEADER as u32).to_le_bytes()); + extra[6..10].copy_from_slice(&1u32.to_le_bytes()); + file.splice(footer..footer, extra); + let moved_footer = file.len() - SIGNATURE.len() - 8; + assert_eq!(u32::from_le_bytes(file[moved_footer + 4..moved_footer + 8].try_into().unwrap()), directory as u32); + assert_eq!(u16::from_le_bytes(file[directory..directory + 2].try_into().unwrap()), 2); + assert_eq!(&file[directory + 12..directory + 22], &extra); + assert!(matches!(dec(&file), Err(_))); +} + +#[test] +fn nonempty_developer_reference_inside_pixels_rejects_the_whole_image() { + let mut file = header(2, 2, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255, 0, 255, 0]); + let directory = file.len() as u32; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&(HEADER as u32 + 3).to_le_bytes()); + file.extend_from_slice(&1u32.to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + assert!(matches!(dec(&file), Err(_)), "an overlapping reference must not return a partial image"); +} + +#[test] +fn inflated_developer_count_scans_the_next_chunk_as_an_entry_and_rejects_its_pixel_payload() { + // a directory claiming two entries over one real entry: the ten bytes after it, scanned as + // the second entry, name a payload inside the pixels, which is refused like any other + let mut file = header(2, 2, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255, 0, 255, 0]); + let directory = file.len() as u32; + file.extend_from_slice(&2u16.to_le_bytes()); + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&(directory + 22).to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(&2u16.to_le_bytes()); + file.extend_from_slice(&(HEADER as u32 + 3).to_le_bytes()); + file.extend_from_slice(&1u32.to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + assert!(matches!(dec(&file), Err(_)), "a scanned entry's payload inside the pixels is refused"); +} + +#[test] +fn legacy_pixels_that_end_in_signature_at_or_after_the_header_are_read_as_a_footer() { + // a footerless 36×1 grayscale image whose last 26 bytes happen to be eight zeros and the + // signature: the putative trailer begins at byte 28, so it is taken for a footer and the + // image, left with 10 pixel bytes, is rejected as truncated (the ambiguity the body states) + let prefix = 18usize; + let width = (prefix + SIGNATURE.len()) as u16; + let mut file = header(3, width, 1, 8, 0x20); + file.extend(vec![0; prefix]); + file.extend_from_slice(SIGNATURE); + assert!(matches!(dec(&file), Err(_))); +} + +#[test] +fn mutated_tga_footer_files_never_panic() { + let mut file = header(2, 2, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255, 0, 255, 0]); + file.extend_from_slice(&[0; 8]); + file.extend_from_slice(SIGNATURE); + for end in 0..file.len() { + assert!(std::panic::catch_unwind(|| dec(&file[..end])).is_ok(), "truncation at {end}"); + } + for at in 0..file.len() { + let mut mutated = file.clone(); + mutated[at] ^= 0xFF; + assert!(std::panic::catch_unwind(|| dec(&mutated)).is_ok(), "mutation at {at}"); + } + for metadata in 0..4 { + let file = preceding_metadata(true, metadata); + assert!(dec(&file).is_ok()); + let footer = file.len() - SIGNATURE.len() - 8; + for end in 0..footer { + let mut truncated = file[..end].to_vec(); + truncated.extend_from_slice(&file[footer..]); + assert!(std::panic::catch_unwind(|| dec(&truncated)).is_ok(), "metadata {metadata}, footer-preserving truncation at {end}"); + } + for at in 0..file.len() { + let mut mutated = file.clone(); + mutated[at] ^= 0xFF; + assert!(std::panic::catch_unwind(|| dec(&mutated)).is_ok(), "metadata {metadata}, mutation at {at}"); + } + } +} From b6b33b6096a247514f5cbd1741633913e7261e91 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:40:41 -0500 Subject: [PATCH 019/471] tga: record original metadata-boundary source attribution --- ATTRIBUTION.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index c087ecda1..e6f928645 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -99,3 +99,5 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso ## Binary Apple property lists `crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API. + +`crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained. From e555cc0afe12d14a3fed4e369e78d45cf084109a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:48:49 -0500 Subject: [PATCH 020/471] fix(tga): use eager static footer errors --- crates/tga/src/lib.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index 88f4efe6c..06a9941a0 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -216,14 +216,11 @@ fn image_data_end(bytes: &[u8], first_pixel: usize) -> Result { let Some(footer) = bytes.len().checked_sub(SIGNATURE.len() + 8).filter(|&at| at >= HEADER) else { return Ok(bytes.len()); }; - let offsets = bytes.get(footer..footer + 8).and_then(|b| b.first_chunk::<8>()).ok_or_else(|| Error::Malformed("truncated footer"))?; - let extension = offsets.first_chunk::<4>().map(|v| u32::from_le_bytes(*v) as usize).ok_or_else(|| Error::Malformed("truncated footer"))?; - let directory = offsets - .get(4..) - .and_then(|v| v.first_chunk::<4>()) - .map(|v| u32::from_le_bytes(*v) as usize) - .ok_or_else(|| Error::Malformed("truncated footer"))?; - let body = bytes.get(..footer).ok_or_else(|| Error::Malformed("truncated footer"))?; + let offsets = bytes.get(footer..footer + 8).and_then(|b| b.first_chunk::<8>()).ok_or(Error::Malformed("truncated footer"))?; + let extension = offsets.first_chunk::<4>().map(|v| u32::from_le_bytes(*v) as usize).ok_or(Error::Malformed("truncated footer"))?; + let directory = + offsets.get(4..).and_then(|v| v.first_chunk::<4>()).map(|v| u32::from_le_bytes(*v) as usize).ok_or(Error::Malformed("truncated footer"))?; + let body = bytes.get(..footer).ok_or(Error::Malformed("truncated footer"))?; let mut end = footer; if let Some(offset) = metadata_offset(body, extension, first_pixel) { From 9399c25341289c1d523e64c92a1b95735c81933f Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:10:22 +0100 Subject: [PATCH 021/471] image-codecs: decode AVIF pixels with craft-video's pure-Rust AV1 decoder New optional `avif-decode` feature (native targets) reads still AVIF files: the HEIF/ISOBMFF item graph, grids, clap/irot/imir, alpha auxiliary images (including premultiplied), 8/10/12-bit 4:0:0, 4:2:0, 4:2:2 and 4:4:4, nclx/ICC colour, Exif and XMP, and PQ/HLG to linear F32 through cicp::linearize as the HEIF path does. The container walk follows LightCraft #3017 by @emmertex. Every size is checked against the caller's Limits before the AV1 decoder runs, the decoder's memory budget is capped by max_alloc, and the decode is wrapped in catch_unwind so a decoder panic becomes an error. Fixtures are tiny ffmpeg/libaom files; the 4:2:0 planes match libdav1d bit for bit. Adds a decode_avif fuzz target. Part of storytold/craft-libs#778. --- crates/image-codecs/Cargo.toml | 4 + crates/image-codecs/README.md | 27 +- crates/image-codecs/fuzz/Cargo.toml | 9 +- .../fuzz/fuzz_targets/decode_avif.rs | 14 + crates/image-codecs/src/cicp.rs | 2 +- crates/image-codecs/src/codecs.rs | 2 + crates/image-codecs/src/codecs/avif_decode.rs | 1239 +++++++++++++++++ crates/image-codecs/src/format.rs | 13 +- crates/image-codecs/src/lib.rs | 24 +- .../tests/fixtures/avif/README.md | 12 + .../tests/fixtures/avif/quad-420-bt709.avif | Bin 0 -> 369 bytes .../fixtures/avif/quad-420-bt709.dav1d.yuv | 1 + .../fixtures/avif/quad-444-gbr-lossless.avif | Bin 0 -> 345 bytes .../tests/fixtures/avif/quad-source.rgb | 1 + crates/image-codecs/tests/it/detect_caps.rs | 11 +- 15 files changed, 1335 insertions(+), 24 deletions(-) create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs create mode 100644 crates/image-codecs/src/codecs/avif_decode.rs create mode 100644 crates/image-codecs/tests/fixtures/avif/README.md create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-420-bt709.avif create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-420-bt709.dav1d.yuv create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-444-gbr-lossless.avif create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-source.rgb diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index df71d9860..1b422f912 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -15,6 +15,9 @@ corpus = [] default = [] avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"] heif = ["dep:craft-heif"] +# AVIF decode: craft-video's clean-room pure-Rust AV1 decoder. Native targets only (on wasm32 a +# decoder panic aborts instead of becoming an error), so wasm32 builds leave AVIF unreadable. +avif-decode = ["dep:craft-video", "craft-video/av1-decode"] [dependencies] crc32fast = "1.5" @@ -41,6 +44,7 @@ hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["st [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rayon = { workspace = true } +craft-video = { workspace = true, optional = true } exr = { version = "1.73", default-features = false, features = ["rayon"] } [dev-dependencies] diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 6e9e11617..1728d8308 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -1,9 +1,10 @@ # craft-image-codecs Codecs for flat raster image formats, written in pure Rust. This crate defines its own `Image` -type and shares its baseline and extended JPEG decoders through `craft-jpeg`, and its optional -HEIF decoder through `craft-heif`. It builds for `wasm32-unknown-unknown` and does no filesystem -I/O: decoders take `&[u8]` and encoders return `Vec`. +type and shares its baseline and extended JPEG decoders through `craft-jpeg`, its optional HEIF +decoder through `craft-heif`, and its optional AVIF decoder through `craft-video`'s AV1 decoder. +It builds for `wasm32-unknown-unknown` and does no filesystem I/O: decoders take `&[u8]` and +encoders return `Vec`. It follows the "Avoiding GIMP's hole" rules in `docs/architecture.md` §1.1: @@ -56,7 +57,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | -| AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | +| AVIF | with feature `avif-decode` (native only) | only with feature `avif` | U8, U16 (10/12-bit decodes to U16; PQ/HLG to linear F32) | RGB, RGBA | yes (auxiliary alpha) | read (`colr` prof/rICC) | read | read | no | no | yes | `craft-video` AV1 decoder; `image` + `ravif` to write | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | "Native" means the data is stored and read back without conversion. Anything else is converted by @@ -177,10 +178,20 @@ embedded table (and a PNG's transparent entry) so `photocraft-io` can open them decodes to RGB. Image sequences, overlays and identity derivations, multilayer HEVC and some 4:2:2/4:4:4 streams return `CodecError::Unsupported` or `Malformed`, never wrong pixels. Writing needs an HEVC encoder and every mature one is C, so HEIF is listed in `ASYMMETRIC_EXCEPTIONS`. -* **AVIF.** Encoding uses `ravif`, which is pure Rust. Decoding - needs `dav1d`, which is C. AVIF is therefore read-unsupported, and write support is gated - behind the non-default `avif` feature. In a default build it is neither readable nor writable, - so the symmetric guarantee holds. It is listed in `ASYMMETRIC_EXCEPTIONS`. +* **AVIF.** Encoding uses `ravif`, which is pure Rust, behind the non-default `avif` feature. + Decoding (`codecs::avif_decode`, behind the non-default `avif-decode` feature) reads the HEIF + boxes itself and decodes the AV1 pictures with `craft-video`'s clean-room pure-Rust AV1 decoder: + one `av01` image or a `grid`, 8/10/12-bit, 4:2:0/4:2:2/4:4:4/monochrome, the `nclx` colour (or + the sequence header's) and an ICC profile, an `auxl` alpha plane (`prem` too), the Exif and XMP + items, and `clap`/`irot`/`imir`, which are applied unless `keep_orientation` is set, as for HEIF. + PQ and HLG open as linear light in F32, as HEIF does. Image sequences (animated AVIF), depth and + gain-map auxiliary images and layered progressive images aren't read. It is bounded like the + other decoders: the `Limits` are checked against the declared size before any picture decodes, + against every picture and every output buffer, `max_alloc` caps the AV1 decoder's picture store, + box, item and extent counts are capped, item data can't exceed the file, and a decoder panic is + `CodecError::Malformed`. wasm32 builds leave AVIF unreadable, since a panic there aborts. In a + default build AVIF is neither readable nor writable, so the symmetric guarantee holds; a build + with only one of the two features is listed in `ASYMMETRIC_EXCEPTIONS`. * **Lossy WebP.** Lossless (VP8L, via `image-webp`) is the default. `webp_lossless: false` writes a lossy file with our own clean-room VP8 key-frame encoder (`codecs::vp8`, from RFC 6386; `webp_quality` 0–100 on libwebp's scale sets the quantizer). Alpha travels losslessly diff --git a/crates/image-codecs/fuzz/Cargo.toml b/crates/image-codecs/fuzz/Cargo.toml index 3257a3953..30226c76e 100644 --- a/crates/image-codecs/fuzz/Cargo.toml +++ b/crates/image-codecs/fuzz/Cargo.toml @@ -14,7 +14,7 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" -craft-image-codecs = { path = "..", features = ["heif"] } +craft-image-codecs = { path = "..", features = ["heif", "avif-decode"] } # Called directly too: the codec turns its panics into errors (see decode_heif.rs). heic-rs = "=0.1.4" @@ -76,3 +76,10 @@ path = "fuzz_targets/decode_webp.rs" test = false doc = false bench = false + +[[bin]] +name = "decode_avif" +path = "fuzz_targets/decode_avif.rs" +test = false +doc = false +bench = false diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs new file mode 100644 index 000000000..9a4b8978d --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, probe_size, DecodeOptions, Format, Limits}; + +// The AVIF box reader and craft-video's AV1 decoder parse untrusted files: run with `-timeout=10` +// so a hang is reported, not just a panic. The codec turns an AV1 decoder panic into an error, so +// only the box reader's own panics would reach the fuzzer. Both orientation paths are covered. +fuzz_target!(|data: &[u8]| { + let keep_orientation = data.first().is_some_and(|b| b & 1 == 1); + let limits = Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }; + let _ = probe_size(data); + let _ = decode_as_with(Format::Avif, data, &DecodeOptions { limits, keep_orientation }); +}); diff --git a/crates/image-codecs/src/cicp.rs b/crates/image-codecs/src/cicp.rs index 290c82bc3..e6911d1b0 100644 --- a/crates/image-codecs/src/cicp.rs +++ b/crates/image-codecs/src/cicp.rs @@ -99,7 +99,7 @@ pub fn linear_to_pq(linear: f32) -> f32 { /// Relative luminance weights of the RGB primaries `code` (BT.2020's for anything else, the /// space BT.2100 defines HLG in). -fn luma_weights(code: u16) -> [f32; 3] { +pub(crate) fn luma_weights(code: u16) -> [f32; 3] { match code { 1 => [0.2126, 0.7152, 0.0722], 12 => [0.2290, 0.6917, 0.0793], diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 0288ae22b..71db85f18 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -1,4 +1,6 @@ pub(crate) mod avif; +#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] +pub(crate) mod avif_decode; pub(crate) mod dds; pub(crate) mod dds_bc; pub(crate) mod deep_exr; diff --git a/crates/image-codecs/src/codecs/avif_decode.rs b/crates/image-codecs/src/codecs/avif_decode.rs new file mode 100644 index 000000000..302433661 --- /dev/null +++ b/crates/image-codecs/src/codecs/avif_decode.rs @@ -0,0 +1,1239 @@ +//! AVIF (AV1 Image File Format) decoding, behind the non-default `avif-decode` feature, native +//! targets only. +//! +//! The container is HEIF (ISO/IEC 23008-12) over ISO-BMFF. This module reads the boxes itself +//! (`meta`, `pitm`, `iloc`, `iinf`, `iref`, `iprp`/`ipco`/`ipma`, `idat`), and `craft-video`'s +//! clean-room AV1 decoder (`av1-decode`, pure Rust, no `unsafe`) decodes the AV1 still pictures. +//! The container reader, colour conversion and transforms are adapted from @emmertex's LightCraft +//! AVIF importer (storytold/lightcraft#3017). +//! +//! Supported: the primary item as one `av01` image or a `grid` of them; 8-, 10- and 12-bit; +//! 4:2:0, 4:2:2, 4:4:4 and monochrome; full or limited range; the `nclx` colour description (or the +//! AV1 sequence header's) and an ICC profile (`prof` / `rICC`); an alpha auxiliary image (`auxl`, +//! premultiplied with `prem` too); the primary item's Exif and XMP items. `clap`, `irot` and `imir` +//! are applied unless the caller keeps the stored orientation, as for HEIF. PQ and HLG files +//! open as linear light in 32-bit float, as HEIF does. Not read: image sequences (animated AVIF), +//! depth and gain-map auxiliary images, layered (`lsel`/`a1lx`) progressive images. +//! +//! Bounded for hostile input: box nesting, item, property and extent counts are capped; item data +//! can't add up to more than the file; the [`Limits`] are checked against the declared size +//! before any picture is decoded, against every decoded picture and against each output buffer; +//! the AV1 decoder's picture store is capped by `max_alloc` too; and a panic in the decoder is +//! a malformed-file error. wasm32 builds leave AVIF unreadable: a panic there aborts instead of +//! unwinding. +//! +//! Sources: ISO/IEC 23008-12 (HEIF) and the AV1 Image File Format specification v1.1.0 (box +//! layouts), ITU-T H.273 (colour code points). + +use std::panic::{AssertUnwindSafe, catch_unwind}; + +use craft_video::av1::decode as av1; + +use crate::Format; +use crate::cicp::Cicp; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, Metadata, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Avif; +/// Deepest box nesting followed (meta → iprp → ipco → property is 4). +const MAX_DEPTH: u32 = 8; +/// Most boxes, items, properties, associations or extents accepted: far above real files. +const MAX_ITEMS: usize = 1 << 16; +/// The AV1 decoder's own picture-store cap (`craft-video`'s default on 64-bit targets). +const AV1_MEMORY_BUDGET: u64 = 4 << 30; +/// The `auxC` types of an alpha plane (AVIF and the older HEVC-in-HEIF spelling). +const ALPHA_URNS: [&[u8]; 2] = [b"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha", b"urn:mpeg:hevc:2015:auxid:1"]; + +type Result = std::result::Result; + +fn bad(m: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, m) +} + +/// A big-endian reader over a byte slice that never panics. +struct Rd<'a> { + b: &'a [u8], + p: usize, +} + +impl<'a> Rd<'a> { + fn new(b: &'a [u8]) -> Self { + Rd { b, p: 0 } + } + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.p.checked_add(n).ok_or_else(|| bad("box overflows"))?; + let s = self.b.get(self.p..end).ok_or_else(|| bad("box truncated"))?; + self.p = end; + Ok(s) + } + fn u8(&mut self) -> Result { + Ok(self.take(1)?.first().copied().unwrap_or(0)) + } + fn u16(&mut self) -> Result { + Ok(self.uint(2)? as u16) + } + fn u32(&mut self) -> Result { + Ok(self.uint(4)? as u32) + } + /// An unsigned big-endian number of `n` (0 to 8) bytes. + fn uint(&mut self, n: usize) -> Result { + if n > 8 { + return Err(bad("field too wide")); + } + Ok(self.take(n)?.iter().fold(0u64, |a, &x| (a << 8) | u64::from(x))) + } + /// A full box's version, its flags skipped. + fn version(&mut self) -> Result { + let v = self.u8()?; + self.take(3)?; + Ok(v) + } + /// An item ID: 16 bits in version 0 boxes, 32 bits otherwise. + fn id(&mut self, wide: bool) -> Result { + if wide { self.u32() } else { self.u16().map(u32::from) } + } + fn cstr(&mut self) -> Result<&'a [u8]> { + let rest = self.rest(); + let n = rest.iter().position(|&c| c == 0).unwrap_or(rest.len()); + let s = self.take(n)?; + if self.p < self.b.len() { + self.p += 1; + } + Ok(s) + } + fn rest(&self) -> &'a [u8] { + self.b.get(self.p..).unwrap_or(&[]) + } +} + +/// The child boxes of `b`: (type, payload). +fn boxes(b: &[u8]) -> Result> { + let mut out = Vec::new(); + let mut p = 0usize; + while p < b.len() { + let mut r = Rd::new(b.get(p..).unwrap_or(&[])); + if r.b.len() < 8 { + break; // trailing padding + } + let size32 = r.u32()?; + let t = r.take(4)?; + let ty = [t[0], t[1], t[2], t[3]]; + let (hdr, size) = match size32 { + 0 => (8usize, r.b.len() as u64), + 1 => (16, r.uint(8)?), + s => (8, u64::from(s)), + }; + let size = usize::try_from(size).map_err(|_| bad("box too large"))?; + if size < hdr || size > r.b.len() { + return Err(bad(format!("box {} has a bad size", String::from_utf8_lossy(&ty)))); + } + out.push((ty, r.b.get(hdr..size).unwrap_or(&[]))); + if out.len() > MAX_ITEMS { + return Err(bad("too many boxes")); + } + p = p.checked_add(size).ok_or_else(|| bad("box overflows"))?; + } + Ok(out) +} + +#[derive(Clone, Debug, Default)] +struct Extent { + offset: u64, + length: u64, +} + +#[derive(Clone, Debug, Default)] +struct Loc { + method: u8, + base: u64, + extents: Vec, +} + +#[derive(Clone, Copy, Debug)] +enum Transform { + /// Clean aperture: width, height, horizontal and vertical offset of the centre. + Clap { w: f64, h: f64, dx: f64, dy: f64 }, + /// Anticlockwise rotation by `n` × 90°. + Rot(u8), + /// Mirror, as HEIF amendment 2, libheif and libavif read it: 0 = top and bottom exchanged, + /// 1 = left and right exchanged. + Mir(u8), +} + +#[derive(Clone, Debug)] +enum Prop { + Av1c(Vec), + Nclx(Cicp), + Icc(Vec), + Ispe(u32, u32), + AuxC(Vec), + T(Transform), + Other, +} + +struct Item { + id: u32, + kind: [u8; 4], + /// `mime` items: the content type. + content_type: Option>, +} + +#[derive(Default)] +struct Container<'a> { + file: &'a [u8], + primary: u32, + items: Vec, + locs: Vec<(u32, Loc)>, + idat: &'a [u8], + props: Vec, + /// item → 1-based property indices, in order. + assoc: Vec<(u32, Vec)>, + /// (type, from, to…) + refs: Vec<([u8; 4], u32, Vec)>, + /// `iloc` extents read so far, across all items (capped at [`MAX_ITEMS`]). + extents: usize, +} + +fn parse(file: &[u8]) -> Result> { + let top = boxes(file)?; + let meta = top.iter().find(|(t, _)| t == b"meta").map(|(_, b)| *b).ok_or_else(|| { + if top.iter().any(|(t, _)| t == b"moov") { + CodecError::unsupported(F, "AVIF image sequences (animated AVIF) aren't read") + } else { + bad("no meta box") + } + })?; + let mut c = Container { file, ..Default::default() }; + let meta = meta.get(4..).ok_or_else(|| bad("meta box truncated"))?; + for (t, b) in boxes(meta)? { + let mut r = Rd::new(b); + match &t { + b"pitm" => { + let v = r.version()?; + c.primary = r.id(v != 0)?; + } + b"iloc" => parse_iloc(&mut r, &mut c)?, + b"iinf" => parse_iinf(&mut r, &mut c)?, + b"iref" => parse_iref(&mut r, &mut c)?, + b"idat" => c.idat = b, + b"iprp" => parse_iprp(b, &mut c, 0)?, + _ => {} + } + } + if c.primary == 0 { + return Err(bad("no primary item")); + } + Ok(c) +} + +fn parse_iinf(r: &mut Rd, c: &mut Container) -> Result<()> { + let v = r.version()?; + let _count = r.id(v != 0)?; + for (t, b) in boxes(r.rest())? { + if &t != b"infe" { + continue; + } + let mut r = Rd::new(b); + let v = r.version()?; + if v < 2 { + continue; + } + let id = r.id(v != 2)?; + r.u16()?; // protection index + let kind = r.take(4)?; + let kind = [kind[0], kind[1], kind[2], kind[3]]; + let _name = r.cstr()?; + let content_type = (&kind == b"mime").then(|| r.cstr().ok().map(<[u8]>::to_vec)).flatten(); + c.items.push(Item { id, kind, content_type }); + if c.items.len() > MAX_ITEMS { + return Err(bad("too many items")); + } + } + Ok(()) +} + +fn parse_iref(r: &mut Rd, c: &mut Container) -> Result<()> { + let v = r.version()?; + for (t, b) in boxes(r.rest())? { + let mut r = Rd::new(b); + let from = r.id(v != 0)?; + let n = r.u16()?; + let mut to = Vec::with_capacity(usize::from(n).min(r.rest().len() / 2 + 1)); + for _ in 0..n { + to.push(r.id(v != 0)?); + } + c.refs.push((t, from, to)); + if c.refs.len() > MAX_ITEMS { + return Err(bad("too many item references")); + } + } + Ok(()) +} + +fn parse_iloc(r: &mut Rd, c: &mut Container) -> Result<()> { + let v = r.version()?; + if v > 2 { + return Err(CodecError::unsupported(F, "iloc version")); + } + let a = r.u8()?; + let b = r.u8()?; + let (off_size, len_size, base_size) = (usize::from(a >> 4), usize::from(a & 15), usize::from(b >> 4)); + let index_size = if v >= 1 { usize::from(b & 15) } else { 0 }; + let count = if v < 2 { u32::from(r.u16()?) } else { r.u32()? }; + for _ in 0..count { + let id = r.id(v >= 2)?; + let method = if v >= 1 { (r.u16()? & 15) as u8 } else { 0 }; + r.u16()?; // data reference index + let base = r.uint(base_size)?; + let n = r.u16()?; + let mut extents = Vec::with_capacity(usize::from(n).min(64)); + for _ in 0..n { + r.uint(index_size)?; + let offset = r.uint(off_size)?; + let length = r.uint(len_size)?; + extents.push(Extent { offset, length }); + // Zero-width offset and length fields read no bytes, so the file size alone doesn't + // bound this. + c.extents += 1; + if c.extents > MAX_ITEMS { + return Err(bad("too many item extents")); + } + } + c.locs.push((id, Loc { method, base, extents })); + if c.locs.len() > MAX_ITEMS { + return Err(bad("too many items")); + } + } + Ok(()) +} + +fn parse_iprp(b: &[u8], c: &mut Container, depth: u32) -> Result<()> { + if depth > MAX_DEPTH { + return Err(bad("boxes nested too deep")); + } + for (t, b) in boxes(b)? { + match &t { + b"ipco" => { + for (t, b) in boxes(b)? { + c.props.push(parse_prop(&t, b)?); + if c.props.len() > MAX_ITEMS { + return Err(bad("too many properties")); + } + } + } + b"ipma" => parse_ipma(b, c)?, + _ => {} + } + } + Ok(()) +} + +fn parse_ipma(b: &[u8], c: &mut Container) -> Result<()> { + let mut r = Rd::new(b); + let v = r.u8()?; + let flags = r.take(3)?; + let wide = flags.get(2).is_some_and(|f| f & 1 != 0); + let n = r.u32()?; + for _ in 0..n { + let id = r.id(v != 0)?; + let k = r.u8()?; + let mut list = Vec::with_capacity(usize::from(k)); + for _ in 0..k { + let idx = if wide { usize::from(r.u16()? & 0x7fff) } else { usize::from(r.u8()? & 0x7f) }; + list.push(idx); + } + c.assoc.push((id, list)); + if c.assoc.len() > MAX_ITEMS { + return Err(bad("too many property associations")); + } + } + Ok(()) +} + +fn parse_prop(t: &[u8; 4], b: &[u8]) -> Result { + let mut r = Rd::new(b); + Ok(match t { + b"av1C" => Prop::Av1c(b.get(4..).unwrap_or(&[]).to_vec()), + b"colr" => match r.take(4)? { + b"nclx" => Prop::Nclx(Cicp { primaries: r.u16()?, transfer: r.u16()?, matrix: r.u16()?, full_range: r.u8()? & 0x80 != 0 }), + b"prof" | b"rICC" => Prop::Icc(r.rest().to_vec()), + _ => Prop::Other, + }, + b"ispe" => { + r.version()?; + Prop::Ispe(r.u32()?, r.u32()?) + } + b"auxC" => { + r.version()?; + Prop::AuxC(r.cstr()?.to_vec()) + } + b"irot" => Prop::T(Transform::Rot(r.u8()? & 3)), + b"imir" => Prop::T(Transform::Mir(r.u8()? & 1)), + b"clap" => { + let mut q = || -> Result { + let n = r.u32()? as i32; + let d = r.u32()? as i32; + if d == 0 { + return Err(bad("clap with a zero denominator")); + } + Ok(f64::from(n) / f64::from(d)) + }; + let (w, h, dx, dy) = (q()?, q()?, q()?, q()?); + Prop::T(Transform::Clap { w, h, dx, dy }) + } + _ => Prop::Other, + }) +} + +impl Container<'_> { + fn item_type(&self, id: u32) -> Option<[u8; 4]> { + self.items.iter().find(|t| t.id == id).map(|t| t.kind) + } + fn props_of(&self, id: u32) -> impl Iterator { + self.assoc.iter().filter(move |a| a.0 == id).flat_map(|a| a.1.iter()).filter_map(|&i| i.checked_sub(1).and_then(|i| self.props.get(i))) + } + fn data(&self, id: u32) -> Result> { + let loc = self.locs.iter().find(|l| l.0 == id).map(|l| &l.1).ok_or_else(|| bad(format!("item {id} has no location")))?; + let src = match loc.method { + 0 => self.file, + 1 => self.idat, + _ => return Err(CodecError::unsupported(F, "item data by reference to another item")), + }; + let mut out = Vec::new(); + for e in &loc.extents { + let start = loc.base.checked_add(e.offset).ok_or_else(|| bad("item offset overflows"))?; + let start = usize::try_from(start).map_err(|_| bad("item offset out of range"))?; + let end = if e.length == 0 { + src.len() + } else { + usize::try_from(e.length).ok().and_then(|l| start.checked_add(l)).ok_or_else(|| bad("item length overflows"))? + }; + let chunk = src.get(start..end).ok_or_else(|| bad("item data runs past the end of the file"))?; + // Every byte comes from the file, so a real item is never larger than it; repeated or + // overlapping extents that add up to more are refused before they allocate. + if out.len().saturating_add(chunk.len()) > self.file.len() { + return Err(bad("item data is larger than the file")); + } + out.extend_from_slice(chunk); + } + Ok(out) + } + fn refs_from(&self, kind: &[u8; 4], from: u32) -> Vec { + self.refs.iter().filter(|r| &r.0 == kind && r.1 == from).flat_map(|r| r.2.iter().copied()).collect() + } + /// Items with a `kind` reference to `to`. + fn refs_to(&self, kind: &[u8; 4], to: u32) -> impl Iterator { + self.refs.iter().filter(move |r| &r.0 == kind && r.2.contains(&to)).map(|r| r.1) + } + fn ispe(&self, id: u32) -> Option<(u32, u32)> { + self.props_of(id).find_map(|p| if let Prop::Ispe(w, h) = p { Some((*w, *h)) } else { None }) + } + fn transforms(&self, id: u32) -> Vec { + self.props_of(id).filter_map(|p| if let Prop::T(t) = p { Some(*t) } else { None }).collect() + } + fn nclx(&self, id: u32) -> Option { + self.props_of(id).find_map(|p| if let Prop::Nclx(n) = p { Some(*n) } else { None }) + } + fn icc(&self, id: u32) -> Option> { + self.props_of(id).find_map(|p| if let Prop::Icc(b) = p { Some(b.clone()) } else { None }) + } + /// The primary item's alpha plane: an auxiliary image (`auxl`) whose `auxC` says alpha. + fn alpha_item(&self) -> Option { + self.refs_to(b"auxl", self.primary).find(|&id| { + matches!(self.item_type(id), Some(t) if &t == b"av01" || &t == b"grid") + && self.props_of(id).any(|p| matches!(p, Prop::AuxC(urn) if ALPHA_URNS.contains(&urn.as_slice()))) + }) + } + /// The primary item's colour is premultiplied by its alpha (`prem` from colour to alpha). + fn premultiplied(&self, alpha: u32) -> bool { + self.refs_from(b"prem", self.primary).contains(&alpha) + } + /// The first item of a metadata kind that describes (`cdsc`) the primary item. + fn metadata_item(&self, is: impl Fn(&Item) -> bool) -> Option { + let described: Vec = self.refs_to(b"cdsc", self.primary).collect(); + self.items.iter().filter(|i| is(i)).map(|i| i.id).find(|id| described.contains(id)) + } +} + +/// The size of an image item (`av01` or `grid`) before its transforms. +fn item_size(c: &Container, id: u32) -> Result<(u32, u32)> { + let kind = c.item_type(id).ok_or_else(|| bad("an image item is not described"))?; + let (w, h) = match (c.ispe(id), &kind) { + (_, b"grid") => grid_header(&c.data(id)?)?.2, + (Some(s), b"av01") => s, + (None, b"av01") => return Err(bad("an AV1 image item has no size (ispe)")), + _ => return Err(CodecError::unsupported(F, "the image item is not an AV1 image or a grid")), + }; + if w == 0 || h == 0 { + return Err(bad("zero dimension")); + } + Ok((w, h)) +} + +/// The size after `clap` / `irot`, from a stored size. +fn displayed(mut w: u32, mut h: u32, ts: &[Transform]) -> (u32, u32) { + for t in ts { + match *t { + Transform::Clap { w: cw, h: ch, .. } if cw >= 1.0 && ch >= 1.0 && cw.is_finite() && ch.is_finite() => { + w = (cw.round() as u32).clamp(1, w); + h = (ch.round() as u32).clamp(1, h); + } + Transform::Rot(n) if n % 2 == 1 => std::mem::swap(&mut w, &mut h), + _ => {} + } + } + (w, h) +} + +/// (rows, columns, output size) of a `grid` item. +fn grid_header(d: &[u8]) -> Result<(u32, u32, (u32, u32))> { + let mut r = Rd::new(d); + r.u8()?; // version + let flags = r.u8()?; + let rows = u32::from(r.u8()?) + 1; + let cols = u32::from(r.u8()?) + 1; + let n = if flags & 1 != 0 { 4 } else { 2 }; + let w = r.uint(n)? as u32; + let h = r.uint(n)? as u32; + Ok((rows, cols, (w, h))) +} + +/// The upright size [`decode`] returns (`keep_orientation` off), from the boxes alone. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32)> { + let c = parse(bytes)?; + let (w, h) = item_size(&c, c.primary)?; + limits.check_size(w, h)?; + Ok(displayed(w, h, &c.transforms(c.primary))) +} + +/// One decoded AV1 picture. +struct Pic { + w: usize, + h: usize, + bits: u8, + ssx: usize, + ssy: usize, + mono: bool, + planes: [Vec; 3], + cicp: Cicp, +} + +fn av1_error(e: av1::Error) -> CodecError { + match e { + av1::Error::Unsupported(w) => CodecError::unsupported(F, format!("AV1: {w}")), + other => bad(format!("AV1: {other}")), + } +} + +/// Decodes the `av01` item `id`, refusing pictures over `limits`. +fn decode_av1(c: &Container, id: u32, limits: &Limits) -> Result { + if c.item_type(id) != Some(*b"av01") { + return Err(CodecError::unsupported(F, "an image tile is not an AV1 image")); + } + // A tile's declared size counts before its picture is decoded too. + if let Some((w, h)) = c.ispe(id) { + limits.check(w, h, ChannelLayout::Rgb, SampleType::U16)?; + } + let data = c.data(id)?; + let config = c.props_of(id).find_map(|p| if let Prop::Av1c(o) = p { Some(o.clone()) } else { None }); + let mut dec = av1::Decoder::new(); + dec.set_memory_budget(limits.max_alloc.min(AV1_MEMORY_BUDGET)); + let mut pics = catch_unwind(AssertUnwindSafe(|| -> std::result::Result, av1::Error> { + let mut pics = Vec::new(); + // The sequence header normally travels in the item too; the av1C copy covers files + // where it doesn't. + if let Some(cfg) = config.filter(|o| !o.is_empty()) { + pics.extend(dec.decode(&cfg)?); + } + pics.extend(dec.decode(&data)?); + pics.extend(dec.flush_result()?); + Ok(pics) + })) + .map_err(|_| bad("the AV1 decoder failed on this file"))? + .map_err(av1_error)?; + if pics.is_empty() { + return Err(bad("the AV1 data holds no picture")); + } + let p = pics.swap_remove(0); + limits.check(p.width, p.height, ChannelLayout::Rgb, SampleType::U16)?; + let (w, h) = (p.width as usize, p.height as usize); + let (ssx, ssy) = (usize::from(p.subsampling_x.min(1)), usize::from(p.subsampling_y.min(1))); + let need = w.checked_mul(h).ok_or_else(|| bad("picture too large"))?; + let cneed = ((w + ssx) >> ssx) * ((h + ssy) >> ssy); + if need == 0 || p.planes[0].len() < need || (!p.mono_chrome && (p.planes[1].len() < cneed || p.planes[2].len() < cneed)) { + return Err(bad("AV1 picture planes are short")); + } + let cicp = Cicp { + primaries: u16::from(p.color_primaries), + transfer: u16::from(p.transfer_characteristics), + matrix: u16::from(p.matrix_coefficients), + full_range: p.full_range, + }; + Ok(Pic { w, h, bits: p.bit_depth.clamp(8, 16), ssx, ssy, mono: p.mono_chrome, planes: p.planes, cicp }) +} + +/// Kr, Kb of an H.273 matrix code point; `None` = identity (GBR). +fn coefficients(matrix: u16, primaries: u16) -> Result> { + Ok(match matrix { + 0 => None, + 1 => Some((0.2126, 0.0722)), + 4 => Some((0.30, 0.11)), + 7 => Some((0.212, 0.087)), + 9 | 10 => Some((0.2627, 0.0593)), + // Chromaticity-derived non-constant luminance. + 12 => { + let [kr, _, kb] = crate::cicp::luma_weights(primaries); + Some((kr, kb)) + } + // 2 (unspecified: AVIF readers assume BT.601), 5, 6 (BT.601) and reserved values. + 2 | 5 | 6 | 3 | 15.. => Some((0.299, 0.114)), + _ => return Err(CodecError::unsupported(F, format!("matrix coefficients {matrix} (YCgCo, constant luminance or ICtCp)"))), + }) +} + +/// A chroma sample at luma position (x, y), bilinear between chroma sites centred on their +/// luma pairs. +#[inline] +fn chroma_at(plane: &[u16], cw: usize, ch: usize, ssx: usize, ssy: usize, x: usize, y: usize) -> f32 { + let pos = |v: usize, ss: usize, n: usize| -> (usize, usize, f32) { + if ss == 0 { + return (v.min(n - 1), v.min(n - 1), 0.0); + } + let f = ((v as f32 + 0.5) / 2.0 - 0.5).max(0.0); + let i = f as usize; + (i.min(n - 1), (i + 1).min(n - 1), f - i as f32) + }; + let (x0, x1, fx) = pos(x, ssx, cw); + let (y0, y1, fy) = pos(y, ssy, ch); + let s = |xx: usize, yy: usize| f32::from(plane.get(yy * cw + xx).copied().unwrap_or(0)); + let top = s(x0, y0) * (1.0 - fx) + s(x1, y0) * fx; + let bot = s(x0, y1) * (1.0 - fx) + s(x1, y1) * fx; + top * (1.0 - fy) + bot * fy +} + +/// The sample scaling of a picture: luma offset and range, chroma range and midpoint. +struct Levels { + yoff: f32, + yrange: f32, + crange: f32, + mid: f32, +} + +impl Levels { + fn new(bits: u8, full_range: bool) -> Self { + let max = ((1u32 << bits) - 1) as f32; + let scale = f32::from(1u16 << (bits - 8).min(8)); + let (yoff, yrange, crange) = if full_range { (0.0, max, max) } else { (16.0 * scale, 219.0 * scale, 224.0 * scale) }; + Levels { yoff, yrange, crange, mid: f32::from(1u16 << (bits - 1).min(15)) } + } +} + +fn to_u16(v: f32) -> u16 { + (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 +} + +/// Where a decoded picture goes: an interleaved 16-bit canvas of `ch` channels. +struct Canvas { + w: usize, + h: usize, + ch: usize, + data: Vec, +} + +impl Canvas { + fn new(w: u32, h: u32, layout: ChannelLayout, limits: &Limits) -> Result { + limits.check(w, h, layout, SampleType::U16)?; + let (w, h, ch) = (w as usize, h as usize, layout.channels()); + let len = w.checked_mul(h).and_then(|n| n.checked_mul(ch)).ok_or_else(|| bad("image too large"))?; + Ok(Canvas { w, h, ch, data: vec![0; len] }) + } + + /// The picture's YCbCr as non-linear R'G'B' into channels 0..3, its top-left at (x0, y0). + fn put_colour(&mut self, p: &Pic, cicp: Cicp, x0: usize, y0: usize) -> Result<()> { + let lv = Levels::new(p.bits, cicp.full_range); + let k = coefficients(cicp.matrix, cicp.primaries)?; + let (cw, chh) = ((p.w + p.ssx) >> p.ssx, (p.h + p.ssy) >> p.ssy); + for y in 0..p.h.min(self.h.saturating_sub(y0)) { + for x in 0..p.w.min(self.w.saturating_sub(x0)) { + let luma = (f32::from(p.planes[0][y * p.w + x]) - lv.yoff) / lv.yrange; + let (cb, cr) = if p.mono { + (lv.mid, lv.mid) + } else { + (chroma_at(&p.planes[1], cw, chh, p.ssx, p.ssy, x, y), chroma_at(&p.planes[2], cw, chh, p.ssx, p.ssy, x, y)) + }; + let rgb = match k { + // Identity (GBR): Y = G, Cb = B, Cr = R, all three at the luma levels. + None => [(cr - lv.yoff) / lv.yrange, luma, (cb - lv.yoff) / lv.yrange], + Some((kr, kb)) => { + let (cb, cr) = ((cb - lv.mid) / lv.crange, (cr - lv.mid) / lv.crange); + let r = luma + 2.0 * (1.0 - kr) * cr; + let b = luma + 2.0 * (1.0 - kb) * cb; + [r, (luma - kr * r - kb * b) / (1.0 - kr - kb), b] + } + }; + let o = ((y0 + y) * self.w + x0 + x) * self.ch; + for (d, v) in self.data[o..o + 3].iter_mut().zip(rgb) { + *d = to_u16(v); + } + } + } + Ok(()) + } + + /// The picture's luma as alpha into channel 3, its top-left at (x0, y0). + fn put_alpha(&mut self, p: &Pic, x0: usize, y0: usize) { + let lv = Levels::new(p.bits, p.cicp.full_range); + for y in 0..p.h.min(self.h.saturating_sub(y0)) { + for x in 0..p.w.min(self.w.saturating_sub(x0)) { + let a = (f32::from(p.planes[0][y * p.w + x]) - lv.yoff) / lv.yrange; + self.data[((y0 + y) * self.w + x0 + x) * self.ch + 3] = to_u16(a); + } + } + } +} + +/// Decodes the image item `id` (one `av01` or a `grid` of them) into `canvas`, through `put`. +/// Returns the first picture's bit depth and colour description. +fn render( + c: &Container, + id: u32, + limits: &Limits, + canvas: &mut Canvas, + put: &mut dyn FnMut(&mut Canvas, &Pic, usize, usize) -> Result<()>, +) -> Result<(u8, Cicp)> { + if c.item_type(id) != Some(*b"grid") { + let p = decode_av1(c, id, limits)?; + if (p.w, p.h) != (canvas.w, canvas.h) { + return Err(bad("the AV1 picture's size differs from its item's")); + } + put(canvas, &p, 0, 0)?; + return Ok((p.bits, p.cicp)); + } + let (rows, cols, _) = grid_header(&c.data(id)?)?; + let tiles = c.refs_from(b"dimg", id); + if tiles.is_empty() || tiles.len() as u64 != u64::from(rows) * u64::from(cols) { + return Err(bad("grid tile count doesn't match its rows and columns")); + } + let mut first: Option<(usize, usize, u8, Cicp)> = None; + for (i, &t) in tiles.iter().enumerate() { + let p = decode_av1(c, t, limits)?; + let (tw, th, ..) = *first.get_or_insert((p.w, p.h, p.bits, p.cicp)); + if (p.w, p.h) != (tw, th) { + return Err(bad("grid tiles differ in size")); + } + if tw.saturating_mul(cols as usize) < canvas.w || th.saturating_mul(rows as usize) < canvas.h { + return Err(bad("grid tiles don't cover the grid")); + } + put(canvas, &p, (i % cols as usize) * tw, (i / cols as usize) * th)?; + } + first.map(|(_, _, bits, cicp)| (bits, cicp)).ok_or_else(|| bad("grid without tiles")) +} + +/// Applies the container's transforms in order to an interleaved canvas. +fn apply_transforms(mut cv: Canvas, ts: &[Transform]) -> Canvas { + for t in ts { + cv = match *t { + Transform::Clap { w, h, dx, dy } => clap(cv, w, h, dx, dy), + Transform::Rot(n) => (0..n).fold(cv, |cv, _| rotate(cv)), + Transform::Mir(axis) => mirror(cv, axis), + }; + } + cv +} + +fn clap(cv: Canvas, cw: f64, chh: f64, dx: f64, dy: f64) -> Canvas { + if !(cw >= 1.0 && chh >= 1.0 && cw.is_finite() && chh.is_finite() && dx.is_finite() && dy.is_finite()) { + return cv; + } + let (w, h, ch) = (cv.w, cv.h, cv.ch); + let nw = (cw.round() as usize).clamp(1, w); + let nh = (chh.round() as usize).clamp(1, h); + // The centre of the aperture is the image centre plus the offset. + let x0 = ((w as f64 - 1.0) / 2.0 + dx - (nw as f64 - 1.0) / 2.0).round().clamp(0.0, (w - nw) as f64) as usize; + let y0 = ((h as f64 - 1.0) / 2.0 + dy - (nh as f64 - 1.0) / 2.0).round().clamp(0.0, (h - nh) as f64) as usize; + let mut data = Vec::with_capacity(nw * nh * ch); + for y in y0..y0 + nh { + data.extend_from_slice(&cv.data[(y * w + x0) * ch..(y * w + x0 + nw) * ch]); + } + Canvas { w: nw, h: nh, ch, data } +} + +/// 90° anticlockwise: (x, y) → (y, w - 1 - x), the new size h × w. +fn rotate(cv: Canvas) -> Canvas { + let (w, h, ch) = (cv.w, cv.h, cv.ch); + let mut data = vec![0u16; cv.data.len()]; + for y in 0..h { + for x in 0..w { + let d = ((w - 1 - x) * h + y) * ch; + let s = (y * w + x) * ch; + data[d..d + ch].copy_from_slice(&cv.data[s..s + ch]); + } + } + Canvas { w: h, h: w, ch, data } +} + +fn mirror(mut cv: Canvas, axis: u8) -> Canvas { + let (w, h, ch) = (cv.w, cv.h, cv.ch); + if axis == 0 { + for y in 0..h / 2 { + let (top, bottom) = cv.data.split_at_mut((h - 1 - y) * w * ch); + top[y * w * ch..(y + 1) * w * ch].swap_with_slice(&mut bottom[..w * ch]); + } + } else { + for row in cv.data.chunks_exact_mut(w * ch) { + for x in 0..w / 2 { + for c in 0..ch { + row.swap(x * ch + c, (w - 1 - x) * ch + c); + } + } + } + } + cv +} + +/// Straight colour from colour premultiplied by alpha. +fn unpremultiply(cv: &mut Canvas) { + for px in cv.data.as_chunks_mut::<4>().0 { + let a = u32::from(px[3]); + if a == 0 { + continue; + } + for v in &mut px[..3] { + *v = (u32::from(*v) * 65535 / a).min(65535) as u16; + } + } +} + +/// The primary item's Exif (TIFF structure, its offset prefix dropped) and XMP. +fn read_metadata(c: &Container) -> (Option>, Option) { + let exif = c.metadata_item(|i| &i.kind == b"Exif").and_then(|id| c.data(id).ok()).and_then(|d| { + let offset = usize::try_from(u32::from_be_bytes(d.get(..4)?.try_into().ok()?)).ok()?; + let tiff = d.get(4usize.checked_add(offset)?..)?; + (tiff.starts_with(b"II*\0") || tiff.starts_with(b"MM\0*")).then(|| tiff.to_vec()) + }); + let xmp = c + .metadata_item(|i| &i.kind == b"mime" && i.content_type.as_deref() == Some(b"application/rdf+xml")) + .and_then(|id| c.data(id).ok()) + .map(|d| String::from_utf8_lossy(&d).trim_end_matches('\0').to_owned()); + (exif, xmp) +} + +/// Decodes the primary image. `keep_orientation` leaves the container's `clap`, `irot` and +/// `imir` unapplied (the stored pixels), as for HEIF. +pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> Result { + let c = parse(bytes)?; + let id = c.primary; + let (w, h) = item_size(&c, id)?; + let alpha = c.alpha_item().filter(|&a| item_size(&c, a).is_ok_and(|s| s == (w, h))); + let layout = if alpha.is_some() { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + // The declared size, before any picture is decoded. + let mut canvas = Canvas::new(w, h, layout, limits)?; + let file_cicp = c.nclx(id); + let mut colour = |cv: &mut Canvas, p: &Pic, x: usize, y: usize| cv.put_colour(p, file_cicp.unwrap_or(p.cicp), x, y); + let (bits, stream_cicp) = render(&c, id, limits, &mut canvas, &mut colour)?; + if let Some(a) = alpha { + render(&c, a, limits, &mut canvas, &mut |cv, p, x, y| { + cv.put_alpha(p, x, y); + Ok(()) + })?; + if c.premultiplied(a) { + unpremultiply(&mut canvas); + } + } + if !keep_orientation { + canvas = apply_transforms(canvas, &c.transforms(id)); + } + let (w, h) = (canvas.w as u32, canvas.h as u32); + let image = Image::from_u16(w, h, layout, &canvas.data)?; + drop(canvas); + let mut img = if bits > 8 { image } else { image.convert(layout, SampleType::U8) }; + let cicp = file_cicp.unwrap_or(stream_cicp); + img.icc = c.icc(id); + img.cicp = Some(cicp); + let (exif, xmp) = read_metadata(&c); + img.meta = Metadata { + exif: exif.map(|e| if keep_orientation { e } else { crate::orientation::upright_exif(&e).into_owned() }), + xmp: xmp.map(|x| if keep_orientation { x } else { crate::orientation::upright_xmp(&x).into_owned() }), + ..Default::default() + }; + // PQ or HLG: linear light in 32-bit float, as for HEIF. + if cicp.is_hdr() { + limits.check(w, h, layout, SampleType::F32)?; + return crate::cicp::linearize(img, cicp); + } + Ok(img) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::DecodeOptions; + + const QUAD_420: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-420-bt709.avif"); + const QUAD_420_DAV1D: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-420-bt709.dav1d.yuv"); + const QUAD_GBR_LOSSLESS: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-444-gbr-lossless.avif"); + const QUAD_SOURCE: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-source.rgb"); + + fn rgb8(img: &Image) -> Vec { + img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[1], p[2]]).collect() + } + + fn mean_abs_diff(a: &[u8], b: &[u8]) -> f64 { + assert_eq!(a.len(), b.len()); + a.iter().zip(b).map(|(&x, &y)| (f64::from(x) - f64::from(y)).abs()).sum::() / a.len() as f64 + } + + /// A box of type `t` around `body`. + fn bx(t: &[u8; 4], body: &[u8]) -> Vec { + let mut v = ((body.len() + 8) as u32).to_be_bytes().to_vec(); + v.extend_from_slice(t); + v.extend_from_slice(body); + v + } + + /// A full box: version 0, no flags. + fn fbx(t: &[u8; 4], body: &[u8]) -> Vec { + let mut b = vec![0, 0, 0, 0]; + b.extend_from_slice(body); + bx(t, &b) + } + + /// An `infe` (version 2) for item `id` of `kind`. + fn infe(id: u16, kind: &[u8; 4]) -> Vec { + let mut b = vec![2, 0, 0, 0]; + b.extend_from_slice(&id.to_be_bytes()); + b.extend_from_slice(&[0, 0]); + b.extend_from_slice(kind); + b.push(0); + bx(b"infe", &b) + } + + fn ispe(w: u32, h: u32) -> Vec { + let mut b = w.to_be_bytes().to_vec(); + b.extend_from_slice(&h.to_be_bytes()); + fbx(b"ispe", &b) + } + + /// The AV1 data and `av1C` of an encoded file's primary item. + fn av1_of(file: &[u8]) -> (Vec, Vec) { + let c = parse(file).unwrap(); + let cfg = c.props_of(c.primary).find_map(|p| if let Prop::Av1c(o) = p { Some(o.clone()) } else { None }).unwrap(); + (c.data(c.primary).unwrap(), cfg) + } + + /// An AVIF of `items` (id, type, data, property indices) with `props`, `refs` and primary 1. + /// All item data sits in `idat` (construction method 1). + /// An item: ID, type, data and 1-based property indices. + type TestItem<'a> = (u16, &'a [u8; 4], &'a [u8], &'a [u8]); + /// An item reference: type, from, to. + type TestRef<'a> = (&'a [u8; 4], u16, &'a [u16]); + + fn build(items: &[TestItem], props: &[Vec], refs: &[TestRef]) -> Vec { + let mut iinf = vec![0, 0, 0, 0]; + iinf.extend_from_slice(&(items.len() as u16).to_be_bytes()); + let mut iloc = vec![1, 0, 0, 0, 0x44, 0x00]; + iloc.extend_from_slice(&(items.len() as u16).to_be_bytes()); + let mut ipma = vec![0, 0, 0, 0]; + ipma.extend_from_slice(&(items.len() as u32).to_be_bytes()); + let mut idat = Vec::new(); + for &(id, kind, data, assoc) in items { + iinf.extend(infe(id, kind)); + iloc.extend_from_slice(&id.to_be_bytes()); + iloc.extend_from_slice(&[0, 1, 0, 0, 0, 1]); // method 1, data ref 0, one extent + iloc.extend_from_slice(&(idat.len() as u32).to_be_bytes()); + iloc.extend_from_slice(&(data.len() as u32).to_be_bytes()); + idat.extend_from_slice(data); + ipma.extend_from_slice(&id.to_be_bytes()); + ipma.push(assoc.len() as u8); + ipma.extend(assoc.iter().map(|&p| p | 0x80)); + } + let mut iref = vec![0, 0, 0, 0]; + for &(kind, from, to) in refs { + let mut b = from.to_be_bytes().to_vec(); + b.extend_from_slice(&(to.len() as u16).to_be_bytes()); + to.iter().for_each(|t| b.extend_from_slice(&t.to_be_bytes())); + iref.extend(bx(kind, &b)); + } + let mut meta = vec![0, 0, 0, 0]; + meta.extend(fbx(b"hdlr", b"\0\0\0\0pict\0\0\0\0\0\0\0\0\0\0\0\0\0")); + meta.extend(fbx(b"pitm", &[0, 1])); + meta.extend(bx(b"iinf", &iinf)); + meta.extend(bx(b"iloc", &iloc)); + meta.extend(bx(b"iref", &iref)); + meta.extend(bx(b"iprp", &[bx(b"ipco", &props.concat()), bx(b"ipma", &ipma)].concat())); + meta.extend(bx(b"idat", &idat)); + let mut file = bx(b"ftyp", b"avif\0\0\0\0avifmif1miaf"); + file.extend(bx(b"meta", &meta)); + file + } + + #[test] + fn libaom_420_limited_range_matches_dav1d() { + let img = crate::decode(QUAD_420).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (33, 17, ChannelLayout::Rgb, SampleType::U8)); + assert_eq!(img.cicp.map(|c| (c.matrix, c.full_range)), Some((1, false)), "BT.709 matrix, limited range"); + assert_eq!(crate::probe_size(QUAD_420).unwrap(), (33, 17)); + // The AV1 picture is libdav1d's, sample for sample. + let c = parse(QUAD_420).unwrap(); + let p = decode_av1(&c, c.primary, &Limits::default()).unwrap(); + assert_eq!((p.w, p.h, p.bits, p.ssx, p.ssy, p.mono), (33, 17, 8, 1, 1, false)); + let planes: Vec = p.planes.iter().flatten().map(|&v| v as u8).collect(); + assert_eq!(planes, QUAD_420_DAV1D); + let ours = rgb8(&img); + // The quadrant centres are the source's colours, give or take the lossy coding. + for (x, y) in [(5usize, 4usize), (27, 4), (5, 13), (27, 13)] { + let i = (y * 33 + x) * 3; + let d = mean_abs_diff(&ours[i..i + 3], &QUAD_SOURCE[i..i + 3]); + assert!(d < 12.0, "({x}, {y}): {:?} vs {:?}", &ours[i..i + 3], &QUAD_SOURCE[i..i + 3]); + } + } + + #[test] + fn libaom_lossless_identity_matrix_is_exact() { + let img = crate::decode(QUAD_GBR_LOSSLESS).unwrap(); + assert_eq!((img.width(), img.height(), img.sample_type()), (33, 17, SampleType::U8)); + assert_eq!(img.cicp.map(|c| (c.matrix, c.full_range)), Some((0, true))); + assert_eq!(rgb8(&img), QUAD_SOURCE); + } + + #[test] + fn limits_are_checked_before_decoding() { + let tight = |max_pixels, max_alloc| DecodeOptions { limits: Limits { max_pixels, max_alloc, ..Limits::default() }, ..Default::default() }; + assert!(matches!(crate::decode_with(QUAD_420, &tight(33 * 17 - 1, u64::MAX)), Err(CodecError::LimitExceeded(_)))); + // The 16-bit intermediate counts: 33 × 17 RGB at 2 bytes. + assert!(matches!(crate::decode_with(QUAD_420, &tight(u64::MAX, 33 * 17 * 6 - 1)), Err(CodecError::LimitExceeded(_)))); + assert!(crate::decode_with(QUAD_420, &tight(33 * 17, Limits::default().max_alloc)).is_ok()); + // `max_alloc` is the AV1 decoder's picture-store budget too. + assert!(crate::decode_with(QUAD_420, &tight(33 * 17, 33 * 17 * 6)).is_err()); + // A declared size over the limits fails from the boxes alone. + let (av1, cfg) = av1_of(QUAD_420); + let huge = build(&[(1, b"av01", &av1, &[1, 2])], &[ispe(1 << 20, 1 << 20), bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat())], &[]); + assert!(matches!(crate::decode(&huge), Err(CodecError::LimitExceeded(_)))); + assert!(matches!(crate::probe_size(&huge), Err(CodecError::LimitExceeded(_)))); + } + + #[test] + fn a_picture_that_differs_from_its_ispe_is_refused() { + let (av1, cfg) = av1_of(QUAD_420); + let file = build(&[(1, b"av01", &av1, &[1, 2])], &[ispe(32, 17), bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat())], &[]); + assert!(matches!(crate::decode(&file), Err(CodecError::Malformed { .. }))); + } + + #[test] + fn a_grid_is_assembled_from_its_tiles_and_cropped() { + // Two copies of the 33 × 17 picture side by side, cropped to 60 × 15 by the grid. + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + let grid = [0u8, 0, 0, 1, 0, 60, 0, 15]; + let file = build( + &[(1, b"grid", &grid, &[1]), (2, b"av01", &av1, &[2, 3]), (3, b"av01", &av1, &[2, 3])], + &[ispe(60, 15), ispe(33, 17), av1c, fbx(b"pixi", &[3, 8, 8, 8])], + &[(b"dimg", 1, &[2, 3])], + ); + assert_eq!(crate::probe_size(&file).unwrap(), (60, 15)); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height()), (60, 15)); + let got = rgb8(&img); + for y in 0..15 { + for x in 0..60 { + let s = (y * 33 + x % 33) * 3; + assert_eq!(&got[(y * 60 + x) * 3..][..3], &QUAD_SOURCE[s..s + 3], "({x}, {y})"); + } + } + // Tiles that can't cover the grid are refused. + let wide = [0u8, 0, 0, 1, 0, 70, 0, 15]; + let file = build( + &[(1, b"grid", &wide, &[1]), (2, b"av01", &av1, &[2, 3]), (3, b"av01", &av1, &[2, 3])], + &[ispe(70, 15), ispe(33, 17), bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat())], + &[(b"dimg", 1, &[2, 3])], + ); + assert!(matches!(crate::decode(&file), Err(CodecError::Malformed { .. }))); + } + + #[test] + fn container_rotation_and_mirror_are_applied_unless_kept() { + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + let file = build(&[(1, b"av01", &av1, &[1, 2, 3, 4])], &[ispe(33, 17), av1c, bx(b"irot", &[1]), bx(b"imir", &[0])], &[]); + assert_eq!(crate::probe_size(&file).unwrap(), (17, 33)); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height()), (17, 33)); + let got = rgb8(&img); + // Rotated 90° anticlockwise, then top and bottom exchanged: a transpose, (x, y) from (y, x). + for y in 0..33 { + for x in 0..17 { + let s = (x * 33 + y) * 3; + assert_eq!(&got[(y * 17 + x) * 3..][..3], &QUAD_SOURCE[s..s + 3], "({x}, {y})"); + } + } + let kept = crate::decode_with(&file, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!((kept.width(), kept.height(), rgb8(&kept)), (33, 17, QUAD_SOURCE.to_vec())); + } + + #[test] + fn transforms_rotate_mirror_and_crop() { + // 3 × 2, one channel, value = index + let cv = || Canvas { w: 3, h: 2, ch: 1, data: (0..6).collect() }; + let r = apply_transforms(cv(), &[Transform::Rot(1)]); + // anticlockwise: the right column becomes the top row + assert_eq!((r.w, r.h, r.data), (2, 3, vec![2, 5, 1, 4, 0, 3])); + // axis 0 exchanges top and bottom, axis 1 left and right (libheif's and libavif's reading) + assert_eq!(apply_transforms(cv(), &[Transform::Mir(0)]).data, vec![3, 4, 5, 0, 1, 2]); + assert_eq!(apply_transforms(cv(), &[Transform::Mir(1)]).data, vec![2, 1, 0, 5, 4, 3]); + let c = apply_transforms(cv(), &[Transform::Clap { w: 1.0, h: 1.0, dx: 1.0, dy: 0.0 }]); + assert_eq!((c.w, c.h), (1, 1)); + assert!(c.data == [2] || c.data == [5]); + assert_eq!(displayed(3, 2, &[Transform::Rot(3)]), (2, 3)); + // A non-finite or empty aperture is ignored. + assert_eq!(apply_transforms(cv(), &[Transform::Clap { w: f64::NAN, h: 1.0, dx: 0.0, dy: 0.0 }]).data.len(), 6); + } + + #[test] + fn hostile_files_are_errors_not_panics() { + for file in [QUAD_420, QUAD_GBR_LOSSLESS] { + // every truncation + for n in 0..file.len() { + let _ = crate::probe_size(&file[..n]); + let _ = crate::decode(&file[..n]); + } + // byte flips through the container and the AV1 data + for i in 0..file.len() { + for x in [0x01u8, 0x80, 0xff] { + let mut m = file.to_vec(); + m[i] ^= x; + let _ = crate::probe_size(&m); + let _ = crate::decode(&m); + } + } + } + // box sizes that point far outside the file + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend_from_slice(b"\xff\xff\xff\xffmeta\0\0\0\0"); + assert!(crate::decode(&b).is_err()); + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend_from_slice(b"\0\0\0\x01meta\xff\xff\xff\xff\xff\xff\xff\xff"); + assert!(crate::probe_size(&b).is_err()); + } + + #[test] + fn an_animated_avif_is_unsupported() { + let mut b = bx(b"ftyp", b"avis\0\0\0\0avis"); + b.extend(bx(b"moov", &[])); + assert!(matches!(crate::decode(&b), Err(CodecError::Unsupported { format: Format::Avif, .. }))); + } + + #[test] + fn zero_width_iloc_extents_are_capped() { + // iloc v2 with zero-byte offset/length/base/index fields: each extent costs no input bytes + let mut iloc = vec![2, 0, 0, 0, 0, 0]; + iloc.extend_from_slice(&3u32.to_be_bytes()); + for id in 1..=3u32 { + iloc.extend_from_slice(&id.to_be_bytes()); + iloc.extend_from_slice(&[0, 0, 0, 0, 0xff, 0xff]); // method, data ref, 65535 extents + } + let mut meta = vec![0, 0, 0, 0]; + meta.extend(bx(b"iloc", &iloc)); + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend(bx(b"meta", &meta)); + assert!(matches!(crate::decode(&b), Err(CodecError::Malformed { message, .. }) if message.contains("extents"))); + } + + #[test] + fn item_data_larger_than_the_file_is_refused() { + // one item whose 1000 extents each repeat the whole file (length 0 = to the end) + let mut iloc = vec![1, 0, 0, 0, 0x44, 0]; + iloc.extend_from_slice(&1u16.to_be_bytes()); + iloc.extend_from_slice(&[0, 1, 0, 0, 0, 0]); // id 1, method 0, data ref 0 + iloc.extend_from_slice(&1000u16.to_be_bytes()); + for _ in 0..1000 { + iloc.extend_from_slice(&[0; 8]); + } + let mut meta = vec![0, 0, 0, 0]; + meta.extend(fbx(b"pitm", &[0, 1])); + meta.extend(bx(b"iloc", &iloc)); + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend(bx(b"meta", &meta)); + let c = parse(&b).unwrap(); + assert!(matches!(c.data(1), Err(CodecError::Malformed { message, .. }) if message.contains("larger than the file"))); + } + + #[test] + fn unsupported_matrices_are_errors() { + for m in [8u16, 11, 13, 14] { + assert!(matches!(coefficients(m, 1), Err(CodecError::Unsupported { .. })), "{m}"); + } + assert_eq!(coefficients(0, 1).unwrap(), None); + assert!(coefficients(12, 9).unwrap().is_some()); + } + + #[cfg(feature = "avif")] + mod with_the_encoder { + use super::*; + use crate::codecs::avif::{HdrTransfer, encode_avif_hdr, encode_avif_with_speed}; + + /// A smooth test card with a saturated band. + fn card(w: usize, h: usize, ch: usize) -> Vec { + (0..w * h) + .flat_map(|i| { + let (x, y) = (i % w, i / w); + let px = + [(x * 255 / (w - 1)) as u8, (y * 255 / (h - 1)) as u8, if x < w / 4 { 230 } else { 40 }, (255 - x * 200 / (w - 1)) as u8]; + px.into_iter().take(ch) + }) + .collect() + } + + #[test] + fn ravif_rgb_round_trip_is_10_bit() { + let (w, h) = (48, 32); + let src = Image::from_u8(w as u32, h as u32, ChannelLayout::Rgb, card(w, h, 3)).unwrap(); + let file = encode_avif_with_speed(&src, 95, 10).unwrap(); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (48, 32, ChannelLayout::Rgb, SampleType::U16)); + let diff = mean_abs_diff(&rgb8(&img), &card(w, h, 3)); + assert!(diff < 2.5, "mean difference {diff}"); + } + + #[test] + fn ravif_alpha_round_trips() { + let (w, h) = (40, 24); + let src = Image::from_u8(w as u32, h as u32, ChannelLayout::Rgba, card(w, h, 4)).unwrap(); + let file = encode_avif_with_speed(&src, 95, 10).unwrap(); + let img = crate::decode(&file).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + let got = img.to_rgba8(); + let want = card(w, h, 4); + let alpha = |v: &[u8]| v.iter().skip(3).step_by(4).copied().collect::>(); + let diff = mean_abs_diff(&alpha(&got), &alpha(&want)); + assert!(diff < 2.0, "alpha mean difference {diff}"); + } + + #[test] + fn exif_is_read_from_its_item() { + let mut src = Image::from_u8(16, 8, ChannelLayout::Rgb, card(16, 8, 3)).unwrap(); + let exif = b"II*\0\x08\0\0\0\0\0\0\0\0\0".to_vec(); + src.meta.exif = Some(exif.clone()); + let file = encode_avif_with_speed(&src, 80, 10).unwrap(); + let img = crate::decode_with(&file, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(img.meta.exif.as_deref(), Some(exif.as_slice())); + } + + #[test] + fn hdr_round_trips_keep_highlights_above_white() { + let (w, h) = (32usize, 16usize); + // linear BT.2020: left half grey 0.18, right half 4 × SDR white + let rgb: Vec = (0..w * h).flat_map(|i| if i % w < w / 2 { [0.18f32; 3] } else { [4.0; 3] }).collect(); + for t in [HdrTransfer::Pq, HdrTransfer::Hlg] { + let file = encode_avif_hdr(w as u32, h as u32, &rgb, 98, 10, None, t).unwrap(); + let img = crate::decode(&file).unwrap(); + assert_eq!(img.sample_type(), SampleType::F32, "{t:?}"); + assert_eq!(img.cicp.map(|c| c.transfer), Some(Cicp::TRANSFER_LINEAR), "{t:?}"); + let v = img.to_f32_samples().unwrap(); + let at = |x: usize| v[(8 * w + x) * 3 + 1]; + assert!((at(4) / 0.18 - 1.0).abs() < 0.08, "{t:?}: grey {}", at(4)); + assert!((at(w - 4) / 4.0 - 1.0).abs() < 0.08, "{t:?}: highlight {}", at(w - 4)); + } + } + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index ecaf945cd..fbdb412b1 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -26,7 +26,8 @@ pub enum Format { OpenExr, /// Radiance RGBE. Hdr, - /// Encode-only behind the `avif` feature; see [`ASYMMETRIC_EXCEPTIONS`]. + /// Encoded behind the `avif` feature and decoded behind `avif-decode` (native targets); see + /// [`ASYMMETRIC_EXCEPTIONS`] for a build with only one of them. Avif, /// HEIF/HEIC (HEVC-coded): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Heif, @@ -89,8 +90,10 @@ pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[ ), ( Format::Avif, - "AVIF encode uses ravif (pure Rust) but decoding requires dav1d (C); read stays unsupported \ - until a pure-Rust AV1 decoder is viable. Only enabled with the non-default `avif` feature.", + "AVIF encode (ravif and rav1e, pure Rust) is behind the non-default `avif` feature and AVIF \ + decode (craft-video's clean-room pure-Rust AV1 decoder) behind the non-default \ + `avif-decode` feature, native targets only (on wasm32 a decoder panic would abort). A build \ + with only one of them reads or writes AVIF, not both.", ), (Format::Jxl, "Lossless JPEG XL export uses the pure-Rust zune-jpegxl encoder; JPEG XL reading is tracked separately and is not yet available."), ( @@ -298,7 +301,9 @@ pub fn caps(format: Format) -> FormatCaps { Format::Qoi => base, Format::OpenExr => FormatCaps { depths: &[S::F16, S::F32], layouts: RGB_GRAY, ..base }, Format::Hdr => FormatCaps { depths: &[S::F32], layouts: &[L::Rgb], alpha: false, lossy: true, ..base }, - Format::Avif => FormatCaps { read: false, write: cfg!(feature = "avif"), lossy: true, ..base }, + Format::Avif => { + FormatCaps { read: cfg!(all(feature = "avif-decode", not(target_arch = "wasm32"))), write: cfg!(feature = "avif"), lossy: true, ..base } + } Format::Heif => { FormatCaps { read: cfg!(feature = "heif"), write: false, depths: &[S::U8, S::U16], icc: true, exif: true, xmp: true, lossy: true, ..base } } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..a4480b2b8 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -106,7 +106,11 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l), + #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] + Format::Avif => codecs::avif_decode::decode(bytes, l, opts.keep_orientation), + #[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))] + Format::Avif => via_image::decode(format, bytes, l), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l), }?; finish_format(format, bytes, img, opts) } @@ -163,10 +167,11 @@ fn finish_format(format: Format, bytes: &[u8], mut img: Image, opts: &DecodeOpti } // Turn the pixels upright, like Photoshop: a TIFF records it in the decoded page's own // directory, the others in their EXIF block. The metadata is rewritten to Orientation = 1 - // on the way. HEIF keeps it in its container, and its decoder has already applied it. + // on the way. HEIF and AVIF keep it in their container, and their decoders have already + // applied it. let o = match format { Format::Tiff => tiff::orientation(bytes, None), - Format::Heif => 1, + Format::Heif | Format::Avif => 1, Format::Dpx => dpx::orientation(bytes), Format::Cineon => dpx::cineon_orientation(bytes), _ => img.meta.exif.as_deref().map_or(1, exif_orientation), @@ -195,10 +200,11 @@ pub fn decode_png_rows>( } /// The upright pixel size [`decode`] would return, read from the file's header without decoding -/// pixels, for WebP, OpenEXR (flat) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same +/// pixels, for WebP, OpenEXR (flat), AVIF (with `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same /// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an -/// EXIF orientation of 5–8, so wherever [`decode`] succeeds the two agree. Other formats (and -/// deep EXR or AVIF) return [`CodecError::Unsupported`]: decode those to learn their size. +/// EXIF orientation of 5–8 (AVIF: its container's `clap` and `irot`), so wherever [`decode`] +/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]: decode +/// those to learn their size. pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; if !caps(format).read { @@ -208,7 +214,11 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { let (w, h, exif) = match format { Format::WebP => webp::probe(bytes, l)?, Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?, - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::probe(format, bytes, l)?, + #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] + Format::Avif => codecs::avif_decode::probe(bytes, l).map(|(w, h)| (w, h, None))?, + #[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))] + Format::Avif => via_image::probe(format, bytes, l)?, + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::probe(format, bytes, l)?, _ => return Err(CodecError::unsupported(format, "no header-only size probe")), }; let o = exif.as_deref().map_or(1, exif_orientation); diff --git a/crates/image-codecs/tests/fixtures/avif/README.md b/crates/image-codecs/tests/fixtures/avif/README.md new file mode 100644 index 000000000..b8b3a8c2a --- /dev/null +++ b/crates/image-codecs/tests/fixtures/avif/README.md @@ -0,0 +1,12 @@ +# AVIF decode fixtures + +Small AVIF files from an independent encoder (libaom through FFmpeg 7.1), for `src/codecs/avif_decode.rs`. + +- `quad-source.rgb`: the 33×17 source, raw RGB24 four-colour quadrants (red, green, blue, near-white). +- `quad-420-bt709.avif`: 8-bit 4:2:0, limited range, BT.709 matrix, odd size (FFmpeg leaves the primaries and transfer unspecified): + `ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 12 -still-picture 1 -pix_fmt yuv420p -colorspace bt709 -color_primaries bt709 -color_trc iec61966-2-1 -color_range tv quad-420-bt709.avif` +- `quad-420-bt709.dav1d.yuv`: that file's AV1 picture as libdav1d decodes it, planar 8-bit 4:2:0 (Y, then Cb, then Cr): + `ffmpeg -c:v libdav1d -i quad-420-bt709.avif -f rawvideo -pix_fmt yuv420p quad-420-bt709.dav1d.yuv` +- `quad-444-gbr-lossless.avif`: lossless 8-bit 4:4:4 with the identity (GBR) matrix, full range: + `ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 0 -still-picture 1 -pix_fmt gbrp -colorspace rgb -color_range pc quad-444-gbr-lossless.avif`. + It decodes to `quad-source.rgb` exactly. diff --git a/crates/image-codecs/tests/fixtures/avif/quad-420-bt709.avif b/crates/image-codecs/tests/fixtures/avif/quad-420-bt709.avif new file mode 100644 index 0000000000000000000000000000000000000000..419196cb178743782cf147ea50d111e2b8642d42 GIT binary patch literal 369 zcmZQzU{FXasVqn=%S>Yc0uY^>nP!-qnV9D5Xy^nK`k9+rk_eIm0{x7XoFWL5fuSHX zxdg@r(E&hyX;G?2VqQv4Dp(a?L1sxVSP3JLl*`P?PX_T^7#J9V5JWOCDgx;MAlAst z%mX_CEF%Tvq%r}8LA=B=14HNhoP4mES(ybz1we7{%!1^6AT5$vT#yP<2m~Or1c6wf zAhRM9B*4tU!2zUr63Yyo8yR>&Lc+=UIYoKNITZ{{KnOIQfk8a8AU6@j00N-HSQwd_ zS%A!l+?2!;E=~zih2|6v4i*6;XO{y&vw`U4k2c-I-AWygkL2ynp7_}AP4*4enEOQm q)z5b1%rN?KQ|H$?;i_Mo{+`I#w8y3Pf9fWWdacYc0uY^>nP!-qnV9D5Xy^zO`k9+rk_eIm0{x7XoFWL5fuSHX zxdg@r(E&hyX;G?2VqQv4Dp(a?L1sxVSP3JLl*`P?PX_T^7#J9V5JWOCDgtQ>AlAst z%mX_CEF%Tvq%r}8LA=B=14HNhoP4mES(ybz1we7{%!1^6AT5$vT#yP<2m~Or1c6wf zAhRM9B*4tU!2zUr63Yyo8x?>o1_t5e{G6h^jRAvYzlgp0#MRG~S8BZJXM2k7n(sn>EClKWl Date: Sun, 15 Nov 2026 10:14:40 -0500 Subject: [PATCH 022/471] fix(tga): bound aggregate decoder storage and reserve fallibly --- crates/tga/src/lib.rs | 36 +++++++++++++++++++++++++------- crates/tga/src/metadata_tests.rs | 22 +++++++++---------- crates/tga/src/tests.rs | 33 +++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index 06a9941a0..5174a8093 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -59,11 +59,13 @@ pub type Result = std::result::Result; pub struct Limits { /// Most pixels (width × height) a picture may have. Default 2^28 (16384 × 16384). pub max_pixels: u64, + /// Maximum combined palette, stored-pixel and RGBA allocation bytes. Default 256 MiB. + pub max_decoded_bytes: usize, } impl Default for Limits { fn default() -> Self { - Self { max_pixels: 1 << 28 } + Self { max_pixels: 1 << 28, max_decoded_bytes: 256 << 20 } } } @@ -313,10 +315,6 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { }; let end = image_data_end(bytes, first_pixel)?; let bytes = bytes.get(..end).ok_or(err("truncated image data"))?; - if let Some(layout) = map_entry { - let map = bytes.get(map_start..first_pixel).ok_or(err("truncated colour map"))?; - palette = map.chunks_exact(layout.bytes()).map(|entry| layout.rgba(entry)).collect(); - } let at = first_pixel; let (stored_as, mapped) = match kind & 7 { 1 => { @@ -335,14 +333,35 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { }; let data = bytes.get(at..).unwrap_or_default(); let bpp = stored_as.bytes(); + let stored_bytes = pixels.checked_mul(bpp).ok_or(Error::LimitExceeded("image size"))?; + let rgba_bytes = pixels.checked_mul(4).ok_or(Error::LimitExceeded("image size"))?; + let palette_bytes = if map_entry.is_some() { map_len * 4 } else { 0 }; + let aggregate = + stored_bytes.checked_add(rgba_bytes).and_then(|n| n.checked_add(palette_bytes)).ok_or(Error::LimitExceeded("decode allocation bytes"))?; + if aggregate > limits.max_decoded_bytes { + return Err(Error::LimitExceeded("decode allocation bytes")); + } + if let Some(layout) = map_entry { + let map = bytes.get(map_start..first_pixel).ok_or(err("truncated colour map"))?; + palette.try_reserve_exact(map_len).map_err(|_| Error::LimitExceeded("allocation failed"))?; + palette.extend(map.chunks_exact(layout.bytes()).map(|entry| layout.rgba(entry))); + } // The stored values, in file order. let stored: Vec = if rle { unpack_rle(data, pixels, bpp)? } else { let n = pixels.checked_mul(bpp).ok_or(Error::LimitExceeded("image size"))?; - data.get(..n).ok_or(err("truncated pixel data"))?.to_vec() + { + let source = data.get(..n).ok_or(err("truncated pixel data"))?; + let mut values = Vec::new(); + values.try_reserve_exact(n).map_err(|_| Error::LimitExceeded("allocation failed"))?; + values.extend_from_slice(source); + values + } }; - let mut rgba = vec![0u8; pixels.checked_mul(4).ok_or(Error::LimitExceeded("image size"))?]; + let mut rgba = Vec::new(); + rgba.try_reserve_exact(rgba_bytes).map_err(|_| Error::LimitExceeded("allocation failed"))?; + rgba.resize(rgba_bytes, 0u8); for (i, v) in stored.chunks_exact(bpp).enumerate().take(pixels) { let (x, y) = (i % w, i / w); let x = if right_to_left { w - 1 - x } else { x }; @@ -386,7 +405,8 @@ fn unpack_rle(mut data: &[u8], pixels: usize, bpp: usize) -> Result> { return Err(short); } let total = pixels.checked_mul(bpp).ok_or(Error::LimitExceeded("image size"))?; - let mut out = Vec::with_capacity(total); + let mut out = Vec::new(); + out.try_reserve_exact(total).map_err(|_| Error::LimitExceeded("allocation failed"))?; while out.len() < total { let (&head, rest) = data.split_first().ok_or(short.clone())?; let count = usize::from(head & 0x7F) + 1; diff --git a/crates/tga/src/metadata_tests.rs b/crates/tga/src/metadata_tests.rs index a5c6c0b12..583f5d691 100644 --- a/crates/tga/src/metadata_tests.rs +++ b/crates/tga/src/metadata_tests.rs @@ -124,7 +124,7 @@ fn importing_truncated_pixels_does_not_consume_tga_footer() { file.extend_from_slice(&offsets); file.extend_from_slice(SIGNATURE); let result = dec(&file); - assert!(matches!(result, Err(_)), "kind {kind}, metadata {metadata:?}: metadata is not a second pixel"); + assert!(result.is_err(), "kind {kind}, metadata {metadata:?}: metadata is not a second pixel"); } } for kind in [1, 9] { @@ -136,7 +136,7 @@ fn importing_truncated_pixels_does_not_consume_tga_footer() { file.extend_from_slice(&[0; 8]); file.extend_from_slice(SIGNATURE); let result = dec(&file); - assert!(matches!(result, Err(_)), "kind {kind}: the footer is not colour-map data"); + assert!(result.is_err(), "kind {kind}: the footer is not colour-map data"); } } @@ -200,7 +200,7 @@ fn preceding_metadata_cannot_supply_missing_pixels() { let mut accepted = Vec::new(); for metadata in 0..4 { let result = dec(&preceding_metadata(false, metadata)); - if !matches!(result, Err(_)) { + if result.is_ok() { accepted.push(metadata); } } @@ -305,7 +305,7 @@ fn malformed_optional_metadata_cannot_supply_missing_pixels() { let footer = file.len() - SIGNATURE.len() - 8; let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; file[directory + 8..directory + 12].copy_from_slice(&u32::MAX.to_le_bytes()); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); // The declared entry count exceeds the bytes before the footer, but the first entry's // payload still precedes the directory and cannot be used as a second pixel. @@ -313,17 +313,17 @@ fn malformed_optional_metadata_cannot_supply_missing_pixels() { let footer = file.len() - SIGNATURE.len() - 8; let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); let mut file = preceding_metadata(false, 2); let footer = file.len() - SIGNATURE.len() - 8; let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; file[extension..extension + 2].copy_from_slice(&494u16.to_le_bytes()); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); let mut file = preceding_metadata(false, 2); let footer = file.len() - SIGNATURE.len() - 8; file.remove(footer - 1); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); } #[test] @@ -341,7 +341,7 @@ fn valid_developer_directory_with_overlapping_payload_rejects_pixels() { assert_eq!(u32::from_le_bytes(file[moved_footer + 4..moved_footer + 8].try_into().unwrap()), directory as u32); assert_eq!(u16::from_le_bytes(file[directory..directory + 2].try_into().unwrap()), 2); assert_eq!(&file[directory + 12..directory + 22], &extra); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); } #[test] @@ -356,7 +356,7 @@ fn nonempty_developer_reference_inside_pixels_rejects_the_whole_image() { file.extend_from_slice(&0u32.to_le_bytes()); file.extend_from_slice(&directory.to_le_bytes()); file.extend_from_slice(SIGNATURE); - assert!(matches!(dec(&file), Err(_)), "an overlapping reference must not return a partial image"); + assert!(dec(&file).is_err(), "an overlapping reference must not return a partial image"); } #[test] @@ -376,7 +376,7 @@ fn inflated_developer_count_scans_the_next_chunk_as_an_entry_and_rejects_its_pix file.extend_from_slice(&0u32.to_le_bytes()); file.extend_from_slice(&directory.to_le_bytes()); file.extend_from_slice(SIGNATURE); - assert!(matches!(dec(&file), Err(_)), "a scanned entry's payload inside the pixels is refused"); + assert!(dec(&file).is_err(), "a scanned entry's payload inside the pixels is refused"); } #[test] @@ -389,7 +389,7 @@ fn legacy_pixels_that_end_in_signature_at_or_after_the_header_are_read_as_a_foot let mut file = header(3, width, 1, 8, 0x20); file.extend(vec![0; prefix]); file.extend_from_slice(SIGNATURE); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); } #[test] diff --git a/crates/tga/src/tests.rs b/crates/tga/src/tests.rs index 06109a179..76472075f 100644 --- a/crates/tga/src/tests.rs +++ b/crates/tga/src/tests.rs @@ -116,8 +116,8 @@ fn hostile_headers_fail_without_allocating() { fn the_pixel_limit_is_checked() { let mut f = header(3, 2, 2, 8, 0); f.extend_from_slice(&[0; 4]); - assert!(decode(&f, &Limits { max_pixels: 4 }).is_ok()); - assert_eq!(decode(&f, &Limits { max_pixels: 3 }), Err(Error::LimitExceeded("pixel count"))); + assert!(decode(&f, &Limits { max_pixels: 4, ..Limits::default() }).is_ok()); + assert_eq!(decode(&f, &Limits { max_pixels: 3, ..Limits::default() }), Err(Error::LimitExceeded("pixel count"))); } #[test] @@ -235,3 +235,32 @@ fn written_files_read_back() { assert_eq!(r.rgba, want, "{layout:?}"); } } + +#[test] +fn aggregate_decode_budget_applies_before_rle_expansion() { + let mut file = header(10, 16, 16, 32, 0x28); + file.extend_from_slice(&[255, 0, 0, 255, 255, 255, 0, 0, 255, 255]); + let mut limits = Limits::default(); + limits.max_decoded_bytes = 2047; + assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); + limits.max_decoded_bytes = 2048; + assert_eq!(decode(&file, &limits).unwrap().rgba.len(), 1024); +} +#[test] +fn default_budget_rejects_large_rle_header_without_large_fixture() { + let file = header(10, 16384, 16384, 32, 0x28); + assert_eq!(decode(&file, &Limits::default()), Err(Error::LimitExceeded("decode allocation bytes"))); +} +#[test] +fn aggregate_decode_budget_counts_raw_and_palette_storage() { + let mut file = header(1, 1, 1, 8, 0x20); + file[1] = 1; + file[5] = 1; + file[7] = 24; + file.extend_from_slice(&[0, 0, 255, 0]); + let mut limits = Limits::default(); + limits.max_decoded_bytes = 8; + assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); + limits.max_decoded_bytes = 9; + assert_eq!(decode(&file, &limits).unwrap().rgba, [255, 0, 0, 255]); +} From a33a81b1f19b348deab1ec5bc3942cb26095e692 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:15:49 -0500 Subject: [PATCH 023/471] test(tga): initialize custom decode limits directly --- crates/tga/src/tests.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/tga/src/tests.rs b/crates/tga/src/tests.rs index 76472075f..887c2915a 100644 --- a/crates/tga/src/tests.rs +++ b/crates/tga/src/tests.rs @@ -240,8 +240,7 @@ fn written_files_read_back() { fn aggregate_decode_budget_applies_before_rle_expansion() { let mut file = header(10, 16, 16, 32, 0x28); file.extend_from_slice(&[255, 0, 0, 255, 255, 255, 0, 0, 255, 255]); - let mut limits = Limits::default(); - limits.max_decoded_bytes = 2047; + let mut limits = Limits { max_decoded_bytes: 2047, ..Limits::default() }; assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); limits.max_decoded_bytes = 2048; assert_eq!(decode(&file, &limits).unwrap().rgba.len(), 1024); @@ -258,8 +257,7 @@ fn aggregate_decode_budget_counts_raw_and_palette_storage() { file[5] = 1; file[7] = 24; file.extend_from_slice(&[0, 0, 255, 0]); - let mut limits = Limits::default(); - limits.max_decoded_bytes = 8; + let mut limits = Limits { max_decoded_bytes: 8, ..Limits::default() }; assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); limits.max_decoded_bytes = 9; assert_eq!(decode(&file, &limits).unwrap().rgba, [255, 0, 0, 255]); From 7b51909d40deb15397ec90695dc611bde5d13e34 Mon Sep 17 00:00:00 2001 From: Francesco BV Date: Sun, 15 Nov 2026 10:28:08 -0500 Subject: [PATCH 024/471] shx: preserve CADCraft shape compiler and errors Original source by Francesco BV in cadcraft52c1e6de, copied from main7064a685. App font lookup, text layout and glyph interpretation are not moved. --- crates/shx/src/compile.rs | 313 ++++++++++++++++++++++++++++++++++++++ crates/shx/src/error.rs | 29 ++++ 2 files changed, 342 insertions(+) create mode 100644 crates/shx/src/compile.rs create mode 100644 crates/shx/src/error.rs diff --git a/crates/shx/src/compile.rs b/crates/shx/src/compile.rs new file mode 100644 index 000000000..2e8cb3c25 --- /dev/null +++ b/crates/shx/src/compile.rs @@ -0,0 +1,313 @@ +//! Shape source (`.shp`) to a compiled SHX file, as COMPILE does. +//! +//! A source is a list of definitions: a header line `*number,defbytes,NAME` followed by its +//! specification bytes (on as many lines as needed, ending with 0). Numbers are decimal, or +//! hexadecimal after a leading `0` (`012`, `08cfb`); parentheses only group; `;` starts a +//! comment. A big font starts with `*BIGFONT nchars,nranges,first,last,…` and a Unicode font +//! with `*UNIFONT,6,name` followed by `above,below,modes,encoding,type,0`. Shape 0 is a text +//! font's record (`above,below,modes,0`). +//! +//! Operands are encoded by what they are: a subshape number takes two bytes in a Unicode font +//! (and after `7,0` in a big font), displacements and bulges are two's complement, and an arc's +//! direction byte (`-043`) keeps its sign in the high bit. `defbytes` is not checked: the +//! program's own length is written. + +use super::ShxError; +use super::parse::{BIGFONT, SHAPES_10, UNIFONT}; + +/// Largest source compiled. +const MAX_SOURCE: usize = 8 << 20; + +#[derive(Clone, Copy, PartialEq)] +enum Kind { + Shapes, + Big, + Uni, +} + +struct Shape { + number: u16, + name: String, + program: Vec, +} + +/// A number token: value (sign included). +fn number(tok: &str, line: usize) -> Result { + let t: String = tok.chars().filter(|c| !matches!(c, '(' | ')') && !c.is_whitespace()).collect(); + let (neg, digits) = match t.strip_prefix('-') { + Some(d) => (true, d), + None => (false, t.strip_prefix('+').unwrap_or(&t)), + }; + let err = || ShxError::Source(line, format!("not a number: {tok:?}")); + let v = if digits.len() > 1 && digits.starts_with('0') { + i64::from_str_radix(digits.get(1..).unwrap_or_default(), 16).map_err(|_| err())? + } else { + digits.parse::().map_err(|_| err())? + }; + if v > 0xFFFF { + return Err(err()); + } + Ok(if neg { -v } else { v }) +} + +/// The comma-separated numbers of a line (comment removed, blank fields skipped). +fn numbers(text: &str, line: usize) -> Result, ShxError> { + text.split(',').map(str::trim).filter(|t| !t.is_empty() && *t != "(" && *t != ")").map(|t| number(t, line)).collect() +} + +/// The spec-byte encoder: walks the tokens as the reader walks the bytes. +struct Enc<'a> { + toks: &'a [i64], + i: usize, + out: Vec, + kind: Kind, + line: usize, +} + +impl Enc<'_> { + fn next(&mut self) -> Result { + let v = self.toks.get(self.i).copied().ok_or_else(|| ShxError::Source(self.line, "the shape ends inside a command".into()))?; + self.i += 1; + Ok(v) + } + fn byte(&mut self) -> Result<(), ShxError> { + let v = self.next()?; + let b = u8::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not a byte")))?; + self.out.push(b); + Ok(()) + } + /// Two's complement −128..127. + fn signed(&mut self) -> Result { + let v = self.next()?; + let b = i8::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not −128..127")))?; + self.out.extend(b.to_le_bytes()); + Ok(v) + } + /// Sign in the high bit (an arc's direction byte). + fn sign_magnitude(&mut self) -> Result<(), ShxError> { + let v = self.next()?; + let m = u8::try_from(v.unsigned_abs()) + .ok() + .filter(|m| *m < 0x80) + .ok_or_else(|| ShxError::Source(self.line, format!("{v} is not an octant byte")))?; + self.out.push(if v < 0 { m | 0x80 } else { m }); + Ok(()) + } + fn word(&mut self) -> Result<(), ShxError> { + let v = self.next()?; + let w = u16::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not a shape number")))?; + self.out.extend(w.to_be_bytes()); + Ok(()) + } + + fn program(mut self) -> Result, ShxError> { + while self.i < self.toks.len() { + let code = self.next()?; + let c = u8::try_from(code).map_err(|_| ShxError::Source(self.line, format!("{code} is not a code")))?; + self.out.push(c); + match c { + 0 => break, + 3 | 4 => self.byte()?, + 7 => match self.kind { + Kind::Uni => self.word()?, + Kind::Big => { + let first = self.toks.get(self.i).copied().unwrap_or(0); + if first == 0 { + self.byte()?; + self.word()?; + for _ in 0..4 { + self.byte()?; + } + } else { + self.byte()?; + } + } + Kind::Shapes => self.byte()?, + }, + 8 => { + self.signed()?; + self.signed()?; + } + 9 => loop { + let (x, y) = (self.signed()?, self.signed()?); + if x == 0 && y == 0 { + break; + } + }, + 10 => { + self.byte()?; + self.sign_magnitude()?; + } + 11 => { + for _ in 0..4 { + self.byte()?; + } + self.sign_magnitude()?; + } + 12 => { + for _ in 0..3 { + self.signed()?; + } + } + 13 => loop { + let (x, y) = (self.signed()?, self.signed()?); + if x == 0 && y == 0 { + break; + } + self.signed()?; + }, + _ => {} + } + } + if self.out.last() != Some(&0) { + self.out.push(0); + } + Ok(self.out) + } +} + +/// Compile shape source into an SHX file (a shape file or text font, a big font, or a Unicode +/// font, as its first definition says). +pub fn compile(source: &str) -> Result, ShxError> { + if source.len() > MAX_SOURCE { + return Err(ShxError::TooLarge); + } + let mut kind = Kind::Shapes; + let mut ranges: Vec<(u16, u16)> = Vec::new(); + let mut uni_info: Option<(String, Vec)> = None; + // Definitions: header (number, name, line) and the numbers that follow it. + let mut defs: Vec<(i64, String, usize, Vec)> = Vec::new(); + let mut uni_header_pending = false; + for (no, raw) in source.lines().enumerate() { + let line = no + 1; + let text = raw.split(';').next().unwrap_or_default().trim(); + if text.is_empty() { + continue; + } + if let Some(head) = text.strip_prefix('*') { + let upper = head.trim_start().to_ascii_uppercase(); + if let Some(rest) = upper.strip_prefix("BIGFONT") { + kind = Kind::Big; + let v = numbers(rest, line)?; + let n = usize::try_from(v.get(1).copied().unwrap_or(0)).unwrap_or(0); + for k in 0..n { + let lo = v.get(2 + 2 * k).copied().unwrap_or(0); + let hi = v.get(3 + 2 * k).copied().unwrap_or(lo); + let to = |x: i64| u16::try_from(x).map_err(|_| ShxError::Source(line, "bad escape range".into())); + ranges.push((to(lo)?, to(hi)?)); + } + continue; + } + if upper.starts_with("UNIFONT") { + kind = Kind::Uni; + let name = head.splitn(3, ',').nth(2).unwrap_or_default().trim().to_string(); + uni_info = Some((name, Vec::new())); + uni_header_pending = true; + continue; + } + uni_header_pending = false; + let mut parts = head.splitn(3, ','); + let num = number(parts.next().unwrap_or_default().trim(), line)?; + let _defbytes = parts.next(); + let name = parts.next().unwrap_or_default().trim().to_string(); + defs.push((num, name, line, Vec::new())); + continue; + } + let v = numbers(text, line)?; + if uni_header_pending { + if let Some((_, info)) = uni_info.as_mut() { + for x in v { + info.push(u8::try_from(x).map_err(|_| ShxError::Source(line, "bad font record".into()))?); + } + } + continue; + } + match defs.last_mut() { + Some(d) => d.3.extend(v), + None => return Err(ShxError::Source(line, "specification bytes before any *number,defbytes,NAME line".into())), + } + } + let mut shapes: Vec = Vec::new(); + for (num, name, line, toks) in defs { + let number = u16::try_from(num).map_err(|_| ShxError::Source(line, format!("shape number {num} out of range")))?; + let program = if number == 0 { + // The font record: plain bytes. + toks.iter().map(|x| u8::try_from(*x).map_err(|_| ShxError::Source(line, "bad font record".into()))).collect::, _>>()? + } else { + Enc { toks: &toks, i: 0, out: Vec::new(), kind, line }.program()? + }; + if shapes.iter().any(|s| s.number == number) { + return Err(ShxError::Source(line, format!("shape {number} is defined twice"))); + } + shapes.push(Shape { number, name, program }); + } + if shapes.is_empty() { + return Err(ShxError::Empty); + } + shapes.sort_by_key(|s| s.number); + let record = |s: &Shape| -> Result, ShxError> { + let mut r: Vec = s.name.bytes().filter(|b| *b != 0).collect(); + r.push(0); + r.extend(&s.program); + if r.len() > usize::from(u16::MAX) { Err(ShxError::TooLarge) } else { Ok(r) } + }; + let count = u16::try_from(shapes.len()).map_err(|_| ShxError::TooLarge)?; + let len16 = |r: &[u8]| u16::try_from(r.len()).unwrap_or(u16::MAX); + let mut out = Vec::new(); + match kind { + Kind::Shapes => { + out.extend(SHAPES_10); + out.extend(shapes.first().map_or(0, |s| s.number).to_le_bytes()); + out.extend(shapes.last().map_or(0, |s| s.number).to_le_bytes()); + out.extend(count.to_le_bytes()); + let recs: Vec> = shapes.iter().map(record).collect::>()?; + for (s, r) in shapes.iter().zip(&recs) { + out.extend(s.number.to_le_bytes()); + out.extend(len16(r).to_le_bytes()); + } + for r in &recs { + out.extend(r); + } + out.extend(b"EOF"); + } + Kind::Big => { + out.extend(BIGFONT); + let nranges = u16::try_from(ranges.len()).map_err(|_| ShxError::TooLarge)?; + out.extend(count.to_le_bytes()); + out.extend(count.to_le_bytes()); + out.extend(nranges.to_le_bytes()); + for (lo, hi) in &ranges { + out.extend(lo.to_le_bytes()); + out.extend(hi.to_le_bytes()); + } + let recs: Vec> = shapes.iter().map(record).collect::>()?; + let mut off = out.len() + 8 * recs.len(); + for (s, r) in shapes.iter().zip(&recs) { + out.extend(s.number.to_le_bytes()); + out.extend(len16(r).to_le_bytes()); + out.extend(u32::try_from(off).map_err(|_| ShxError::TooLarge)?.to_le_bytes()); + off += r.len(); + } + for r in &recs { + out.extend(r); + } + } + Kind::Uni => { + out.extend(UNIFONT); + let (name, info) = uni_info.unwrap_or_default(); + out.extend((u32::from(count) + 1).to_le_bytes()); + let mut rec: Vec = name.bytes().filter(|b| *b != 0).collect(); + rec.push(0); + rec.extend(&info); + out.extend(len16(&rec).to_le_bytes()); + out.extend(&rec); + for s in &shapes { + let r = record(s)?; + out.extend(s.number.to_le_bytes()); + out.extend(len16(&r).to_le_bytes()); + out.extend(r); + } + } + } + Ok(out) +} diff --git a/crates/shx/src/error.rs b/crates/shx/src/error.rs new file mode 100644 index 000000000..0addd1c98 --- /dev/null +++ b/crates/shx/src/error.rs @@ -0,0 +1,29 @@ +/// Why a font file can't be read or a shape source can't be compiled. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ShxError { + /// Not an SHX file (unknown signature). + NotShx, + /// The file ends inside its header or index. + Truncated, + /// The file is larger, or has more of something, than CADCraft reads. + TooLarge, + /// The file is well-formed but holds no shape. + Empty, + /// Shape source error: line and message. + Source(usize, String), +} + +impl std::fmt::Display for ShxError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ShxError::NotShx => f.write_str("not an SHX shape or font file"), + ShxError::Truncated => f.write_str("the SHX file is truncated"), + ShxError::TooLarge => f.write_str("the SHX file is too large"), + ShxError::Empty => f.write_str("the SHX file has no shapes"), + ShxError::Source(line, msg) => write!(f, "line {line}: {msg}"), + } + } +} + +impl std::error::Error for ShxError {} + From b3307f7814629961841b80ab907116252e41b34d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:30:48 -0500 Subject: [PATCH 025/471] shx: expose shared bounded shape compiler with hostile-source tests --- Cargo.lock | 4 ++++ Cargo.toml | 1 + crates/shx/Cargo.toml | 11 +++++++++++ crates/shx/README.md | 8 ++++++++ crates/shx/src/compile.rs | 20 +++++++++++++++++--- crates/shx/src/error.rs | 1 - crates/shx/src/lib.rs | 8 ++++++++ crates/shx/tests/compiler.rs | 30 ++++++++++++++++++++++++++++++ 8 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 crates/shx/Cargo.toml create mode 100644 crates/shx/README.md create mode 100644 crates/shx/src/lib.rs create mode 100644 crates/shx/tests/compiler.rs diff --git a/Cargo.lock b/Cargo.lock index add90f8c2..3e9aeba59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2610,6 +2610,10 @@ dependencies = [ name = "craft-sheet" version = "0.1.0" +[[package]] +name = "craft-shx" +version = "0.1.0" + [[package]] name = "craft-silhouette-studio" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index eae758e17..b68880781 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-shx = { path = "crates/shx" } craft-smime = { path = "crates/smime" } craft-ber = { path = "crates/ber" } craft-proto = { path = "crates/proto" } diff --git a/crates/shx/Cargo.toml b/crates/shx/Cargo.toml new file mode 100644 index 000000000..012f9d2f9 --- /dev/null +++ b/crates/shx/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "craft-shx" +description = "Bounded SHP shape-source compiler for SHX shape, big-font and Unicode containers" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true diff --git a/crates/shx/README.md b/crates/shx/README.md new file mode 100644 index 000000000..691890447 --- /dev/null +++ b/crates/shx/README.md @@ -0,0 +1,8 @@ +# craft-shx + +Compiles SHP shape source into SHX shapes, big-font and Unicode containers. +Extracted from CADCraft by Francesco BV (cadcraft52c1e6de); no font assets are bundled. +App font discovery, text layout and glyph interpretation are not part of this crate. +The original 8 MiB source and u16 record bounds remain; malformed double signs, +excess escape ranges and oversized Unicode font records are refused. Duplicate +shape detection is linear rather than a repeated whole-list scan. diff --git a/crates/shx/src/compile.rs b/crates/shx/src/compile.rs index 2e8cb3c25..0d91a9545 100644 --- a/crates/shx/src/compile.rs +++ b/crates/shx/src/compile.rs @@ -13,7 +13,9 @@ //! program's own length is written. use super::ShxError; -use super::parse::{BIGFONT, SHAPES_10, UNIFONT}; +const SHAPES_10: &[u8] = b"AutoCAD-86 shapes 1.0\r\n\x1a"; +const BIGFONT: &[u8] = b"AutoCAD-86 bigfont 1.0\r\n\x1a"; +const UNIFONT: &[u8] = b"AutoCAD-86 unifont 1.0\r\n\x1a"; /// Largest source compiled. const MAX_SOURCE: usize = 8 << 20; @@ -44,7 +46,7 @@ fn number(tok: &str, line: usize) -> Result { } else { digits.parse::().map_err(|_| err())? }; - if v > 0xFFFF { + if !(0..=0xFFFF).contains(&v) { return Err(err()); } Ok(if neg { -v } else { v }) @@ -190,6 +192,11 @@ pub fn compile(source: &str) -> Result, ShxError> { kind = Kind::Big; let v = numbers(rest, line)?; let n = usize::try_from(v.get(1).copied().unwrap_or(0)).unwrap_or(0); + // A big font has byte-valued lead ranges. Bound repeated headers too, + // so a tiny declaration cannot amplify into millions of ranges. + if n > 256 || ranges.len().saturating_add(n) > 256 { + return Err(ShxError::TooLarge); + } for k in 0..n { let lo = v.get(2 + 2 * k).copied().unwrap_or(0); let hi = v.get(3 + 2 * k).copied().unwrap_or(lo); @@ -210,6 +217,9 @@ pub fn compile(source: &str) -> Result, ShxError> { let num = number(parts.next().unwrap_or_default().trim(), line)?; let _defbytes = parts.next(); let name = parts.next().unwrap_or_default().trim().to_string(); + if defs.len() >= usize::from(u16::MAX) { + return Err(ShxError::TooLarge); + } defs.push((num, name, line, Vec::new())); continue; } @@ -228,6 +238,7 @@ pub fn compile(source: &str) -> Result, ShxError> { } } let mut shapes: Vec = Vec::new(); + let mut seen = std::collections::HashSet::new(); for (num, name, line, toks) in defs { let number = u16::try_from(num).map_err(|_| ShxError::Source(line, format!("shape number {num} out of range")))?; let program = if number == 0 { @@ -236,7 +247,7 @@ pub fn compile(source: &str) -> Result, ShxError> { } else { Enc { toks: &toks, i: 0, out: Vec::new(), kind, line }.program()? }; - if shapes.iter().any(|s| s.number == number) { + if !seen.insert(number) { return Err(ShxError::Source(line, format!("shape {number} is defined twice"))); } shapes.push(Shape { number, name, program }); @@ -296,6 +307,9 @@ pub fn compile(source: &str) -> Result, ShxError> { out.extend(UNIFONT); let (name, info) = uni_info.unwrap_or_default(); out.extend((u32::from(count) + 1).to_le_bytes()); + if name.len().saturating_add(1).saturating_add(info.len()) > usize::from(u16::MAX) { + return Err(ShxError::TooLarge); + } let mut rec: Vec = name.bytes().filter(|b| *b != 0).collect(); rec.push(0); rec.extend(&info); diff --git a/crates/shx/src/error.rs b/crates/shx/src/error.rs index 0addd1c98..135214f54 100644 --- a/crates/shx/src/error.rs +++ b/crates/shx/src/error.rs @@ -26,4 +26,3 @@ impl std::fmt::Display for ShxError { } impl std::error::Error for ShxError {} - diff --git a/crates/shx/src/lib.rs b/crates/shx/src/lib.rs new file mode 100644 index 000000000..e595e90f5 --- /dev/null +++ b/crates/shx/src/lib.rs @@ -0,0 +1,8 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] +//! SHP source compilation to SHX containers, extracted from CADCraft. +//! Font discovery, glyph execution and text layout remain consumer responsibilities. +mod compile; +mod error; +pub use compile::compile; +pub use error::ShxError; diff --git a/crates/shx/tests/compiler.rs b/crates/shx/tests/compiler.rs new file mode 100644 index 000000000..f558e185a --- /dev/null +++ b/crates/shx/tests/compiler.rs @@ -0,0 +1,30 @@ +use craft_shx::{ShxError, compile}; + +#[test] +fn fixed_shape_bytes() { + let b = compile("*65,2,A\n010,0\n").unwrap(); + let mut expected = b"AutoCAD-86 shapes 1.0\r\n\x1a".to_vec(); + expected.extend([65, 0, 65, 0, 1, 0, 65, 0, 4, 0, b'A', 0, 0x10, 0]); + expected.extend(b"EOF"); + assert_eq!(b, expected); +} +#[test] +fn hostile_sources_are_refused() { + for src in [ + "*1,2,X\n7\n", + "*1,2,X\n9,(1,1)\n", + "*99999,1,X\n0\n", + "*1,1,X\n300,0\n", + "010,0\n", + "*1,1,X\n1,0zz,0\n", + "*1,1,X\n0\n*1,1,Y\n0\n", + "*--9223372036854775808,1,X\n0", + "*BIGFONT 1,65535\n*1,1,X\n0", + ] { + assert!(compile(src).is_err(), "{src:?}"); + } + assert_eq!(compile(""), Err(ShxError::Empty)); + assert_eq!(compile(&" ".repeat((8 << 20) + 1)), Err(ShxError::TooLarge)); + let oversized_header = format!("*UNIFONT,6,{}\n10,2,0,0,0,0\n*65,1,A\n0", "n".repeat(65536)); + assert_eq!(compile(&oversized_header), Err(ShxError::TooLarge)); +} From a2541707240580153c8ea9ef3043ec0257468d0a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:40 +0000 Subject: [PATCH 026/471] Share WebP header inspection --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/webp_probe.rs | 47 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 crates/image-codecs/src/webp_probe.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..961a04327 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -31,6 +31,7 @@ pub mod orientation; pub mod resolution; pub mod source_settings; pub mod web; +pub mod webp_probe; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs new file mode 100644 index 000000000..8376274df --- /dev/null +++ b/crates/image-codecs/src/webp_probe.rs @@ -0,0 +1,47 @@ +//! Header-only WebP inspection with image-chunk completeness checks. + +use crate::{CodecError, Format}; + +/// Stored width, height and EXIF orientation without decoding pixels. The image payload must +/// fit in the input, but its compressed contents are not validated. Uses the default decode limits. +pub fn header(bytes: &[u8]) -> Result<(u32, u32, u16), CodecError> { + let err = |s: &str| CodecError::malformed(Format::WebP, s); + let mut p = 12usize; + let mut has_image = false; + let mut exif = None; + while let Some(h) = bytes.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size); + if matches!(&h[..4], b"VP8 " | b"VP8L" | b"ANMF") { + if end > bytes.len() { + return Err(err("truncated image data")); + } + has_image = true; + } else if &h[..4] == b"EXIF" && end <= bytes.len() && exif.is_none() { + exif = bytes.get(p + 8..end); + } + p = end.saturating_add(size & 1); + } + if !has_image { + return Err(err("no image data")); + } + let (w, h) = crate::probe_size(bytes)?; + let orientation = exif.map_or(1, crate::exif_orientation); + let (w, h) = if (5..=8).contains(&orientation) { (h, w) } else { (w, h) }; + Ok((w, h, orientation)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rejects_missing_and_truncated_image_chunks() { + let mut bytes = b"RIFF\x00\x00\x00\x00WEBP".to_vec(); + assert!(header(&bytes).is_err()); + bytes.extend_from_slice(b"VP8 "); + bytes.extend_from_slice(&10u32.to_le_bytes()); + bytes.push(0); + assert!(header(&bytes).is_err()); + } +} From 3c5007339754def577b3c5274f523d96384a35c4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:41 +0000 Subject: [PATCH 027/471] Share bounded SHX container reader --- crates/shx/src/lib.rs | 2 + crates/shx/src/parse.rs | 281 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 crates/shx/src/parse.rs diff --git a/crates/shx/src/lib.rs b/crates/shx/src/lib.rs index e595e90f5..ce330450c 100644 --- a/crates/shx/src/lib.rs +++ b/crates/shx/src/lib.rs @@ -4,5 +4,7 @@ //! Font discovery, glyph execution and text layout remain consumer responsibilities. mod compile; mod error; +pub mod parse; pub use compile::compile; pub use error::ShxError; +pub use parse::{ParsedFont, Record, ShxKind}; diff --git a/crates/shx/src/parse.rs b/crates/shx/src/parse.rs new file mode 100644 index 000000000..9cd4a35ad --- /dev/null +++ b/crates/shx/src/parse.rs @@ -0,0 +1,281 @@ +//! Reading the compiled SHX containers (layouts in the module documentation). Every count and +//! offset comes from the file, so each is checked against its length; records that run past +//! the end are dropped and the rest kept. + +use super::ShxError; +use std::collections::HashMap; + +/// The SHX container kind. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ShxKind { + Shapes, + BigFont, + Unifont, +} +/// Byte ranges of one shape name and pen program in [`ParsedFont::data`]. +#[derive(Clone, Copy, Debug)] +pub struct Record { + pub name: (usize, usize), + pub program: (usize, usize), +} +/// Decoded container records and metrics; glyph execution remains a caller concern. +pub struct ParsedFont { + pub kind: ShxKind, + pub name: String, + pub header: bool, + pub above: f64, + pub below: f64, + pub dual: bool, + pub encoding: u8, + pub char_width: f64, + pub ranges: Vec<(u16, u16)>, + pub data: Vec, + pub records: HashMap, +} + +pub const SHAPES_10: &[u8] = b"AutoCAD-86 shapes 1.0\r\n\x1a"; +pub const SHAPES_11: &[u8] = b"AutoCAD-86 shapes 1.1\r\n\x1a"; +pub const BIGFONT: &[u8] = b"AutoCAD-86 bigfont 1.0\r\n\x1a"; +pub const UNIFONT: &[u8] = b"AutoCAD-86 unifont 1.0\r\n\x1a"; + +/// Largest file read (the largest big fonts are a few megabytes). +pub const MAX_FILE_BYTES: usize = 32 << 20; +/// Most escape ranges in a big font (lead bytes: at most 256). +const MAX_RANGES: usize = 256; + +/// A checked little-endian reader. +struct Rd<'a> { + b: &'a [u8], + pos: usize, +} + +impl<'a> Rd<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8], ShxError> { + let end = self.pos.checked_add(n).ok_or(ShxError::Truncated)?; + let s = self.b.get(self.pos..end).ok_or(ShxError::Truncated)?; + self.pos = end; + Ok(s) + } + fn u16(&mut self) -> Result { + match self.take(2)? { + [a, b] => Ok(u16::from_le_bytes([*a, *b])), + _ => Err(ShxError::Truncated), + } + } + fn u32(&mut self) -> Result { + match self.take(4)? { + [a, b, c, d] => Ok(u32::from_le_bytes([*a, *b, *c, *d])), + _ => Err(ShxError::Truncated), + } + } +} + +/// A font being read. +struct Font { + kind: ShxKind, + name: String, + header: bool, + above: f64, + below: f64, + dual: bool, + encoding: u8, + char_width: f64, + ranges: Vec<(u16, u16)>, + records: HashMap, +} + +impl Font { + fn new(kind: ShxKind) -> Font { + Font { + kind, + name: String::new(), + header: false, + above: 0.0, + below: 0.0, + dual: false, + encoding: 0, + char_width: 0.0, + ranges: Vec::new(), + records: HashMap::new(), + } + } + + /// Add the record of shape `number` at `start..end` of `data`: a NUL-terminated name, then + /// the program. Shape 0 is the font record. A record without a name terminator is dropped; + /// the first record of a number wins. + fn record(&mut self, data: &[u8], number: u16, start: usize, end: usize) { + let Some(bytes) = data.get(start..end) else { return }; + let Some(nul) = bytes.iter().position(|b| *b == 0) else { return }; + let name = (start, start + nul); + let program = (start + nul + 1, end); + if number == 0 { + if !self.header { + self.font_record(data.get(program.0..program.1).unwrap_or_default(), data.get(name.0..name.1).unwrap_or_default()); + } + return; + } + self.records.entry(number).or_insert(Record { name, program }); + } + + /// The font record: above, below, modes (and for a unifont encoding and embedding); an + /// extended big font's is character height, 0, modes, character width. + fn font_record(&mut self, info: &[u8], name: &[u8]) { + self.header = true; + self.name = name.iter().take(255).map(|c| char::from(*c)).collect(); + let byte = |i: usize| info.get(i).copied().unwrap_or(0); + self.above = f64::from(byte(0)); + self.below = f64::from(byte(1)); + self.dual = byte(2) == 2; + if self.kind == ShxKind::Unifont { + self.encoding = byte(3); + } + if self.kind == ShxKind::BigFont && info.len() >= 5 && byte(1) == 0 { + // Extended: height, 0, modes, width, 0. + self.below = 0.0; + self.char_width = f64::from(byte(3)); + } + } + + fn finish(self, data: Vec) -> Result { + if self.records.is_empty() { + return Err(ShxError::Empty); + } + Ok(ParsedFont { + kind: self.kind, + name: self.name, + header: self.header, + above: self.above, + below: self.below, + dual: self.dual, + encoding: self.encoding, + char_width: if self.char_width > 0.0 { self.char_width } else { self.above }, + ranges: self.ranges, + data, + records: self.records, + }) + } +} + +pub fn parse(bytes: &[u8]) -> Result { + if bytes.len() > MAX_FILE_BYTES { + return Err(ShxError::TooLarge); + } + if bytes.starts_with(SHAPES_10) || bytes.starts_with(SHAPES_11) { + shapes(bytes) + } else if bytes.starts_with(BIGFONT) { + bigfont(bytes) + } else if bytes.starts_with(UNIFONT) { + unifont(bytes) + } else { + Err(ShxError::NotShx) + } +} + +fn shapes(bytes: &[u8]) -> Result { + let mut r = Rd { b: bytes, pos: SHAPES_10.len() }; + let _first = r.u16()?; + let _last = r.u16()?; + let count = usize::from(r.u16()?); + // The whole index must be there before anything is read from it. + let index = r.take(count.checked_mul(4).ok_or(ShxError::TooLarge)?)?; + let mut f = Font::new(ShxKind::Shapes); + let mut pos = r.pos; + for [a, b, c, d] in index.as_chunks::<4>().0 { + let (number, len) = (u16::from_le_bytes([*a, *b]), usize::from(u16::from_le_bytes([*c, *d]))); + let Some(end) = pos.checked_add(len).filter(|e| *e <= bytes.len()) else { break }; + f.record(bytes, number, pos, end); + pos = end; + } + f.finish(bytes.to_vec()) +} + +fn bigfont(bytes: &[u8]) -> Result { + let mut r = Rd { b: bytes, pos: BIGFONT.len() }; + let index_size = usize::from(r.u16()?); + let count = usize::from(r.u16()?); + let nranges = usize::from(r.u16()?); + if nranges > MAX_RANGES { + return Err(ShxError::TooLarge); + } + let mut f = Font::new(ShxKind::BigFont); + for _ in 0..nranges { + let (lo, hi) = (r.u16()?, r.u16()?); + f.ranges.push((lo.min(hi), lo.max(hi))); + } + // The index has `count` entries of 8 bytes; a file whose count doesn't fit but whose index + // size does is read by that. + let fits = |n: usize| n.checked_mul(8).and_then(|l| r.pos.checked_add(l)).is_some_and(|e| e <= bytes.len()); + let n = if fits(count) { + count + } else if fits(index_size) { + index_size + } else { + return Err(ShxError::Truncated); + }; + let index = r.take(n.checked_mul(8).ok_or(ShxError::TooLarge)?)?; + for [a, b, c, d, o0, o1, o2, o3] in index.as_chunks::<8>().0 { + let number = u16::from_le_bytes([*a, *b]); + let len = usize::from(u16::from_le_bytes([*c, *d])); + let Ok(off) = usize::try_from(u32::from_le_bytes([*o0, *o1, *o2, *o3])) else { continue }; + if len == 0 { + continue; + } + let Some(end) = off.checked_add(len).filter(|e| *e <= bytes.len()) else { continue }; + f.record(bytes, number, off, end); + } + f.finish(bytes.to_vec()) +} + +fn unifont(bytes: &[u8]) -> Result { + let mut r = Rd { b: bytes, pos: UNIFONT.len() }; + let count = r.u32()?; + let info_len = usize::from(r.u16()?); + let info = r.take(info_len)?; + let mut f = Font::new(ShxKind::Unifont); + let nul = info.iter().position(|b| *b == 0).unwrap_or(info.len()); + f.font_record(info.get(nul.saturating_add(1)..).unwrap_or_default(), info.get(..nul).unwrap_or_default()); + // `count` includes the font record; never trusted beyond the data that is there. + let mut left = count.saturating_sub(1); + while left > 0 { + left -= 1; + let Ok(number) = r.u16() else { break }; + let Ok(len) = r.u16().map(usize::from) else { break }; + let start = r.pos; + let Some(end) = start.checked_add(len).filter(|e| *e <= bytes.len()) else { break }; + r.pos = end; + f.record(bytes, number, start, end); + } + f.finish(bytes.to_vec()) +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + #[test] + fn reads_shapes_bigfont_and_unifont_records() { + for (source, kind, shape) in [ + ("*0,4,font\n6,2,2,0\n*65,4,A\n1,064,0\n", ShxKind::Shapes, 65), + ("*BIGFONT 2,1,081,09F\n*0,4,big\n8,0,0,0\n*082A0,4,square\n1,080,0\n", ShxKind::BigFont, 0x82a0), + ("*UNIFONT,6,uni\n6,2,0,0,0,0\n*00041,4,a\n1,064,0\n", ShxKind::Unifont, 65), + ] { + let bytes = crate::compile(source).unwrap(); + let font = parse(&bytes).unwrap(); + assert_eq!(font.kind, kind); + assert!(font.header); + let record = font.records.get(&shape).unwrap(); + assert_eq!(font.data.get(record.program.1 - 1), Some(&0)); + } + } + #[test] + fn truncation_limits_and_unknown_signatures_are_errors() { + assert!(matches!(parse(b"not a font"), Err(ShxError::NotShx))); + assert!(matches!(parse(&vec![0; MAX_FILE_BYTES + 1]), Err(ShxError::TooLarge))); + let mut shapes = SHAPES_10.to_vec(); + shapes.extend([1, 0, 255, 255, 255, 255]); + assert!(matches!(parse(&shapes), Err(ShxError::Truncated))); + let mut big = BIGFONT.to_vec(); + big.extend([255, 255, 255, 255, 255, 255]); + assert!(matches!(parse(&big), Err(ShxError::TooLarge))); + } +} From b919ac40f3873fd3893c7ad97e6351940714c366 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:48:46 -0500 Subject: [PATCH 028/471] test(tga): validate metadata fixture selector explicitly --- crates/tga/src/metadata_tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/tga/src/metadata_tests.rs b/crates/tga/src/metadata_tests.rs index 583f5d691..85c434bf4 100644 --- a/crates/tga/src/metadata_tests.rs +++ b/crates/tga/src/metadata_tests.rs @@ -163,6 +163,7 @@ fn complete_tga_footer_and_metadata_do_not_change_pixels() { } fn preceding_metadata(complete: bool, metadata: usize) -> Vec { + assert!(metadata <= 3, "test metadata selector must be 0..=3"); let mut file = header(2, if complete { 1 } else { 2 }, 1, 24, 0x20); file.extend_from_slice(&[0, 0, 255]); let payload_offset = file.len() as u32; @@ -180,7 +181,7 @@ fn preceding_metadata(complete: bool, metadata: usize) -> Vec { 1 => file.extend_from_slice(&[0; 2048]), 2 => file.extend_from_slice(&[1, 1, 0, 255, 0]), 3 => file.extend_from_slice(&18u32.to_le_bytes()), - _ => unreachable!(), + _ => {} } let extension_offset = file.len() as u32; let mut extension = [0; 495]; From 8a1bad8c2cc47f71c2a75fb19b67b54762fe8389 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:56 +0000 Subject: [PATCH 029/471] Cover WebP stored orientation --- crates/image-codecs/src/webp_probe.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs index 8376274df..d70fac38d 100644 --- a/crates/image-codecs/src/webp_probe.rs +++ b/crates/image-codecs/src/webp_probe.rs @@ -35,6 +35,14 @@ pub fn header(bytes: &[u8]) -> Result<(u32, u32, u16), CodecError> { mod tests { use super::*; + #[test] + fn preserves_stored_dimensions_with_exif_orientation() { + let mut image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + image.meta.exif = Some(b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00".to_vec()); + let bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); + } + #[test] fn rejects_missing_and_truncated_image_chunks() { let mut bytes = b"RIFF\x00\x00\x00\x00WEBP".to_vec(); From 82e258ab6eae4f4d6cd9dfe463a8640fd20c96d9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:50:14 +0000 Subject: [PATCH 030/471] Exercise camera orientation in WebP fixture --- crates/image-codecs/src/webp_probe.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs index d70fac38d..8e33a71cf 100644 --- a/crates/image-codecs/src/webp_probe.rs +++ b/crates/image-codecs/src/webp_probe.rs @@ -39,7 +39,11 @@ mod tests { fn preserves_stored_dimensions_with_exif_orientation() { let mut image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); image.meta.exif = Some(b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00".to_vec()); - let bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + let mut bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + // The writer correctly makes exported pixels upright; simulate a camera file carrying orientation 6. + let exif = bytes.windows(4).position(|v| v == b"EXIF").unwrap(); + let tiff = exif + 8; + bytes[tiff + 18] = 6; assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); } From 5ae0c89fdfca73c924db0521e2772e67c6cd49d3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:50:50 +0000 Subject: [PATCH 031/471] audio: share compatible PCM essence wrappers --- crates/audio/src/essence_pcm.rs | 147 ++++++++++++++++++++++++++++++++ crates/audio/src/lib.rs | 4 + 2 files changed, 151 insertions(+) create mode 100644 crates/audio/src/essence_pcm.rs diff --git a/crates/audio/src/essence_pcm.rs b/crates/audio/src/essence_pcm.rs new file mode 100644 index 000000000..593ebb1c2 --- /dev/null +++ b/crates/audio/src/essence_pcm.rs @@ -0,0 +1,147 @@ +//! Compatibility WAVE and AIFF wrappers for embedded PCM essence in interchange files. +//! PCM inside the model is interleaved little-endian signed integers (8-bit unsigned for WAVE). + +pub use crate::{pcm_aiff_header as aiff_header, pcm_wav_file as wav_file, pcm_wav_header as wav_header}; + +/// PCM, channels, sample rate and bits of a WAVE file (integer PCM only). +pub fn parse_wav(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { + if b.len() < 12 || &b[..4] != b"RIFF" || &b[8..12] != b"WAVE" { + return None; + } + let mut at = 12; + let mut fmt = None; + while at + 8 <= b.len() { + let id = &b[at..at + 4]; + let len = u32::from_le_bytes(b[at + 4..at + 8].try_into().ok()?) as usize; + let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())]; + if id == b"fmt " && body.len() >= 16 { + let tag = u16::from_le_bytes([body[0], body[1]]); + let ch = u16::from_le_bytes([body[2], body[3]]); + let sr = u32::from_le_bytes(body[4..8].try_into().ok()?); + let bits = u16::from_le_bytes([body[14], body[15]]); + if tag != 1 && tag != 0xFFFE { + return None; + } + if tag == 0xFFFE { + // The first two bytes of the sub-format GUID carry the real format code. + let sub = body.get(24..26)?; + if u16::from_le_bytes([sub[0], sub[1]]) != 1 { + return None; + } + } + fmt = Some((ch, sr, bits)); + } else if id == b"data" { + let (ch, sr, bits) = fmt?; + return Some((body.to_vec(), ch, sr, bits)); + } + at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?; + } + None +} + +/// A complete AIFF file (big-endian PCM) from little-endian PCM. +pub fn aiff_file(pcm: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Vec { + let bps = bits.div_ceil(8) as usize; + let frames = pcm.len() / (bps * channels.max(1) as usize).max(1); + let mut v = aiff_header(frames as u32, channels, sample_rate, bits); + for s in pcm.chunks_exact(bps.max(1)) { + if bps == 1 { + v.push(s[0] ^ 0x80); // WAVE 8-bit is unsigned, AIFF signed + } else { + v.extend(s.iter().rev()); + } + } + if (v.len() - 8) & 1 == 1 { + v.push(0); + } + v +} + +/// PCM (converted to little-endian), channels, sample rate and bits of an AIFF / AIFF-C (`NONE` +/// / `twos` / `sowt`) file. +pub fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { + if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") { + return None; + } + let mut at = 12; + let mut comm = None; + while at + 8 <= b.len() { + let id = &b[at..at + 4]; + let len = u32::from_be_bytes(b[at + 4..at + 8].try_into().ok()?) as usize; + let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())]; + if id == b"COMM" && body.len() >= 18 { + let ch = u16::from_be_bytes([body[0], body[1]]); + let bits = u16::from_be_bytes([body[6], body[7]]); + let sr = from_extended(body[8..18].try_into().ok()?).round() as u32; + let little = body.len() >= 22 && &body[18..22] == b"sowt"; + comm = Some((ch, sr, bits, little)); + } else if id == b"SSND" && body.len() >= 8 { + let (ch, sr, bits, little) = comm?; + let off = u32::from_be_bytes(body[..4].try_into().ok()?) as usize; + let data = body.get(8usize.checked_add(off)?..)?; + let bps = bits.div_ceil(8) as usize; + let mut pcm = Vec::with_capacity(data.len()); + for s in data.chunks_exact(bps.max(1)) { + if bps == 1 { + pcm.push(s[0] ^ 0x80); + } else if little { + pcm.extend_from_slice(s); + } else { + pcm.extend(s.iter().rev()); + } + } + return Some((pcm, ch, sr, bits)); + } + at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?; + } + None +} + +fn from_extended(b: [u8; 10]) -> f64 { + let e = (u16::from_be_bytes([b[0], b[1]]) & 0x7FFF) as i32; + let m = u64::from_be_bytes(b[2..10].try_into().unwrap_or([0; 8])); + if e == 0 && m == 0 { + return 0.0; + } + m as f64 / (1u64 << 63) as f64 * 2f64.powi(e - 16383) +} + +#[cfg(test)] +mod tests { + /// A mutated EDL whose clip ran for days produced an OMF descriptor for more than 4 GiB of + /// PCM: the RIFF size field overflowed ("attempt to add with overflow"). + #[test] + fn huge_wav_header_saturates() { + let h = super::wav_header(u32::MAX, 2, u32::MAX, 24); + assert_eq!(&h[4..8], &u32::MAX.to_le_bytes()); + } + + use super::*; + + #[test] + fn accepts_truncated_essence_summaries() { + let mut wav = wav_file(&[1, 2, 3, 4], 1, 48_000, 16); + wav.truncate(46); + assert_eq!(parse_wav(&wav), Some((vec![1, 2], 1, 48_000, 16))); + let mut aiff = aiff_file(&[1, 2, 3, 4], 1, 48_000, 16); + aiff.truncate(56); + assert_eq!(parse_aiff(&aiff), Some((vec![1, 2], 1, 48_000, 16))); + } + + #[test] + fn wav_and_aiff_round_trip() { + let pcm: Vec = (0..48u8).collect(); // 8 stereo 24-bit frames + let w = wav_file(&pcm, 2, 48_000, 24); + assert_eq!(parse_wav(&w), Some((pcm.clone(), 2, 48_000, 24))); + let a = aiff_file(&pcm, 2, 44_100, 24); + assert_eq!(parse_aiff(&a), Some((pcm.clone(), 2, 44_100, 24))); + let p16: Vec = (0..32u8).collect(); + assert_eq!(parse_aiff(&aiff_file(&p16, 1, 96_000, 16)), Some((p16, 1, 96_000, 16))); + assert!(parse_wav(b"RIFF").is_none()); + assert!(parse_aiff(&a[..30]).is_none()); + for cut in 0..w.len() { + let _ = parse_wav(&w[..cut]); + let _ = parse_aiff(&a[..cut.min(a.len())]); + } + } +} diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..7bbfbbcf5 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -66,6 +66,10 @@ mod wav; feature = "vorbis-encode" ))] mod write; + +/// PCM WAVE and AIFF wrappers for interchange essence. +#[cfg(feature = "pcm-encode")] +pub mod essence_pcm; #[cfg(feature = "vorbis-encode")] pub use write::{OGG_BITRATES, encode_ogg}; From 2f2d1d84e3a36b3f93e81507aeff3f0da97a7162 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:51:27 +0000 Subject: [PATCH 032/471] Share bounded SHX glyph program interpreter --- crates/shx/src/lib.rs | 1 + crates/shx/src/program.rs | 443 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 444 insertions(+) create mode 100644 crates/shx/src/program.rs diff --git a/crates/shx/src/lib.rs b/crates/shx/src/lib.rs index ce330450c..e330e59fd 100644 --- a/crates/shx/src/lib.rs +++ b/crates/shx/src/lib.rs @@ -5,6 +5,7 @@ mod compile; mod error; pub mod parse; +pub mod program; pub use compile::compile; pub use error::ShxError; pub use parse::{ParsedFont, Record, ShxKind}; diff --git a/crates/shx/src/program.rs b/crates/shx/src/program.rs new file mode 100644 index 000000000..9115d1c6d --- /dev/null +++ b/crates/shx/src/program.rs @@ -0,0 +1,443 @@ +//! The shape pen program: vector length/direction bytes and special codes 0–14, run into +//! polylines. Bounded everywhere: subshape depth, location stack, steps, points, coordinates. +//! +//! - A byte with a non-zero high nibble is a vector: length (high nibble) in one of 16 +//! directions (low nibble, counter-clockwise from east in 22.5° steps; the off-axis ones +//! are half-unit or unit steps on the minor axis). +//! - 0 end, 1 pen down, 2 pen up, 3/4 divide/multiply vector lengths by the next byte, +//! 5/6 push/pop the location, 7 subshape (one byte; two bytes high first in a Unicode font; +//! `0`, a two-byte number, base point, width and height in an extended big font), +//! 8 one X-Y displacement, 9 displacements up to (0,0), 10 octant arc (radius, +//! ±0SC: sign = clockwise, S start octant, C octants, 0 = 8), 11 fractional arc (start +//! offset, end offset, high and low radius, ±0SC; offsets in 1/256 of 45° from the octant +//! boundaries), 12 bulge arc (dx, dy, bulge: ±127 = semicircle), 13 bulge arcs up to (0,0), +//! 14 the next command is for vertical text only (dual-orientation fonts). +//! - Displacements and bulges are two's complement bytes; the arc direction byte keeps its +//! sign in the high bit. + +use std::f64::consts::{FRAC_PI_4, TAU}; + +use super::ShxKind; +use std::ops::{Add, Mul, Neg, Sub}; + +/// A point in SHX font units. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Point { + pub x: f64, + pub y: f64, +} +impl Point { + pub const ZERO: Self = Self { x: 0.0, y: 0.0 }; + pub const fn new(x: f64, y: f64) -> Self { + Self { x, y } + } + fn is_finite(self) -> bool { + self.x.is_finite() && self.y.is_finite() + } + fn len(self) -> f64 { + self.x.hypot(self.y) + } + fn len2(self) -> f64 { + self.x * self.x + self.y * self.y + } +} +impl Add for Point { + type Output = Self; + fn add(self, rhs: Self) -> Self { + Self::new(self.x + rhs.x, self.y + rhs.y) + } +} +impl Sub for Point { + type Output = Self; + fn sub(self, rhs: Self) -> Self { + Self::new(self.x - rhs.x, self.y - rhs.y) + } +} +impl Mul for Point { + type Output = Self; + fn mul(self, rhs: f64) -> Self { + Self::new(self.x * rhs, self.y * rhs) + } +} +impl Neg for Point { + type Output = Self; + fn neg(self) -> Self { + Self::new(-self.x, -self.y) + } +} + +/// Font records and metrics the bounded shape interpreter needs. +pub trait Font { + fn kind(&self) -> ShxKind; + fn dual(&self) -> bool; + fn above(&self) -> f64; + fn char_width(&self) -> f64; + fn program(&self, number: u16) -> Option<&[u8]>; +} +/// Pen-down strokes and advance in font units. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Glyph { + pub strokes: Vec>, + pub advance: Point, +} + +/// Subshape nesting (a cycle ends here). +const MAX_DEPTH: usize = 16; +/// Location stack (the format promises four). +const MAX_STACK: usize = 64; +/// Bytes run per glyph, subshapes included. +const MAX_STEPS: usize = 200_000; +/// Points per glyph. +const MAX_POINTS: usize = 50_000; +/// Coordinates in font units. +const MAX_COORD: f64 = 1.0e7; +/// Arc segments per full turn. +const ARC_STEPS: f64 = 64.0; + +/// Why a program stops early: the glyph is then not drawn. +#[derive(Debug)] +pub struct Broken; + +type Res = Result<(), Broken>; + +struct Pen<'a> { + font: &'a dyn Font, + vertical: bool, + pos: Point, + down: bool, + /// Vector length factor (codes 3 and 4). + scale: f64, + /// Per-axis factor of an extended big font subshape. + axes: Point, + stack: Vec, + strokes: Vec>, + active: Vec, + steps: usize, + points: usize, +} + +pub fn outline(font: &dyn Font, number: u16, vertical: bool) -> Result { + let mut pen = Pen { + font, + vertical, + pos: Point::ZERO, + down: true, + scale: 1.0, + axes: Point::new(1.0, 1.0), + stack: Vec::new(), + strokes: Vec::new(), + active: Vec::new(), + steps: 0, + points: 0, + }; + pen.run(number, 0)?; + pen.flush(); + Ok(Glyph { strokes: pen.strokes, advance: pen.pos }) +} + +/// The 16 vector directions (unit length on the major axis). +fn direction(d: u8) -> Point { + let (x, y) = match d & 15 { + 0 => (1.0, 0.0), + 1 => (1.0, 0.5), + 2 => (1.0, 1.0), + 3 => (0.5, 1.0), + 4 => (0.0, 1.0), + 5 => (-0.5, 1.0), + 6 => (-1.0, 1.0), + 7 => (-1.0, 0.5), + 8 => (-1.0, 0.0), + 9 => (-1.0, -0.5), + 10 => (-1.0, -1.0), + 11 => (-0.5, -1.0), + 12 => (0.0, -1.0), + 13 => (0.5, -1.0), + 14 => (1.0, -1.0), + _ => (1.0, -0.5), + }; + Point::new(x, y) +} + +/// A two's complement operand byte. +fn signed(b: u8) -> f64 { + f64::from(i8::from_le_bytes([b])) +} + +/// A program being read: its bytes and position. +struct Cur<'p> { + b: &'p [u8], + pos: usize, +} + +impl Cur<'_> { + fn byte(&mut self) -> Result { + let v = self.b.get(self.pos).copied().ok_or(Broken)?; + self.pos += 1; + Ok(v) + } +} + +impl Pen<'_> { + fn step(&mut self) -> Res { + self.steps += 1; + if self.steps > MAX_STEPS { Err(Broken) } else { Ok(()) } + } + + fn flush(&mut self) { + if self.active.len() >= 2 { + self.strokes.push(std::mem::take(&mut self.active)); + } else { + self.active.clear(); + } + } + + /// Move the pen by `d` font units (already scaled), drawing when the pen is down. + fn to(&mut self, target: Point) -> Res { + if !target.is_finite() || target.x.abs().max(target.y.abs()) > MAX_COORD { + return Err(Broken); + } + if self.down { + if self.active.is_empty() { + self.active.push(self.pos); + self.points += 1; + } + self.active.push(target); + self.points += 1; + if self.points > MAX_POINTS { + return Err(Broken); + } + } else { + self.flush(); + } + self.pos = target; + Ok(()) + } + + /// A displacement in vector units (times the length factor and the subshape axes). + fn by(&mut self, d: Point) -> Res { + let k = d * self.scale; + self.to(self.pos + Point::new(k.x * self.axes.x, k.y * self.axes.y)) + } + + /// An arc of `radius` vector units from the pen, starting at angle `start` on its circle + /// and turning `sweep` radians (positive counter-clockwise). + fn arc(&mut self, radius: f64, start: f64, sweep: f64) -> Res { + let r = radius * self.scale; + if !(r.is_finite() && r > 0.0 && sweep.is_finite()) { + return Ok(()); + } + let n = if self.down { (sweep.abs() / TAU * ARC_STEPS).ceil().clamp(1.0, ARC_STEPS) as usize } else { 1 }; + let from = self.pos; + let s0 = Point::new(start.cos(), start.sin()); + for i in 1..=n { + let a = start + sweep * i as f64 / n as f64; + let d = (Point::new(a.cos(), a.sin()) - s0) * r; + self.to(from + Point::new(d.x * self.axes.x, d.y * self.axes.y))?; + } + Ok(()) + } + + /// An arc to the displacement `d` with `bulge` (−127..127, ±127 a semicircle). + fn bulge(&mut self, d: Point, bulge: f64) -> Res { + let b = bulge.clamp(-127.0, 127.0) / 127.0; + if b == 0.0 || !self.down || d.len2() == 0.0 { + return self.by(d); + } + // Centre from the chord midpoint along its left normal; sweep 4·atan(b). + let centre = d * 0.5 + Point::new(-d.y, d.x) * ((1.0 - b * b) / (4.0 * b)); + let rel = -centre; + self.arc(rel.len(), rel.y.atan2(rel.x), 4.0 * b.atan()) + } + + /// The octant byte `±0SC`: (clockwise, start octant, octants with 0 = 8). + fn octants(spec: u8) -> (f64, f64, f64) { + let sign = if spec & 0x80 != 0 { -1.0 } else { 1.0 }; + let s = f64::from((spec >> 4) & 7); + let c = match spec & 7 { + 0 => 8.0, + c => f64::from(c), + }; + (sign, s, c) + } + + /// The vector unit size of an extended big font's subshape cell. + fn cell(&self) -> Point { + let h = if self.font.above() > 0.0 { self.font.above() } else { 1.0 }; + let w = if self.font.char_width() > 0.0 { self.font.char_width() } else { h }; + Point::new(w, h) + } + + fn run(&mut self, number: u16, depth: usize) -> Res { + if depth >= MAX_DEPTH { + return Err(Broken); + } + let program = self.font.program(number).ok_or(Broken)?; + let mut r = Cur { b: program, pos: 0 }; + let mut skip = false; + while r.pos < r.b.len() { + self.step()?; + let code = r.byte()?; + // Code 14: in a dual-orientation font the next command is for vertical text only. + let exec = !skip; + skip = false; + match code { + 0 => return Ok(()), + 1 if exec => self.down = true, + 2 if exec => { + self.down = false; + self.flush(); + } + 3 | 4 => { + let k = f64::from(r.byte()?); + if exec && k > 0.0 { + let s = if code == 3 { self.scale / k } else { self.scale * k }; + if !(1e-9..=MAX_COORD).contains(&s) { + return Err(Broken); + } + self.scale = s; + } + } + 5 if exec => { + if self.stack.len() >= MAX_STACK { + return Err(Broken); + } + self.stack.push(self.pos); + } + 6 if exec => { + self.flush(); + if let Some(p) = self.stack.pop() { + self.pos = p; + } + } + 7 => self.subshape(&mut r, exec, depth)?, + 8 | 9 | 12 | 13 => loop { + self.step()?; + let d = Point::new(signed(r.byte()?), signed(r.byte()?)); + if matches!(code, 9 | 13) && d == Point::ZERO { + break; + } + let bulge = if matches!(code, 12 | 13) { signed(r.byte()?) } else { 0.0 }; + if exec { + self.bulge(d, bulge)?; + } + if matches!(code, 8 | 12) { + break; + } + }, + 10 => { + let radius = f64::from(r.byte()?); + let (sign, s, c) = Self::octants(r.byte()?); + if exec { + self.arc(radius, s * FRAC_PI_4, sign * c * FRAC_PI_4)?; + } + } + 11 => { + let start = f64::from(r.byte()?) / 256.0; + let end = match r.byte()? { + 0 => 1.0, + e => f64::from(e) / 256.0, + }; + let radius = f64::from(u16::from_be_bytes([r.byte()?, r.byte()?])); + let (sign, s, c) = Self::octants(r.byte()?); + if exec { + // From `start` past the first octant boundary to `end` past the last + // octant's. + self.arc(radius, (s + sign * start) * FRAC_PI_4, sign * (c - 1.0 + end - start) * FRAC_PI_4)?; + } + } + 14 => skip = self.font.dual() && !self.vertical && exec, + 1..=15 => {} + v => { + if exec { + self.by(direction(v) * f64::from(v >> 4))?; + } + } + } + } + Ok(()) + } + + /// Code 7 and its operands. + fn subshape(&mut self, r: &mut Cur, exec: bool, depth: usize) -> Res { + match self.font.kind() { + ShxKind::Unifont => { + let n = u16::from_be_bytes([r.byte()?, r.byte()?]); + if exec { + self.run(n, depth + 1)?; + } + } + ShxKind::BigFont => { + let first = r.byte()?; + if first != 0 { + if exec { + self.run(u16::from(first), depth + 1)?; + } + return Ok(()); + } + // Extended: the primitive drawn in the box (x, y, width, height) of the cell. + let n = u16::from_be_bytes([r.byte()?, r.byte()?]); + let (x, y, w, h) = (f64::from(r.byte()?), f64::from(r.byte()?), f64::from(r.byte()?), f64::from(r.byte()?)); + if !exec { + return Ok(()); + } + let (pos, axes, down) = (self.pos, self.axes, self.down); + let cell = self.cell(); + self.flush(); + self.pos = pos + Point::new(x * axes.x, y * axes.y) * self.scale; + self.axes = Point::new(axes.x * w / cell.x, axes.y * h / cell.y); + let res = self.run(n, depth + 1); + self.flush(); + self.axes = axes; + self.pos = pos; + self.down = down; + res?; + } + ShxKind::Shapes => { + let n = u16::from(r.byte()?); + if exec { + self.run(n, depth + 1)?; + } + } + } + Ok(()) + } +} + +impl Font for crate::ParsedFont { + fn kind(&self) -> ShxKind { + self.kind + } + fn dual(&self) -> bool { + self.dual + } + fn above(&self) -> f64 { + self.above + } + fn char_width(&self) -> f64 { + self.char_width + } + fn program(&self, number: u16) -> Option<&[u8]> { + let r = self.records.get(&number)?; + self.data.get(r.program.0..r.program.1) + } +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + #[test] + fn executes_shape_vectors_without_app_geometry_types() { + let bytes = crate::compile("*1,3,A\n1,010,0\n").unwrap(); + let font = crate::parse::parse(&bytes).unwrap(); + let glyph = outline(&font, 1, false).unwrap(); + assert_eq!(glyph.strokes, vec![vec![Point::new(0.0, 0.0), Point::new(1.0, 0.0)]]); + assert_eq!(glyph.advance, Point::new(1.0, 0.0)); + } + #[test] + fn recursive_shapes_stop_at_the_depth_bound() { + let bytes = crate::compile("*1,3,A\n7,1,0\n").unwrap(); + let font = crate::parse::parse(&bytes).unwrap(); + assert!(outline(&font, 1, false).is_err()); + } +} From 5a36112af6567a60491c47361a9fdd44ad362979 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 16:51:49 +0100 Subject: [PATCH 033/471] craft-image-codecs: SGI rows may share compressed data and must start after the offset tables Addresses review findings: the RLE size check no longer demands separate payload space for every row, and a row offset inside the offset tables is refused. A row without its trailing zero count still reads, as in ffmpeg. --- crates/image-codecs/src/codecs/sgi.rs | 56 +++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/crates/image-codecs/src/codecs/sgi.rs b/crates/image-codecs/src/codecs/sgi.rs index 1e31b5f0a..64c80299c 100644 --- a/crates/image-codecs/src/codecs/sgi.rs +++ b/crates/image-codecs/src/codecs/sgi.rs @@ -120,8 +120,9 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { // Reject inputs that cannot hold the declared pixels before allocating anything for them. let table = if h.rle { rows.checked_mul(8).ok_or_else(|| bad("image size overflow"))? } else { 0 }; let min_len = if h.rle { - // Every row needs at least a packet and a terminator. - HEADER + table + rows * bps + // Both offset tables must fit; rows may share compressed data, so the data itself is + // checked row by row against the offsets below. + HEADER + table } else { HEADER + total.checked_mul(bps).ok_or_else(|| bad("image size overflow"))? }; @@ -138,7 +139,10 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { if h.rle { let start = be32(b, HEADER + index * 4) as usize; let len = be32(b, HEADER + rows * 4 + index * 4) as usize; - let end = start.checked_add(len).filter(|&e| e <= b.len() && start >= HEADER).ok_or_else(|| bad("RLE row lies outside the file"))?; + let end = start + .checked_add(len) + .filter(|&e| e <= b.len() && start >= HEADER + table) + .ok_or_else(|| bad("RLE row lies outside the file"))?; read_rle_row(&b[start..end], h.wide, &mut row)?; } else { let start = HEADER + index * w * bps; @@ -335,6 +339,52 @@ mod tests { assert!(decode(&huge, &Limits::default()).is_err()); } + /// A 1-plane, 8-bit RLE file whose `rows` all start where `starts[i]` says, each `len` bytes long. + fn rle_file(w: u16, starts: &[u32], len: u32, payload: &[u8]) -> Vec { + let mut f = vec![0u8; HEADER]; + f[0..2].copy_from_slice(&MAGIC.to_be_bytes()); + f[2] = 1; + f[3] = 1; + f[4..6].copy_from_slice(&2u16.to_be_bytes()); + f[6..8].copy_from_slice(&w.to_be_bytes()); + f[8..10].copy_from_slice(&(starts.len() as u16).to_be_bytes()); + f[10..12].copy_from_slice(&1u16.to_be_bytes()); + for s in starts { + f.extend_from_slice(&s.to_be_bytes()); + } + for _ in starts { + f.extend_from_slice(&len.to_be_bytes()); + } + f.extend_from_slice(payload); + f + } + + /// The format lets table entries share one compressed row: eight rows, one payload. + #[test] + fn rows_may_share_compressed_data() { + let table = HEADER + 8 * 8; + let f = rle_file(1, &[table as u32; 8], 3, &[0x81, 255, 0]); + assert_eq!(f.len(), table + 3); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.dimensions(), img.data()), ((1, 8), &[255u8; 8][..])); + } + + /// Compressed rows start after both tables: an offset into a table is refused. + #[test] + fn rows_may_not_start_inside_the_offset_tables() { + // 3x1: the tables end at 520; 515 reads the table's own bytes as a packet list. + let f = rle_file(3, &[515], 3, &[0, 0, 0, 0, 0]); + assert!(decode(&f, &Limits::default()).is_err()); + } + + /// FFmpeg's decoder, run as an oracle, also reads a row that ends with its last packet and no + /// zero terminator; so does this one. + #[test] + fn a_row_without_its_terminator_still_reads() { + let f = rle_file(1, &[520], 2, &[0x81, 42]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[42]); + } + #[test] fn palette_mapped_files_are_unsupported() { let img = gradient(4, 4, ChannelLayout::Gray); From e9eaaf5641640b4e2735b0af79ef589c5a74a88a Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 16:52:35 +0100 Subject: [PATCH 034/471] craft-image-codecs: Sun Raster 1-bit colour maps, opaque standard 32-bit pixels, fallible output buffer Addresses review findings: a 1-bit file with an RGB colour map reads as colour; the unused first byte of standard 32-bit pixels is no longer read as alpha; the writer's buffer is reserved with try_reserve_exact. README lists the native layouts correctly. --- crates/image-codecs/README.md | 2 +- crates/image-codecs/src/codecs/sunraster.rs | 52 +++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index f9f5f53e6..a37760d80 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,7 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | -| Sun Raster (`.ras`) | yes (incl. RLE and colour maps) | yes (uncompressed) | U8 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | +| Sun Raster (`.ras`) | yes (incl. RLE and colour maps) | yes (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs/sunraster.rs b/crates/image-codecs/src/codecs/sunraster.rs index 84d60f145..85c24acc5 100644 --- a/crates/image-codecs/src/codecs/sunraster.rs +++ b/crates/image-codecs/src/codecs/sunraster.rs @@ -97,9 +97,18 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { for x in 0..wu { match depth { 1 => { - // A set bit is black. let bit = (row[x / 8] >> (7 - x % 8)) & 1; - out.push(if bit == 1 { 0 } else { 255 }); + if map_type == 1 { + // The bit is an index into the (two-entry or longer) colour map. + let i = usize::from(bit); + if i >= entries { + return Err(bad("pixel index is outside the colour map")); + } + out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]); + } else { + // A set bit is black. + out.push(if bit == 1 { 0 } else { 255 }); + } } 8 if map_type == 1 => { let i = usize::from(row[x]); @@ -115,7 +124,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { } _ => { let p = &row[4 * x..4 * x + 4]; - out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], p[0]] }); + // In the standard types the first byte (X in XBGR) is unused, not alpha. + out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], 255] }); } } } @@ -123,6 +133,13 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { Image::from_u8(w, h, layout, out) } +/// A buffer of `cap` bytes, or an error instead of an abort when the allocation cannot be made. +fn reserve(cap: usize) -> std::result::Result, std::collections::TryReserveError> { + let mut v = Vec::new(); + v.try_reserve_exact(cap)?; + Ok(v) +} + /// Writes an uncompressed rasterfile: 8-bit gray, 24-bit RGB or 32-bit RGBA, in the RGB-ordered type. pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { let enc = |msg: String| CodecError::encode(F, msg); @@ -145,7 +162,8 @@ pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { if data.len() != wu * hu * bytes { return Err(enc("the image has the wrong number of samples".into())); } - let mut out = Vec::with_capacity(HEADER + len as usize); + let mut out = reserve(HEADER.checked_add(len as usize).ok_or_else(|| enc("image too large".into()))?) + .map_err(|_| enc("the image is too large to encode".into()))?; for v in [MAGIC, w, h, depth as u32, len, 3, 0, 0] { out.extend_from_slice(&v.to_be_bytes()); } @@ -218,6 +236,32 @@ mod tests { assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0, 255, 0]); } + #[test] + fn one_bit_files_with_a_colour_map_read_as_colour() { + // Two entries: red plane, green plane, blue plane; bit 0 is entry 0. + let map = [10, 20, 30, 40, 50, 60]; + let f = file(1, 1, &map, 1, 3, 1, &[0b0100_0000, 0]); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10, 30, 50, 20, 40, 60, 10, 30, 50][..])); + // A one-entry map has no entry for a set bit. + assert!(decode(&file(1, 1, &[1, 2, 3], 1, 1, 1, &[0b1000_0000, 0]), &Limits::default()).is_err()); + } + + #[test] + fn the_unused_first_byte_of_standard_32_bit_pixels_is_not_alpha() { + let img = decode(&file(32, 1, &[], 0, 1, 1, &[0, 30, 20, 10]), &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgba, &[10, 20, 30, 255][..])); + // In the RGB-ordered type the first byte is the alpha the writer stores there. + let img = decode(&file(32, 3, &[], 0, 1, 1, &[7, 10, 20, 30]), &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20, 30, 7]); + } + + #[test] + fn an_output_buffer_too_large_to_allocate_is_an_error() { + assert!(reserve(usize::MAX).is_err()); + assert!(reserve(16).is_ok()); + } + #[test] fn hostile_input_is_an_error_not_a_panic() { let img = pattern(9, 5, ChannelLayout::Rgb); From 8ce5335e8fc93c4e9546286390a0b0d35897585a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:53:30 +0000 Subject: [PATCH 035/471] aaf: preserve bounded FilmCraft OMF traversal --- crates/aaf/src/lib.rs | 4 + crates/aaf/src/omf/read.rs | 255 +++++++++++++++++++++++++++++++++++-- crates/aaf/src/pcm.rs | 5 - 3 files changed, 248 insertions(+), 16 deletions(-) diff --git a/crates/aaf/src/lib.rs b/crates/aaf/src/lib.rs index ee7448bc0..4968c5b39 100644 --- a/crates/aaf/src/lib.rs +++ b/crates/aaf/src/lib.rs @@ -29,7 +29,11 @@ mod roundtrip_tests; #[cfg(test)] mod testdoc; +/// Diagnostic types used by readers and writers. +pub use craft_timeline::report; pub use craft_timeline::report::Report; +/// Exact time types used by the composition model. +pub use craft_timeline::time; /// Error from a reader or writer. Unsupported *features* never error; they go in the [`Report`]. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] diff --git a/crates/aaf/src/omf/read.rs b/crates/aaf/src/omf/read.rs index d45b6ff63..b9c3bd9d1 100644 --- a/crates/aaf/src/omf/read.rs +++ b/crates/aaf/src/omf/read.rs @@ -12,6 +12,10 @@ type Uid = [u8; 12]; const MAX_REFERENCE_VISITS: usize = 65_536; +/// A source-clip search stopped at its work limit. +#[derive(Clone, Copy)] +struct SearchLimit; + struct R<'a, 'b> { c: &'b Container<'a>, report: &'b mut Report, @@ -21,15 +25,43 @@ struct R<'a, 'b> { media: HashMap>, doc: Document, sources: HashMap<(Uid, u32, String), usize>, + /// Component visits left for [`R::segment`] over the whole document. + budget: usize, + /// Outcome of the source-clip search under each segment: deterministic, so it is made once. + clip_search: HashMap, SearchLimit>>, } +/// Components one document may expand into timeline items; a small file can reference the same +/// sequence from many places and would otherwise multiply without bound. +const MAX_COMPONENT_VISITS: usize = 1_000_000; + +/// Reported when the shared component budget runs out. +const BUDGET_WARNING: &str = "OMF composition is too large to import completely (the remaining components are dropped)"; +/// Most visited components, and most queued references, one source-clip search may spend. +const MAX_CLIP_SEARCH_VISITS: usize = 100_000; +const MAX_CLIP_SEARCH_PENDING: usize = 1_000_000; + pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { + read_with_budget(bytes, report, MAX_COMPONENT_VISITS) +} + +fn read_with_budget(bytes: &[u8], report: &mut Report, budget: usize) -> Result { let perr = |m: String| Error::Parse { format: "OMF", message: m }; let c = Container::open(bytes).map_err(perr)?; let class_of = |o: u32| c.get(o, "OMFI:OOBJ:ObjClass").and_then(|v| v.get(..4).map(|b| [b[0], b[1], b[2], b[3]])); let head = c.objects.keys().copied().find(|&o| class_of(o) == Some(*b"HEAD")).ok_or_else(|| perr("no OMF header object".into()))?; let be = c.get(head, "OMFI:HEAD:ByteOrder").is_none_or(|v| v.first() != Some(&0x49)); - let mut r = R { c: &c, report, be, mobs: HashMap::new(), media: HashMap::new(), doc: Document::default(), sources: HashMap::new() }; + let mut r = R { + c: &c, + report, + be, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget, + clip_search: HashMap::new(), + }; let mob_list = r.refs(head, "OMFI:HEAD:Mobs"); let mob_list = if mob_list.is_empty() { c.objects.keys().copied().filter(|&o| matches!(class_of(o).as_ref(), Some(b"CMOB" | b"MMOB" | b"SMOB"))).collect() @@ -66,8 +98,14 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { cmobs.iter().copied().filter(|m| r.uid(*m, "OMFI:MOBJ:MobID").is_none_or(|id| !referenced.contains(&id))).collect() }; for m in top { + // every composition spends from the budget too: a list of up to 65,535 repeated entries must not yield that many + if r.budget == 0 && !r.doc.compositions.is_empty() { + r.report.warn(BUDGET_WARNING); + break; + } let comp = r.composition(m); r.doc.compositions.push(comp); + r.budget = r.budget.saturating_sub(1); } if r.doc.compositions.is_empty() { return Err(Error::Empty); @@ -166,6 +204,13 @@ impl R<'_, '_> { let slots = self.refs(m, "OMFI:MOBJ:Slots"); let mut na = 0; for s in slots { + if self.budget == 0 { + self.report.warn(BUDGET_WARNING); + break; + } + // Every slot spends a unit, skipped ones too: a list of up to 65,535 repeated slots + // must not cost that much work in each of 65,535 repeated compositions. + self.budget -= 1; let Some(seg) = self.r#ref(s, "OMFI:MSLT:Segment") else { continue }; let rate = self.rational(s, "OMFI:MSLT:EditRate").filter(|r| r.0 > 0 && r.1 > 0).unwrap_or((25, 1)); let kind = self.data_kind(seg); @@ -213,9 +258,15 @@ impl R<'_, '_> { fn segment(&mut self, seg: u32, rate: (i64, i64), depth: usize) -> Vec { let len = units_to_ticks(self.len(seg), rate.0, rate.1); - if depth > 32 { + // Depth alone does not bound a graph that references the same component many times at + // every level, so each visit also spends from a per-document budget. + if depth > 32 || self.budget == 0 { + if self.budget == 0 { + self.report.warn(BUDGET_WARNING); + } return vec![CItem::Filler(len)]; } + self.budget -= 1; match &self.class(seg) { b"SEQU" => { let mut out = Vec::new(); @@ -306,12 +357,44 @@ impl R<'_, '_> { .find(|&s| self.r#ref(s, "OMFI:MSLT:TrackDesc").and_then(|t| self.u32(t, "OMFI:TRKD:TrackID")) == Some(track)) } - fn first_clip(&self, seg: u32) -> Option { - match &self.class(seg) { - b"SCLP" => Some(seg), - b"SEQU" => self.refs(seg, "OMFI:SEQU:Components").into_iter().find_map(|c| self.first_clip(c)), - _ => None, + /// First source clip under `seg`, depth-first; a sequence referencing itself (directly or + /// through a cycle) ends the search rather than the stack. `Err` when the search spent its + /// budget of visited components or queued references before finishing (the answer is unknown). + fn first_clip(&mut self, seg: u32) -> std::result::Result, SearchLimit> { + if let Some(found) = self.clip_search.get(&seg) { + return *found; } + let found = self.first_clip_within(seg, MAX_CLIP_SEARCH_VISITS, MAX_CLIP_SEARCH_PENDING); + self.clip_search.insert(seg, found); + found + } + + fn first_clip_within(&self, seg: u32, max_visits: usize, max_pending: usize) -> std::result::Result, SearchLimit> { + let mut visited = HashSet::new(); + let mut todo = vec![seg]; + let mut queued = 1usize; + while let Some(seg) = todo.pop() { + if !visited.insert(seg) { + continue; + } + if visited.len() > max_visits { + return Err(SearchLimit); + } + match &self.class(seg) { + b"SCLP" => return Ok(Some(seg)), + b"SEQU" => { + let comps = self.refs(seg, "OMFI:SEQU:Components"); + // sequences may share one component list, so queued references grow with expansions, not visits + queued = queued.saturating_add(comps.len()); + if queued > max_pending { + return Err(SearchLimit); + } + todo.extend(comps.into_iter().rev()); + } + _ => {} + } + } + Ok(None) } fn resolve(&mut self, id: Uid, track: u32, start: Tick, master: Option, depth: usize) -> Option<(usize, Tick)> { @@ -328,7 +411,13 @@ impl R<'_, '_> { match &self.class(mob) { b"MMOB" => { let key = master.unwrap_or_else(|| id.iter().map(|b| format!("{b:02x}")).collect()); - let clip = self.first_clip(seg)?; + let clip = match self.first_clip(seg) { + Ok(c) => c?, + Err(SearchLimit) => { + self.report.warn("a master mob is too large to search for its source clip (imported as a gap)"); + return None; + } + }; let inner = units_to_ticks(self.i32(clip, "OMFI:SCLP:StartTime").unwrap_or(0), rate.0, rate.1); let next = self.uid(clip, "OMFI:SCLP:SourceID")?; let next_track = self.u32(clip, "OMFI:SCLP:SourceTrackID")?; @@ -353,7 +442,9 @@ impl R<'_, '_> { let file_rate = sr.filter(|r| r.0 > 0 && r.1 > 0).unwrap_or(rate); let length = units_to_ticks(self.i32(desc, "OMFI:MDFL:Length").unwrap_or(0), file_rate.0, file_rate.1); let summary = self.c.get(desc, "OMFI:WAVD:Summary").or_else(|| self.c.get(desc, "OMFI:AIFD:Summary")).unwrap_or_default(); - let fmt = crate::pcm::parse_wav(&summary).or_else(|| crate::pcm::parse_aiff(&summary)).map(|(_, ch, sr, bits)| (ch as u32, sr, bits)); + let fmt = craft_audio::essence_pcm::parse_wav(&summary) + .or_else(|| craft_audio::essence_pcm::parse_aiff(&summary)) + .map(|(_, ch, sr, bits)| (ch as u32, sr, bits)); let mut path = None; for l in self.refs(desc, "OMFI:MDES:Locator") { let p = match &self.class(l) { @@ -373,7 +464,8 @@ impl R<'_, '_> { let (file_channels, sample_rate, bits) = fmt.unwrap_or((1, file_rate.0 as u32, 16)); let channel = (file_channels > 1 && sound_slots > 1).then(|| track.saturating_sub(1).min(file_channels - 1)); // timecode of the physical source - let tc = self.first_clip(seg).and_then(|clip| { + let first = self.first_clip(seg).ok().flatten(); + let tc = first.and_then(|clip| { let tid = self.uid(clip, "OMFI:SCLP:SourceID")?; let tape = *self.mobs.get(&tid)?; let (start_tc, fps, trate) = self.refs(tape, "OMFI:MOBJ:Slots").into_iter().find_map(|s| { @@ -415,7 +507,7 @@ impl R<'_, '_> { nested: None, }; if let Some(d) = self.media.get(&id) { - if let Some((pcm, ch, sr, b)) = crate::pcm::parse_wav(d).or_else(|| crate::pcm::parse_aiff(d)) { + if let Some((pcm, ch, sr, b)) = craft_audio::essence_pcm::parse_wav(d).or_else(|| craft_audio::essence_pcm::parse_aiff(d)) { s.embedded = Some(pcm); s.file_channels = ch as u32; s.channels = if s.channel.is_some() { 1 } else { ch as u32 }; @@ -437,3 +529,144 @@ impl R<'_, '_> { } } } + +#[cfg(test)] +mod tests { + use super::super::bento::Writer; + use super::*; + + fn obj(w: &mut Writer, class: &[u8; 4]) -> u32 { + let id = w.new_object(); + w.set(id, "OMFI:OOBJ:ObjClass", "omfi:ClassID", class); + id + } + fn list(w: &mut Writer, o: u32, p: &str, targets: &[u32]) { + let mut v = (targets.len() as u16).to_be_bytes().to_vec(); + for t in targets { + v.extend_from_slice(&t.to_be_bytes()); + } + w.set(o, p, "omfi:ObjRefArray", &v); + } + + fn search(max_visits: usize, max_pending: usize, build: impl FnOnce(&mut Writer) -> u32) -> std::result::Result, SearchLimit> { + let mut w = Writer::new(); + let root = build(&mut w); + let bytes = w.finish(); + let c = Container::open(&bytes).unwrap(); + let mut report = Report::default(); + let r = R { + c: &c, + report: &mut report, + be: true, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget: 0, + clip_search: HashMap::new(), + }; + r.first_clip_within(root, max_visits, max_pending) + } + + #[test] + fn clip_search_reports_its_limits() { + // found within the limits + assert!(matches!( + search(10, 10, |w| { + let (s, c) = (obj(w, b"SEQU"), obj(w, b"SCLP")); + list(w, s, "OMFI:SEQU:Components", &[c]); + s + }), + Ok(Some(_)) + )); + // too many distinct components before any clip + assert!( + search(5, 1000, |w| { + let (s, f) = (obj(w, b"SEQU"), (0..20).map(|_| obj(w, b"FILL")).collect::>()); + list(w, s, "OMFI:SEQU:Components", &f); + s + }) + .is_err() + ); + // few visits, but sequences that each queue a long list of references + assert!( + search(1000, 500, |w| { + let f = obj(w, b"FILL"); + let inner: Vec = (0..20).map(|_| obj(w, b"SEQU")).collect(); + for &i in &inner { + list(w, i, "OMFI:SEQU:Components", &[f; 100]); + } + let s = obj(w, b"SEQU"); + list(w, s, "OMFI:SEQU:Components", &inner); + s + }) + .is_err() + ); + // a search that ends without a clip is not a limit + assert!(matches!(search(10, 10, |w| obj(w, b"FILL")), Ok(None))); + } + + #[test] + fn skipped_slots_spend_the_budget() { + let mut w = Writer::new(); + let head = obj(&mut w, b"HEAD"); + let cm = obj(&mut w, b"CMOB"); + let slot = obj(&mut w, b"MSLT"); + list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]); + list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]); + list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm]); + let bytes = w.finish(); + let mut report = Report::default(); + read_with_budget(&bytes, &mut report, 50).unwrap(); + assert!(report.mentions(BUDGET_WARNING), "slots with no segment cost nothing before"); + } + + #[test] + fn a_source_clip_search_is_made_once_per_segment() { + let mut w = Writer::new(); + let (s, c) = (obj(&mut w, b"SEQU"), obj(&mut w, b"SCLP")); + list(&mut w, s, "OMFI:SEQU:Components", &[c]); + let bytes = w.finish(); + let c = Container::open(&bytes).unwrap(); + let mut report = Report::default(); + let mut r = R { + c: &c, + report: &mut report, + be: true, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget: 0, + clip_search: HashMap::new(), + }; + let first = r.first_clip(s).ok(); + assert!(matches!(first, Some(Some(_)))); + assert_eq!(r.clip_search.len(), 1); + assert_eq!(r.first_clip(s).ok(), first); + assert_eq!(r.clip_search.len(), 1); + } + + #[test] + fn repeated_slots_and_compositions_spend_the_budget() { + let mut w = Writer::new(); + let head = obj(&mut w, b"HEAD"); + let cm = obj(&mut w, b"CMOB"); + let dd = obj(&mut w, b"DDEF"); + w.set(dd, "OMFI:DDEF:DataKindID", "omfi:UniqueName", b"omfi:data:Sound\0"); + let fill = obj(&mut w, b"FILL"); + w.set(fill, "OMFI:CPNT:DataKind", "omfi:ObjRef", &dd.to_be_bytes()); + let slot = obj(&mut w, b"MSLT"); + w.set(slot, "OMFI:MSLT:Segment", "omfi:ObjRef", &fill.to_be_bytes()); + list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]); + list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]); + list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm; 200]); + let bytes = w.finish(); + let mut report = Report::default(); + let doc = read_with_budget(&bytes, &mut report, 50).unwrap(); + let tracks: usize = doc.compositions.iter().map(|c| c.tracks.len()).sum(); + assert!(tracks <= 50, "{tracks} tracks from a budget of 50"); + assert!(doc.compositions.len() <= 2, "{} compositions", doc.compositions.len()); + assert!(report.has_warnings()); + } +} diff --git a/crates/aaf/src/pcm.rs b/crates/aaf/src/pcm.rs index 789c976a8..9992b2d3b 100644 --- a/crates/aaf/src/pcm.rs +++ b/crates/aaf/src/pcm.rs @@ -4,8 +4,3 @@ pub(crate) fn parse_wav(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { craft_audio::wav_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits)) } - -/// The same for an AIFF / AIFF-C (`NONE`, `twos`, `sowt`) file. -pub(crate) fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { - craft_audio::aiff_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits)) -} From 52a6dd6aa7aadf7a57f715135ec1dcbc51088515 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:46:32 +0000 Subject: [PATCH 036/471] aaf: expose bounded Bento container access --- crates/aaf/src/omf.rs | 2 +- crates/aaf/src/omf/bento.rs | 25 ++++++++++++++++++------- crates/aaf/tests/bento_api.rs | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 crates/aaf/tests/bento_api.rs diff --git a/crates/aaf/src/omf.rs b/crates/aaf/src/omf.rs index 57e0f756c..bbd2e8571 100644 --- a/crates/aaf/src/omf.rs +++ b/crates/aaf/src/omf.rs @@ -21,7 +21,7 @@ //! transition's name) are not in the specification; they were first written by FilmCraft and keep //! their names so that existing files read back. -pub(crate) mod bento; +pub mod bento; mod read; mod write; diff --git a/crates/aaf/src/omf/bento.rs b/crates/aaf/src/omf/bento.rs index 00a7e2a26..171196fe1 100644 --- a/crates/aaf/src/omf/bento.rs +++ b/crates/aaf/src/omf/bento.rs @@ -35,8 +35,8 @@ use std::collections::{BTreeMap, HashMap}; -pub(crate) const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7]; -pub(crate) const LABEL_SIZE: usize = 24; +pub const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7]; +pub const LABEL_SIZE: usize = 24; // standard object ids const TOC_OBJECT: u32 = 1; @@ -82,7 +82,7 @@ const MAX_VALUE_BYTES: usize = 16 << 20; const MAX_NAME_BYTES: usize = 4 << 20; /// Builds a Bento container. -pub(crate) struct Writer { +pub struct Writer { data: Vec, names: HashMap<(u32, String), u32>, next: u32, @@ -90,6 +90,12 @@ pub(crate) struct Writer { entries: Vec<(u32, u32, u32, Vec)>, } +impl Default for Writer { + fn default() -> Self { + Self::new() + } +} + impl Writer { pub fn new() -> Writer { Writer { data: Vec::new(), names: HashMap::new(), next: MIN_USER_ID, entries: Vec::new() } @@ -217,9 +223,9 @@ pub(crate) struct Prop { } /// A parsed Bento container. -pub(crate) struct Container<'a> { +pub struct Container<'a> { data: &'a [u8], - pub objects: BTreeMap>, + pub(crate) objects: BTreeMap>, names: HashMap, by_name: HashMap, } @@ -229,11 +235,16 @@ fn be32(b: &[u8], at: usize) -> Option { } /// Whether `b` ends with a Bento label. -pub(crate) fn sniff(b: &[u8]) -> bool { +pub fn sniff(b: &[u8]) -> bool { b.len() >= LABEL_SIZE && b[b.len() - LABEL_SIZE..b.len() - LABEL_SIZE + 8] == MAGIC } impl<'a> Container<'a> { + /// Object identifiers in ascending order. + pub fn object_ids(&self) -> impl Iterator + '_ { + self.objects.keys().copied() + } + pub fn open(data: &'a [u8]) -> Result, String> { if !sniff(data) { return Err("no Bento label at the end of the file".into()); @@ -324,7 +335,7 @@ impl<'a> Container<'a> { } /// The bytes of a value. - pub fn value(&self, p: &Prop) -> Result, String> { + pub(crate) fn value(&self, p: &Prop) -> Result, String> { let total = p.segs.iter().try_fold(0usize, |total, s| { let len = match s { Seg::Imm(b) => b.len(), diff --git a/crates/aaf/tests/bento_api.rs b/crates/aaf/tests/bento_api.rs new file mode 100644 index 000000000..9df70c661 --- /dev/null +++ b/crates/aaf/tests/bento_api.rs @@ -0,0 +1,17 @@ +use craft_aaf::omf::bento::{Container, Writer, sniff}; + +#[test] +fn external_consumer_reads_named_values_and_references() { + let mut writer = Writer::new(); + let first = writer.new_object(); + let second = writer.new_object(); + writer.set(first, "test:value", "test:type", b"long value"); + writer.set_references(first, &[second]); + let bytes = writer.finish(); + assert!(sniff(&bytes)); + let container = Container::open(&bytes).unwrap(); + assert!(container.object_ids().any(|id| id == first)); + assert_eq!(container.get(first, "test:value").as_deref(), Some(b"long value".as_slice())); + assert_eq!(container.resolve(first, second), second); + assert!(Container::open(&bytes[..bytes.len() - 1]).is_err()); +} From 99e448d9199191e86a7dddc6b39a5a1e29aad24c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:58:28 +0000 Subject: [PATCH 037/471] aaf: bound duplicate property detection work --- crates/aaf/src/aaf/hostile_tests.rs | 16 ++++++++++++++++ crates/aaf/src/aaf/store.rs | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/aaf/src/aaf/hostile_tests.rs b/crates/aaf/src/aaf/hostile_tests.rs index 853b0000c..527e50da8 100644 --- a/crates/aaf/src/aaf/hostile_tests.rs +++ b/crates/aaf/src/aaf/hostile_tests.rs @@ -108,3 +108,19 @@ fn objects_nested_too_deeply_are_an_error() { let e = store::read(&chain(250)).unwrap_err(); assert!(e.contains("nested too deeply"), "{e}"); } + +/// Many distinct property IDs still reject a duplicate without quadratic scanning. +#[test] +fn duplicate_after_many_distinct_properties_is_rejected() { + let mut props = vec![0x4C, 0x20]; + props.extend_from_slice(&8193u16.to_le_bytes()); + for id in (0..8192u16).chain(std::iter::once(7)) { + props.extend_from_slice(&id.to_le_bytes()); + props.extend_from_slice(&0x82u16.to_le_bytes()); + props.extend_from_slice(&0u16.to_le_bytes()); + } + let mut writer = Writer::new(Version::V3); + writer.stream(Writer::ROOT, "properties", props).unwrap(); + let e = store::read(&writer.finish()).unwrap_err(); + assert!(e.contains("duplicate property id"), "{e}"); +} diff --git a/crates/aaf/src/aaf/store.rs b/crates/aaf/src/aaf/store.rs index 69b14fcf2..859c0ea77 100644 --- a/crates/aaf/src/aaf/store.rs +++ b/crates/aaf/src/aaf/store.rs @@ -461,11 +461,13 @@ impl R<'_> { if at > props.len() { return Err(bad("truncated property index")); } + // A properties stream can name tens of thousands of ids: do not rescan all earlier ids. + let mut seen = std::collections::HashSet::with_capacity(n); for i in 0..n { let (Some(pid), Some(sf), Some(len)) = (rd_u16(&props, 4 + i * 6), rd_u16(&props, 6 + i * 6), rd_u16(&props, 8 + i * 6)) else { return Err(bad("truncated property index")); }; - if o.props.iter().any(|(previous, _)| *previous == pid) { + if !seen.insert(pid) { return Err(bad("duplicate property id")); } let v = props.get(at..at + len as usize).ok_or_else(|| bad("truncated property value"))?; From f751827790237ed116980c3f981f2e2b72d788a1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:01:42 +0000 Subject: [PATCH 038/471] audio: share streaming PCM export encoding --- crates/audio/src/lib.rs | 3 + crates/audio/src/pcm_export.rs | 264 +++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 crates/audio/src/pcm_export.rs diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..4a5f352d1 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -66,6 +66,9 @@ mod wav; feature = "vorbis-encode" ))] mod write; + +/// Streaming WAVE and AIFF headers and sample packing. +pub mod pcm_export; #[cfg(feature = "vorbis-encode")] pub use write::{OGG_BITRATES, encode_ogg}; diff --git a/crates/audio/src/pcm_export.rs b/crates/audio/src/pcm_export.rs new file mode 100644 index 000000000..c8456d785 --- /dev/null +++ b/crates/audio/src/pcm_export.rs @@ -0,0 +1,264 @@ +//! Audio-only files: WAV (RIFF, little-endian PCM; RF64 past 4 GiB) and AIFF (big-endian PCM), +//! 16, 24 or 32-bit integer or 32-bit float ([`SampleFormat`]), for streaming export. +//! Float WAV is `WAVE_FORMAT_IEEE_FLOAT` with a `fact` chunk; float AIFF is AIFF-C (`fl32`). +//! +//! The header is written first from the known length ([`header`]), then the samples in chunks +//! ([`encode`]) and the pad byte ([`pad`]), so an export streams to disk instead of building the +//! file in memory. + +/// PCM representation in an exported file. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SampleFormat { + /// 16-bit signed integer. + Int16, + /// 24-bit signed integer. + Int24, + /// 32-bit signed integer. + Int32, + /// 32-bit IEEE float. + Float32, +} + +impl SampleFormat { + /// Bits per sample. + pub fn bits(self) -> u16 { + match self { + Self::Int16 => 16, + Self::Int24 => 24, + Self::Int32 | Self::Float32 => 32, + } + } + /// Bytes per sample. + pub fn bytes(self) -> usize { + usize::from(self.bits() / 8) + } + /// Whether this is a floating point format. + pub fn is_float(self) -> bool { + self == Self::Float32 + } + /// Clip and quantize one sample with symmetric integer full scale; NaN becomes silence. + pub fn quantise(self, s: f32) -> i32 { + let s = s.clamp(-1.0, 1.0); + match self { + Self::Int16 => (s * 32_767.0).round() as i32, + Self::Int24 => (s * 8_388_607.0).round() as i32, + Self::Int32 => (f64::from(s) * 2_147_483_647.0).round() as i32, + Self::Float32 => 0, + } + } + /// Sanitize a floating sample: NaN becomes silence, infinity becomes the largest finite value. + pub fn float(s: f32) -> f32 { + if s.is_nan() { 0.0 } else { s.clamp(f32::MIN, f32::MAX) } + } +} + +/// The container of an audio-only export. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PcmContainer { + /// RIFF/WAVE or RF64. + Wav, + /// AIFF or AIFF-C. + Aiff, +} + +/// Bytes of sample data for `frames` frames, or None when that does not fit 64 bits. +fn data_len(frames: u64, channels: u16, format: SampleFormat) -> Option { + frames.checked_mul(u64::from(channels))?.checked_mul(format.bytes() as u64) +} + +/// The pad byte after an odd-sized data chunk (RIFF and AIFF chunks are word-aligned). +pub fn pad(frames: u64, channels: u16, format: SampleFormat) -> &'static [u8] { + if data_len(frames, channels, format).is_some_and(|n| n & 1 == 1) { &[0] } else { &[] } +} + +/// `KSDATAFORMAT_SUBTYPE_PCM` / `_IEEE_FLOAT` after their first two bytes (the format tag). +const SUBTYPE_TAIL: [u8; 14] = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71]; + +/// AIFF-C format version 1 (`FVER`). +const AIFC_VERSION_1: u32 = 0xA280_5140; + +/// The AIFF-C compression name of `fl32`, as a Pascal string of even length. +const FL32_NAME: &[u8] = b"\x1532-bit floating point"; + +/// Everything before the samples of a file of `frames` frames. A WAV whose RIFF size would not +/// fit 32 bits (4 GiB: 24-bit 5.1 at 96 kHz after about 41 minutes) is written as RF64 (EBU Tech +/// 3306), with the 64-bit sizes in a `ds64` chunk. AIFF has no 64-bit form, so a longer AIFF is +/// an error naming WAV instead. +pub fn header(container: PcmContainer, channels: u16, sample_rate: u32, format: SampleFormat, frames: u64) -> Result, String> { + let too_long = || "the audio is too long to write".to_string(); + let bits = format.bits(); + let bps = bits / 8; + let float = format.is_float(); + let data = data_len(frames, channels, format).ok_or_else(too_long)?; + let block_align = channels.checked_mul(bps).ok_or("too many audio channels")?; + match container { + PcmContainer::Wav => { + let byte_rate = + u32::try_from(u64::from(sample_rate) * u64::from(block_align)).map_err(|_| "the audio sample rate is too high".to_string())?; + let ext = channels > 2; + // plain PCM has no cbSize; IEEE float has an empty one + let fmt_len: u32 = if ext { + 40 + } else if float { + 18 + } else { + 16 + }; + // every format but integer PCM has a `fact` chunk (the frame count; RF64: in `ds64`) + let fact_len: u64 = if float { 12 } else { 0 }; + let padded = data.checked_add(data & 1).ok_or_else(too_long)?; + // "WAVE" + fmt chunk + fact chunk + data chunk header + samples + let riff = (4 + 8 + u64::from(fmt_len) + fact_len + 8).checked_add(padded).ok_or_else(too_long)?; + let rf64 = riff > u64::from(u32::MAX); + let mut v = Vec::with_capacity(100); + if rf64 { + const DS64: u64 = 8 + 28; + v.extend_from_slice(b"RF64"); + v.extend_from_slice(&u32::MAX.to_le_bytes()); + v.extend_from_slice(b"WAVEds64"); + v.extend_from_slice(&28u32.to_le_bytes()); + v.extend_from_slice(&riff.checked_add(DS64).ok_or_else(too_long)?.to_le_bytes()); + v.extend_from_slice(&data.to_le_bytes()); + v.extend_from_slice(&frames.to_le_bytes()); + v.extend_from_slice(&0u32.to_le_bytes()); // no table entries + } else { + v.extend_from_slice(b"RIFF"); + v.extend_from_slice(&(riff as u32).to_le_bytes()); + v.extend_from_slice(b"WAVE"); + } + let tag: u16 = if float { 3 } else { 1 }; + v.extend_from_slice(b"fmt "); + v.extend_from_slice(&fmt_len.to_le_bytes()); + v.extend_from_slice(&(if ext { 0xFFFE } else { tag }).to_le_bytes()); + v.extend_from_slice(&channels.to_le_bytes()); + v.extend_from_slice(&sample_rate.to_le_bytes()); + v.extend_from_slice(&byte_rate.to_le_bytes()); + v.extend_from_slice(&block_align.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + if ext { + // cbSize, valid bits, channel mask, KSDATAFORMAT_SUBTYPE_PCM / _IEEE_FLOAT + v.extend_from_slice(&22u16.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + let mask: u32 = if channels == 6 { 0x3F } else { 0 }; + v.extend_from_slice(&mask.to_le_bytes()); + v.extend_from_slice(&tag.to_le_bytes()); + v.extend_from_slice(&SUBTYPE_TAIL); + } else if float { + v.extend_from_slice(&0u16.to_le_bytes()); + } + if float { + v.extend_from_slice(b"fact"); + v.extend_from_slice(&4u32.to_le_bytes()); + v.extend_from_slice(&(if rf64 { u32::MAX } else { u32::try_from(frames).unwrap_or(u32::MAX) }).to_le_bytes()); + } + v.extend_from_slice(b"data"); + v.extend_from_slice(&(if rf64 { u32::MAX } else { data as u32 }).to_le_bytes()); + Ok(v) + } + PcmContainer::Aiff => { + let aiff_too_long = || "AIFF holds at most 4 GB of audio: export WAV, which has no such limit".to_string(); + let frames32 = u32::try_from(frames).map_err(|_| aiff_too_long())?; + // SSND: offset + block size + samples + let ssnd = u32::try_from(data.checked_add(8).ok_or_else(aiff_too_long)?).map_err(|_| aiff_too_long())?; + // floats need AIFF-C: a version chunk, and the compression type and name in COMM + let comm: u32 = if float { 18 + 4 + FL32_NAME.len() as u32 } else { 18 }; + let fver: u32 = if float { 8 + 4 } else { 0 }; + let form = (4u32 + fver + (8 + comm)) + .checked_add(8) + .and_then(|n| n.checked_add(ssnd)) + .and_then(|n| n.checked_add(ssnd & 1)) + .ok_or_else(aiff_too_long)?; + let mut v = Vec::with_capacity(90); + v.extend_from_slice(b"FORM"); + v.extend_from_slice(&form.to_be_bytes()); + if float { + v.extend_from_slice(b"AIFCFVER"); + v.extend_from_slice(&4u32.to_be_bytes()); + v.extend_from_slice(&AIFC_VERSION_1.to_be_bytes()); + } else { + v.extend_from_slice(b"AIFF"); + } + v.extend_from_slice(b"COMM"); + v.extend_from_slice(&comm.to_be_bytes()); + v.extend_from_slice(&channels.to_be_bytes()); + v.extend_from_slice(&frames32.to_be_bytes()); + v.extend_from_slice(&bits.to_be_bytes()); + v.extend_from_slice(&extended(sample_rate)); + if float { + v.extend_from_slice(b"fl32"); + v.extend_from_slice(FL32_NAME); + } + v.extend_from_slice(b"SSND"); + v.extend_from_slice(&ssnd.to_be_bytes()); + v.extend_from_slice(&0u32.to_be_bytes()); // offset + v.extend_from_slice(&0u32.to_be_bytes()); // block size + Ok(v) + } + } +} + +/// Append interleaved samples in `format` (little-endian for WAV, big-endian for AIFF). +pub fn encode(container: PcmContainer, interleaved: &[f32], format: SampleFormat, out: &mut Vec) { + let bps = format.bytes(); + out.reserve(interleaved.len().saturating_mul(bps)); + for &s in interleaved { + let q = if format.is_float() { SampleFormat::float(s).to_bits() as i32 } else { format.quantise(s) }; + match container { + PcmContainer::Wav => out.extend_from_slice(q.to_le_bytes().get(..bps).unwrap_or_default()), + PcmContainer::Aiff => out.extend_from_slice(q.to_be_bytes().get(4 - bps..).unwrap_or_default()), + } + } +} + +/// A whole file in memory (short audio; exports stream with [`header`] and [`encode`]). +fn write_whole(container: PcmContainer, interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + let frames = (interleaved.len() / usize::from(channels.max(1))) as u64; + let mut v = header(container, channels, sample_rate, format, frames)?; + encode(container, interleaved, format, &mut v); + v.extend_from_slice(pad(frames, channels, format)); + Ok(v) +} + +/// A RIFF/WAVE file of interleaved samples. More than two channels are written as +/// `WAVE_FORMAT_EXTENSIBLE` with the speaker mask of the layout (6 channels: L, R, C, LFE, Ls, Rs +/// = `0x3F`). +pub fn write_wav(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + write_whole(PcmContainer::Wav, interleaved, channels, sample_rate, format) +} + +/// IEEE 754 80-bit extended encoding of a sample rate (AIFF `COMM`). +pub fn extended(rate: u32) -> [u8; 10] { + let mut out = [0u8; 10]; + if rate == 0 { + return out; + } + let shift = rate.leading_zeros(); + let mant = (rate as u64) << (32 + shift); + let exp = 16383 + 31 - shift as u16; + out[..2].copy_from_slice(&exp.to_be_bytes()); + out[2..].copy_from_slice(&mant.to_be_bytes()); + out +} + +/// An AIFF file (`FORM`/`AIFF`, `COMM` + `SSND`; AIFF-C for floats) of interleaved samples. +pub fn write_aiff(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + write_whole(PcmContainer::Aiff, interleaved, channels, sample_rate, format) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn streaming_formats_preserve_samples_and_bounds() { + let samples = [1.0, -1.0, f32::NAN]; + let wav = write_wav(&samples, 1, 48_000, SampleFormat::Int24).unwrap(); + assert_eq!(&wav[..4], b"RIFF"); + assert_eq!(&wav[44..53], &[255, 255, 127, 1, 0, 128, 0, 0, 0]); + let h = header(PcmContainer::Wav, 6, 96_000, SampleFormat::Int24, 96_000 * 3600).unwrap(); + assert_eq!(&h[..4], b"RF64"); + assert!(header(PcmContainer::Aiff, 2, 48_000, SampleFormat::Int24, u64::MAX).is_err()); + let aiff = write_aiff(&[0.25, -3.0], 1, 44_100, SampleFormat::Float32).unwrap(); + assert_eq!(&aiff[8..12], b"AIFC"); + assert_eq!(&aiff[aiff.len() - 8..], [0.25f32.to_be_bytes(), (-3.0f32).to_be_bytes()].concat()); + } +} From 6b82b6a77c995847084311402d27a6361b6491ed Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:05:22 +0000 Subject: [PATCH 039/471] aaf: support legacy reference and locator conventions --- crates/aaf/src/aaf.rs | 47 +++++++++++++++++++++++++++++++++++-- crates/aaf/src/aaf/read.rs | 32 +++++++++++++++++++------ crates/aaf/src/aaf/tests.rs | 29 +++++++++++++++++++++++ crates/aaf/src/aaf/write.rs | 18 ++++++++++---- 4 files changed, 113 insertions(+), 13 deletions(-) diff --git a/crates/aaf/src/aaf.rs b/crates/aaf/src/aaf.rs index 05894f94e..efa618fb3 100644 --- a/crates/aaf/src/aaf.rs +++ b/crates/aaf/src/aaf.rs @@ -57,13 +57,56 @@ pub struct WriteOptions<'a> { /// reported. pub fn write(doc: &Document, opts: &WriteOptions<'_>, report: &mut Report) -> Result> { let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 }; - write::write(doc, version, &opts.identity, report) + write::write(doc, version, &opts.identity, &ReferenceOptions::default(), report) +} + +/// Optional interpretation choices for applications retaining older interchange behavior. +#[derive(Clone, Copy, Debug)] +pub struct ReadOptions { + /// Interpret a source clip's start using its target slot's edit rate. + pub source_slot_rate: bool, + /// Use the referenced mob name when the clip has no explicit name. + pub infer_clip_name: bool, + /// Decode percent escapes in relative network locators. + pub decode_relative_locator: bool, + /// Report selector and nested-scope flattening. + pub report_flattening: bool, + /// Accept truncated PCM WAVE essence summaries. + pub permissive_pcm: bool, +} +impl Default for ReadOptions { + fn default() -> Self { + Self { source_slot_rate: true, infer_clip_name: true, decode_relative_locator: true, report_flattening: true, permissive_pcm: false } + } +} +/// Read using explicitly selected interpretation choices. +pub fn read_with_options(bytes: &[u8], options: &ReadOptions, report: &mut Report) -> Result { + read::read(bytes, options, report) +} + +/// Optional compatibility choices for writing source references and locators. +#[derive(Clone, Copy, Debug)] +pub struct ReferenceOptions { + /// Write source offsets in target-slot units. + pub source_slot_rate: bool, + /// Percent-encode relative locators. + pub encode_relative_locator: bool, +} +impl Default for ReferenceOptions { + fn default() -> Self { + Self { source_slot_rate: true, encode_relative_locator: true } + } +} +/// Write with explicitly selected reference and locator conventions. +pub fn write_with_references(doc: &Document, opts: &WriteOptions<'_>, references: &ReferenceOptions, report: &mut Report) -> Result> { + let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 }; + write::write(doc, version, &opts.identity, references, report) } /// Read an AAF file: its top-level compositions, the compositions nested in them and their /// sources. What cannot be represented is skipped and named in `report`. pub fn read(bytes: &[u8], report: &mut Report) -> Result { - read::read(bytes, report) + read::read(bytes, &ReadOptions::default(), report) } /// Whether `bytes` look like an AAF file (a compound file whose root has the AAF root class or a diff --git a/crates/aaf/src/aaf/read.rs b/crates/aaf/src/aaf/read.rs index 31c841c5d..9739f87ba 100644 --- a/crates/aaf/src/aaf/read.rs +++ b/crates/aaf/src/aaf/read.rs @@ -67,6 +67,7 @@ fn indirect_f64(d: &[u8]) -> Option { struct R<'a> { report: &'a mut Report, + options: super::ReadOptions, mobs: HashMap, essence: HashMap, doc: Document, @@ -90,7 +91,7 @@ struct NestClip<'a> { /// Compositions nested deeper than this are read as gaps. const MAX_NESTING: usize = 16; -pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { +pub(crate) fn read(bytes: &[u8], options: &super::ReadOptions, report: &mut Report) -> Result { let root = store::read(bytes).map_err(|e| Error::Parse { format: "AAF", message: e })?; let header = root.strong(pid::ROOT_HEADER).ok_or_else(|| Error::Parse { format: "AAF", message: "no header object".into() })?; let content = header.strong(pid::CONTENT).ok_or_else(|| Error::Parse { format: "AAF", message: "no content storage".into() })?; @@ -106,7 +107,8 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { essence.insert(id, d); } } - let mut r = R { report, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() }; + let mut r = + R { report, options: *options, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() }; // Top-level compositions: those no other composition uses, among the ones tagged top-level // when any are. (Premiere Pro tags a nested sequence's composition top-level too; it is still // only a nested sequence.) Compositions that only use each other in a circle all count. The @@ -331,7 +333,9 @@ impl R<'_> { items } x if x == cls::SELECTOR => { - self.report.info("a selector was flattened to its selected input"); + if self.options.report_flattening { + self.report.info("a selector was flattened to its selected input"); + } match c.strong(pid::SELECTED) { Some(s) => { let mut items = self.segment(s, rate, kind, depth); @@ -342,7 +346,9 @@ impl R<'_> { } } x if x == cls::NESTED_SCOPE => { - self.report.info("a nested scope was flattened to its final input"); + if self.options.report_flattening { + self.report.info("a nested scope was flattened to its final input"); + } match c.objs(pid::NESTED_SLOTS).last() { Some(s) => { let mut items = self.segment(s, rate, kind, depth); @@ -388,7 +394,12 @@ impl R<'_> { let len = units_to_ticks(len_of(c), rate.0, rate.1); let tagged_name = tagged_values(c, pid::COMPONENT_USER_COMMENTS).into_iter().find(|(k, _)| k == "Clip Name").map(|(_, v)| v); let (Some(id), Some(slot)) = (c.mob_id(pid::SOURCE_ID), c.u32(pid::SOURCE_MOB_SLOT_ID)) else { return CItem::Filler(len) }; - let name = tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME))).unwrap_or_default(); + let name = if self.options.infer_clip_name { + tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME))) + } else { + tagged_name + } + .unwrap_or_default(); if id == [0; 32] { return CItem::Filler(len); } @@ -401,6 +412,7 @@ impl R<'_> { .and_then(|mob| mob.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(slot))) .and_then(|s| rate_of(s, pid::EDIT_RATE)) .unwrap_or(rate); + let source_rate = if self.options.source_slot_rate { source_rate } else { rate }; let start = units_to_ticks(c.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1); match self.resolve(id, slot, start, kind, master_key, 0) { Some((source, t)) => CItem::Clip(CClip { len, source, start: t, gain: None, name }), @@ -506,6 +518,7 @@ impl R<'_> { .and_then(|m| m.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(next_slot))) .and_then(|s| rate_of(s, pid::EDIT_RATE)) .unwrap_or(rate); + let source_rate = if self.options.source_slot_rate { source_rate } else { rate }; let inner = units_to_ticks(clip.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1); if next == [0; 32] { return None; @@ -545,7 +558,11 @@ impl R<'_> { DataKind::Sound => CKind::Sound, _ => kind, }; - let path = d.objs(pid::LOCATOR).iter().find_map(|l| l.string(pid::URL_STRING)).map(|u| locator_path(&u)); + let path = d + .objs(pid::LOCATOR) + .iter() + .find_map(|l| l.string(pid::URL_STRING)) + .map(|u| if self.options.decode_relative_locator { locator_path(&u) } else { craft_timeline::path::file_url_to_path(&u) }); let file_rate = rate_of(d, pid::SAMPLE_RATE).unwrap_or(rate); let length = d .i64(pid::FILE_LENGTH) @@ -604,7 +621,8 @@ impl R<'_> { if let Some(data) = self.essence.get(&id) { s.embedded = Some(data.to_vec()); // embedded essence may be a whole WAVE / AIFF file - if let Some((pcm, ch, sr, bits)) = crate::pcm::parse_wav(data) { + let pcm = if self.options.permissive_pcm { craft_audio::essence_pcm::parse_wav(data) } else { crate::pcm::parse_wav(data) }; + if let Some((pcm, ch, sr, bits)) = pcm { s.embedded = Some(pcm); s.file_channels = ch as u32; s.channels = if s.channel.is_some() { 1 } else { ch as u32 }; diff --git a/crates/aaf/src/aaf/tests.rs b/crates/aaf/src/aaf/tests.rs index 3e7794471..34eb2bbb3 100644 --- a/crates/aaf/src/aaf/tests.rs +++ b/crates/aaf/src/aaf/tests.rs @@ -557,3 +557,32 @@ fn relative_media_locator_round_trips_escaped_characters() { let got = read_clean(&bytes); assert!(got.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav"))); } + +#[test] +fn compatibility_options_keep_relative_paths_and_parent_rate() { + let mut doc = crate::testdoc::sound_only(None); + doc.compositions[0].sample_rate = 44_100; + doc.sources[0].path = Some("Audio Files/100%20 #1.wav".into()); + let mut report = Report::default(); + let references = ReferenceOptions { source_slot_rate: false, encode_relative_locator: false }; + let bytes = write_with_references(&doc, &WriteOptions { identity: IDENTITY, small_sectors: false }, &references, &mut report).unwrap(); + let root = store::read(&bytes).unwrap(); + let mut all = Vec::new(); + walk(&root, &mut all); + assert!( + all.iter().filter(|o| o.class == cls::NETWORK_LOCATOR).any(|o| o.string(pid::URL_STRING).as_deref() == Some("Audio Files/100%20 #1.wav")) + ); + let starts: Vec = all.iter().filter(|o| o.class == cls::SOURCE_CLIP).filter_map(|o| o.i64(pid::START_TIME)).collect(); + assert!(starts.contains(&17_640), "parent rate start: {starts:?}"); + let opts = ReadOptions { + source_slot_rate: false, + infer_clip_name: false, + decode_relative_locator: false, + report_flattening: false, + permissive_pcm: true, + }; + let back = read_with_options(&bytes, &opts, &mut report).unwrap(); + assert!(back.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav"))); + let clip = back.compositions[0].tracks[0].items.iter().find_map(|i| if let CItem::Clip(c) = i { Some(c) } else { None }).unwrap(); + assert_eq!(clip.start, FrameRate::FPS_25.tick_of(10)); +} diff --git a/crates/aaf/src/aaf/write.rs b/crates/aaf/src/aaf/write.rs index 8c9aae88e..1fcc6a332 100644 --- a/crates/aaf/src/aaf/write.rs +++ b/crates/aaf/src/aaf/write.rs @@ -75,6 +75,7 @@ fn definition(class: Auid, id: Auid, name: &str) -> Obj { struct W<'a> { report: &'a mut Report, inexact: bool, + references: super::ReferenceOptions, } struct IdGen { @@ -350,9 +351,15 @@ fn add_tape_mob(first: &Source, tape_id: [u8; 32], timecode: Option<(i64, FrameR } /// Serialise a composition document. -pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Identity<'_>, report: &mut Report) -> Result> { +pub(crate) fn write( + doc: &Document, + version: craft_cfb::Version, + identity: &Identity<'_>, + references: &super::ReferenceOptions, + report: &mut Report, +) -> Result> { let comp = doc.compositions.first().ok_or(Error::Empty)?; - let mut w = W { report, inexact: false }; + let mut w = W { report, inexact: false, references: *references }; let mut ids = IdGen::for_document(doc, comp); let MediaOutput { mut mobs, essence, master_ref } = write_media(doc, &mut w, &mut ids); let mut used_ops: Vec = Vec::new(); @@ -360,7 +367,8 @@ pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Iden // ---- nested compositions: a clip refers to one as it does to a master mob, through the // composition's first track of the clip's kind (a broken-out channel: that channel's track) - let mut refs = Refs { mobs: master_ref, rates: doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() }; + let rates = if references.source_slot_rate { doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() } else { HashMap::new() }; + let mut refs = Refs { mobs: master_ref, rates }; let nested_ids: Vec<[u8; 32]> = doc.nested.iter().map(|_| ids.next()).collect(); for (si, s) in doc.sources.iter().enumerate() { let Some((comp, id)) = s.nested.and_then(|n| Some((doc.nested.get(n)?, nested_ids.get(n)?))) else { continue }; @@ -795,8 +803,10 @@ fn descriptor(w: &mut W, s: &Source) -> Obj { if let Some(p) = &s.path { let url = if craft_timeline::path::is_absolute(p) { craft_timeline::path::path_to_file_url(p, false) - } else { + } else if w.references.encode_relative_locator { craft_timeline::path::encode_uri_path(p) + } else { + p.clone() }; d.set(pid::LOCATOR, Value::StrongVec(vec![Obj::new(cls::NETWORK_LOCATOR).data_prop(pid::URL_STRING, utf16z(&url))])); } From 58854566ed89f6b27f304da15eb8922133fb8924 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:10:39 -0500 Subject: [PATCH 040/471] feat: add craft-format (versioned JSON file formats) Port from solidcraft: versioned JSON file format system with: - Format families with hand-written upgrade steps and folded formats - Pending changes that auto-number on release - Container format: content-addressed blobs (SHA-256) in ZIP packages - Newer file handling: read what you can, keep the rest No external dependencies beyond serde_json and craft-zip. Hostile-input bounded reading: sizes, counts, nesting capped. --- crates/format/Cargo.toml | 20 ++ crates/format/src/build.rs | 100 +++++++ crates/format/src/container.rs | 401 +++++++++++++++++++++++++++ crates/format/src/container_tests.rs | 67 +++++ crates/format/src/lib.rs | 252 +++++++++++++++++ crates/format/src/lib_tests.rs | 134 +++++++++ crates/format/src/newer.rs | 401 +++++++++++++++++++++++++++ crates/format/src/newer_tests.rs | 48 ++++ 8 files changed, 1423 insertions(+) create mode 100644 crates/format/Cargo.toml create mode 100644 crates/format/src/build.rs create mode 100644 crates/format/src/container.rs create mode 100644 crates/format/src/container_tests.rs create mode 100644 crates/format/src/lib.rs create mode 100644 crates/format/src/lib_tests.rs create mode 100644 crates/format/src/newer.rs create mode 100644 crates/format/src/newer_tests.rs diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml new file mode 100644 index 000000000..943e0b9b6 --- /dev/null +++ b/crates/format/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "craft-format" +description = "Versioned JSON file formats: the format tag, the registry of versions (hand-written steps, then one folder of changes per version, then the pending changes), the upgrade of an older file and the telling of a newer one" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde_json = { workspace = true } +# The container: content-addressed blobs (SHA-256) in a ZIP package. +sha2 = { version = "0.10", default-features = false, features = ["std"] } +craft-zip = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/format/src/build.rs b/crates/format/src/build.rs new file mode 100644 index 000000000..8fb879235 --- /dev/null +++ b/crates/format/src/build.rs @@ -0,0 +1,100 @@ +//! For a build script: the generated lists of a family's changes. Each change is one `.rs` file in +//! the family's folder, in `v/` once a release fixed its number, else in `pending/`; the lists +//! are generated so that a change adds a file and never edits a line another change also edits. +//! +//! The generated code, for the app to `include!`, is per family a `FOLDED` list (each folded +//! format and its parts) and a `PENDING` list, both prefixed by the family's `list_prefix`, then a +//! module per change file. It names the app's `Part` type, which the including module has in scope +//! (`type Part = craft_format::Part;`), and a change file's step as `step` (a +//! `pub fn step(v: &mut Value) -> Result<(), AppError>`, when the file has one). + +use std::fmt::Write as _; +use std::path::{Path, PathBuf}; + +/// A family's change files: their folder (relative to the crate's manifest), the last format +/// before them, and the prefix of the family's lists. +pub struct Family { + pub folder: &'static str, + pub base: u32, + pub list_prefix: &'static str, +} + +/// What [`lists`] made: the code to write, and the folders the build script reruns on. +pub struct Lists { + pub code: String, + pub watch: Vec, +} + +/// The `FOLDED` and `PENDING` lists of every family, and a module for each change file. Refuses +/// versions that are not contiguous from a family's base, a change file not named with lowercase +/// letters, digits and dashes, and one that doesn't start with a `//!` note. +pub fn lists(manifest: &Path, families: &[Family]) -> Result { + let mut code = String::new(); + let mut mods = String::new(); + let mut watch = Vec::new(); + for family in families { + let root = manifest.join(family.folder); + family_lists(family, &root, &mut code, &mut mods)?; + watch.push(root); + } + code.push_str(&mods); + Ok(Lists { code, watch }) +} + +/// A family's `FOLDED` and `PENDING` lists; its folded formats follow on from its base. +fn family_lists(family: &Family, root: &Path, out: &mut String, mods: &mut String) -> Result<(), String> { + let Family { folder, base, list_prefix } = family; + let mut folded: Vec<(u32, PathBuf)> = std::fs::read_dir(root) + .map_err(|e| format!("{}: {e}", root.display()))? + .filter_map(|entry| { + let path = entry.ok()?.path(); + let version = path.file_name()?.to_str()?.strip_prefix('v')?.parse().ok()?; + path.is_dir().then_some((version, path)) + }) + .collect(); + folded.sort(); + for (i, (version, _)) in folded.iter().enumerate() { + let expected = base + 1 + i as u32; + if *version != expected { + return Err(format!("{folder}/: expected v{expected}/ next, found v{version}/ (versions are contiguous)")); + } + } + // Module names start with the family's (`drawing_pending_…`), so two families' changes of one + // name never clash. + let module = list_prefix.to_ascii_lowercase(); + let _ = writeln!( + out, + "/// The changes of each format after the base, oldest first (`{folder}/v/`).\nconst {list_prefix}FOLDED: &[(u32, &[Part])] = &[" + ); + for (version, dir) in &folded { + let _ = writeln!(out, " ({version}, &["); + parts(dir, &format!("{module}v{version}"), out, mods)?; + out.push_str(" ]),\n"); + } + let _ = writeln!(out, "];\n\n/// The changes no release has a number for yet (`{folder}/pending/`).\nconst {list_prefix}PENDING: &[Part] = &["); + parts(&root.join("pending"), &format!("{module}pending"), out, mods)?; + out.push_str("];\n\n"); + Ok(()) +} + +/// The `Part`s of the change files in `dir`, in file-name order, and a module for each. +fn parts(dir: &Path, prefix: &str, out: &mut String, mods: &mut String) -> Result<(), String> { + let Ok(entries) = std::fs::read_dir(dir) else { return Ok(()) }; + let mut files: Vec = entries.filter_map(|e| e.ok().map(|e| e.path())).filter(|p| p.extension().is_some_and(|x| x == "rs")).collect(); + files.sort(); + for path in files { + let name = path.file_stem().and_then(|s| s.to_str()).unwrap_or_default().to_string(); + if name.is_empty() || !name.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') { + return Err(format!("{}: name a format change file with lowercase letters, digits and dashes", path.display())); + } + let text = std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display()))?; + if !text.trim_start().starts_with("//!") { + return Err(format!("{}: start a format change with a `//!` note", path.display())); + } + let module = format!("{prefix}_{}", name.replace('-', "_")); + let step = if text.contains("fn step(") { format!("Some({module}::step)") } else { "None".into() }; + let _ = writeln!(out, " Part {{ name: {name:?}, step: {step} }},"); + let _ = writeln!(mods, "#[path = {:?}]\nmod {module};", path.display().to_string()); + } + Ok(()) +} diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs new file mode 100644 index 000000000..a987bb1fb --- /dev/null +++ b/crates/format/src/container.rs @@ -0,0 +1,401 @@ +//! The file container: a ZIP package of named parts (the file's JSON, binary payloads, optional +//! caches), never extracted to filesystem paths. Payloads are content-addressed blobs, +//! `blobs/.`, stored once however often they are referred to. Every size is +//! bounded by the app's [`Limits`]: the file, each part, all parts expanded, the payloads resolved, +//! and the number of entries; a part is read no further than its declared size (a decompression +//! bomb stops there). What a part holds, and how a payload is encoded, is the app's. + +use std::collections::{BTreeMap, BTreeSet}; +use std::io::{Cursor, Read, Seek, SeekFrom, Write}; +use std::sync::Arc; + +use serde_json::Value; +use sha2::{Digest, Sha256}; + +use crate::FormatError; + +/// How hard the payloads are compressed: Fast favours interactive file operations; Compact trades +/// CPU time for space. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Compression { + #[default] + Fast, + Compact, +} + +/// An app's budgets for its containers. The functions are read each time they are needed, so a +/// limit the app lets the user raise takes effect at once. +#[derive(Clone, Copy)] +pub struct Limits { + /// The most entries a container holds. + pub max_entries: usize, + /// The most bytes all parts may expand to, and all payloads may resolve to. + pub max_expanded: fn() -> usize, + /// The most bytes a part of this name may expand to. + pub part_limit: fn(&str) -> usize, + /// The Deflate level of a compressed part of this name and size. + pub level: fn(&str, usize) -> i64, + /// Whether a container of this many bytes may be read, or why not. + pub check_size: fn(u64) -> Result<(), String>, + /// Whether the work is cancelled (a save the person stopped): a long part is written a piece + /// at a time and stops before the next piece when this says so. `None`: it cannot be + /// cancelled here. + pub cancelled: Option bool>, +} + +fn invalid(message: impl std::fmt::Display) -> FormatError { + FormatError::Invalid(message.to_string()) +} + +/// Whether the bytes start as a container does, whatever the file is called. +pub fn is_container(bytes: &[u8]) -> bool { + bytes.starts_with(b"PK\x03\x04") +} + +/// The SHA-256 of the bytes. +pub fn sha256(bytes: &[u8]) -> [u8; 32] { + Sha256::digest(bytes).into() +} + +fn hex(digest: &[u8; 32]) -> String { + use std::fmt::Write as _; + let mut out = String::with_capacity(64); + for byte in digest { + // Writing to a `String` cannot fail. + let _ = write!(&mut out, "{byte:02x}"); + } + out +} + +/// The SHA-256 of the bytes, in lowercase hexadecimal: a blob's name, and how a cache is keyed to +/// the file it was written for. +pub fn digest(bytes: &[u8]) -> String { + hex(&sha256(bytes)) +} + +/// One part a [`Writer`] wrote, with the SHA-256 of its uncompressed content. A caller that signs +/// a container uses these to bind the parts without reading the file back. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PartDigest { + pub name: String, + pub len: u64, + pub digest: [u8; 32], +} + +/// A container being written. +pub struct Writer { + zip: zip::ZipWriter>>, + names: BTreeSet, + expanded: usize, + resolved: usize, + limits: Limits, + /// Which written parts to record for a signature, and the records so far. + tracked: Option<(fn(&str) -> bool, Vec)>, +} + +impl Writer { + pub fn new(limits: Limits) -> Writer { + Writer { zip: zip::ZipWriter::new(Cursor::new(Vec::new())), names: BTreeSet::new(), expanded: 0, resolved: 0, limits, tracked: None } + } + + /// Record the parts written from now on whose name `include` accepts, with their SHA-256, for + /// [`Writer::take_parts`]. Used by a caller that signs the container. + pub fn track_parts(&mut self, include: fn(&str) -> bool) { + self.tracked = Some((include, Vec::new())); + } + + /// The tracked parts written so far, oldest first, and stop tracking. + pub fn take_parts(&mut self) -> Option> { + self.tracked.take().map(|(_, parts)| parts) + } + + /// The container's bytes. + pub fn finish(self) -> Result, FormatError> { + Ok(self.zip.finish().map_err(invalid)?.into_inner()) + } + + /// Add a part, compressed or stored. A name already written is written once. + pub fn part(&mut self, name: &str, bytes: &[u8], compressed: bool) -> Result<(), FormatError> { + self.part_with_digest(name, bytes, compressed, None) + } + + /// [`Writer::part`] with a digest the caller already worked out (a blob's name is its digest), + /// so a signed save does not hash the same bytes twice. + fn part_with_digest(&mut self, name: &str, bytes: &[u8], compressed: bool, digest: Option<[u8; 32]>) -> Result<(), FormatError> { + if bytes.len() > (self.limits.part_limit)(name) { + return Err(invalid("part too large")); + } + if !self.names.insert(name.to_owned()) { + return Ok(()); + } + self.expanded = self.expanded.checked_add(bytes.len()).ok_or_else(|| invalid("expanded size overflow"))?; + if self.expanded > (self.limits.max_expanded)() || self.names.len() > self.limits.max_entries { + return Err(invalid("package too large")); + } + if let Some((include, parts)) = &mut self.tracked + && include(name) + { + let digest = digest.unwrap_or_else(|| sha256(bytes)); + parts.push(PartDigest { name: name.to_owned(), len: bytes.len() as u64, digest }); + } + let method = if compressed { zip::CompressionMethod::Deflated } else { zip::CompressionMethod::Stored }; + let level = (self.limits.level)(name, bytes.len()); + // A part of 4 GiB or more (only with a raised file limit) needs a ZIP64 entry. + let options = zip::write::SimpleFileOptions::default() + .compression_method(method) + .compression_level(compressed.then_some(level)) + .large_file(bytes.len() as u64 >= u64::from(u32::MAX)); + self.zip.start_file(name, options).map_err(invalid)?; + // A piece at a time: a long compression (a large mesh or model cache) stops on Cancel + // within a piece instead of after the whole part. + for piece in bytes.chunks(1 << 20) { + if self.limits.cancelled.is_some_and(|cancelled| cancelled()) { + return Err(FormatError::Cancelled); + } + self.zip.write_all(piece).map_err(invalid)?; + } + Ok(()) + } + + /// Would `entries` more optional parts of `extra` bytes, stored uncompressed, still keep the + /// file within `budget` and its expanded size within the reader's budget? + pub fn room(&self, entries: usize, extra: usize, budget: usize) -> bool { + self.names.len().saturating_add(entries) <= self.limits.max_entries && extra <= self.space(entries, budget) + } + + /// The bytes `entries` more optional parts may hold together ([`Writer::room`]). + pub fn space(&self, entries: usize, budget: usize) -> usize { + if self.names.len().saturating_add(entries) > self.limits.max_entries { + return 0; + } + // The bytes written so far, plus what the open compressed part may still hold back. + let written = self.zip.get_ref().map_or(usize::MAX, |c| c.get_ref().len()).saturating_add(1 << 20); + let directory = self.names.len().saturating_add(entries).saturating_mul(256).saturating_add(65_536); + budget.saturating_sub(written.saturating_add(directory)).min((self.limits.max_expanded)().saturating_sub(self.expanded)) + } + + /// Add a payload as a blob, `blobs/.`, and say its name, for the JSON that + /// refers to it. + pub fn blob(&mut self, bytes: &[u8], extension: &str, compressed: bool) -> Result { + self.resolved = self + .resolved + .checked_add(bytes.len()) + .filter(|&n| n <= (self.limits.max_expanded)()) + .ok_or_else(|| invalid("resolved payloads too large"))?; + let digest = sha256(bytes); + let name = format!("blobs/{}.{extension}", hex(&digest)); + self.part_with_digest(&name, bytes, compressed, Some(digest))?; + Ok(Value::String(name)) + } +} + +/// A container being read, over a seekable source. Opening checks the directory (the entry count, +/// unique and plain names, every part's declared size), not the parts. +pub struct Reader { + zip: zip::ZipArchive, + blobs: BTreeMap>, + /// The bytes of payloads resolved so far, repeats counted: bounded by the expanded budget. + pub hydrated: usize, + document: Option>, + limits: Limits, +} + +impl Reader { + pub fn new(mut source: R, limits: Limits) -> Result { + let length = source.seek(SeekFrom::End(0)).map_err(invalid)?; + (limits.check_size)(length).map_err(invalid)?; + let tail_length = length.min(65_557) as usize; + source.seek(SeekFrom::Start(length - tail_length as u64)).map_err(invalid)?; + let mut tail = vec![0; tail_length]; + source.read_exact(&mut tail).map_err(invalid)?; + // zip's name index can coalesce duplicate central-directory entries. The bounded format + // does not need ZIP64/multiple disks: verify the declared entry count as well so + // duplicate names cannot silently select a payload. + let eocd = tail.windows(4).rposition(|v| v == b"PK\x05\x06").ok_or_else(|| invalid("missing ZIP directory"))?; + let end = tail.get(eocd..).filter(|v| v.len() >= 22).ok_or_else(|| invalid("truncated ZIP directory"))?; + let u16_at = |offset| -> Result { + let array: [u8; 2] = end.get(offset..offset + 2).ok_or_else(|| invalid("truncated ZIP directory"))?.try_into().map_err(invalid)?; + Ok(u16::from_le_bytes(array)) + }; + let declared = u16_at(10)? as usize; + if declared > limits.max_entries + || u16_at(4)? != 0 + || u16_at(6)? != 0 + || u16_at(8)? as usize != declared + || end.len() != 22 + u16_at(20)? as usize + { + return Err(invalid("too many entries or unsupported ZIP directory")); + } + // A container is a ZIP with nothing in front of it. Saying so skips zip's search for a + // prefix, which reads back through the stored parts and would load a large payload + // before a preview can be shown. + let config = zip::read::Config { archive_offset: zip::read::ArchiveOffset::Known(0) }; + let mut zip = zip::ZipArchive::with_config(config, source).map_err(invalid)?; + if u16_at(4)? != 0 + || u16_at(6)? != 0 + || u16_at(8)? as usize != zip.len() + || u16_at(10)? as usize != zip.len() + || end.len() != 22 + u16_at(20)? as usize + { + return Err(invalid("duplicate entries or unsupported ZIP directory")); + } + let mut names = BTreeSet::new(); + let mut total = 0u64; + for i in 0..zip.len() { + let part = zip.by_index_raw(i).map_err(invalid)?; + let name = part.name().map_err(invalid)?.into_owned(); + if name.is_empty() + || name.contains('\\') + || name.split('/').any(|c| c.is_empty() || c == "." || c == "..") + || !names.insert(name.to_owned()) + { + return Err(invalid("duplicate or invalid archive entry")); + } + total = total.checked_add(part.size()).ok_or_else(|| invalid("expanded size overflow"))?; + let limit = (limits.part_limit)(&name); + if part.size() > limit as u64 || total > (limits.max_expanded)() as u64 { + return Err(invalid("expanded package too large")); + } + } + Ok(Self { zip, blobs: BTreeMap::new(), hydrated: 0, document: None, limits }) + } + + /// The file's own part, `document.json`, read once and kept: provisional caches and the + /// authoritative reading are bound to the same bytes, even if another process overwrites the + /// open file in place. Blobs remain hash-checked. + pub fn document(&mut self, limit: usize) -> Result, FormatError> { + if self.document.is_none() { + self.document = Some(self.limited_part("document.json", limit)?.into()); + } + self.document.as_ref().filter(|d| d.len() <= limit).cloned().ok_or_else(|| invalid("metadata budget")) + } + + /// Whether the container has a part of this name. + pub fn has_part(&self, name: &str) -> bool { + self.zip.index_for_name(name).is_some() + } + + /// The names of every part, in the container's order. + pub fn part_names(&self) -> Vec { + self.zip.file_names().filter_map(|name| name.ok().map(|name| name.into_owned())).collect() + } + + /// The part of this name, at most `limit` bytes. + pub fn limited_part(&mut self, name: &str, limit: usize) -> Result, FormatError> { + let part = self.zip.by_name(name).map_err(|e| invalid(format!("required part `{name}`: {e}")))?; + let expected = part.size(); + if expected > limit as u64 { + return Err(invalid("part too large")); + } + let mut bytes = Vec::new(); + bytes.try_reserve_exact(expected as usize).map_err(|_| invalid("could not reserve required part"))?; + // A compressed part may expand beyond its declared size (a decompression bomb): stop + // one byte past it instead of at the limit. + part.take(expected + 1).read_to_end(&mut bytes).map_err(invalid)?; + if bytes.len() > limit || bytes.len() as u64 != expected { + return Err(invalid("part too large or length mismatch")); + } + Ok(bytes) + } + + /// The blob `value`'s `field` names, of this extension, within its part limit. + pub fn blob(&mut self, value: &Value, field: &str, extension: &str) -> Result<&[u8], FormatError> { + let limit = (self.limits.part_limit)(&format!("blob.{extension}")); + self.limited_blob(value, field, extension, limit) + } + + /// [`Reader::blob`] within `limit` bytes. Its hash is checked when it is first read, and every + /// reference counts against the resolved budget. + pub fn limited_blob(&mut self, value: &Value, field: &str, extension: &str, limit: usize) -> Result<&[u8], FormatError> { + let name = value.get(field).and_then(Value::as_str).ok_or_else(|| invalid(format!("missing `{field}` reference")))?; + let hash = name + .strip_prefix("blobs/") + .and_then(|n| n.strip_suffix(&format!(".{extension}"))) + .filter(|h| h.len() == 64 && h.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))) + .ok_or_else(|| invalid("invalid blob reference"))?; + if !self.blobs.contains_key(name) { + let bytes = self.limited_part(name, limit)?; + if digest(&bytes) != hash { + return Err(invalid(format!("blob hash mismatch: {name}"))); + } + self.blobs.insert(name.to_owned(), bytes); + } + let bytes = self.blobs.get(name).ok_or_else(|| invalid("missing blob"))?; + if bytes.len() > limit { + return Err(invalid("blob exceeds its resource budget")); + } + // Count repeated references too: a tiny archive must not expand into unbounded owned + // strings or arrays when the same blob is referenced repeatedly. + self.hydrated = self + .hydrated + .checked_add(bytes.len()) + .filter(|&n| n <= (self.limits.max_expanded)()) + .ok_or_else(|| invalid("resolved payloads too large"))?; + Ok(bytes) + } +} + +/// A small PNG picture of the file, kept in the container as a disposable cache keyed to the +/// exact file it shows: `cache/thumbnail.json` (its key) and `cache/thumbnail.png`. Reading it +/// never resolves a payload; a picture that isn't one, or is stale, is a miss. +#[derive(Clone, Copy, Debug)] +pub struct Thumbnail { + /// The key's format tag. + pub format: &'static str, + /// The build that wrote it: only the same build reads it back. + pub app: &'static str, + /// Its width and height in pixels. + pub size: [usize; 2], + /// The most bytes it may have. + pub max_bytes: usize, +} + +impl Thumbnail { + /// Whether the bytes start as a PNG of this size within the byte budget (not a decode: a + /// consumer decodes with its own dimension and allocation limits). + pub fn valid(&self, bytes: &[u8]) -> bool { + bytes.len() <= self.max_bytes + && bytes.get(..8) == Some(b"\x89PNG\r\n\x1a\n") + && bytes.get(8..16) == Some(b"\0\0\0\rIHDR") + && bytes.get(16..20) == Some((self.size[0] as u32).to_be_bytes().as_slice()) + && bytes.get(20..24) == Some((self.size[1] as u32).to_be_bytes().as_slice()) + } + + /// Add the picture, keyed to `document` (the file's own part), when it is one and there is + /// room for it within `budget`; otherwise leave it out, never failing the save. + pub fn write(&self, writer: &mut Writer, png: &[u8], document: &[u8], budget: usize) -> Result<(), FormatError> { + if !self.valid(png) { + return Ok(()); + } + let key = + serde_json::to_vec(&serde_json::json!({"format": self.format, "app": self.app, "document": digest(document), "payload": digest(png)})) + .map_err(invalid)?; + if !writer.room(2, key.len().saturating_add(png.len()), budget) { + return Ok(()); + } + writer.part("cache/thumbnail.json", &key, false)?; + writer.part("cache/thumbnail.png", png, false) + } + + /// The picture, when this build wrote it for exactly the file's own part (`document.json`, + /// read within `document_limit`). + pub fn read(&self, reader: &mut Reader, document_limit: usize) -> Result, FormatError> { + let key: Value = serde_json::from_slice(&reader.limited_part("cache/thumbnail.json", 4096)?).map_err(invalid)?; + let document = reader.document(document_limit)?; + if key.get("format").and_then(Value::as_str) != Some(self.format) + || key.get("app").and_then(Value::as_str) != Some(self.app) + || key.get("document").and_then(Value::as_str) != Some(digest(&document).as_str()) + { + return Err(invalid("thumbnail key")); + } + let png = reader.limited_part("cache/thumbnail.png", self.max_bytes)?; + if !self.valid(&png) || key.get("payload").and_then(Value::as_str) != Some(digest(&png).as_str()) { + return Err(invalid("thumbnail payload")); + } + Ok(png) + } +} + +#[cfg(test)] +#[path = "container_tests.rs"] +mod tests; diff --git a/crates/format/src/container_tests.rs b/crates/format/src/container_tests.rs new file mode 100644 index 000000000..bda37d000 --- /dev/null +++ b/crates/format/src/container_tests.rs @@ -0,0 +1,67 @@ +use super::*; + +const LIMITS: Limits = Limits { + max_entries: 8, + max_expanded: || 1 << 20, + part_limit: |name| if name.ends_with(".big") { 1 << 20 } else { 1024 }, + level: |_, _| 1, + check_size: |length| if length <= 1 << 20 { Ok(()) } else { Err("too large".into()) }, + cancelled: None, +}; + +/// Parts and blobs written are read back as they were; a blob is stored once, named by its hash, +/// and checked against it. +#[test] +fn parts_and_blobs_read_back() { + let mut writer = Writer::new(LIMITS); + writer.part("document.json", br#"{"a":1}"#, true).unwrap(); + let first = writer.blob(b"payload", "bin", false).unwrap(); + let again = writer.blob(b"payload", "bin", true).unwrap(); + assert_eq!(first, again, "the same payload is one blob"); + assert_eq!(first, Value::String(format!("blobs/{}.bin", digest(b"payload")))); + let bytes = writer.finish().unwrap(); + assert!(is_container(&bytes)); + let mut reader = Reader::new(Cursor::new(bytes), LIMITS).unwrap(); + assert_eq!(reader.part_names().len(), 2); + assert!(reader.has_part("document.json") && !reader.has_part("other.json")); + assert_eq!(&*reader.document(64).unwrap(), br#"{"a":1}"#); + let reference = serde_json::json!({"blob": first}); + assert_eq!(reader.blob(&reference, "blob", "bin").unwrap(), b"payload"); + assert!(reader.blob(&reference, "blob", "png").is_err(), "another extension"); + assert_eq!(reader.hydrated, 7); + reader.hydrated = (LIMITS.max_expanded)(); + assert!(reader.blob(&reference, "blob", "bin").unwrap_err().to_string().contains("resolved payloads too large")); +} + +/// A part past its limit, or more entries than the limit, are refused as they are written. +#[test] +fn budgets_are_kept_when_writing() { + let mut writer = Writer::new(LIMITS); + assert_eq!(writer.part("small.json", &[0; 1025], false), Err(FormatError::Invalid("part too large".into()))); + assert!(writer.part("large.big", &[0; 1025], true).is_ok()); + for i in 0..7 { + writer.part(&format!("{i}.json"), b"x", false).unwrap(); + } + assert_eq!(writer.part("one-more.json", b"x", false), Err(FormatError::Invalid("package too large".into()))); + assert!(!writer.room(1, 0, 1 << 20), "no room for an entry past the limit"); +} + +/// A thumbnail is kept only for the file it shows. +#[test] +fn a_thumbnail_is_keyed_to_its_file() { + let picture = Thumbnail { format: "app-thumbnail/1", app: "1.0", size: [2, 1], max_bytes: 1024 }; + let mut png = b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR".to_vec(); + png.extend_from_slice(&2u32.to_be_bytes()); + png.extend_from_slice(&1u32.to_be_bytes()); + assert!(picture.valid(&png)); + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{}", false).unwrap(); + picture.write(&mut writer, &png, b"{}", 8 << 20).unwrap(); + let mut reader = Reader::new(Cursor::new(writer.finish().unwrap()), LIMITS).unwrap(); + assert_eq!(picture.read(&mut reader, 64).unwrap(), png); + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{\"changed\":1}", false).unwrap(); + picture.write(&mut writer, &png, b"{}", 8 << 20).unwrap(); + let mut reader = Reader::new(Cursor::new(writer.finish().unwrap()), LIMITS).unwrap(); + assert!(picture.read(&mut reader, 64).is_err(), "stale"); +} diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs new file mode 100644 index 000000000..8eb495417 --- /dev/null +++ b/crates/format/src/lib.rs @@ -0,0 +1,252 @@ +//! Versioned file formats: what an app needs to read every file it ever wrote, and to open a file +//! a newer build of it wrote. +//! +//! A file starts with `"format": "N"`. A [`Family`] is one kind of file (an app's designs, +//! its drawings): its tag, its own numbers, and how a file of an older number is brought up to +//! this build's, step by step (N → N+1 → … → current). The app's code only knows the current +//! format; all translation lives in the steps: +//! - the steps written out by hand for the first formats, one per format ([`Formats::hand`]); +//! - then one folder per format, `v/`, one file per change in it ([`Part`]), each with a step +//! when files from before the change are rewritten as they open ([`Formats::folded`]); +//! - then the changes no release has a number for yet, in `pending/` ([`Formats::pending`]): +//! while there are any, the build writes the next number, with their names in the file's +//! `format_pending`, so a later build runs just the steps a file is missing. A release moves +//! them into the next `v/`. [`build`] lists the folders for a build script. +//! +//! A file a newer build wrote says so by a number past this build's or a change name this build +//! doesn't have ([`Family::newer`]); the app then reads what it can and keeps the rest +//! ([`newer`]). A file is saved as JSON, or in a [`container`] with its binary payloads. +//! +//! Steps are JSON-level (the file's shape) and return the app's own error type; every error this +//! crate makes is a [`FormatError`], which the app's error type takes `From`. + +use serde_json::Value; + +pub mod build; +pub mod container; +pub mod newer; + +/// Why a file is refused: its text says what is wrong, as the user reads it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum FormatError { + /// The file isn't one of the family's, or it is damaged. + Invalid(String), + /// The work was cancelled (a save the person stopped): nothing more was written. + Cancelled, +} + +impl std::fmt::Display for FormatError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + FormatError::Invalid(why) => f.write_str(why), + FormatError::Cancelled => f.write_str("cancelled"), + } + } +} + +impl std::error::Error for FormatError {} + +/// One JSON-level upgrade step: rewrites a file in place, or says why it can't. +pub type Step = fn(&mut Value) -> Result<(), E>; + +/// One change to a family's file format: a file in its `v/` or `pending/` folder. +pub struct Part { + /// The file's name, which a file written while the change was pending lists. + pub name: &'static str, + /// How a file from before the change is rewritten as it opens, if it is. + pub step: Option>, +} + +/// Another spelling of a change name that a file of one format may carry (a build that named the +/// change otherwise): read as the change it stands for, never written. +#[derive(Clone, Copy, Debug)] +pub struct Alias { + /// The format whose files may say it. + pub format: u32, + /// The spelling in the file. + pub name: &'static str, + /// The change it stands for. + pub known_as: &'static str, +} + +/// A family's upgrade path: the steps written out by hand, the folded formats after them (each a +/// list of changes) and the pending changes, which make the next format. +pub struct Formats<'a, E> { + /// Entry `i` turns format `i + 1` into `i + 2`. + pub hand: &'a [Step], + /// Each format after the hand-written ones, oldest first, and its changes. + pub folded: &'a [(u32, &'a [Part])], + /// The changes no release has a number for yet. + pub pending: &'a [Part], +} + +/// A kind of file whose format is versioned: its own format tag, its own numbers and its own +/// changes. Reading a file's format, telling a file a newer build saved and the upgrade are the +/// same for every family. +pub struct Family<'a, E> { + /// The app, as messages name it. + pub app: &'static str, + /// What a file of the family is called in messages. + pub noun: &'static str, + /// The tag before the number in a file's `format`. + pub prefix: &'static str, + /// The tag files saved under an older name carry: read, never written. + pub legacy_prefix: Option<&'static str>, + /// The format of a file that names none, if such files exist. + pub untagged: Option, + /// Other spellings of change names that files may carry. + pub aliases: &'a [Alias], + /// The upgrade path. + pub formats: Formats<'a, E>, +} + +/// How a file shows a newer build saved it ([`Family::newer`]). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Newer { + /// The number the file says. + pub format: u32, + /// The number this build reads up to. + pub reads_up_to: u32, + /// The changes the file names that this build doesn't have. + pub unknown_changes: Vec, +} + +impl Formats<'_, E> { + /// The format this build writes: the last folded one, or the next while changes are pending. + pub const fn current(&self) -> u32 { + self.hand.len() as u32 + 1 + self.folded.len() as u32 + if self.pending.is_empty() { 0 } else { 1 } + } + + /// The changes format `version` is made of, if it is made of changes. + fn parts(&self, version: u32) -> Option<&[Part]> { + let after = version.checked_sub(self.hand.len() as u32 + 2)? as usize; + match self.folded.get(after) { + Some((_, parts)) => Some(parts), + None if after == self.folded.len() && !self.pending.is_empty() => Some(self.pending), + None => None, + } + } + + /// Whether a change of this name is anywhere in this build's history. + fn knows_part_name(&self, name: &str) -> bool { + self.folded.iter().flat_map(|(_, parts)| *parts).chain(self.pending).any(|part| part.name == name) + } +} + +impl Family<'_, E> { + /// The format this build writes: the last folded one, or the next while changes are pending. + pub const fn current(&self) -> u32 { + self.formats.current() + } + + /// What a file of the family is called in messages. + pub const fn noun(&self) -> &'static str { + self.noun + } + + /// The names of the pending changes this build writes into a file (`format_pending`). + pub fn pending_names(&self) -> Vec { + self.formats.pending.iter().map(|p| p.name.to_string()).collect() + } + + /// The format tag of format `n`. + pub fn format_string(&self, n: u32) -> String { + format!("{}{n}", self.prefix) + } + + /// The version in one of this family's format tags; `None` for any other tag, another + /// family's included. + pub fn version_of(&self, s: &str) -> Option { + s.strip_prefix(self.prefix).or_else(|| self.legacy_prefix.and_then(|p| s.strip_prefix(p)))?.parse().ok() + } + + /// The change a name a file of format `version` lists stands for: its alias's, or itself. + /// An alias counts only while this build has the change it stands for, so recognising it + /// never grants support to another reader. + fn canonical_applied_name<'name>(&self, version: u32, name: &'name str) -> &'name str { + match self.aliases.iter().find(|a| a.format == version && a.name == name) { + Some(alias) if self.formats.knows_part_name(alias.known_as) => alias.known_as, + _ => name, + } + } + + /// What says a file of format `version` with the changes `applied` came from a newer build: + /// a number past this build's, or a change this build doesn't have anywhere in its history + /// (a branch's pending change may have landed at a later number). A list beside a format that + /// isn't made of changes means nothing (a tool or a test set an older number on a newer file). + /// `None` for a file this build fully knows. + pub fn newer(&self, version: u32, applied: Option<&[String]>) -> Option { + let current = self.current(); + let listed = applied.filter(|_| version > current || self.formats.parts(version).is_some()).unwrap_or_default(); + let unknown_changes: Vec = + listed.iter().filter(|name| !self.formats.knows_part_name(self.canonical_applied_name(version, name))).cloned().collect(); + (version > current || !unknown_changes.is_empty()).then_some(Newer { format: version, reads_up_to: current, unknown_changes }) + } +} + +impl> Family<'_, E> { + /// Read a file's format and take its `format_pending` out of its JSON, before anything else + /// in it is read: a file that isn't this family's is refused here. One a newer build saved is + /// not: [`Family::newer`] says so. + pub fn take_version(&self, v: &mut Value) -> Result<(u32, Option>), E> { + let (app, noun) = (self.app, self.noun); + let not_ours = |what: String| E::from(FormatError::Invalid(format!("not a {app} {noun} ({what})"))); + let Some(obj) = v.as_object() else { return Err(not_ours("expected a JSON object".into())) }; + let version = match obj.get("format") { + None => self.untagged.ok_or_else(|| not_ours("no format".into()))?, + Some(Value::String(f)) => self.version_of(f).ok_or_else(|| not_ours(format!("format `{f}`")))?, + Some(_) => return Err(not_ours("format is not a string".into())), + }; + let applied = take_names(v, "format_pending", noun).map_err(E::from)?; + if version == 0 { + return Err(not_ours("format 0".into())); + } + Ok((version, applied)) + } + + /// Bring a file of format `version` up to this build's; `applied` is its `format_pending` + /// (both from [`Family::take_version`]): the changes of its format it already has, when it + /// was written while they were pending. The others' steps run first, and already-applied + /// changes never run again at their later number. A file past this build's number has no + /// steps to run. + pub fn upgrade(&self, v: &mut Value, version: u32, applied: Option<&[String]>) -> Result<(), E> { + let formats = &self.formats; + if version == 0 { + return Err(E::from(FormatError::Invalid(format!("not a {} {} (format 0)", self.app, self.noun)))); + } + // A list beside a format that isn't made of changes is ignored, like any field an upgrade + // drops. + let applied = applied.filter(|_| formats.parts(version).is_some()); + let already_applied = + |part: &Part| applied.is_some_and(|names| names.iter().any(|name| self.canonical_applied_name(version, name) == part.name)); + if applied.is_some() { + for part in formats.parts(version).unwrap_or_default().iter().filter(|part| !already_applied(part)) { + part.step.map_or(Ok(()), |step| step(v))?; + } + } + for from in version..self.current() { + match formats.hand.get(from as usize - 1) { + Some(step) => step(v)?, + None => { + for part in formats.parts(from + 1).unwrap_or_default().iter().filter(|part| !already_applied(part)) { + part.step.map_or(Ok(()), |step| step(v))?; + } + } + } + } + Ok(()) + } +} + +/// Take a short list of names out of a file's JSON: its `format_pending` (the pending changes it +/// has). +fn take_names(v: &mut Value, key: &str, noun: &str) -> Result>, FormatError> { + let Some(list) = v.as_object_mut().and_then(|o| o.remove(key)) else { return Ok(None) }; + let invalid = || FormatError::Invalid(format!("damaged {noun}: `{key}` is not a short list of names")); + let list = list.as_array().filter(|l| l.len() <= 1000).ok_or_else(invalid)?; + list.iter().map(|n| n.as_str().filter(|n| n.len() <= 128).map(str::to_string).ok_or_else(invalid)).collect::>().map(Some) +} + +#[cfg(test)] +#[path = "lib_tests.rs"] +mod tests; diff --git a/crates/format/src/lib_tests.rs b/crates/format/src/lib_tests.rs new file mode 100644 index 000000000..6874ddbf4 --- /dev/null +++ b/crates/format/src/lib_tests.rs @@ -0,0 +1,134 @@ +use super::*; +use serde_json::json; + +/// The app's error, as an app has one. +#[derive(Debug, PartialEq)] +struct AppError(String); + +impl From for AppError { + fn from(e: FormatError) -> Self { + AppError(e.to_string()) + } +} + +/// Each step appends its name to the file's `log`, so the order steps ran in can be read back. +fn log(v: &mut Value, step: &str) { + if let Some(list) = v.get_mut("log").and_then(Value::as_array_mut) { + list.push(json!(step)); + } +} + +const HAND: [Step; 2] = [ + |v| { + log(v, "1to2"); + Ok(()) + }, + |v| { + log(v, "2to3"); + Ok(()) + }, +]; +const V4: [Part; 2] = [ + Part { + name: "a", + step: Some(|v| { + log(v, "a"); + Ok(()) + }), + }, + Part { name: "b", step: None }, +]; +const V5: [Part; 1] = [Part { + name: "c", + step: Some(|v| { + log(v, "c"); + Ok(()) + }), +}]; +const FOLDED: [(u32, &[Part]); 2] = [(4, &V4), (5, &V5)]; +const PENDING: [Part; 2] = [ + Part { + name: "d", + step: Some(|v| { + log(v, "d"); + Ok(()) + }), + }, + Part { + name: "e", + step: Some(|v| { + log(v, "e"); + Ok(()) + }), + }, +]; +const ALIASES: [Alias; 1] = [Alias { format: 5, name: "old-c", known_as: "c" }]; + +/// Format 1 untagged; hand steps to 3; folded 4 and 5; pending changes make 6. +const FAMILY: Family<'static, AppError> = Family { + app: "App", + noun: "design", + prefix: "app/", + legacy_prefix: Some("old/"), + untagged: Some(1), + aliases: &ALIASES, + formats: Formats { hand: &HAND, folded: &FOLDED, pending: &PENDING }, +}; + +fn steps_run(version: u32, applied: Option<&[String]>) -> Vec { + let mut v = json!({"log": []}); + FAMILY.upgrade(&mut v, version, applied).unwrap(); + v["log"].as_array().unwrap().iter().map(|s| s.as_str().unwrap().to_string()).collect() +} + +#[test] +fn tags_and_numbers() { + assert_eq!(FAMILY.current(), 6); + assert_eq!(FAMILY.format_string(6), "app/6"); + assert_eq!(FAMILY.version_of("app/4"), Some(4)); + assert_eq!(FAMILY.version_of("old/3"), Some(3), "the legacy tag is read"); + assert_eq!(FAMILY.version_of("other/3"), None); + assert_eq!(FAMILY.pending_names(), vec!["d".to_string(), "e".to_string()]); +} + +#[test] +fn a_file_is_taken_apart_or_refused_with_the_apps_words() { + let mut v = json!({"format": "app/6", "format_pending": ["d"], "x": 1}); + assert_eq!(FAMILY.take_version(&mut v), Ok((6, Some(vec!["d".to_string()])))); + assert!(v.get("format_pending").is_none(), "taken out"); + assert_eq!(FAMILY.take_version(&mut json!({})), Ok((1, None)), "untagged"); + let refused = |mut v: Value| FAMILY.take_version(&mut v).unwrap_err().0; + assert_eq!(refused(json!([])), "not a App design (expected a JSON object)"); + assert_eq!(refused(json!({"format": "other/2"})), "not a App design (format `other/2`)"); + assert_eq!(refused(json!({"format": 2})), "not a App design (format is not a string)"); + assert_eq!(refused(json!({"format": "app/0"})), "not a App design (format 0)"); + assert_eq!(refused(json!({"format": "app/2", "format_pending": [1]})), "damaged design: `format_pending` is not a short list of names"); +} + +#[test] +fn every_step_runs_once_in_order() { + assert_eq!(steps_run(1, None), ["1to2", "2to3", "a", "c", "d", "e"]); + assert_eq!(steps_run(4, None), ["c", "d", "e"]); + assert_eq!(steps_run(6, None), Vec::::new()); + // A file written while `d` was pending has it: only `e` runs. + assert_eq!(steps_run(6, Some(&["d".to_string()])), ["e"]); + // A file of format 5 written while `c` was still pending, without it: `c` runs first. + assert_eq!(steps_run(5, Some(&[])), ["c", "d", "e"]); + // Under its other spelling `c` counts as had. + assert_eq!(steps_run(5, Some(&["old-c".to_string()])), ["d", "e"]); + // A list beside a hand-written format means nothing. + assert_eq!(steps_run(2, Some(&["d".to_string()])), ["2to3", "a", "c", "d", "e"]); + let mut v = json!({}); + assert_eq!(FAMILY.upgrade(&mut v, 0, None), Err(AppError("not a App design (format 0)".into()))); +} + +#[test] +fn a_newer_file_is_told_by_its_number_or_an_unknown_change() { + assert_eq!(FAMILY.newer(6, None), None); + assert_eq!(FAMILY.newer(6, Some(&["d".to_string()])), None); + assert_eq!(FAMILY.newer(7, None), Some(Newer { format: 7, reads_up_to: 6, unknown_changes: Vec::new() })); + assert_eq!(FAMILY.newer(6, Some(&["z".to_string()])), Some(Newer { format: 6, reads_up_to: 6, unknown_changes: vec!["z".to_string()] })); + assert_eq!(FAMILY.newer(5, Some(&["old-c".to_string()])), None, "an alias of a known change"); + assert_eq!(FAMILY.newer(4, Some(&["old-c".to_string()])), Some(Newer { format: 4, reads_up_to: 6, unknown_changes: vec!["old-c".to_string()] })); + assert_eq!(FAMILY.newer(2, Some(&["z".to_string()])), None, "a list beside a hand-written format means nothing"); +} diff --git a/crates/format/src/newer.rs b/crates/format/src/newer.rs new file mode 100644 index 000000000..e9cb51eb1 --- /dev/null +++ b/crates/format/src/newer.rs @@ -0,0 +1,401 @@ +//! A file a newer build saved: it opens, with a warning, and saving it loses nothing. +//! +//! A newer format number, or a change the file names that this build doesn't have, says a newer +//! build saved the file ([`crate::Family::newer`]). It may use nothing this build lacks, so the app +//! reads it as usual. When part of it can't be read, that part stays out of the way and the rest +//! opens: the app reads the elements of its lists it can ([`NewerFile::keep_element`]), then any +//! other top-level section it can't read is left out whole ([`leave_out_unreadable`], +//! [`read_around`]), and a field it doesn't know, which typed reading would drop, is noticed by +//! comparing the file with what was read ([`NewerFile::compare`]). +//! +//! All of these are named in [`NewerFile::not_understood`] and go back into the file when it is +//! saved ([`NewerFile::restore_into`]), as they came. What can't be put back because the file was +//! changed around it is what `restore_into` returns. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use serde_json::{Map, Value, json}; + +use crate::Newer; + +/// The most sections left out one at a time to find the one a file can't be read with. +const MAX_SECTION_TRIES: usize = 64; + +/// What the file says and holds that this build doesn't read. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct NewerFile { + /// The app, as the notice names it. + pub app: &'static str, + /// The number the file says. + pub format: u32, + /// The number this build reads up to. + pub reads_up_to: u32, + /// The changes the file names that this build doesn't have. + pub unknown_changes: Vec, + /// What this build couldn't read, by name. + pub not_understood: Vec, + /// Fields known records lost in typed reading. + kept: Option, + /// Top-level sections left out whole. + sections: Vec
, + /// Elements left out of a top-level list: its key and the element. + elements: Vec<(String, Value)>, + /// The parts of a container this build has no use for: name and bytes. A blob only one of the + /// kept pieces refers to is one of them. + pub parts: Vec<(String, Arc<[u8]>)>, + /// The file's `container.json`, when this build doesn't know what it says. + pub container: Option, +} + +#[derive(Clone, Debug, PartialEq)] +struct Section { + key: String, + value: Value, + /// This build's record of the section, without the file's: while it is unchanged, the file's + /// goes back. + empty: Option, +} + +/// The fields of a record that typed reading dropped. +#[derive(Clone, Debug, PartialEq)] +enum Kept { + Field(Value), + Fields(BTreeMap), + Items(Vec<(Anchor, Kept)>), +} + +/// Which element of a list some kept fields belong to. +#[derive(Clone, Debug, PartialEq)] +enum Anchor { + Id(u64), + Name(String), + /// Its place, good only while the list keeps its length. + Index { + at: usize, + of: usize, + }, +} + +impl NewerFile { + /// A file of `app` that `newer` says a newer build saved, nothing left out of it yet. + pub fn new(app: &'static str, newer: &Newer) -> NewerFile { + NewerFile { + app, + format: newer.format, + reads_up_to: newer.reads_up_to, + unknown_changes: newer.unknown_changes.clone(), + ..NewerFile::default() + } + } + + /// What to tell the user, whatever else is said. + pub fn notice(&self) -> String { + let (app, reads, format) = (self.app, self.reads_up_to, self.format); + match self.unknown_changes.first() { + Some(change) if format <= reads => { + format!("Saved by a newer {app} (format {format} with `{change}`, which this version doesn't know). If something's missing, update.") + } + _ => format!("Saved by a newer {app} (format {format}, this version reads up to {reads}). If something's missing, update."), + } + } + + /// Every line to report: the notice, then what couldn't be read, by name. + pub fn warnings(&self) -> Vec { + let unread = self.not_understood.iter().map(|what| format!("Not read: {what}. Saving keeps it.")); + std::iter::once(self.notice()).chain(unread).collect() + } + + /// What a reply says about it. + pub fn report(&self) -> Value { + json!({ + "format": self.format, + "reads_up_to": self.reads_up_to, + "unknown_changes": self.unknown_changes, + "not_understood": self.not_understood, + "notice": self.notice(), + }) + } + + /// Whether the next save needs a container to keep what the file holds: plain JSON has no room + /// for parts beside the file's own. + pub fn needs_container(&self) -> bool { + !self.parts.is_empty() + } + + /// Whether anything kept back names this text (a blob's name in a container). + pub fn mentions(&self, text: &str) -> bool { + fn kept(k: &Kept, text: &str) -> bool { + match k { + Kept::Field(v) => value_mentions(v, text), + Kept::Fields(m) => m.values().any(|x| kept(x, text)), + Kept::Items(l) => l.iter().any(|(_, x)| kept(x, text)), + } + } + self.kept.as_ref().is_some_and(|k| kept(k, text)) + || self.sections.iter().any(|s| value_mentions(&s.value, text)) + || self.elements.iter().any(|(_, v)| value_mentions(v, text)) + } + + /// Say that a section the reader couldn't finish reading is left out. `empty` is what the + /// app writes for the section while it holds nothing, if anything. + pub fn keep_section(&mut self, key: &str, value: Value, empty: Option, why: &str) { + self.not_understood.push(format!("section `{key}` ({why})")); + self.sections.push(Section { key: key.into(), value, empty }); + } + + /// Say that an element of the top-level list `key` is left out, as `note` names it; it goes + /// back at the end of the list when the file is saved. + pub fn keep_element(&mut self, key: &str, element: Value, note: String) { + self.not_understood.push(note); + self.elements.push((key.into(), element)); + } + + /// Compare the file as it reads (`file`, after the same upgrade as what was read) with what was + /// read of it (`known`, its typed record written out): what `known` lacks is kept and named. + /// An object whose `type` is `placeholder_tag` (an element the app kept whole) and the fields + /// in `container_only` (what a container adds beside the file's own) are not compared; nor + /// are the top level's `format` and `format_pending`. + pub fn compare(&mut self, file: &Value, known: &Value, placeholder_tag: &str, container_only: &[&str]) { + let mut paths = Vec::new(); + self.kept = vanished(file, known, "", &mut paths, placeholder_tag, container_only); + self.not_understood.extend(paths); + for section in &mut self.sections { + section.empty = known.get(§ion.key).cloned(); + } + } + + /// Write back what was kept of the file into `value`, the file as this build writes it. + /// Returns what couldn't go back, because the file was changed around it. + pub fn restore_into(&self, value: &mut Value) -> Vec { + let mut lost = Vec::new(); + if let Some(kept) = &self.kept { + apply(kept, value, "", &mut lost); + } + if let Some(top) = value.as_object_mut() { + for section in &self.sections { + if top.get(§ion.key) == section.empty.as_ref() { + top.insert(section.key.clone(), section.value.clone()); + } else { + lost.push(format!("section `{}`, replaced by what was edited here", section.key)); + } + } + for (key, element) in &self.elements { + let name = element.get("name"); + match top.entry(key.clone()).or_insert_with(|| json!([])) { + Value::Array(list) if name.is_none() || !list.iter().any(|x| x.get("name") == name) => list.push(element.clone()), + _ => lost.push(format!("`{key}` element {}, replaced by what was made here", name.and_then(Value::as_str).unwrap_or("unnamed"))), + } + } + } + lost + } +} + +fn value_mentions(v: &Value, text: &str) -> bool { + match v { + Value::String(s) => s == text, + Value::Array(a) => a.iter().any(|x| value_mentions(x, text)), + Value::Object(o) => o.values().any(|x| value_mentions(x, text)), + _ => false, + } +} + +/// The first line of a reader's error, without the list of every kind it knows, and without the +/// `" file: "` its reader starts it with. +pub fn short<'a>(why: &'a str, noun: &str) -> &'a str { + let why = why.split(", expected").next().unwrap_or(why); + why.strip_prefix(&format!("{noun} file: ")).unwrap_or(why) +} + +/// Leave a section out and keep it. A section the file needs is left empty. +fn move_out(top: &mut Map, key: &str, why: &str, noun: &str, file: &mut NewerFile) { + let Some(value) = top.remove(key) else { return }; + file.not_understood.push(format!("section `{key}` ({})", short(why, noun))); + file.sections.push(Section { key: key.into(), value, empty: None }); +} + +/// Every top-level section of `top` that doesn't read by itself (`piece`, the app's reading of a +/// file holding just that section) is left out and kept in `file`. `noun` is what a file of the +/// app's is called, which its reader's errors start with. +pub fn leave_out_unreadable( + top: &mut Map, + file: &mut NewerFile, + noun: &str, + piece: impl Fn(&str, &Value) -> Result<(), E>, +) { + let keys: Vec = top.keys().cloned().collect(); + for key in &keys { + let value = top.get(key).cloned().unwrap_or_default(); + if let Err(why) = piece(key, &value) { + move_out(top, key, &why.to_string(), noun, file); + } + } +} + +/// Read `top` with `attempt`, leaving out the sections it can't be read with: if it doesn't read, +/// each section but the `core` ones (which the file can't be read without) is tried left out alone, +/// and as a last resort every one of them is. What is left out is kept in `file`. The error is the +/// first attempt's, when even the core alone doesn't read. +pub fn read_around( + top: &mut Map, + file: &mut NewerFile, + core: &[&str], + noun: &str, + attempt: impl Fn(&Map) -> Result, +) -> Result { + let first = match attempt(top) { + Ok(doc) => return Ok(doc), + Err(why) => why.to_string(), + }; + // Sections that are fine by themselves but not together with the rest. + let keys: Vec = top.keys().filter(|k| !core.contains(&k.as_str())).cloned().collect(); + // Each try reads the whole file: a file of many sections goes straight to the last resort. + for key in keys.iter().filter(|_| keys.len() <= MAX_SECTION_TRIES) { + let mut without = top.clone(); + without.remove(key); + if let Ok(doc) = attempt(&without) { + move_out(top, key, &first, noun, file); + return Ok(doc); + } + } + let mut bare = top.clone(); + bare.retain(|key, _| core.contains(&key.as_str())); + match attempt(&bare) { + Ok(doc) => { + for key in &keys { + move_out(top, key, &first, noun, file); + } + Ok(doc) + } + Err(_) => Err(first), + } +} + +/// A value a writer leaves out without losing anything: not worth telling anyone about. +pub fn looks_default(v: &Value) -> bool { + match v { + Value::Null => true, + Value::Bool(b) => !b, + Value::Number(n) => n.as_f64() == Some(0.0), + Value::String(s) => s.is_empty(), + Value::Array(a) => a.is_empty(), + Value::Object(o) => o.is_empty(), + } +} + +/// What `raw` has that `known`, what was read of it, doesn't. `paths` gets those fields' names. +fn vanished(raw: &Value, known: &Value, path: &str, paths: &mut Vec, placeholder_tag: &str, container_only: &[&str]) -> Option { + match (raw, known) { + (Value::Object(raw), Value::Object(known)) => { + if known.get("type").and_then(Value::as_str) == Some(placeholder_tag) { + return None; + } + let mut kept = BTreeMap::new(); + for (key, value) in raw { + if container_only.contains(&key.as_str()) || (path.is_empty() && matches!(key.as_str(), "format" | "format_pending")) { + continue; + } + let inner = if path.is_empty() { key.clone() } else { format!("{path}.{key}") }; + match known.get(key) { + None => { + if !looks_default(value) { + paths.push(format!("field `{inner}`")); + } + kept.insert(key.clone(), Kept::Field(value.clone())); + } + Some(known) => { + if let Some(k) = vanished(value, known, &inner, paths, placeholder_tag, container_only) { + kept.insert(key.clone(), k); + } + } + } + } + (!kept.is_empty()).then_some(Kept::Fields(kept)) + } + (Value::Array(raw), Value::Array(known)) => { + let mut items = Vec::new(); + for (at, element) in raw.iter().enumerate() { + let anchor = anchor_of(element).unwrap_or(Anchor::Index { at, of: raw.len() }); + let Some(partner) = find(known, &anchor) else { continue }; + let label = match (element.get("name").and_then(Value::as_str), &anchor) { + (Some(name), _) => format!("{path}[{name}]"), + (None, Anchor::Id(id)) => format!("{path}[{id}]"), + _ => format!("{path}[{at}]"), + }; + if let Some(k) = vanished(element, partner, &label, paths, placeholder_tag, container_only) { + items.push((anchor, k)); + } + } + (!items.is_empty()).then_some(Kept::Items(items)) + } + _ => None, + } +} + +fn anchor_of(element: &Value) -> Option { + if let Some(id) = element.get("id").and_then(Value::as_u64) { + return Some(Anchor::Id(id)); + } + element.get("name").and_then(Value::as_str).map(|name| Anchor::Name(name.to_string())) +} + +fn find<'a>(list: &'a [Value], anchor: &Anchor) -> Option<&'a Value> { + match anchor { + Anchor::Id(id) => list.iter().find(|x| x.get("id").and_then(Value::as_u64) == Some(*id)), + Anchor::Name(name) => list.iter().find(|x| x.get("name").and_then(Value::as_str) == Some(name)), + Anchor::Index { at, of } => (list.len() == *of).then(|| list.get(*at)).flatten(), + } +} + +fn find_mut<'a>(list: &'a mut [Value], anchor: &Anchor) -> Option<&'a mut Value> { + match anchor { + Anchor::Id(id) => list.iter_mut().find(|x| x.get("id").and_then(Value::as_u64) == Some(*id)), + Anchor::Name(name) => list.iter_mut().find(|x| x.get("name").and_then(Value::as_str) == Some(name)), + Anchor::Index { at, of } => (list.len() == *of).then(|| list.get_mut(*at)).flatten(), + } +} + +/// Put the kept fields back. An element that is gone was deleted here, its fields with it; one +/// whose place can't be told any more, and a field whose record is gone, go into `lost`. +fn apply(kept: &Kept, value: &mut Value, path: &str, lost: &mut Vec) { + match kept { + Kept::Field(_) => {} + Kept::Fields(fields) => { + let Some(object) = value.as_object_mut() else { + lost.push(format!("field `{path}`")); + return; + }; + for (key, sub) in fields { + let inner = if path.is_empty() { key.clone() } else { format!("{path}.{key}") }; + match (sub, object.get_mut(key)) { + (Kept::Field(v), None) => { + object.insert(key.clone(), v.clone()); + } + (Kept::Field(_), Some(_)) => {} + (sub, Some(inner_value)) => apply(sub, inner_value, &inner, lost), + // The list it was in is empty and not written: its elements were deleted. + (Kept::Items(_), None) => {} + (_, None) => lost.push(format!("field `{inner}`")), + } + } + } + Kept::Items(items) => { + let Some(list) = value.as_array_mut() else { + lost.push(format!("field `{path}`")); + return; + }; + for (anchor, sub) in items { + match (find_mut(list, anchor), anchor) { + (Some(element), _) => apply(sub, element, path, lost), + (None, Anchor::Index { at, .. }) => lost.push(format!("element {at} of `{path}`")), + (None, _) => {} + } + } + } + } +} + +#[cfg(test)] +#[path = "newer_tests.rs"] +mod tests; diff --git a/crates/format/src/newer_tests.rs b/crates/format/src/newer_tests.rs new file mode 100644 index 000000000..90e5b0037 --- /dev/null +++ b/crates/format/src/newer_tests.rs @@ -0,0 +1,48 @@ +use super::*; + +fn newer() -> Newer { + Newer { format: 9, reads_up_to: 8, unknown_changes: Vec::new() } +} + +/// What typed reading dropped is named and goes back on saving, where it was. +#[test] +fn dropped_fields_are_named_and_put_back() { + let file = json!({"name": "A", "format": "app/9", "items": [{"id": 1, "size": 2, "colour": "red"}], "extra": {"x": 1}}); + let known = json!({"name": "A", "items": [{"id": 1, "size": 2}]}); + let mut said = NewerFile::new("App", &newer()); + said.compare(&file, &known, "placeholder", &[]); + assert_eq!(said.not_understood, ["field `extra`", "field `items[1].colour`"]); + let mut saved = json!({"name": "A", "items": [{"id": 1, "size": 3}]}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"name": "A", "items": [{"id": 1, "size": 3, "colour": "red"}], "extra": {"x": 1}})); + assert_eq!(said.notice(), "Saved by a newer App (format 9, this version reads up to 8). If something's missing, update."); +} + +/// A section that can't be read is left out, kept, and goes back while it is unchanged here. +#[test] +fn unreadable_sections_are_left_out_and_kept() { + let mut top = match json!({"name": "A", "good": 1, "bad": 2}) { + Value::Object(m) => m, + _ => unreachable!(), + }; + let mut said = NewerFile::new("App", &newer()); + leave_out_unreadable(&mut top, &mut said, "design", |key: &str, _: &Value| if key == "bad" { Err("design file: no") } else { Ok(()) }); + assert!(!top.contains_key("bad")); + assert_eq!(said.not_understood, ["section `bad` (no)"]); + let read = + read_around( + &mut top, + &mut said, + &["name"], + "design", + |top: &Map| { + if top.contains_key("good") { Err("design file: clash") } else { Ok(top.len()) } + }, + ); + assert_eq!(read, Ok(1)); + assert_eq!(said.not_understood, ["section `bad` (no)", "section `good` (clash)"]); + let mut saved = json!({"name": "A"}); + said.compare(&saved.clone(), &saved.clone(), "placeholder", &[]); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"name": "A", "good": 1, "bad": 2})); +} From 939146a5b159a268ee5fc67e26b3dc915b4d4094 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:11 +0000 Subject: [PATCH 041/471] tga dpx: share uncompressed RGBA8 export --- crates/dpx/src/lib.rs | 3 + crates/dpx/src/rgba8_dpx10.rs | 105 ++++++++++++++++++++++++++++++++++ crates/tga/src/lib.rs | 3 + crates/tga/src/rgba8_raw.rs | 60 +++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 crates/dpx/src/rgba8_dpx10.rs create mode 100644 crates/tga/src/rgba8_raw.rs diff --git a/crates/dpx/src/lib.rs b/crates/dpx/src/lib.rs index a762566f8..26d9e8656 100644 --- a/crates/dpx/src/lib.rs +++ b/crates/dpx/src/lib.rs @@ -34,6 +34,9 @@ //! Input is treated as hostile: every header read is bounds-checked, the picture size is capped //! by [`Limits`] and the image data must be present in full before anything is allocated. +/// RGBA8-to-10-bit DPX encoding. +pub mod rgba8_dpx10; + #[cfg(test)] mod tests; diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs new file mode 100644 index 000000000..6ec5e23b4 --- /dev/null +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -0,0 +1,105 @@ +//! Uncompressed RGB(A) output from top-down RGBA8 pixels. +/// Why the supplied frame cannot be encoded. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rgba8EncodeError { + /// Empty, overflowing, or mismatched dimensions. + FrameSize, + /// A dimension cannot fit the output format. + TooLarge, +} +fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { + let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; + if w == 0 || h == 0 || rgba.len() != n { + return Err(Rgba8EncodeError::FrameSize); + } + Ok(n) +} +/// Header size of the DPX files we write (file + image + orientation + film + TV headers). +const DPX_HEADER: usize = 2048; + +/// A DPX file: 10-bit RGB (8-bit values widened by bit replication, so 0 → 0 and 255 → 1023). +pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::result::Result, Rgba8EncodeError> { + let n = checked_len(rgba, w, h)?; + // one 32-bit word per pixel; `n` is already `w * h * 4` + let size = n.checked_add(DPX_HEADER).ok_or_else(|| Rgba8EncodeError::FrameSize)?; + let file_size = u32::try_from(size).map_err(|_| Rgba8EncodeError::TooLarge)?; + // SMPTE ST 268: undefined numeric fields are all ones, undefined text fields are NUL + let mut hd = vec![0xFF_u8; DPX_HEADER]; + for (a, b) in [ + (36, 660), // file name, creation time, creator, project, copyright + (664, 768), // reserved + (1356, 1408), // reserved + (1432, 1620), // source file name, time, input device, serial number + (1644, 1664), // reserved + (1664, 1712), // film manufacturer, type, offset, prefix, count, format + (1732, 1920), // frame id, slate, reserved + (1972, 2048), // reserved + ] { + hd[a..b].fill(0); + } + for e in 0..8 { + let d = 780 + e * 72 + 40; + hd[d..d + 32].fill(0); // element descriptions + } + let put32 = |hd: &mut Vec, at: usize, v: u32| hd[at..at + 4].copy_from_slice(&v.to_be_bytes()); + let put16 = |hd: &mut Vec, at: usize, v: u16| hd[at..at + 2].copy_from_slice(&v.to_be_bytes()); + // file information header + hd[0..4].copy_from_slice(b"SDPX"); + put32(&mut hd, 4, DPX_HEADER as u32); // offset to the image data + hd[8..16].copy_from_slice(b"V2.0\0\0\0\0"); + put32(&mut hd, 16, file_size); + put32(&mut hd, 20, 1); // ditto key: new frame + put32(&mut hd, 24, 1664); // generic header length + put32(&mut hd, 28, 384); // industry header length + put32(&mut hd, 32, 0); // user data length + let creator = creator.as_bytes(); + let n = creator.len().min(100); + hd[160..160 + n].copy_from_slice(&creator[..n]); + // image information header + put16(&mut hd, 768, 0); // orientation: left to right, top to bottom + put16(&mut hd, 770, 1); // one image element + put32(&mut hd, 772, w); + put32(&mut hd, 776, h); + let el = 780; + put32(&mut hd, el, 0); // unsigned + put32(&mut hd, el + 4, 0); // reference low data code + put32(&mut hd, el + 12, 1023); // reference high data code + hd[el + 20] = 50; // descriptor: RGB + hd[el + 21] = 6; // transfer: ITU-R BT.709 + hd[el + 22] = 6; // colorimetric: ITU-R BT.709 + hd[el + 23] = 10; // bit depth + put16(&mut hd, el + 24, 1); // packing: filled to 32-bit words, method A + put16(&mut hd, el + 26, 0); // no encoding (uncompressed) + put32(&mut hd, el + 28, DPX_HEADER as u32); // offset to the element's data + put32(&mut hd, el + 32, 0); // end-of-line padding + put32(&mut hd, el + 36, 0); // end-of-image padding + // image orientation header: no offset, square pixels + put32(&mut hd, 1408, 0); + put32(&mut hd, 1412, 0); + put32(&mut hd, 1424, w); + put32(&mut hd, 1428, h); + put32(&mut hd, 1628, 1); + put32(&mut hd, 1632, 1); + // television header: progressive + hd[1928] = 0; + let mut out = Vec::with_capacity(size); + out.extend_from_slice(&hd); + let ten = |v: u8| (u32::from(v) << 2) | (u32::from(v) >> 6); + for &[r, g, b, _] in rgba.as_chunks::<4>().0 { + out.extend_from_slice(&((ten(r) << 22) | (ten(g) << 12) | (ten(b) << 2)).to_be_bytes()); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn packed_rgb_and_creator() { + let bytes = encode_rgba8_dpx10(&[255, 0, 128, 255], 1, 1, "FilmCraft").unwrap(); + assert_eq!(&bytes[..4], b"SDPX"); + assert_eq!(&bytes[160..169], b"FilmCraft"); + assert_eq!(&bytes[2048..], &((1023u32 << 22) | (514u32 << 2)).to_be_bytes()); + assert_eq!(encode_rgba8_dpx10(&[], 0, 1, ""), Err(Rgba8EncodeError::FrameSize)); + } +} diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index ac9d21497..3a92052ff 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -18,6 +18,9 @@ //! Input is treated as hostile: the picture size is capped by [`Limits`], and a run-length file //! must be long enough to hold the pixels its header declares before anything is allocated. +/// Uncompressed RGBA8-to-Targa encoding. +pub mod rgba8_raw; + #[cfg(test)] mod tests; diff --git a/crates/tga/src/rgba8_raw.rs b/crates/tga/src/rgba8_raw.rs new file mode 100644 index 000000000..acec4b9bc --- /dev/null +++ b/crates/tga/src/rgba8_raw.rs @@ -0,0 +1,60 @@ +//! Uncompressed RGB(A) output from top-down RGBA8 pixels. +/// Why the supplied frame cannot be encoded. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rgba8EncodeError { + /// Empty, overflowing, or mismatched dimensions. + FrameSize, + /// A dimension cannot fit the 16-bit Targa header. + Dimensions, +} +fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { + let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; + if w == 0 || h == 0 || rgba.len() != n { + return Err(Rgba8EncodeError::FrameSize); + } + Ok(n) +} +/// An uncompressed Targa file: 24-bit BGR, or 32-bit BGRA with `alpha` (straight alpha, as the +/// frame carries it). +pub fn encode_rgba8_raw(rgba: &[u8], w: u32, h: u32, alpha: bool) -> std::result::Result, Rgba8EncodeError> { + checked_len(rgba, w, h)?; + let (tw, th) = match (u16::try_from(w), u16::try_from(h)) { + (Ok(tw), Ok(th)) => (tw, th), + _ => return Err(Rgba8EncodeError::Dimensions), + }; + let bpp: usize = if alpha { 4 } else { 3 }; + let row = w as usize * 4; + let mut out = Vec::with_capacity(18 + w as usize * h as usize * bpp + 26); + out.extend_from_slice(&[0, 0, 2]); // no ID, no colour map, uncompressed true-colour + out.extend_from_slice(&[0; 5]); // colour map specification (unused) + out.extend_from_slice(&[0, 0, 0, 0]); // x / y origin + out.extend_from_slice(&tw.to_le_bytes()); + out.extend_from_slice(&th.to_le_bytes()); + out.push(if alpha { 32 } else { 24 }); + out.push(if alpha { 8 } else { 0 }); // attribute bits; bottom-left origin + // bottom-left origin: the last frame row comes first + for line in rgba.chunks_exact(row).rev() { + for &[r, g, b, a] in line.as_chunks::<4>().0 { + out.extend_from_slice(&[b, g, r]); + if alpha { + out.push(a); + } + } + } + // TGA 2.0 footer without extension or developer areas + out.extend_from_slice(&[0; 8]); + out.extend_from_slice(b"TRUEVISION-XFILE.\0"); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn raw_round_trip() { + let bytes = encode_rgba8_raw(&[1, 2, 3, 4], 1, 1, true).unwrap(); + assert_eq!(&bytes[18..22], &[3, 2, 1, 4]); + assert_eq!(&bytes[bytes.len() - 18..], b"TRUEVISION-XFILE.\0"); + assert_eq!(encode_rgba8_raw(&[], 0, 1, false), Err(Rgba8EncodeError::FrameSize)); + } +} From 9c3c82e1c63e53248f99531a71cb8401f5c76150 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:16:04 +0000 Subject: [PATCH 042/471] audio: fix PCM export module documentation --- crates/audio/src/pcm_export.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/audio/src/pcm_export.rs b/crates/audio/src/pcm_export.rs index c8456d785..14e45783b 100644 --- a/crates/audio/src/pcm_export.rs +++ b/crates/audio/src/pcm_export.rs @@ -2,8 +2,8 @@ //! 16, 24 or 32-bit integer or 32-bit float ([`SampleFormat`]), for streaming export. //! Float WAV is `WAVE_FORMAT_IEEE_FLOAT` with a `fact` chunk; float AIFF is AIFF-C (`fl32`). //! -//! The header is written first from the known length ([`header`]), then the samples in chunks -//! ([`encode`]) and the pad byte ([`pad`]), so an export streams to disk instead of building the +//! The header is written first from the known length (`header`), then the samples in chunks +//! (`encode`) and the pad byte (`pad`), so an export streams to disk instead of building the //! file in memory. /// PCM representation in an exported file. From 2aa7f7f8d211786650d6cc3a4d3f6be165ddb30a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:22:05 +0000 Subject: [PATCH 043/471] dpx: fix RGBA writer lint --- crates/dpx/src/rgba8_dpx10.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs index 6ec5e23b4..05ce20b15 100644 --- a/crates/dpx/src/rgba8_dpx10.rs +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -21,7 +21,7 @@ const DPX_HEADER: usize = 2048; pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::result::Result, Rgba8EncodeError> { let n = checked_len(rgba, w, h)?; // one 32-bit word per pixel; `n` is already `w * h * 4` - let size = n.checked_add(DPX_HEADER).ok_or_else(|| Rgba8EncodeError::FrameSize)?; + let size = n.checked_add(DPX_HEADER).ok_or(Rgba8EncodeError::FrameSize)?; let file_size = u32::try_from(size).map_err(|_| Rgba8EncodeError::TooLarge)?; // SMPTE ST 268: undefined numeric fields are all ones, undefined text fields are NUL let mut hd = vec![0xFF_u8; DPX_HEADER]; From da7007385609e66337f9ba37c5aca8ca6215b2f2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:23:46 +0000 Subject: [PATCH 044/471] Expose tolerant JPEG marker inspection --- crates/metadata/src/container.rs | 146 +++++++++++++++++++++++++++++-- 1 file changed, 137 insertions(+), 9 deletions(-) diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs index 842020683..c972867e1 100644 --- a/crates/metadata/src/container.rs +++ b/crates/metadata/src/container.rs @@ -84,22 +84,83 @@ pub fn embedded(bytes: &[u8]) -> Embedded { const XMP_SIG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; const XMP_EXT_SIG: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; +/// JPEG frame, marker and metadata details collected before the first scan. +#[derive(Debug, Default)] +pub struct JpegInspection { + /// Stored frame width. + pub width: u32, + /// Stored frame height. + pub height: u32, + /// Number of frame components. + pub components: u8, + /// Sample precision. + pub precision: u8, + /// Start-of-frame marker byte. + pub sof: u8, + /// Components in the first scan. + pub first_scan_components: u8, + /// Number of APP1 segments. + pub app1: u32, + /// First EXIF TIFF payload. + pub exif: Option>, + /// Offset of the EXIF TIFF payload. + pub exif_offset: usize, + /// First standard XMP packet. + pub xmp: Option, + /// Reassembled ICC profile. + pub icc: Option>, + /// ICC chunks were malformed or inconsistent. + pub icc_invalid: bool, + /// Adobe APP14 transform byte. + pub adobe_transform: Option, + /// MPF TIFF offset and payload. + pub mpf: Option<(usize, Vec)>, + /// ISO gain-map APP2 payload following its identifier. + pub iso_gainmap: Option>, + valid: bool, +} + +impl JpegInspection { + /// Whether metadata identifies this image as a gain map. + pub fn is_gain_map(&self) -> bool { + self.iso_gainmap.as_ref().is_some_and(|p| p.len() > 4) || self.xmp.as_deref().is_some_and(|x| x.contains("hdrgm:GainMapMax")) + } +} + +/// Inspect markers before the first scan, tolerating garbage between segments and truncated +/// final payloads. Returns `None` for a missing SOI or an invalid segment length. +pub fn inspect_jpeg(b: &[u8]) -> Option { + if !b.starts_with(&[0xff, 0xd8]) { + return None; + } + let (embedded, _, mut inspection) = jpeg_segments_inner(b, false, true); + if !inspection.valid { + return None; + } + inspection.exif = embedded.exif; + inspection.xmp = embedded.xmp.map(|x| x.trim_end_matches('\0').to_string()); + inspection.icc = embedded.icc; + Some(inspection) +} + /// Walk JPEG markers up to SOS and collect APP1 Exif / XMP, APP2 ICC and APP13 IPTC. pub fn jpeg_segments(b: &[u8]) -> Embedded { - jpeg_segments_inner(b, false).0 + jpeg_segments_inner(b, false, false).0 } /// Walk JPEG metadata and return every ordinary XMP APP1 packet in segment order, along with the /// usual [`Embedded`] projection (which retains only the first ordinary packet). pub fn jpeg_segments_with_all_xmp(b: &[u8]) -> (Embedded, Vec) { - jpeg_segments_inner(b, true) + let (embedded, packets, _) = jpeg_segments_inner(b, true, false); + (embedded, packets) } -fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec) { +fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embedded, Vec, JpegInspection) { let mut out = Embedded::default(); let mut xmp_packets = Vec::new(); + let mut inspection = JpegInspection { valid: true, ..Default::default() }; if !b.starts_with(&[0xff, 0xd8]) { - return (out, xmp_packets); + return (out, xmp_packets, inspection); } let mut icc_parts: [Option<&[u8]>; 255] = [None; 255]; let mut icc_total = None; @@ -110,6 +171,10 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec (Embedded, Vec (Embedded, Vec= 6 && inspection.components == 0 { + inspection.precision = payload[0]; + inspection.sof = marker; + inspection.height = u32::from(u16::from_be_bytes([payload[1], payload[2]])); + inspection.width = u32::from(u16::from_be_bytes([payload[3], payload[4]])); + inspection.components = payload[5]; + } + if marker == 0xe1 { + inspection.app1 = inspection.app1.saturating_add(1); + } + if marker == 0xe1 && payload.starts_with(b"Exif\0\0") && out.exif.is_none() { + inspection.exif_offset = start + 6; + } + if marker == 0xe2 && payload.starts_with(b"MPF\0") && inspection.mpf.is_none() { + inspection.mpf = Some((start + 4, payload[4..].to_vec())); + } + if marker == 0xe2 && inspection.iso_gainmap.is_none() { + if let Some(data) = payload.strip_prefix(b"urn:iso:std:iso:ts:21496:-1\0") { + inspection.iso_gainmap = Some(data.to_vec()); + } + } + if marker == 0xee && payload.starts_with(b"Adobe") && payload.len() >= 12 { + inspection.adobe_transform = Some(payload[11]); + } + } match marker { - 0xe1 if payload.starts_with(b"Exif\0") && out.exif.is_none() => out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()), + 0xe1 if payload.starts_with(if inspect { b"Exif\0\0" } else { b"Exif\0" }) && out.exif.is_none() => { + out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()) + } 0xe1 if payload.starts_with(XMP_SIG) && out.xmp.is_none() => { let packet = String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned(); if collect_all_xmp { @@ -145,7 +242,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec { xmp_packets.push(String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned()); } - 0xe1 if payload.starts_with(XMP_EXT_SIG) => { + 0xe1 if !inspect && payload.starts_with(XMP_EXT_SIG) => { // GUID (32) + full length (4) + offset (4) + data let p = &payload[XMP_EXT_SIG.len()..]; if p.len() >= 40 { @@ -184,7 +281,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec out.iptc = photoshop_iptc(&payload[14..]), + 0xed if !inspect && payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]), _ => {} } i = end; @@ -214,8 +311,11 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec, marker: u8, data: &[u8]) { + out.extend_from_slice(&[0xff, marker]); + out.extend_from_slice(&((data.len() as u16) + 2).to_be_bytes()); + out.extend_from_slice(data); + } + #[test] + fn tolerant_walk_keeps_offsets_frame_and_icc_chunks() { + let mut b = vec![0xff, 0xd8, 0x44]; + segment(&mut b, 0xe1, b"Exif\0\0II"); + segment(&mut b, 0xe2, b"ICC_PROFILE\0\x02\x02B"); + segment(&mut b, 0xe2, b"ICC_PROFILE\0\x01\x02A"); + segment(&mut b, 0xc0, &[8, 0, 2, 0, 3, 3]); + b.extend_from_slice(&[0xff, 0xda, 0, 3, 3]); + let info = inspect_jpeg(&b).unwrap(); + assert_eq!((info.width, info.height, info.components, info.first_scan_components), (3, 2, 3, 3)); + assert_eq!(info.app1, 1); + assert_eq!(&b[info.exif_offset..info.exif_offset + 2], b"II"); + assert_eq!(info.icc.as_deref(), Some(&b"AB"[..])); + assert!(!info.icc_invalid); + // The existing strict API still stops at the garbage byte. + assert!(jpeg_segments(&b).exif.is_none()); + assert!(inspect_jpeg(&[0xff, 0xd8, 0xff, 0xe1, 0, 1]).is_none()); + } +} From e275d533cfacf21817200694e48aa7054a2f5a2c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:24:17 +0000 Subject: [PATCH 045/471] Export JPEG inspection API --- crates/metadata/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index ff2c530fa..bf6700455 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -37,7 +37,7 @@ pub mod tags; mod xmp; mod xmp_merge; -pub use container::{Embedded, embedded, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; +pub use container::{Embedded, JpegInspection, embedded, inspect_jpeg, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; pub use datetime::{DatePrecision, DateTime}; pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif}; pub use focus::FocusPoint; From 159b942defe3613c91a4beec5fadfcb1593f2600 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:24:27 +0000 Subject: [PATCH 046/471] Share contextual directional text shaping --- Cargo.lock | 1 + crates/font-catalog/Cargo.toml | 1 + crates/font-catalog/src/lib.rs | 2 + crates/font-catalog/src/shape.rs | 150 +++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 crates/font-catalog/src/shape.rs diff --git a/Cargo.lock b/Cargo.lock index 9424cf3fe..3e1487dca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1761,6 +1761,7 @@ name = "craft-font-catalog" version = "0.1.0" dependencies = [ "fontique", + "harfrust 0.13.3", "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index 1f0830a63..c53d871b8 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -15,6 +15,7 @@ default = ["system"] system = ["dep:fontique"] [dependencies] +harfrust = "0.13" kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 7da4ca310..176a38738 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -26,6 +26,7 @@ mod fontconfig; mod outline; mod registry; mod scan; +mod shape; mod suitcase; pub mod system; @@ -34,6 +35,7 @@ pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fontconfig::fontconfig_dirs; pub use outline::{FontOutline, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; +pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run}; #[cfg(test)] mod tests; diff --git a/crates/font-catalog/src/shape.rs b/crates/font-catalog/src/shape.rs new file mode 100644 index 000000000..7fb0814d3 --- /dev/null +++ b/crates/font-catalog/src/shape.rs @@ -0,0 +1,150 @@ +//! Shape a run with its source clusters and surrounding text context. + +use std::sync::Arc; + +use harfrust::{BufferClusterLevel, Direction, Feature, Language, Script, ShapeOptions, ShapePlan, Shaper, UnicodeBuffer}; + +/// Parameters for a single directional run. Text is in logical order and `source_start` is its +/// byte offset in the complete source. Context influences joining but emits no glyphs. +pub struct RunInput<'a> { + /// Text in this run. + pub text: &'a str, + /// Its byte offset in the complete source. + pub source_start: usize, + /// Text before the run, within the same shaping context. + pub pre_context: &'a str, + /// Text after the run, within the same shaping context. + pub post_context: &'a str, + /// Direction of shaping. + pub direction: Direction, + /// Language used for language-specific forms. + pub language: Option<&'a Language>, + /// OpenType features with ranges in source byte coordinates. + pub features: &'a [Feature], + /// Use character-level instead of monotone grapheme clusters. + pub character_clusters: bool, + /// Uppercase each mapped character without losing its source cluster. + pub uppercase: bool, +} + +/// A glyph in design units, with its source-byte cluster and directional advances and offsets. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct RunGlyph { + pub glyph_id: u32, + pub cluster: u32, + pub x_advance: i32, + pub y_advance: i32, + pub x_offset: i32, + pub y_offset: i32, +} + +/// A run's source offsets cannot fit HarfRust's 32-bit clusters. +#[derive(Debug, thiserror::Error)] +#[error("shaping cluster exceeds the 32-bit source offset range")] +pub struct ShapeRunError; + +/// Shape a run using a caller's face-specific plan cache. `map_character` applies digit or other +/// character mapping before case conversion; expansions retain the original byte cluster. +/// The plan callback receives the guessed script, direction, language and feature set so it can +/// reuse plans for the particular shaper and its variable-font instance. +pub fn shape_run( + shaper: &Shaper<'_>, + input: &RunInput<'_>, + mut map_character: impl FnMut(char) -> char, + plan: impl FnOnce(&Shaper<'_>, Direction, Option + + "##; + let checked = crate::tree::preflight(svg, &Limits::default()).unwrap(); + assert_eq!( + checked.unsupported_elements(), + vec![("animate", 2), ("animateTransform", 1), ("foreignObject", 1), ("script", 1), ("set", 1)], + "the XHTML div is not an SVG element" + ); + // The same checked XML still parses. + assert!(checked.parse(&usvg::Options::default()).is_ok()); + let plain = crate::tree::preflight(r#""#, &Limits::default()).unwrap(); + assert!(plain.unsupported_elements().is_empty()); +} + +#[cfg(feature = "tree-text")] +#[test] +fn local_text_is_placed_and_bounded_in_its_parents_frame() { + let mut options = usvg::Options::default(); + let fonts = options.fontdb_mut(); + fonts.load_font_data(include_bytes!("../../../ui/tests/fonts/NotoSans-test.ttf").to_vec()); + let family = fonts.faces().next().unwrap().families[0].0.clone(); + let svg = format!( + r#"Hi"# + ); + let tree = parse(svg.as_bytes(), &Limits::default(), &options).unwrap(); + let local = Document::from_tree_local(&tree, 64); + let absolute = Document::from_tree(&tree, 64); + + let groups = group_ids(&local.nodes); + let text = groups.iter().find(|(id, _)| id == "t").unwrap(); + assert!(close([text.1[0], text.1[3]], [1.0, 1.0]) && close([text.1[4], text.1[5]], [0.0, 0.0]), "text sits in its group's frame: {text:?}"); + + let bounds = |nodes: &[Node]| -> [f64; 4] { + let Node::Group { children, .. } = &nodes[0] else { panic!() }; + let Node::Group { bounds, .. } = &children[0] else { panic!() }; + *bounds + }; + let (l, a) = (bounds(&local.nodes), bounds(&absolute.nodes)); + assert!(close([l[0] + 50.0, l[1]], [a[0], a[1]]) && close([l[2], l[3]], [a[2], a[3]]), "local {l:?} vs absolute {a:?}"); + assert!(l[0] > 0.0 && l[0] < 10.0, "near x=5 in the group's frame: {l:?}"); + + let (mut lp, mut ap) = (Vec::new(), Vec::new()); + document_points(&local.nodes, IDENTITY, true, &mut lp); + document_points(&absolute.nodes, IDENTITY, false, &mut ap); + assert!(!lp.is_empty() && lp.len() == ap.len()); + assert!(lp.iter().zip(&ap).all(|(l, a)| close(*l, *a))); +} diff --git a/crates/svg/src/tree/primitive_tests.rs b/crates/svg/src/tree/primitive_tests.rs index 81f4c0979..ac417eb6c 100644 --- a/crates/svg/src/tree/primitive_tests.rs +++ b/crates/svg/src/tree/primitive_tests.rs @@ -109,8 +109,11 @@ fn hand_written_segments_are_recognised_too() { Segment::Close, ], fill: None, + fill_rule: crate::tree::document::FillRule::NonZero, stroke: None, source_path: crate::tree::document::NodePath::default(), + id: String::new(), + transform: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], }; assert_eq!(path.primitive(), Some(Primitive::Ellipse { cx: 5.0, cy: 5.0, rx: 5.0, ry: 5.0 })); } From 472956a8d7626643b0148de5573083474a595934 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 09:55:33 -0500 Subject: [PATCH 321/471] feat: add craft-control (control channel protocol) (#1879) * feat: add craft-control (control channel protocol) Port from solidcraft: the control channel any app may share, no UI toolkit. - Per-launch token files (32 random bytes, user-only permissions) - Loopback server with token-based authentication - Client that talks to the loopback server - Request/response types (ControlRequest, ControlResponse) - Document watch for subscribed connections waiting on changes - Effect type for requests still running when they finish Desktop-only (wasm32 excluded): uses getrandom for token generation. No external dependencies beyond serde and serde_json. * fix: update test directory names from solidcraft-control to craft-control * Record control crate dependencies in workspace lockfile * re-trigger CI * chore(lock): keep the wasm-bindgen family at the version main's runners install --- Cargo.lock | 10 + crates/control/Cargo.toml | 23 + crates/control/src/auth.rs | 673 +++++++++++++ crates/control/src/cancel.rs | 118 +++ crates/control/src/client.rs | 616 ++++++++++++ crates/control/src/client_tests.rs | 190 ++++ crates/control/src/effect.rs | 27 + crates/control/src/lib.rs | 26 + crates/control/src/request.rs | 125 +++ crates/control/src/server.rs | 1433 ++++++++++++++++++++++++++++ 10 files changed, 3241 insertions(+) create mode 100644 crates/control/Cargo.toml create mode 100644 crates/control/src/auth.rs create mode 100644 crates/control/src/cancel.rs create mode 100644 crates/control/src/client.rs create mode 100644 crates/control/src/client_tests.rs create mode 100644 crates/control/src/effect.rs create mode 100644 crates/control/src/lib.rs create mode 100644 crates/control/src/request.rs create mode 100644 crates/control/src/server.rs diff --git a/Cargo.lock b/Cargo.lock index 5f28d1878..50b715040 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1498,6 +1498,16 @@ dependencies = [ "encoding_rs", ] +[[package]] +name = "craft-control" +version = "0.1.0" +dependencies = [ + "getrandom 0.3.4", + "proptest", + "serde", + "serde_json", +] + [[package]] name = "craft-control-protocol" version = "0.1.0" diff --git a/crates/control/Cargo.toml b/crates/control/Cargo.toml new file mode 100644 index 000000000..0a18ccc99 --- /dev/null +++ b/crates/control/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "craft-control" +description = "The control channel any app may share: token files, the loopback server, and its requests; no UI toolkit" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } + +# The loopback server is for desktop apps; the web app is driven through the page instead. +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +# The per-launch control token: 32 random bytes from the operating system (MIT OR Apache-2.0). +getrandom = "0.3" + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/control/src/auth.rs b/crates/control/src/auth.rs new file mode 100644 index 000000000..bb2cef86b --- /dev/null +++ b/crates/control/src/auth.rs @@ -0,0 +1,673 @@ +//! The control channel's per-launch token (docs/control-protocol.md). An app listening for +//! control makes a random token, writes it to a file only the user can read +//! ([`App::token_path`]) and refuses every connection whose first request doesn't carry it, so +//! other users on the same machine (loopback is shared, on Windows across remote-desktop sessions +//! too) can't drive the app. Clients find it with [`App::client_token`]. Next to it, `.pid` +//! names the process that wrote it, so a client refused with a leftover token can say the file is +//! stale. + +use std::ffi::OsString; +use std::io::{Error, ErrorKind, Read, Result, Write}; +use std::path::{Path, PathBuf}; +use std::time::{SystemTime, UNIX_EPOCH}; + +/// Shortest token accepted from the app's `CONTROL_TOKEN` variable (hex characters). +pub const MIN_TOKEN_LEN: usize = 32; + +/// Ports tried from the app's default port upward when it is taken (a second window, another +/// program). +pub const PORT_TRIES: u16 = 16; + +/// Which app a control channel belongs to: where its token files live, its environment and its +/// name in messages. +#[derive(Clone, Copy)] +pub struct App { + /// The app's name in messages ("restart SolidCraft"). + pub name: &'static str, + /// The per-user data folder; the token files go in its `control` folder. + pub data_dir: fn() -> Option, + /// The app's environment variable `name` (`CONTROL_DIR`, `CONTROL_TOKEN`), with the app's prefix. + pub env: fn(&str) -> Option, + /// A time in seconds since the Unix epoch, as the app writes UTC times in messages. + pub utc: fn(u64) -> String, + /// The port the control channel tries first when the launch gives none. Below the ephemeral + /// ranges of Linux (32768+) and Windows/macOS (49152+), so the system never hands it to an + /// outgoing connection. + pub default_port: u16, +} + +/// Where the control channel listens, from the launch's `--control`/environment port and the app's +/// allow-agents setting. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Listen { + /// Exactly this port (`--control PORT`, the app's `CONTROL_PORT`); no fallback. + Exact(u16), + /// The app's [`App::default_port`], or the next free one of [`PORT_TRIES`]. + Default, +} + +/// What a launch listens on. An explicit port wins over the app's allow-agents setting +/// (`enabled`), so a scripted launch works with the setting off; with the setting off and no port, +/// nothing listens. +pub fn plan(explicit: Option, enabled: bool) -> Option { + match explicit { + Some(port) => Some(Listen::Exact(port)), + None if enabled => Some(Listen::Default), + None => None, + } +} + +/// Bind a loopback-only listener on `first`, or on the next free port when it is taken, up to +/// `tries` ports in all. +pub fn bind_with_fallback(first: u16, tries: u16) -> Result { + let mut last = Error::new(ErrorKind::AddrInUse, "no port to try"); + for port in (first..=u16::MAX).take(usize::from(tries.max(1))) { + match std::net::TcpListener::bind(("127.0.0.1", port)) { + Ok(l) => return Ok(l), + // In use; Windows reports a port another program holds exclusively as denied. + Err(e) if matches!(e.kind(), ErrorKind::AddrInUse | ErrorKind::PermissionDenied) => last = e, + Err(e) => return Err(e), + } + } + Err(last) +} + +impl App { + /// The folder of the token files: `/control` (the app's `CONTROL_DIR` replaces it). + fn control_dir(&self) -> Option { + match self.custom_control_dir() { + Some(d) => Some(PathBuf::from(d)), + None => Some((self.data_dir)()?.join("control")), + } + } + + /// The folder the app's `CONTROL_DIR` names in place of the default one. + fn custom_control_dir(&self) -> Option { + (self.env)("CONTROL_DIR").filter(|d| !d.is_empty()) + } + + /// The token file of the app listening on `port`: `/control/.token` + /// (the app's `CONTROL_DIR` replaces the folder). + pub fn token_path(&self, port: u16) -> Option { + Some(self.control_dir()?.join(format!("{port}.token"))) + } + + /// The discovery file: the port of the most recently started app, so a client with no port + /// finds it. It holds only the port; the token stays in its own file. + pub fn current_port_path(&self) -> Option { + Some(self.control_dir()?.join("current.port")) + } + + /// Record `port` as the running app's, readable only by the user like the token files. + pub fn write_current_port(&self, port: u16) -> Result { + let path = self.current_port_path().ok_or_else(|| Error::new(ErrorKind::NotFound, "no data folder"))?; + self.write_token_file(&path, &port.to_string())?; + Ok(path) + } + + /// The port the discovery file names, if there is one. + pub fn read_current_port(&self) -> Option { + parse_port(&std::fs::read_to_string(self.current_port_path()?).ok().filter(|t| t.len() <= 64)?) + } + + /// Remove the discovery file (and its process file) at exit, unless a later app has taken it + /// over: it names another port, or another launch now holds this port's token file. Under + /// Flatpak every instance has its own loopback, so two windows can listen on the same port + /// number and write the same file names; the one closing first must leave the other's (#8794). + /// Run it after [`remove_token_file`], which takes this launch's own token file away first. + pub fn remove_current_port(&self, port: u16, token: &str) { + if let (Some(current), Some(file)) = (self.current_port_path(), self.token_path(port)) { + remove_discovery(¤t, &file, port, token); + } + } + + /// A token set on purpose with the app's `CONTROL_TOKEN` (test harnesses, sandboxed packages + /// whose data folder the client can't see), if it is long enough. + pub fn env_token(&self) -> Option { + (self.env)("CONTROL_TOKEN").and_then(|v| v.into_string().ok()).map(|t| t.trim().to_string()).filter(|t| t.len() >= MIN_TOKEN_LEN) + } + + /// The token a client sends to the app on `port`: `explicit` (`--token`), else the app's + /// `CONTROL_TOKEN`, else the app's token file. + pub fn client_token(&self, port: u16, explicit: Option<&str>) -> Option { + if let Some(t) = explicit.map(str::trim).filter(|t| !t.is_empty()) { + return Some(ClientToken { token: t.to_string(), file: None, app: *self }); + } + if let Some(t) = self.env_token() { + return Some(ClientToken { token: t, file: None, app: *self }); + } + // Read directly, not through any agent's file scope: the app's own file. + read_token_file(&self.token_path(port)?, *self) + } + + /// Write `token` to `path`, readable and writable only by the user: the folder is made `0700` + /// and the file `0600` on Unix; on Windows the file's inherited permissions are replaced by one + /// entry for the user (`icacls`) before the token goes in; when that fails, the still empty file + /// is removed and no token is written. A file left by an earlier launch is replaced. The process + /// file beside it ([`pid_path`]) records this process, best effort. + pub fn write_token_file(&self, path: &Path, token: &str) -> Result<()> { + #[cfg(windows)] + let restrict = |p: &Path| restrict_to_user(p, self.custom_control_dir().is_some()); + #[cfg(not(windows))] + let restrict = |_: &Path| Ok(()); + write_token_file_with(path, token, restrict) + } +} + +fn parse_port(text: &str) -> Option { + text.trim().parse::().ok().filter(|p| *p != 0) +} + +/// [`App::remove_current_port`] on given files: `current` is the discovery file, `file` the token +/// file of `port`. +fn remove_discovery(current: &Path, file: &Path, port: u16, token: &str) { + let named = std::fs::read_to_string(current).ok().filter(|t| t.len() <= 64).and_then(|t| parse_port(&t)); + if named != Some(port) || held_by_another(file, token) { + return; + } + let _ = std::fs::remove_file(current); + let _ = std::fs::remove_file(pid_path(current)); +} + +/// A client token and, when it came from a token file, where that file is. +pub struct ClientToken { + pub token: String, + pub file: Option, + /// The app it is for, named in an explanation. + pub app: App, +} + +/// The token file a client read: its path, when it was written and the process that wrote it. +pub struct TokenSource { + pub path: PathBuf, + pub modified: Option, + /// From `.pid`: the writer's process id and, on Linux, its start time. + pub writer: Option<(u32, Option)>, +} + +impl ClientToken { + /// The app's `error`, and for a refused token read from a file, which file it was, when it + /// was written and whether the instance that wrote it has exited. Never the token itself. + pub fn explain_error(&self, error: &str) -> String { + let Some(src) = &self.file else { return error.to_string() }; + if !error.starts_with("unauthorized") { + return error.to_string(); + } + let written = src + .modified + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|t| (self.app.utc)(t.as_secs())) + .unwrap_or_else(|| "at an unknown time".into()); + let age = src.modified.and_then(|t| t.elapsed().ok()).map(|d| format!(" ({} s ago)", d.as_secs())).unwrap_or_default(); + let path = src.path.display(); + let name = self.app.name; + match src.writer.filter(|(pid, start)| writer_exited(*pid, *start)) { + Some((pid, _)) => format!( + "{error}; stale token file from an instance that exited (process {pid}): {path}, written {written}{age}; delete that file or restart {name}" + ), + None => format!("{error}; token from {path}, written {written}{age}; if {name} was force-quit, restart it or delete that file"), + } + } +} + +/// Files bigger than this are not token files. +const MAX_TOKEN_FILE: u64 = 4096; + +fn read_token_file(path: &Path, app: App) -> Option { + let f = std::fs::File::open(path).ok()?; + let modified = f.metadata().ok().and_then(|m| m.modified().ok()); + let mut text = String::new(); + f.take(MAX_TOKEN_FILE + 1).read_to_string(&mut text).ok()?; + if text.len() as u64 > MAX_TOKEN_FILE { + return None; + } + let token = text.trim(); + if token.is_empty() { + return None; + } + let path = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()); + let writer = read_pid_file(&pid_path(&path)); + Some(ClientToken { token: token.to_string(), file: Some(TokenSource { path, modified, writer }), app }) +} + +/// The process file beside the token file `path`: `7878.token` → `7878.pid`. +pub fn pid_path(path: &Path) -> PathBuf { + path.with_extension("pid") +} + +/// ` []`; anything else (or no file) is no information. +fn read_pid_file(path: &Path) -> Option<(u32, Option)> { + let mut text = String::new(); + std::fs::File::open(path).ok()?.take(256).read_to_string(&mut text).ok()?; + let mut fields = text.split_whitespace(); + let pid = fields.next()?.parse::().ok().filter(|&pid| pid > 0)?; + Some((pid, fields.next().and_then(|s| s.parse().ok()))) +} + +/// Whether the process that wrote a token file is known to be gone: not running, or its id now +/// belongs to a process that started at another time. Unknown counts as running. +fn writer_exited(pid: u32, start: Option) -> bool { + process_running(pid) == Some(false) || start.zip(process_start(pid)).is_some_and(|(saved, now)| saved != now) +} + +/// Whether process `pid` runs; `None` when that can't be found out. +fn process_running(pid: u32) -> Option { + #[cfg(target_os = "linux")] + { + if !Path::new("/proc/self").exists() { + return None; + } + match std::fs::metadata(format!("/proc/{pid}")) { + Ok(_) => Some(true), + Err(e) if e.kind() == ErrorKind::NotFound => Some(false), + Err(_) => None, + } + } + #[cfg(all(unix, not(target_os = "linux")))] + { + let output = std::process::Command::new("ps").args(["-p", &pid.to_string(), "-o", "pid="]).output().ok()?; + if output.status.success() { + return Some(true); + } + (output.status.code() == Some(1) && output.stdout.iter().all(u8::is_ascii_whitespace)).then_some(false) + } + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + let output = std::process::Command::new("tasklist") + .args(["/FI", &format!("PID eq {pid}"), "/FO", "CSV", "/NH"]) + .creation_flags(0x0800_0000) + .output() + .ok()?; + if !output.status.success() || output.stdout.is_empty() { + return None; + } + // `"image","pid",…` per process; with no match, a message line without a pid. + let text = String::from_utf8_lossy(&output.stdout); + Some(text.lines().any(|line| line.split("\",\"").nth(1).and_then(|s| s.trim_matches('"').parse::().ok()) == Some(pid))) + } + #[cfg(not(any(unix, windows)))] + { + let _ = pid; + None + } +} + +/// Linux: the process's start time (clock ticks after boot), which tells a reused id apart. +fn process_start(pid: u32) -> Option { + #[cfg(target_os = "linux")] + { + let mut stat = String::new(); + std::fs::File::open(format!("/proc/{pid}/stat")).ok()?.take(4096).read_to_string(&mut stat).ok()?; + stat.rsplit_once(')')?.1.split_whitespace().nth(19)?.parse().ok() + } + #[cfg(not(target_os = "linux"))] + { + let _ = pid; + None + } +} + +/// `a == b`, taking the same time wherever they differ. +pub fn tokens_match(a: &str, b: &str) -> bool { + let (a, b) = (a.as_bytes(), b.as_bytes()); + a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0 +} + +/// [`App::write_token_file`], with `restrict` limiting the new file to the user (Windows). +fn write_token_file_with(path: &Path, token: &str, restrict: impl FnOnce(&Path) -> Result<()>) -> Result<()> { + let dir = path.parent().ok_or_else(|| Error::new(ErrorKind::InvalidInput, "no folder"))?; + std::fs::create_dir_all(dir)?; + let _ = std::fs::remove_file(path); + let mut opts = std::fs::OpenOptions::new(); + opts.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?; + opts.mode(0o600); + } + let opened = opts.open(path)?; + // On a refusal the file goes with the closure, so it is closed before it is removed. + let mut f = match restrict(path).map(|()| opened) { + Ok(f) => f, + Err(e) => { + let _ = std::fs::remove_file(path); + return Err(e); + } + }; + f.write_all(token.as_bytes())?; + f.write_all(b"\n")?; + f.sync_all()?; + let pid = std::process::id(); + let line = process_start(pid).map_or_else(|| format!("{pid}\n"), |start| format!("{pid} {start}\n")); + let _ = std::fs::write(pid_path(path), line); + Ok(()) +} + +/// Remove a token file this launch wrote, and its process file, while it still holds `token`. +/// Another launch may have written the same file since (a second window on the same port, +/// #8794); its file stays. The process id can't tell the two apart, as every Flatpak instance +/// has the same one, but each launch's token is its own. +pub fn remove_token_file(path: &Path, token: &str) { + if read_token(path).is_some_and(|held| tokens_match(&held, token.trim())) { + let _ = std::fs::remove_file(path); + let _ = std::fs::remove_file(pid_path(path)); + } +} + +/// The token a token file holds, trimmed; `None` when there is no readable token file. +fn read_token(path: &Path) -> Option { + let mut text = String::new(); + std::fs::File::open(path).ok()?.take(MAX_TOKEN_FILE + 1).read_to_string(&mut text).ok()?; + let token = text.trim(); + (text.len() as u64 <= MAX_TOKEN_FILE && !token.is_empty()).then(|| token.to_string()) +} + +/// Whether the token file at `path` holds a token other than `token`: another launch's. +fn held_by_another(path: &Path, token: &str) -> bool { + read_token(path).is_some_and(|held| !tokens_match(&held, token.trim())) +} + +/// How long `icacls` may take (#6145). It finishes at once; on some computers the new process +/// is left suspended (security software holding it), and waiting on it without a limit froze +/// the app. +#[cfg(windows)] +const ICACLS_LIMIT: std::time::Duration = std::time::Duration::from_secs(5); + +/// How a run of `icacls` ended. +#[cfg(any(windows, test))] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum IcaclsEnd { + Succeeded, + Failed, + /// It didn't finish within [`ICACLS_LIMIT`] and was ended. + TimedOut, +} + +/// Whether a token may go into a file after `icacls` ended this way. `custom_folder`: the file is +/// in the folder the app's `CONTROL_DIR` names. A timeout is accepted only in the default folder, +/// the user's own local app data, which other users can't read by default; nothing is known about +/// the permissions of any other folder. +#[cfg(any(windows, test))] +fn restriction_result(end: IcaclsEnd, custom_folder: bool, path: &Path) -> Result<()> { + match end { + IcaclsEnd::Succeeded => Ok(()), + IcaclsEnd::TimedOut if !custom_folder => Ok(()), + IcaclsEnd::TimedOut => Err(Error::new(ErrorKind::TimedOut, format!("icacls did not finish in time to restrict {}", path.display()))), + IcaclsEnd::Failed => Err(Error::other(format!("icacls failed to restrict {}", path.display()))), + } +} + +/// Windows: drop inherited entries and grant only the current user full access. An `icacls` +/// that doesn't finish in time is ended, and [`restriction_result`] decides what that means. +#[cfg(windows)] +fn restrict_to_user(path: &Path, custom_folder: bool) -> Result<()> { + use std::os::windows::process::CommandExt; + let user = std::env::var("USERNAME").map_err(|_| Error::other("USERNAME is not set"))?; + let domain = std::env::var("USERDOMAIN").ok().filter(|d| !d.is_empty()); + let who = domain.map_or(user.clone(), |d| format!("{d}\\{user}")); + let child = std::process::Command::new("icacls") + .arg(path) + .args(["/inheritance:r", "/grant:r", &format!("{who}:F")]) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .creation_flags(0x0800_0000) + .spawn()?; + let end = match wait_within(child, ICACLS_LIMIT)? { + Some(status) if status.success() => IcaclsEnd::Succeeded, + Some(_) => IcaclsEnd::Failed, + None => IcaclsEnd::TimedOut, + }; + restriction_result(end, custom_folder, path) +} + +/// Wait for `child` at most `limit`: its exit status, or `None` (and the child ended) when it +/// hasn't finished by then. +#[cfg(any(windows, test))] +fn wait_within(mut child: std::process::Child, limit: std::time::Duration) -> Result> { + let start = std::time::Instant::now(); + loop { + if let Some(status) = child.try_wait()? { + return Ok(Some(status)); + } + if start.elapsed() >= limit { + // Not waited for afterwards: a suspended process may not end at once. + let _ = child.kill(); + return Ok(None); + } + std::thread::sleep(std::time::Duration::from_millis(20)); + } +} + +/// Removes the token file (path, token) when the app ends, while it still holds that token. +pub struct TokenFile(pub PathBuf, pub String); + +impl Drop for TokenFile { + fn drop(&mut self) { + remove_token_file(&self.0, &self.1); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// An app with no data folder and no environment, whose times read `t`. + const TEST: App = App { name: "TestCraft", data_dir: || None, env: |_| None, utc: |secs| format!("t{secs}"), default_port: 27878 }; + + /// #6145: a child that doesn't finish in time is given up on, not waited for forever. + #[cfg(unix)] + #[test] + fn a_slow_child_is_waited_for_only_so_long() { + use std::time::{Duration, Instant}; + let start = Instant::now(); + let slow = std::process::Command::new("sleep").arg("30").spawn().unwrap(); + assert!(wait_within(slow, Duration::from_millis(200)).unwrap().is_none()); + assert!(start.elapsed() < Duration::from_secs(5), "{:?}", start.elapsed()); + let quick = std::process::Command::new("true").spawn().unwrap(); + assert!(wait_within(quick, Duration::from_secs(5)).unwrap().is_some_and(|s| s.success())); + let failing = std::process::Command::new("false").spawn().unwrap(); + assert!(wait_within(failing, Duration::from_secs(5)).unwrap().is_some_and(|s| !s.success())); + } + + /// #6145's freeze fix stays in the default folder, the user's own; a folder picked with + /// the app's `CONTROL_DIR` gets no token unless the file was really restricted. + #[test] + fn only_the_default_folder_accepts_an_icacls_that_timed_out() { + let path = Path::new("control/7878.token"); + for custom in [false, true] { + assert!(restriction_result(IcaclsEnd::Succeeded, custom, path).is_ok()); + assert!(restriction_result(IcaclsEnd::Failed, custom, path).is_err()); + } + assert!(restriction_result(IcaclsEnd::TimedOut, false, path).is_ok()); + let e = restriction_result(IcaclsEnd::TimedOut, true, path).unwrap_err(); + assert_eq!(e.kind(), ErrorKind::TimedOut); + assert!(e.to_string().contains("7878.token"), "{e}"); + } + + /// A file that couldn't be restricted is removed, empty and with no process file beside it, and + /// the error goes to the caller. A restricted one gets its token. + #[test] + fn a_token_file_that_could_not_be_restricted_is_removed() { + let dir = std::env::temp_dir().join(format!("craft-control-restrict-{}", std::process::id())); + let path = dir.join("7879.token"); + let e = write_token_file_with(&path, "secret-token", |p| { + assert_eq!(std::fs::read(p).unwrap(), b"", "restricted before the token goes in"); + restriction_result(IcaclsEnd::TimedOut, true, p) + }) + .unwrap_err(); + assert_eq!(e.kind(), ErrorKind::TimedOut); + assert!(!path.exists() && !pid_path(&path).exists()); + write_token_file_with(&path, "secret-token", |p| restriction_result(IcaclsEnd::TimedOut, false, p)).unwrap(); + assert_eq!(read_token(&path).as_deref(), Some("secret-token")); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn tokens_compare_whole() { + assert!(tokens_match("abc123", "abc123")); + assert!(!tokens_match("abc123", "abc124")); + assert!(!tokens_match("abc123", "abc12")); + assert!(!tokens_match("", "a")); + } + + /// The preference starts the listener by default, off it doesn't, and a port given on the + /// command line or in the environment wins over it either way. + #[test] + fn the_preference_decides_unless_a_port_is_given() { + assert_eq!(plan(None, true), Some(Listen::Default)); + assert_eq!(plan(None, false), None); + assert_eq!(plan(Some(7878), false), Some(Listen::Exact(7878))); + assert_eq!(plan(Some(7878), true), Some(Listen::Exact(7878))); + } + + /// A taken port falls through to the next free one, on loopback only; a run of taken ports + /// is an error, not a hang. + #[test] + fn a_taken_port_falls_back_to_the_next_free_one() { + let first = bind_with_fallback(TEST.default_port, PORT_TRIES).unwrap(); + let a = first.local_addr().unwrap(); + assert!(a.ip().is_loopback() && (TEST.default_port..TEST.default_port + PORT_TRIES).contains(&a.port()), "{a}"); + let second = bind_with_fallback(a.port(), PORT_TRIES).unwrap(); + assert!(second.local_addr().unwrap().port() > a.port()); + assert!(bind_with_fallback(a.port(), 1).is_err()); + } + + #[test] + fn the_discovery_file_holds_a_port() { + assert_eq!(parse_port("27879\n"), Some(27879)); + assert_eq!(parse_port("0"), None); + assert_eq!(parse_port("70000"), None); + assert_eq!(parse_port("nope"), None); + let dir = std::env::temp_dir().join(format!("solidcraft-current-port-{}", std::process::id())); + let path = dir.join("current.port"); + TEST.write_token_file(&path, "27881").unwrap(); + assert_eq!(parse_port(&std::fs::read_to_string(&path).unwrap()), Some(27881)); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A one-line token file (and one with no process file) still reads. + #[test] + fn a_token_file_reads_with_and_without_its_process_file() { + let dir = std::env::temp_dir().join(format!("craft-control-read-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("7879.token"); + std::fs::write(&path, "legacy-token\n").unwrap(); + let client = read_token_file(&path, TEST).unwrap(); + assert_eq!(client.token, "legacy-token"); + let src = client.file.as_ref().unwrap(); + assert_eq!(src.path, std::fs::canonicalize(&path).unwrap()); + assert!(src.modified.is_some() && src.writer.is_none()); + std::fs::write(pid_path(&path), "not a pid\n").unwrap(); + assert!(read_token_file(&path, TEST).unwrap().file.unwrap().writer.is_none()); + std::fs::write(&path, "x".repeat(4097)).unwrap(); + assert!(read_token_file(&path, TEST).is_none()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A refused token's explanation names the app and writes the time as the app does. + #[test] + fn an_explanation_names_the_app() { + let file = Some(TokenSource { path: PathBuf::from("7878.token"), modified: Some(UNIX_EPOCH), writer: None }); + let client = ClientToken { token: "secret-token".into(), file, app: TEST }; + let error = client.explain_error("unauthorized"); + assert!(error.contains("written t0 (") && error.contains("if TestCraft was force-quit"), "{error}"); + assert!(!error.contains("secret-token"), "{error}"); + let explicit = TEST.client_token(0, Some("explicit-token")).unwrap(); + assert!(explicit.token == "explicit-token" && explicit.file.is_none()); + assert_eq!(explicit.explain_error("unauthorized"), "unauthorized"); + } + + /// A process file naming a process that has exited marks the token file stale. + /// Where the process inspector cannot run (a sandboxed test runner), the exit + /// is unknown, and the explanation must not claim it. + #[cfg(any(unix, windows))] + #[test] + fn a_token_file_from_an_exited_process_is_stale() { + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "auth::tests::tokens_compare_whole"]) + .stdout(std::process::Stdio::null()) + .spawn() + .unwrap(); + let pid = child.id(); + assert!(child.wait().unwrap().success()); + let source = |writer| Some(TokenSource { path: PathBuf::from("7878.token"), modified: None, writer }); + let stale = ClientToken { token: "secret-token".into(), file: source(Some((pid, None))), app: TEST }; + let error = stale.explain_error("unauthorized"); + match process_running(pid) { + Some(false) => { + assert!(error.contains(&format!("stale token file from an instance that exited (process {pid}): 7878.token")), "{error}"); + } + None => assert!(!error.contains("stale") && error.contains("restart it or delete that file"), "{error}"), + Some(true) => panic!("the reaped child {pid} is reported as running"), + } + assert!(!error.contains("secret-token")); + let live = ClientToken { token: "t".into(), file: source(Some((std::process::id(), process_start(std::process::id())))), app: TEST }; + assert!(!live.explain_error("unauthorized").contains("stale")); + #[cfg(target_os = "linux")] + { + let reused = process_start(std::process::id()).map(|s| s + 1); + let reused = ClientToken { token: "t".into(), file: source(Some((std::process::id(), reused))), app: TEST }; + assert!(reused.explain_error("unauthorized").contains("stale token file")); + } + } + + /// The token file is the user's alone, and a client reads it back. + #[cfg(unix)] + #[test] + fn the_token_file_is_private_to_the_user() { + use std::os::unix::fs::PermissionsExt; + let dir = std::env::temp_dir().join(format!("craft-control-auth-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let path = dir.join("control/7878.token"); + std::fs::create_dir_all(dir.join("control")).unwrap(); + std::fs::write(&path, "stale").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + TEST.write_token_file(&path, "0123456789abcdef0123456789abcdef").unwrap(); + let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777; + assert_eq!(mode(&path), 0o600); + assert_eq!(mode(path.parent().unwrap()), 0o700); + assert_eq!(std::fs::read_to_string(&path).unwrap().trim(), "0123456789abcdef0123456789abcdef"); + let writer = read_token_file(&path, TEST).unwrap().file.unwrap().writer; + assert_eq!(writer.map(|(pid, _)| pid), Some(std::process::id())); + drop(TokenFile(path.clone(), "0123456789abcdef0123456789abcdef".into())); + assert!(!path.exists() && !pid_path(&path).exists()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// #8794: under Flatpak two windows can listen on the same port number (each has its own + /// loopback) and write the same files. The one closing first leaves the other's token, process + /// and discovery files; the last one takes them all away, `current.pid` too. + #[test] + fn a_closing_launch_leaves_another_launchs_files() { + let dir = std::env::temp_dir().join(format!("craft-control-two-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let (file, current) = (dir.join("27878.token"), dir.join("current.port")); + let (first, second) = ("a".repeat(64), "b".repeat(64)); + // The first window, then the second on the same port, each writing both files. + for token in [&first, &second] { + TEST.write_token_file(&file, token).unwrap(); + TEST.write_token_file(¤t, "27878").unwrap(); + } + // The first closes: the second's files stay. + remove_token_file(&file, &first); + remove_discovery(¤t, &file, 27878, &first); + assert_eq!(read_token(&file).as_deref(), Some(second.as_str())); + assert!(pid_path(&file).exists() && current.exists() && pid_path(¤t).exists()); + // Another port named in the discovery file is another app's too. + remove_discovery(¤t, &file, 27879, &second); + assert!(current.exists()); + // The second closes: nothing of either is left. + remove_token_file(&file, &format!(" {second}\n")); + remove_discovery(¤t, &file, 27878, &second); + assert!(!file.exists() && !pid_path(&file).exists() && !current.exists() && !pid_path(¤t).exists()); + // With no token file left (removed by hand, or never written), the discovery file of the + // port still goes; a missing file is no error. + TEST.write_token_file(¤t, "27878").unwrap(); + remove_token_file(&file, &second); + remove_discovery(¤t, &file, 27878, &second); + assert!(!current.exists()); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/control/src/cancel.rs b/crates/control/src/cancel.rs new file mode 100644 index 000000000..e03052307 --- /dev/null +++ b/crates/control/src/cancel.rs @@ -0,0 +1,118 @@ +//! Where a control request is, so the connection that sent it can cancel it (`control.cancel`, +//! #1989): shared between the window, which moves it along, and the connection. Whoever changes +//! it first wins. + +use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use std::sync::{Mutex, PoisonError}; + +/// A request's place in the app, and how to stop the work computing it. +#[derive(Default)] +pub struct Ticket { + state: AtomicU8, + /// It is an export computing on a copy, whose worker writes its file. + writes_file: AtomicBool, + /// Tells its worker to stop: set by the window when it hands the request to one. + stopper: Mutex>>, +} + +impl std::fmt::Debug for Ticket { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Ticket").field("state", &self.state).field("writes_file", &self.writes_file).finish_non_exhaustive() + } +} + +/// Waiting its turn (sent, or held behind another request). +const WAITING: u8 = 0; +/// Running on the window thread (or handed to something that can't stop it, such as the file +/// loader). +const RUNNING: u8 = 1; +/// Computing off the window thread. +const COMPUTING: u8 = 2; +const CANCELLED: u8 = 3; + +/// What [`Ticket::cancel`] did. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Cancel { + /// It was waiting its turn: it will not run. + Removed, + /// It was computing off the window thread: it is answered as stopped and nothing of it is + /// applied. An export's worker may still write its file. + Stopped { writes_file: bool }, + /// It runs on the window thread: it will finish. + Running, + /// It was cancelled or stopped before. + Already, +} + +impl Ticket { + /// Cancel the request, unless it already runs on the window thread. + pub fn cancel(&self) -> Cancel { + let mut state = self.state.load(Ordering::Acquire); + loop { + let done = match state { + WAITING => Cancel::Removed, + COMPUTING => Cancel::Stopped { writes_file: self.writes_file.load(Ordering::Acquire) }, + CANCELLED => return Cancel::Already, + _ => return Cancel::Running, + }; + match self.state.compare_exchange(state, CANCELLED, Ordering::AcqRel, Ordering::Acquire) { + Ok(_) => { + self.stop_worker(); + return done; + } + Err(now) => state = now, + } + } + } + + pub fn is_cancelled(&self) -> bool { + self.state.load(Ordering::Acquire) == CANCELLED + } + + /// The window takes the request to run it: false when it was cancelled. + pub fn take(&self) -> bool { + self.moves(WAITING, RUNNING) + } + + /// The work computing it stops on `stop` (a worker's own stop flag). Cancelled already, it is + /// stopped at once. + pub fn on_stop(&self, stop: impl Fn() + Send + Sync + 'static) { + *self.stopper.lock().unwrap_or_else(PoisonError::into_inner) = Some(Box::new(stop)); + if self.is_cancelled() { + self.stop_worker(); + } + } + + /// The window hands it to a worker (`writes_file`: an export on a copy). + pub fn computing(&self, writes_file: bool) { + self.writes_file.store(writes_file, Ordering::Release); + let _ = self.moves(RUNNING, COMPUTING) || self.moves(WAITING, COMPUTING); + } + + /// Its worker is done and it runs on the window thread now (or it never left): false when it + /// was cancelled meanwhile. Only the window moves it on from running, so running stays running. + pub fn back(&self) -> bool { + self.moves(COMPUTING, RUNNING) || self.state.load(Ordering::Acquire) == RUNNING + } + + /// Computed, but it waits its turn again (a file opening meanwhile): false when cancelled. + pub fn wait_again(&self) -> bool { + self.moves(COMPUTING, WAITING) + } + + /// The person at the app stopped it (Stop): its worker stops too. + pub fn stop(&self) { + self.state.store(CANCELLED, Ordering::Release); + self.stop_worker(); + } + + fn stop_worker(&self) { + if let Some(stop) = self.stopper.lock().unwrap_or_else(PoisonError::into_inner).as_ref() { + stop(); + } + } + + fn moves(&self, from: u8, to: u8) -> bool { + self.state.compare_exchange(from, to, Ordering::AcqRel, Ordering::Acquire).is_ok() + } +} diff --git a/crates/control/src/client.rs b/crates/control/src/client.rs new file mode 100644 index 000000000..cbad78850 --- /dev/null +++ b/crates/control/src/client.rs @@ -0,0 +1,616 @@ +//! The control channel's client (docs/control-protocol.md): a connection to an app's loopback +//! control port, with the app's token ([`App::client_token`]) on every request, one bounded +//! round trip per request, a fresh connection after one is lost, and the wait for a request the +//! app says is still running (#1618), through `control.pending`. Its errors tell an app that is +//! not running from one that went away during a call ([`CallError`]). What a request means, and +//! anything an app adds to its parameters, is the caller's. + +use std::io::{BufRead, BufReader, Read, Write}; +use std::net::{TcpStream, ToSocketAddrs}; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; + +use crate::Effect; +use crate::auth::App; + +/// Longest reply line accepted from the app. +pub const MAX_REPLY: u64 = 64 << 20; + +/// How long a reply may take: kernel operations (booleans, fillets) on big parts can take a while. +pub const READ_TIMEOUT: Duration = Duration::from_secs(300); + +/// How long the app may take to answer a request sent [`Request::once`]: a little longer than its +/// control server waits for a long request (3600 s), so its "still running" reply arrives first. +pub const ONCE_TIMEOUT: Duration = Duration::from_secs(3720); + +/// How long connecting to the app may take. +const CONNECT_TIMEOUT: Duration = Duration::from_millis(800); + +/// How often a long wait is reported ([`Connection::with_progress`]). +const PROGRESS_EVERY: Duration = Duration::from_secs(30); + +/// How an error starts when a request is still running in the app after the wait: the request +/// was not cancelled and its reply waits on the connection; [`Connection::pending`] gets it. +pub use crate::STILL_RUNNING; + +/// How long to wait for a request the app says is still running. +#[derive(Clone, Copy, Debug)] +pub struct Waits { + /// How long a call goes on waiting, asking `control.pending`, after the app said its request + /// is still running (the app says so after 60 s), before it says so itself. + pub pending: Duration, + /// How long a later call, or [`Connection::pending`], waits for a request still running from + /// an earlier call. + pub settle: Duration, + /// How often `control.pending` is asked while waiting. + pub poll: Duration, +} + +impl Default for Waits { + fn default() -> Self { + Waits { pending: Duration::from_secs(240), settle: Duration::from_secs(60), poll: Duration::from_millis(500) } + } +} + +/// Why a call has no result. Each holds the message for the caller (an agent reads it). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum CallError { + /// Nothing listens at the address: the app is not running, or it quit before the call was + /// sent. The message says how to start it. + NotRunning(String), + /// The app went away or did not answer during the call, after running it or not. + Lost(String), + /// The request is still running in the app after the wait (it starts with [`STILL_RUNNING`]), + /// or an earlier one is and this one was not sent. + StillRunning(String), + /// The app answered with an error (a refused token says which token file it came from). + Refused(String), + /// The reply was not a control reply. + BadReply(String), +} + +impl CallError { + /// The message. + pub fn message(&self) -> &str { + match self { + CallError::NotRunning(m) | CallError::Lost(m) | CallError::StillRunning(m) | CallError::Refused(m) | CallError::BadReply(m) => m, + } + } +} + +impl std::fmt::Display for CallError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.message()) + } +} + +impl std::error::Error for CallError {} + +impl From for String { + fn from(e: CallError) -> String { + e.message().to_string() + } +} + +/// A request: its method and parameters, how messages name it and what it does if it is still +/// running when its call returns. +#[derive(Clone, Debug)] +pub struct Request { + pub method: String, + pub params: Value, + /// The request in messages ("`engine.execute` (solid.fillet)"). + pub what: String, + /// What it still does when it finishes after its wait. + pub effect: Effect, + /// Never sent twice: on a fresh connection, waiting up to [`ONCE_TIMEOUT`], and once it is + /// being written any error is final, as the app may have run it. Otherwise a request that + /// finds its connection lost is sent once more on a fresh one (the app may have restarted). + pub once: bool, +} + +impl Request { + /// `method` with `params`, named "`method`" in messages, taken to change the document, sent + /// again once on a fresh connection when its connection was lost. + pub fn new(method: &str, params: Value) -> Self { + Request { method: method.to_string(), params, what: format!("`{method}`"), effect: Effect::Changes, once: false } + } +} + +/// A control reply as a call's result: its `result`, or its `error`. +pub fn outcome(v: &Value) -> Result { + if v.get("ok").and_then(Value::as_bool) == Some(true) { + Ok(v.get("result").cloned().unwrap_or(Value::Null)) + } else { + Err(v.get("error").and_then(Value::as_str).unwrap_or("error").to_string()) + } +} + +/// A request the app said is still running. Its reply will come on this connection, through +/// `control.pending`, so the connection is kept for it. +struct Outstanding { + what: String, + /// What it does when it finishes; `None`: not known (a request this client did not send). + effect: Option, + since: Instant, +} + +impl Outstanding { + fn secs(&self) -> u64 { + self.since.elapsed().as_secs() + } +} + +/// What became of a request still running when its call returned, once a later call waited it +/// out: kept for [`Connection::pending`]. +enum Settled { + Finished { what: String, reply: Value }, + Lost { what: String }, +} + +/// The end of a wait for an outstanding request. +enum Waited { + /// It finished: its own reply. + Done(Value), + /// The app holds nothing (any more) for this connection. + Nothing, + /// Still running when the wait ran out. + Running, +} + +/// A connection to the app listening at an address. It connects when a call needs it, so a +/// client started before the app reaches it once it runs. +pub struct Connection { + app: App, + /// What the app calls its document in messages ("design"). + document: &'static str, + addr: String, + port: u16, + conn: Option<(BufReader, TcpStream)>, + /// Counts the connections made, so a caller can tell a fresh one from the one it asked. + serial: u64, + next_id: u64, + /// The token given, when one was. Otherwise each request looks the token up again, since + /// every launch of the app makes a new one. + explicit_token: Option, + /// A request still running in the app; its reply waits on `conn`. + outstanding: Option, + /// An outstanding request's end, once a later call waited for it (for `pending`). + settled: Option, + waits: Waits, + /// Where a long wait is reported. + progress: fn(&str), +} + +impl Connection { + /// A connection to `app` at `addr` (`host:port`), with `token` when given, else the app's + /// token found the usual way ([`App::client_token`]); not connected yet. + pub fn new(app: App, addr: &str, token: Option<&str>) -> Self { + let port = addr.rsplit(':').next().and_then(|p| p.parse::().ok()).unwrap_or(0); + Self { + app, + document: "design", + addr: addr.to_string(), + port, + conn: None, + serial: 0, + next_id: 1, + explicit_token: token.map(str::to_string), + outstanding: None, + settled: None, + waits: Waits::default(), + progress: |_| {}, + } + } + + /// What the app calls its document in messages ("design", the default). + #[must_use] + pub fn with_document(mut self, document: &'static str) -> Self { + self.document = document; + self + } + + /// Report a long wait for a request still running to `progress`, every 30 s. + #[must_use] + pub fn with_progress(mut self, progress: fn(&str)) -> Self { + self.progress = progress; + self + } + + /// How long to wait for a request still running. + pub fn set_waits(&mut self, waits: Waits) { + self.waits = waits; + } + + /// The address it connects to. + pub fn addr(&self) -> &str { + &self.addr + } + + /// The port of the address (0 when it names none). + pub fn port(&self) -> u16 { + self.port + } + + /// The token given, if one was. + pub fn explicit_token(&self) -> Option<&str> { + self.explicit_token.as_deref() + } + + /// Whether a connection is open now. + pub fn is_connected(&self) -> bool { + self.conn.is_some() + } + + /// The number of connections made so far: the same number while the same connection stays + /// open. + pub fn serial(&self) -> u64 { + self.serial + } + + /// Whether a request of this connection is still running in the app. + pub fn has_outstanding(&self) -> bool { + self.outstanding.is_some() + } + + /// Close the connection. A request still running loses its reply with it. + pub fn disconnect(&mut self) { + self.conn = None; + if let Some(o) = self.outstanding.take() { + self.settled = Some(Settled::Lost { what: o.what }); + } + } + + /// Connect afresh. Failing to reach the app at all is `NotConnected`, so [`Connection::explain`] + /// can tell it from a connection lost during a call. + /// + /// # Errors + /// `NotConnected` when nothing listens at the address (or it does not resolve). + pub fn connect(&mut self) -> std::io::Result<()> { + self.disconnect(); + let not_connected = |e: std::io::Error| std::io::Error::new(std::io::ErrorKind::NotConnected, e); + let mut last = std::io::Error::new(std::io::ErrorKind::NotFound, format!("cannot resolve {}", self.addr)); + for sa in self.addr.to_socket_addrs().map_err(not_connected)? { + match TcpStream::connect_timeout(&sa, CONNECT_TIMEOUT) { + Ok(s) => { + s.set_nodelay(true).ok(); + s.set_read_timeout(Some(READ_TIMEOUT)).ok(); + let read = s.try_clone()?; + self.conn = Some((BufReader::new(read), s)); + self.serial += 1; + return Ok(()); + } + Err(e) => last = e, + } + } + Err(not_connected(last)) + } + + /// See whether the app is there now; the error says why not. The connection doesn't stay + /// open: the app closes one that shows no token within a few seconds, and the first call may + /// be long away. That call connects again. + /// + /// # Errors + /// As [`Connection::connect`]. + pub fn try_connect(&mut self) -> std::io::Result<()> { + self.connect()?; + self.disconnect(); + Ok(()) + } + + /// The error for a failed exchange: whether the app is not running (and how to start it) or + /// went away during the call. + pub fn explain(&self, e: &std::io::Error) -> CallError { + let (addr, port, name, document) = (&self.addr, self.port, self.app.name, self.document); + let start = format!("{} --control {port}", name.to_lowercase()); + if e.kind() == std::io::ErrorKind::NotConnected { + CallError::NotRunning(format!( + "no {name} app is listening at {addr} ({e}): it is not running, or it quit. Start it with `{start}` (or ask the user to); the \ + next call connects to it" + )) + } else { + CallError::Lost(format!( + "the {name} app at {addr} did not answer ({e}): it may have quit or crashed during this call, after running it or not. Inspect \ + the {document} before sending a change again; if the app quit, start it with `{start}`" + )) + } + } + + /// The request line for `method`: a fresh id and the app's token (looked up again for every + /// request: an app restarted mid-session has a new one). + pub fn request_line(&mut self, method: &str, params: Value) -> String { + let id = self.next_id; + self.next_id += 1; + let mut msg = json!({"id": id, "method": method, "params": params}); + if let Some(t) = self.app.client_token(self.port, self.explicit_token.as_deref()) { + msg["token"] = json!(t.token); + } + msg.to_string() + } + + /// One request line out and one reply line back, at most [`MAX_REPLY`] long, connecting + /// first when not connected. Any failure closes the connection (a timed-out read leaves a + /// late reply on the stream). + fn roundtrip(&mut self, line: &str) -> std::io::Result { + if self.conn.is_none() { + self.connect()?; + } + let Some((reader, writer)) = self.conn.as_mut() else { return Err(std::io::Error::other("not connected")) }; + // One write: a request that loses its newline still reaches the app as a request. + let sent = writer.write_all(format!("{line}\n").as_bytes()).and_then(|()| writer.flush()); + if let Err(e) = sent { + self.disconnect(); + return Err(e); + } + let mut reply = String::new(); + let n = match reader.by_ref().take(MAX_REPLY).read_line(&mut reply) { + Ok(n) => n, + Err(e) => { + self.disconnect(); + return Err(e); + } + }; + if n == 0 || !reply.ends_with('\n') { + self.disconnect(); + return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "connection closed")); + } + Ok(reply) + } + + fn set_read_timeout(&self, t: Duration) { + if let Some((_, writer)) = &self.conn { + writer.set_read_timeout(Some(t)).ok(); + } + } + + /// A reply line as JSON. + fn parse(reply: &str) -> Result { + serde_json::from_str(reply).map_err(|e| CallError::BadReply(format!("bad reply from the app: {e}"))) + } + + /// A control reply as a call's result; a refused token says which token file it came from + /// (and whether it is stale). + /// + /// # Errors + /// [`CallError::Refused`] with the app's error. + pub fn outcome(&self, v: &Value) -> Result { + outcome(v).map_err(|error| { + CallError::Refused( + self.app.client_token(self.port, self.explicit_token.as_deref()).map_or_else(|| error.clone(), |t| t.explain_error(&error)), + ) + }) + } + + /// `method` with `params`, at once and once: not behind a request still running on this + /// connection, no second try. The app's whole reply (`ok`, `result` or `error`, and anything + /// it adds), for a request the app answers at once on any connection + /// (`control.capabilities`, `control.running`). + /// + /// # Errors + /// As [`Connection::explain`]; [`CallError::BadReply`]. + pub fn ask(&mut self, method: &str, params: Value) -> Result { + let line = self.request_line(method, params); + let reply = self.roundtrip(&line).map_err(|e| self.explain(&e))?; + Self::parse(&reply) + } + + /// `method` with `params`, waiting at most `timeout` for the reply and never behind a request + /// still running: for a request the app answers within a frame, asked again later when the + /// app is busy. One try. + /// + /// # Errors + /// [`CallError::StillRunning`] when a request of this connection is still running; as + /// [`Connection::ask`] and [`Connection::outcome`]. + pub fn quick(&mut self, method: &str, params: Value, timeout: Duration) -> Result { + if self.outstanding.is_some() { + return Err(CallError::StillRunning("a request is still running in the app".into())); + } + if self.conn.is_none() { + self.connect().map_err(|e| self.explain(&e))?; + } + let line = self.request_line(method, params); + self.set_read_timeout(timeout); + let reply = self.roundtrip(&line); + self.set_read_timeout(READ_TIMEOUT); + let reply = reply.map_err(|e| self.explain(&e))?; + let v = Self::parse(&reply)?; + self.outcome(&v) + } + + /// Ask the app, on this connection only, about the request it still runs for it. + fn ask_pending(&mut self) -> Result { + if self.conn.is_none() { + self.disconnect(); + return Err(CallError::Lost("the connection to the app was lost".into())); + } + let v = self.ask("control.pending", json!({}))?; + outcome(&v).map_err(CallError::Refused).inspect_err(|_| self.disconnect()) + } + + /// `control.cancel` for the request still running on this connection. Sent at once, without + /// waiting for that request first, and never on a fresh connection, which has nothing to + /// cancel. Once the app no longer holds it (removed, stopped or finished), later calls don't + /// wait for it. + /// + /// # Errors + /// As [`Connection::ask`]; [`CallError::Refused`]. + pub fn cancel(&mut self, params: Value) -> Result { + if self.conn.is_none() { + return Ok(json!({"cancelled": null, "note": "nothing to cancel: no request of this client is running in the app"})); + } + let v = self.ask("control.cancel", params)?; + let r = outcome(&v).map_err(CallError::Refused); + if r.is_ok() { + self.outstanding = None; + } + r + } + + /// Wait up to `limit` for the outstanding request, asking `control.pending` now and then and + /// reporting how long it takes. An error: the connection, and so the reply, was lost. + fn wait_outstanding(&mut self, limit: Duration) -> Result { + let start = Instant::now(); + let mut told = start; + loop { + let Some(o) = &self.outstanding else { return Ok(Waited::Nothing) }; + let what = o.what.clone(); + let r = self.ask_pending()?; + if r.get("pending").is_none_or(Value::is_null) { + self.outstanding = None; + return Ok(Waited::Nothing); + } + if r.get("done").and_then(Value::as_bool) == Some(true) { + self.outstanding = None; + return Ok(Waited::Done(r.get("reply").cloned().unwrap_or(Value::Null))); + } + let waited = start.elapsed(); + if waited >= limit { + return Ok(Waited::Running); + } + if told.elapsed() >= PROGRESS_EVERY { + let secs = self.outstanding.as_ref().map_or(0, Outstanding::secs); + (self.progress)(&format!("{what} is still running in the app ({secs} s); waiting for it")); + told = Instant::now(); + } + std::thread::sleep(self.waits.poll.min(limit.saturating_sub(waited))); + } + } + + /// The error for the outstanding request still running after its wait. + fn still_running(&self, earlier: bool) -> CallError { + let Some(o) = &self.outstanding else { return CallError::StillRunning(format!("{STILL_RUNNING} a request is still running in the app")) }; + let document = self.document; + let effect = + o.effect.map_or_else(|| format!("it was not cancelled and may still change the {document} when it finishes"), |e| e.when_done(document)); + let settle = self.waits.settle.as_secs(); + CallError::StillRunning(if earlier { + format!( + "{STILL_RUNNING} an earlier request, {}, is still running in the app ({} s; {effect}), so this call was not sent. Call the \ + `pending` tool to wait for its result, then send this call again", + o.what, + o.secs() + ) + } else { + format!( + "{STILL_RUNNING} {} is still running in the app after {} s ({effect}); don't send it again. Call the `pending` tool to \ + wait for its result (up to {settle} s per call); other calls wait for it first", + o.what, + o.secs() + ) + }) + } + + /// Before a new request: wait for one still running from an earlier call, keeping its end for + /// `pending`. Still running after the wait: the new request is not sent. + fn settle_earlier(&mut self) -> Result<(), CallError> { + let Some(what) = self.outstanding.as_ref().map(|o| o.what.clone()) else { return Ok(()) }; + match self.wait_outstanding(self.waits.settle) { + Ok(Waited::Done(reply)) => self.settled = Some(Settled::Finished { what, reply }), + Ok(Waited::Nothing) => {} + Ok(Waited::Running) => return Err(self.still_running(true)), + // The connection was lost, and the earlier reply with it (`pending` says so): the new + // request goes on a fresh connection. + Err(_) => {} + } + Ok(()) + } + + /// `control.pending` for the client: the end of the request still running from an earlier + /// call, waiting for it up to the settle wait: `{pending: what | null, done?, reply?, lost?, + /// running_s?}`. Never sent on a fresh connection, which has nothing pending. + pub fn pending(&mut self) -> Value { + if let Some(s) = self.settled.take() { + return match s { + Settled::Finished { what, reply } => json!({"pending": what, "done": true, "reply": reply}), + Settled::Lost { what } => json!({"pending": what, "lost": true}), + }; + } + let Some(what) = self.outstanding.as_ref().map(|o| o.what.clone()) else { return json!({"pending": null}) }; + match self.wait_outstanding(self.waits.settle) { + Ok(Waited::Done(reply)) => json!({"pending": what, "done": true, "reply": reply}), + Ok(Waited::Nothing) => json!({"pending": null}), + Ok(Waited::Running) => json!({"pending": what, "done": false, "running_s": self.outstanding.as_ref().map_or(0, Outstanding::secs)}), + Err(_) => { + self.settled = None; + json!({"pending": what, "lost": true}) + } + } + } + + /// Send `request` and wait for its reply: the app's whole reply (`ok`, `result` or `error`, + /// and anything it adds). First a request still running from an earlier call is waited for + /// (except for `control.running`, which the app answers at once); a request the app says is + /// still running is waited for through `control.pending`; a request refused unrun as busy + /// behind one this client did not know of is sent once more after that one, when + /// `retry_busy`. + /// + /// # Errors + /// As [`Connection::explain`]: a request whose connection is lost is sent once more on a fresh + /// one (unless [`Request::once`]), and when nothing listens any more the app went away, maybe + /// while running it ([`CallError::Lost`]); [`CallError::StillRunning`]; [`CallError::BadReply`]. + pub fn exchange(&mut self, request: Request, retry_busy: bool) -> Result { + let Request { method, params, what, effect, once } = request; + if method != "control.running" { + self.settle_earlier()?; + } + let again = retry_busy.then(|| params.clone()); + let sent = Instant::now(); + let line = self.request_line(&method, params); + let reply = if once { + // A fresh connection (an idle one may have been closed by an app restart; connecting + // again sends nothing). + self.connect().or_else(|_| self.connect()).and_then(|()| { + self.set_read_timeout(ONCE_TIMEOUT); + let r = self.roundtrip(&line); + self.set_read_timeout(READ_TIMEOUT); + r + }) + } else { + self.roundtrip(&line).or_else(|lost| { + self.roundtrip(&line).map_err(|e| if e.kind() == std::io::ErrorKind::NotConnected && lost.kind() != e.kind() { lost } else { e }) + }) + } + .map_err(|e| self.explain(&e))?; + let v = Self::parse(&reply)?; + // Timed out in the app, still running: wait for its result on this connection. + if v.get("running").and_then(Value::as_bool) == Some(true) { + self.outstanding = Some(Outstanding { what: what.clone(), effect: Some(effect), since: sent }); + (self.progress)(&format!( + "{what} is still running in the app after {} s; waiting up to {} s more for it", + sent.elapsed().as_secs(), + self.waits.pending.as_secs() + )); + let document = self.document; + return match self.wait_outstanding(self.waits.pending)? { + Waited::Done(reply) => Ok(reply), + Waited::Nothing => Err(CallError::Lost(format!( + "{what} timed out in the app, which kept no result for it: inspect the {document} before sending it again" + ))), + Waited::Running => Err(self.still_running(false)), + }; + } + // Refused unrun behind a request this client did not know of: wait for that one, then + // send this one once more. + if v.get("busy").and_then(Value::as_bool) == Some(true) + && let Some(params) = again + { + self.outstanding = Some(Outstanding { what: "an earlier request".into(), effect: None, since: Instant::now() }); + self.settle_earlier()?; + return self.exchange(Request { method, params, what, effect, once }, false); + } + Ok(v) + } + + /// [`Connection::exchange`] (retrying a busy refusal once) and the reply's result. + /// + /// # Errors + /// As [`Connection::exchange`] and [`Connection::outcome`]. + pub fn call(&mut self, request: Request) -> Result { + let v = self.exchange(request, true)?; + self.outcome(&v) + } +} + +#[cfg(test)] +#[path = "client_tests.rs"] +mod tests; diff --git a/crates/control/src/client_tests.rs b/crates/control/src/client_tests.rs new file mode 100644 index 000000000..f5af19a00 --- /dev/null +++ b/crates/control/src/client_tests.rs @@ -0,0 +1,190 @@ +//! The client against the shared server ([`crate::server::Hub`]) on an ephemeral loopback port. + +use super::*; +use crate::ControlRequest; +use crate::server::{Hub, Protocol, Stop, Summary, Waits as ServerWaits}; +use std::ffi::OsString; +use std::net::TcpListener; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::mpsc::channel; + +const TOKEN: &str = "0123456789abcdef0123456789abcdef"; + +fn no_dir() -> Option { + None +} + +fn no_env(_: &str) -> Option { + None +} + +fn utc(secs: u64) -> String { + secs.to_string() +} + +/// An app with no token files and no environment: a client sends only the token it is given. +const APP: App = App { name: "Probe", data_dir: no_dir, env: no_env, utc, default_port: 0 }; + +/// An app whose requests all may change its document. +struct Probe; + +impl Protocol for Probe { + fn summary(&self, method: &str, _: &Value) -> Summary { + Summary { what: format!("`{method}`"), effect: Effect::Changes, command: None, steps: None, long: false } + } + fn document(&self) -> &'static str { + "design" + } + fn change_event(&self) -> &'static str { + "design.changed" + } + fn revision_method(&self) -> &'static str { + "document.revision" + } + fn docs(&self) -> &'static str { + "docs/control-protocol.md" + } + fn capabilities(&self) -> Value { + json!({}) + } +} + +/// The app on `port` (0: an ephemeral one) behind [`TOKEN`]: its window answers each request after +/// `delay` with the request's method and parameters, and the server says a request is still +/// running after `wait`. +fn serve(port: u16, delay: Duration, wait: Duration) -> Stop { + // A port just closed may take a moment to be free again on a loaded machine. + let started = Instant::now(); + let mut bound = TcpListener::bind(("127.0.0.1", port)); + while bound.is_err() && started.elapsed() < Duration::from_secs(2) { + std::thread::sleep(Duration::from_millis(10)); + bound = TcpListener::bind(("127.0.0.1", port)); + } + let listener = bound.unwrap(); + let (tx, rx) = channel::(); + std::thread::spawn(move || { + for req in rx { + std::thread::sleep(delay); + let _ = req.reply.send(json!({"ok": true, "result": {"method": req.method, "params": req.params}})); + } + }); + let waits = ServerWaits { short: wait, long: wait, token: Duration::from_secs(5) }; + let hub = Arc::new(Hub::new(Probe, || {}, 1 << 20).with_waits(waits)); + hub.listen_stoppable(listener, TOKEN.into(), tx, false).unwrap() +} + +fn addr(port: u16) -> String { + format!("127.0.0.1:{port}") +} + +/// A port nothing listens on. +fn free_port() -> u16 { + TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port() +} + +#[test] +fn a_call_is_one_round_trip_with_the_token() { + let app = serve(0, Duration::ZERO, Duration::from_secs(10)); + let mut c = Connection::new(APP, &addr(app.port()), Some(TOKEN)); + assert!(!c.is_connected()); + let r = c.call(Request::new("document.inspect", json!({"a": 1}))).unwrap(); + assert_eq!(r, json!({"method": "document.inspect", "params": {"a": 1}})); + assert!(c.is_connected()); + let serial = c.serial(); + // the next call goes on the same connection + c.call(Request::new("document.inspect", json!({}))).unwrap(); + assert_eq!(c.serial(), serial); + // `try_connect` sees the app there and leaves no connection open + let mut fresh = Connection::new(APP, &addr(app.port()), Some(TOKEN)); + fresh.try_connect().unwrap(); + assert!(!fresh.is_connected()); +} + +#[test] +fn a_wrong_token_or_none_is_refused() { + let app = serve(0, Duration::ZERO, Duration::from_secs(10)); + for token in [Some("fedcba9876543210fedcba9876543210"), None] { + let mut c = Connection::new(APP, &addr(app.port()), token); + match c.call(Request::new("document.inspect", json!({}))) { + Err(CallError::Refused(e)) => assert!(e.starts_with("unauthorized"), "{token:?}: {e}"), + other => panic!("{token:?}: {other:?}"), + } + } +} + +#[test] +fn an_app_not_running_says_how_to_start_it() { + let port = free_port(); + let mut c = Connection::new(APP, &addr(port), Some(TOKEN)); + let e = c.call(Request::new("document.inspect", json!({}))).unwrap_err(); + let CallError::NotRunning(m) = &e else { panic!("{e:?}") }; + assert!(m.starts_with(&format!("no Probe app is listening at 127.0.0.1:{port}")), "{m}"); + assert!(m.contains(&format!("`probe --control {port}`")), "{m}"); + assert_eq!(c.try_connect().unwrap_err().kind(), std::io::ErrorKind::NotConnected); +} + +/// An app that goes away during a call is lost, not "not running"; the next call says it is not +/// running, and once it runs again the next call reaches it. +#[test] +fn an_app_gone_during_a_call_is_lost_and_reached_again_once_back() { + let app = serve(0, Duration::ZERO, Duration::from_secs(10)); + let port = app.port(); + let mut c = Connection::new(APP, &addr(port), Some(TOKEN)); + c.call(Request::new("document.inspect", json!({}))).unwrap(); + app.stop(); + let e = c.call(Request::new("document.inspect", json!({}))).unwrap_err(); + let CallError::Lost(m) = &e else { panic!("{e:?}") }; + assert!(m.contains("may have quit") && m.contains("Inspect the design") && m.contains(&format!("`probe --control {port}`")), "{m}"); + assert!(matches!(c.call(Request::new("document.inspect", json!({}))), Err(CallError::NotRunning(_)))); + let _back = serve(port, Duration::ZERO, Duration::from_secs(10)); + assert_eq!(c.call(Request::new("ui.view", json!({}))).unwrap()["method"], json!("ui.view")); +} + +/// A server that reads a request and closes the connection without a reply: the call is lost +/// (after one more try on a fresh connection), and the reply is never taken for "not running". +#[test] +fn a_connection_closed_without_a_reply_is_lost() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let mut line = String::new(); + let _ = BufReader::new(stream).read_line(&mut line); + } + }); + let mut c = Connection::new(APP, &addr(port), Some(TOKEN)); + let e = c.call(Request::new("document.inspect", json!({}))).unwrap_err(); + assert!(matches!(e, CallError::Lost(_)), "{e:?}"); + assert!(!c.is_connected()); +} + +/// A request the app says is still running is waited for through `control.pending`, on the same +/// connection, and its own reply is the call's result. +#[test] +fn a_request_still_running_is_waited_for_through_pending() { + let app = serve(0, Duration::from_millis(400), Duration::from_millis(50)); + let mut c = Connection::new(APP, &addr(app.port()), Some(TOKEN)); + c.set_waits(Waits { pending: Duration::from_secs(10), settle: Duration::from_secs(10), poll: Duration::from_millis(5) }); + let r = c.call(Request::new("solid.slow", json!({"n": 2}))).unwrap(); + assert_eq!(r, json!({"method": "solid.slow", "params": {"n": 2}})); + assert!(!c.has_outstanding()); + assert_eq!(c.pending(), json!({"pending": null})); +} + +/// Past the wait, the call says the request is still running, and `pending` later has its end. +#[test] +fn a_request_still_running_past_the_wait_is_reported_and_its_end_kept() { + let app = serve(0, Duration::from_millis(400), Duration::from_millis(20)); + let mut c = Connection::new(APP, &addr(app.port()), Some(TOKEN)); + c.set_waits(Waits { pending: Duration::from_millis(30), settle: Duration::from_secs(10), poll: Duration::from_millis(5) }); + let mut slow = Request::new("solid.slow", json!({})); + slow.what = "`solid.slow` (now)".into(); + let e = c.call(slow).unwrap_err(); + let CallError::StillRunning(m) = &e else { panic!("{e:?}") }; + assert!(m.starts_with(STILL_RUNNING) && m.contains("`solid.slow` (now)") && m.contains("will still apply"), "{m}"); + assert!(c.has_outstanding()); + let end = c.pending(); + assert_eq!(end["done"], json!(true), "{end}"); + assert_eq!(end["reply"]["result"]["method"], json!("solid.slow"), "{end}"); +} diff --git a/crates/control/src/effect.rs b/crates/control/src/effect.rs new file mode 100644 index 000000000..aed5cbe59 --- /dev/null +++ b/crates/control/src/effect.rs @@ -0,0 +1,27 @@ +//! What a client is told about a control request that outlasts its wait (docs/control-protocol.md, +//! #1618): the app's control server and a bridged client both say what the request still does when +//! it finishes. Which requests do what is the app's to say. + +/// What a request still running does when it finishes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Effect { + /// It only reads (the document, the window or the command list): nothing changes. + ReadsOnly, + /// It writes a file (an export, a save, a render to a path); the document stays as it is. + WritesFile, + /// It changes the document (or may: anything not known to read or only write a file). + Changes, +} + +impl Effect { + /// What happens when the request finishes, for a message: "it only reads: nothing changes", + /// "it was not cancelled and will still write the file when it finishes; the design does not + /// change", … `document` is what the app calls its document ("design"). + pub fn when_done(self, document: &str) -> String { + match self { + Effect::ReadsOnly => "it only reads: nothing changes".into(), + Effect::WritesFile => format!("it was not cancelled and will still write the file when it finishes; the {document} does not change"), + Effect::Changes => "it was not cancelled and will still apply when it finishes".into(), + } + } +} diff --git a/crates/control/src/lib.rs b/crates/control/src/lib.rs new file mode 100644 index 000000000..26727a97c --- /dev/null +++ b/crates/control/src/lib.rs @@ -0,0 +1,26 @@ +//! The control channel any app may share (docs/control-protocol.md): the per-launch token and its +//! files ([`auth`]), the loopback server that admits a connection by the token and hands each +//! request to the window ([`server`]), the client that talks to it ([`client`]), the request the +//! window answers ([`ControlRequest`]), the document watch subscribed connections wait on +//! ([`DocumentWatch`]), and what a request still running does when it finishes ([`Effect`]). It +//! needs no UI toolkit; the window's own requests over egui (`ui.screenshot`, `ui.resize`) are +//! `craft-control-egui`. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +pub mod auth; +mod cancel; +#[cfg(not(target_arch = "wasm32"))] +pub mod client; +mod effect; +mod request; +#[cfg(not(target_arch = "wasm32"))] +pub mod server; + +pub use cancel::{Cancel, Ticket}; +pub use effect::Effect; +pub use request::{ControlRequest, ControlResponse, DOCUMENT_WATCH, DocumentWatch, Subscriber, cancelled_chat_response, chat_turn_cancelled}; + +/// How an error from the client (`client`, native only) starts when a request is still running +/// in the app after the wait. Outside the client so callers on every target can test for it. +pub const STILL_RUNNING: &str = "still running:"; diff --git a/crates/control/src/request.rs b/crates/control/src/request.rs new file mode 100644 index 000000000..b97c90b76 --- /dev/null +++ b/crates/control/src/request.rs @@ -0,0 +1,125 @@ +//! A control request as the window gets it, and the design watch subscribed connections wait on. + +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::mpsc::Sender; +use std::sync::{Condvar, Mutex, MutexGuard, PoisonError}; +use std::time::Duration; + +use serde_json::{Value, json}; + +pub type ControlResponse = Value; + +pub struct ControlRequest { + pub method: String, + pub params: Value, + pub reply: Sender, + /// Set once the window starts on it; until then it waits its turn (`control.running`, #3654). + pub started: std::sync::Arc, + /// Chat requests already admitted by the gate may outlive a tab switch. This turn's + /// validity follows them through queues, computation and cursor animation. + pub chat_turn: Option>, + /// Where the request is, shared with the connection that sent it so it can cancel it + /// (`control.cancel`, #1989). + pub ticket: std::sync::Arc, + /// Told apart from every other request of this run: names its row while it waits. + pub serial: u64, +} + +impl ControlRequest { + pub fn new(method: impl Into, params: Value) -> (Self, std::sync::mpsc::Receiver) { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + let (tx, rx) = std::sync::mpsc::channel(); + let serial = NEXT.fetch_add(1, Ordering::Relaxed); + (Self { method: method.into(), params, reply: tx, started: Default::default(), chat_turn: None, ticket: Default::default(), serial }, rx) + } + + /// Answer a request whose agent chat turn has ended, and say so: such a request is not run. + pub fn refuse_cancelled_chat(&self) -> bool { + if chat_turn_cancelled(self.chat_turn.as_deref()) { + let _ = self.reply.send(cancelled_chat_response()); + true + } else { + false + } + } +} + +/// Whether the agent chat turn a request belongs to has ended (`None`: it belongs to none). +pub fn chat_turn_cancelled(turn: Option<&std::sync::atomic::AtomicBool>) -> bool { + turn.is_some_and(|active| !active.load(std::sync::atomic::Ordering::Relaxed)) +} + +/// The reply to a request whose agent chat turn has ended. +pub fn cancelled_chat_response() -> Value { + json!({"ok": false, "error": "not run: the agent chat turn ended or its document tab was left"}) +} + +/// The active design's id and revision for control connections that sent `control.subscribe` +/// (#6985): the window publishes them each frame while one is subscribed, and each such +/// connection waits for them to move, then sends the app's change event (`Protocol::change_event`). +#[derive(Default)] +pub struct DocumentWatch { + subscribers: AtomicUsize, + now: Mutex>, + moved: Condvar, +} + +/// The app's [`DocumentWatch`]. +pub static DOCUMENT_WATCH: DocumentWatch = DocumentWatch::new(); + +impl DocumentWatch { + pub const fn new() -> Self { + DocumentWatch { subscribers: AtomicUsize::new(0), now: Mutex::new(None), moved: Condvar::new() } + } + + fn now(&self) -> MutexGuard<'_, Option<(String, u64)>> { + self.now.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// The design as last published (`None` before anything was). + pub fn current(&self) -> Option<(String, u64)> { + self.now().clone() + } + + /// The design as it is now; waiters wake only when it moved. Only the latest is kept, so + /// changes made faster than a connection sends them go out as one. + pub fn publish(&self, document_id: &str, revision: u64) { + let mut now = self.now(); + if now.as_ref().is_some_and(|(id, r)| id == document_id && *r == revision) { + return; + } + *now = Some((document_id.to_string(), revision)); + self.moved.notify_all(); + } + + /// Whether a connection is subscribed: the window publishes only then. + pub fn subscribed(&self) -> bool { + self.subscribers.load(Ordering::Relaxed) > 0 + } + + /// Count a subscribed connection until the returned guard is dropped. + pub fn join(&'static self) -> Subscriber { + self.subscribers.fetch_add(1, Ordering::Relaxed); + Subscriber(self) + } + + /// Wait up to `wait` for the design to differ from `seen`, or for `stop`; the design then + /// (`None` before anything was published). + pub fn wait(&self, seen: &(String, u64), stop: &AtomicBool, wait: Duration) -> Option<(String, u64)> { + let unmoved = |now: &mut Option<(String, u64)>| now.as_ref() == Some(seen) && !stop.load(Ordering::Relaxed); + let (now, _) = self.moved.wait_timeout_while(self.now(), wait, unmoved).unwrap_or_else(PoisonError::into_inner); + now.clone() + } +} + +/// A subscribed connection, counted by its [`DocumentWatch`] while this lives. +pub struct Subscriber(&'static DocumentWatch); + +impl Drop for Subscriber { + fn drop(&mut self) { + self.0.subscribers.fetch_sub(1, Ordering::Relaxed); + // Under the lock, so a connection about to wait sees its `stop` set before it does. + let _now = self.0.now(); + self.0.moved.notify_all(); + } +} diff --git a/crates/control/src/server.rs b/crates/control/src/server.rs new file mode 100644 index 000000000..85664a1aa --- /dev/null +++ b/crates/control/src/server.rs @@ -0,0 +1,1433 @@ +//! The loopback JSON-lines control server: one request per line, one reply per line, behind the +//! launch's token (docs/control-protocol.md). Agents and tests drive an app through it (adapted +//! from CADCraft, MIT OR Apache-2.0). What the app's requests mean is the app's [`Protocol`]; the +//! window answers them from the [`ControlRequest`]s it is sent. + +use std::io::{BufRead, BufReader, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; +use std::sync::mpsc::{Receiver, Sender, TryRecvError, channel}; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; + +use crate::Effect; +use crate::auth::tokens_match; +use crate::cancel::{Cancel, Ticket}; +use crate::request::{ControlRequest, DOCUMENT_WATCH, DocumentWatch, Subscriber}; + +/// What the server needs to know of a request before it sends it to the window: how to name it +/// in messages, what it does if it outlasts its wait, and what `control.running` shows of it. +#[derive(Clone, Debug)] +pub struct Summary { + /// The request in messages ("`engine.execute` (solid.fillet)"). + pub what: String, + /// What it still does when it finishes after its wait. + pub effect: Effect, + /// The command it runs, for `control.running` (never its parameters). + pub command: Option<&'static str>, + /// The number of steps it runs, for `control.running`. + pub steps: Option, + /// It may take as long as a batch. + pub long: bool, +} + +/// What an app's requests mean to its control server. +pub trait Protocol: Send + Sync { + fn summary(&self, method: &str, params: &Value) -> Summary; + /// What the app calls its document in messages ("design"). + fn document(&self) -> &'static str; + /// The event a subscribed connection is sent when the document moves ("design.changed"). + fn change_event(&self) -> &'static str; + /// The method that answers the document's id and revision now ("document.revision"). + fn revision_method(&self) -> &'static str; + /// Where a client reads how to talk to the channel, named when its token is refused + /// ("docs/control-protocol.md"). + fn docs(&self) -> &'static str; + /// The `control.capabilities` result: what this app's channel does beyond the methods every + /// version has, for a client to find out before it relies on it (an older app answers + /// `control.capabilities` with "unknown method"). + fn capabilities(&self) -> Value; +} + +/// An app's control server: its [`Protocol`], how it wakes the window for a request, and the +/// requests every connection of its listeners has the window running (`control.running`). +pub struct Hub { + protocol: Box, + wake: Box, + /// Longest request line accepted. + max_line: usize, + waits: Waits, + watch: &'static DocumentWatch, + running: Arc, + open: AtomicUsize, +} + +impl Hub { + /// A server for `protocol`'s requests; `wake` makes the window look at its requests now. + pub fn new(protocol: impl Protocol + 'static, wake: impl Fn() + Send + Sync + 'static, max_line: usize) -> Self { + Hub { + protocol: Box::new(protocol), + wake: Box::new(wake), + max_line, + waits: Waits::default(), + watch: &DOCUMENT_WATCH, + running: Arc::new(Running::new()), + open: AtomicUsize::new(0), + } + } + + /// Other waits (tests). + pub fn with_waits(mut self, waits: Waits) -> Self { + self.waits = waits; + self + } + + /// Another document watch than the app's [`DOCUMENT_WATCH`] (tests). + pub fn with_watch(mut self, watch: &'static DocumentWatch) -> Self { + self.watch = watch; + self + } + + /// Serve `listener` with `token` on threads of their own, at most [`MAX_CONNECTIONS`] + /// connections at once. `patient`: every request may wait as long as a batch (a person decides + /// on it). A listener that is not on a loopback address is refused: the channel never goes on + /// the network. + pub fn listen(self: &Arc, listener: TcpListener, token: String, requests: Sender, patient: bool) -> std::io::Result<()> { + self.listen_stoppable(listener, token, requests, patient).map(drop) + } + + /// [`Hub::listen`], and a [`Stop`] that closes the listener and the connections it accepted + /// (a channel turned off in the app's settings). Dropping the [`Stop`] leaves it serving. + pub fn listen_stoppable( + self: &Arc, + listener: TcpListener, + token: String, + requests: Sender, + patient: bool, + ) -> std::io::Result { + let addr = listener.local_addr()?; + if !addr.ip().is_loopback() { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("the control channel listens on loopback only, not {addr}"))); + } + let hub = Arc::clone(self); + let token = Arc::new(token); + let shared = Arc::new(Accepted::default()); + let (closed_tx, closed) = channel::<()>(); + let stop = Stop { addr, shared: Arc::clone(&shared), closed }; + std::thread::spawn(move || { + // Dropped when this thread ends, with the listener: `Stop::stop` waits for that. + let _closed = closed_tx; + for stream in listener.incoming().flatten() { + if shared.stopped.load(Ordering::SeqCst) { + break; + } + if hub.open.fetch_add(1, Ordering::Relaxed) >= MAX_CONNECTIONS { + hub.open.fetch_sub(1, Ordering::Relaxed); + let mut refused = stream; + let _ = send(&mut refused, &json!({"ok": false, "error": "too many connections"})); + continue; + } + let id = shared.next.fetch_add(1, Ordering::Relaxed); + if let Ok(copy) = stream.try_clone() { + shared.lock().push((id, copy)); + } + // Stopped while this one was being accepted: it is closed with the rest. + if shared.stopped.load(Ordering::SeqCst) { + shared.close_all(); + } + let (hub, tx, token, shared) = (Arc::clone(&hub), requests.clone(), Arc::clone(&token), Arc::clone(&shared)); + std::thread::spawn(move || { + hub.serve(stream, &token, tx, patient); + hub.open.fetch_sub(1, Ordering::Relaxed); + shared.lock().retain(|(i, _)| *i != id); + }); + } + // A captured value drops after the body's locals, so without this `_closed` would tell + // `Stop::stop` the port is free while the listener still holds it (#14752). + drop(listener); + }); + Ok(stop) + } +} + +/// The connections a stoppable listener accepted, so stopping it can close them too. +#[derive(Default)] +struct Accepted { + stopped: AtomicBool, + next: AtomicU64, + streams: Mutex>, +} + +impl Accepted { + fn lock(&self) -> MutexGuard<'_, Vec<(u64, TcpStream)>> { + self.streams.lock().unwrap_or_else(PoisonError::into_inner) + } + + fn close_all(&self) { + for (_, stream) in self.lock().drain(..) { + let _ = stream.shutdown(std::net::Shutdown::Both); + } + } +} + +/// Stops a listener started with [`Hub::listen_stoppable`]. +pub struct Stop { + addr: std::net::SocketAddr, + shared: Arc, + closed: Receiver<()>, +} + +impl Stop { + /// The port the listener is on. + pub fn port(&self) -> u16 { + self.addr.port() + } + + /// Stop accepting, close the port and end every connection it accepted. Returns once the + /// port is closed (at most a couple of seconds), so the same port can be bound again at once. + pub fn stop(self) { + self.shared.stopped.store(true, Ordering::SeqCst); + // The listener waits in `accept`: a connection of our own wakes it to see the flag. + let _ = TcpStream::connect_timeout(&self.addr, Duration::from_secs(1)); + let _ = self.closed.recv_timeout(Duration::from_secs(2)); + self.shared.close_all(); + } +} + +/// Connections served at once, so a flood must not be able to spawn threads without end. +pub const MAX_CONNECTIONS: usize = 16; +/// Lines that are not JSON before the connection is closed, so garbage cannot loop forever. +const MAX_BAD_LINES: usize = 8; +/// How long a subscribed connection waits after a `design.changed` line before the next: what +/// changes meanwhile (a parameter dragged) goes out as one line, the latest (#6985). +const EVENT_GAP: Duration = Duration::from_millis(100); +/// How long a new connection has to show the token. A client connects when it has a request to +/// send, so this is plenty; one that doesn't send it loses the connection, and its seat among +/// [`MAX_CONNECTIONS`] goes to someone else. +const TOKEN_WAIT: Duration = Duration::from_secs(5); + +/// A new random token: 32 bytes from the operating system, as hex. +pub fn new_token() -> Option { + let mut bytes = [0u8; 32]; + getrandom::fill(&mut bytes).ok()?; + Some(bytes.iter().map(|b| format!("{b:02x}")).collect()) +} + +/// One line without its newline, or `None` at the end of the stream. A client that sends +/// gigabytes with no newline would otherwise make this thread allocate until the process is +/// killed, so the line is refused as soon as it passes `max`. +fn read_line(r: &mut impl BufRead, max: usize) -> std::io::Result>> { + let mut line = Vec::new(); + loop { + let (done, used) = { + let available = r.fill_buf()?; + if available.is_empty() { + return Ok((!line.is_empty()).then_some(line)); + } + match available.iter().position(|b| *b == b'\n') { + Some(i) => { + line.extend_from_slice(available.get(..i).unwrap_or(&[])); + (true, i + 1) + } + None => { + line.extend_from_slice(available); + (false, available.len()) + } + } + }; + r.consume(used); + if done { + return Ok(Some(line)); + } + if line.len() > max { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "request too large")); + } + } +} + +/// A reply line, written to the socket at once. `writeln!` with a JSON value hands the socket each +/// of its pieces apart, a system call for every string, number and comma: half a second for a +/// design's component tree when it has thousands of components. +fn send(out: &mut impl Write, reply: &Value) -> std::io::Result<()> { + let mut line = serde_json::to_vec(reply).map_err(std::io::Error::other)?; + line.push(b'\n'); + out.write_all(&line) +} + +/// The `"token"` of a request line that is a JSON object, read without building the rest of it. +/// Until a connection shows the token, a line costs only its own bytes: parsed whole, a 16 MiB +/// line of `[0,0,…]` is hundreds of MB, and 16 such connections could exhaust memory before any +/// token was checked. +fn token_of(line: &str) -> Option { + struct Token; + impl<'de> serde::de::Visitor<'de> for Token { + type Value = Option; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("a request object") + } + fn visit_map>(self, mut map: A) -> Result { + let mut token = None; + while let Some(key) = map.next_key::()? { + if key == "token" { + token = Some(map.next_value::()?); + } else { + map.next_value::()?; + } + } + Ok(token) + } + } + let mut de = serde_json::Deserializer::from_str(line); + let token = serde::Deserializer::deserialize_map(&mut de, Token).ok()?; + de.end().ok()?; + token +} + +/// The reply to a connection that didn't show the token, naming the app's `docs`; the +/// connection is then closed. +fn unauthorized(docs: &str) -> Value { + json!({"ok": false, "error": format!("unauthorized: send the control token as \"token\" (see {docs})")}) +} + +/// Refuse the connection: the reply, then the end of what we send, then what the client had +/// already sent read and dropped (a little, briefly). Closing a socket with unread input resets +/// the connection, and a reset can reach the client before it has read the refusal. +fn refuse(out: &mut TcpStream, reader: &mut BufReader, docs: &str) { + use std::io::Read; + let _ = send(out, &unauthorized(docs)); + let _ = out.shutdown(std::net::Shutdown::Write); + reader.get_mut().deadline = Some(Instant::now() + Duration::from_millis(500)); + let _ = std::io::copy(&mut reader.by_ref().take(64 * 1024), &mut std::io::sink()); +} + +/// What a client sends. Until it shows the token, all of its reads share one deadline, so neither +/// blank lines nor a line sent a byte at a time buy it more time. After that it may stay idle as +/// long as it likes. +struct ClientInput { + socket: TcpStream, + deadline: Option, +} + +impl ClientInput { + /// No deadline any more: the client showed the token. + fn admit(&mut self) -> std::io::Result<()> { + self.deadline = None; + self.socket.set_read_timeout(None) + } +} + +impl std::io::Read for ClientInput { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if let Some(deadline) = self.deadline { + let left = deadline.saturating_duration_since(Instant::now()); + if left.is_zero() { + return Err(std::io::ErrorKind::TimedOut.into()); + } + self.socket.set_read_timeout(Some(left))?; + } + self.socket.read(buf) + } +} + +/// How long a request waits for the app's reply: most requests, and a batch (or every request, +/// on a patient connection); and how long a new connection has to show the token. +#[derive(Clone, Copy, Debug)] +pub struct Waits { + pub short: Duration, + pub long: Duration, + pub token: Duration, +} + +impl Default for Waits { + fn default() -> Self { + Waits { short: Duration::from_secs(60), long: Duration::from_secs(3600), token: TOKEN_WAIT } + } +} + +/// A request that timed out but is still running in the app (#1618): its id and method, and where +/// its reply will land. +struct Pending { + id: Value, + method: String, + /// The request in messages ("`engine.execute` (solid.fillet)"). + what: String, + reply: Receiver, + /// Where it is in the app, to cancel it (`control.cancel`). + job: Arc, +} + +/// `control.cancel {id?}`: cancel the request this connection still waits for (the one +/// `control.pending` reports; `id`, when given, must name it). A connection can only cancel its +/// own requests. Waiting its turn, it is removed; computing off the window thread, it is stopped +/// as the app's Stop stops it. Either way nothing of it is applied and the connection is free. +/// One that runs on the window thread can't be cancelled; one that finished is answered. +fn cancel(pending: &mut Option, late: Option, id: Option<&Value>, document: &str) -> Value { + let id = id.filter(|v| !v.is_null()); + let mine = pending.as_ref().filter(|p| id.is_none_or(|id| *id == p.id)); + let Some(p) = mine else { + return match id { + None => json!({"ok": true, "result": {"cancelled": null, "note": "nothing to cancel: no request of this connection is running"}}), + Some(id) => json!({"ok": false, "error": format!( + "nothing to cancel: request {} is not running on this connection (a connection can only cancel its own requests)", + id.to_string().chars().take(64).collect::() + )}), + }; + }; + let (what, id) = (format!("request {} ({})", p.id, p.what), p.id.clone()); + let finished = |p: Pending, reply: Value| { + json!({"ok": true, "result": {"cancelled": null, "pending": p.id, "method": p.method, "done": true, "reply": reply, + "note": format!("nothing to cancel: {what} had already finished; its reply is here")}}) + }; + if let Some(reply) = late { + return pending.take().map_or(Value::Null, |p| finished(p, reply)); + } + let outcome = p.job.cancel(); + let note = match outcome { + Cancel::Removed => format!("cancelled: {what} was still waiting its turn and will not run; nothing was applied"), + Cancel::Stopped { writes_file: false } => format!("stopped: {what} was computing off the window thread; nothing was applied"), + Cancel::Stopped { writes_file: true } => { + format!("stopped: {what} was computing on a copy of the {document}, which is unchanged; its worker may still write the file") + } + Cancel::Already => format!("{what} was already cancelled or stopped; nothing of it was applied"), + Cancel::Running => { + // It may have just finished. + if let Some(reply) = pending.as_ref().and_then(|p| p.reply.try_recv().ok()) { + return pending.take().map_or(Value::Null, |p| finished(p, reply)); + } + return json!({"ok": false, "cancellable": false, "error": format!( + "cannot be cancelled: {what} already runs in the app (on its window thread, or as a file it opens) and will \ + still finish; ask `control.pending` for its result. Only a request still waiting its turn, or a change or \ + export computing off the window thread, can be cancelled" + )}); + } + }; + let method = pending.take().map(|p| p.method); + let how = match outcome { + Cancel::Removed => "removed", + Cancel::Stopped { .. } => "stopped", + _ => "already", + }; + json!({"ok": true, "result": {"cancelled": id, "method": method, "outcome": how, "note": note}}) +} + +/// Every request the app has been sent and not answered yet, from every connection, for +/// `control.running` (#3654). Each connection's own thread answers that, so it never waits behind +/// a running request. +struct Running { + list: Mutex>, + connections: AtomicU64, + keys: AtomicU64, +} + +/// What `control.running` says about a request: never its parameters beyond the command id. +struct InFlight { + key: u64, + connection: u64, + id: Value, + method: String, + command: Option<&'static str>, + steps: Option, + since: Instant, + started: Arc, +} + +impl Running { + fn new() -> Self { + Running { list: Mutex::new(Vec::new()), connections: AtomicU64::new(0), keys: AtomicU64::new(0) } + } + + fn lock(&self) -> MutexGuard<'_, Vec> { + self.list.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// List `req`, sent on `connection`, until the returned ticket is dropped; `command` and + /// `steps` as the app's [`Protocol`] summarised it. + fn add(self: &Arc, connection: u64, id: &Value, req: &ControlRequest, command: Option<&'static str>, steps: Option) -> Listing { + let key = self.keys.fetch_add(1, Ordering::Relaxed); + // Ids and methods come from the client: only short ones are repeated to the others. + let id = match id { + Value::Number(_) => id.clone(), + Value::String(s) if s.len() <= 64 => id.clone(), + _ => Value::Null, + }; + let method = req.method.chars().take(64).collect(); + let started = req.started.clone(); + self.lock().push(InFlight { key, connection, id, method, command, steps, since: Instant::now(), started }); + Listing { running: Arc::clone(self), key } + } + + /// The `control.running` result, as `asker`'s connection sees it. + fn result(&self, asker: u64) -> Value { + let rows: Vec = self + .lock() + .iter() + .map(|e| { + let mut r = json!({ + "connection": e.connection, + "id": e.id, + "method": e.method, + "running_s": (e.since.elapsed().as_secs_f64() * 10.0).round() / 10.0, + "queued": !e.started.load(Ordering::Relaxed), + }); + if let Some(c) = e.command { + r["command"] = json!(c); + } + if let Some(n) = e.steps { + r["steps"] = json!(n); + } + if e.connection == asker { + r["self"] = json!(true); + } + r + }) + .collect(); + json!({"running": rows}) + } +} + +/// A listed request; dropped when its reply arrives. +struct Listing { + running: Arc, + key: u64, +} + +impl Drop for Listing { + fn drop(&mut self) { + let key = self.key; + self.running.lock().retain(|e| e.key != key); + } +} + +/// The reply to a timed-out request, received on a thread of its own so the request stays listed +/// until the app answers, also once its connection has closed (#3654). +fn watch(reply: Receiver, ticket: Listing) -> Receiver { + let (late_tx, late_rx) = channel(); + // A small thread per timed-out request (each connection has at most one). If none can be + // made, the reply is lost and `control.pending` says the app ended the request without one. + // The reply is passed on before the request leaves the list: once `control.running` no longer + // shows it, its connection's `control.pending` has it. + let _ = std::thread::Builder::new().stack_size(64 * 1024).spawn(move || { + if let Ok(v) = reply.recv() { + let _ = late_tx.send(v); + } + drop(late_tx); + drop(ticket); + }); + late_rx +} + +/// The reply text for a request that outlasts its wait. A request that only reads changes +/// nothing when it finishes; an export or a save still writes its file, and any other request +/// still applies, as it was not cancelled. +pub fn timeout_message(what: &str, effect: Effect, document: &str) -> String { + match effect { + Effect::ReadsOnly => { + format!("timeout: {what} is still running in the app ({}); ask `control.pending` for its answer", effect.when_done(document)) + } + Effect::WritesFile => format!( + "timeout: {what} is still running in the app and will still write the file when it finishes (it was not cancelled: \ + don't send it again; the {document} does not change); ask `control.pending` for its result, or `control.cancel` to \ + cancel it" + ), + Effect::Changes => format!( + "timeout: {what} is still running in the app and will still apply when it finishes (it was not cancelled: don't send it \ + again); ask `control.pending` for its result, or `control.cancel` to cancel it" + ), + } +} + +/// The reply text for a request refused because an earlier one on the connection still runs. +fn busy_message(p: &Pending, method: &str) -> String { + let method: String = method.chars().take(128).collect(); + format!( + "busy: request {} ({}) is still running, so `{method}` was not run; ask `control.pending` for that request's result, then \ + send this one again", + p.id, p.what + ) +} + +/// A subscribed connection's socket, for its replies and its events: each line is written whole +/// and alone, so an event never lands inside a reply (#6985). +struct Lines { + socket: TcpStream, + turn: Mutex<()>, +} + +impl Lines { + fn send(&self, line: &Value) -> std::io::Result<()> { + let _turn = self.turn.lock().unwrap_or_else(PoisonError::into_inner); + send(&mut &self.socket, line) + } +} + +/// A subscribed connection's seat on the watch: it leaves exactly once, whichever thread sees +/// the connection's end first — the connection's own thread at the end of the stream, or the +/// events thread at the first write a closed client refuses. The seat used to wait for the +/// events thread alone, and that thread only leaves its wait when the design moves, so the +/// count could outlive the connection by a second or more (#8315). +struct Seat(Mutex>); + +impl Seat { + fn new(counted: Subscriber) -> Self { + Seat(Mutex::new(Some(counted))) + } + + /// Leave the watch now: the seat goes once, whoever gets here first. + fn release(&self) { + let counted = self.0.lock().unwrap_or_else(PoisonError::into_inner).take(); + drop(counted); + } +} + +impl Drop for Seat { + fn drop(&mut self) { + self.release(); + } +} + +/// A connection's `control.subscribe` (#6985): its `design.changed` lines, sent from a thread of +/// their own until the connection ends. +struct Subscription { + lines: Arc, + stop: Arc, + seat: Arc, +} + +impl Subscription { + /// Write `reply`, the window's answer to `control.subscribe` naming the design `seen`, on + /// `out`, then send `design`'s changes after it: the reply goes out before any event. `None`, + /// with nothing written, when no thread or socket was left for the events. + fn start( + watch: &'static DocumentWatch, + event: &'static str, + counted: Subscriber, + out: &TcpStream, + seen: (String, u64), + reply: &Value, + ) -> Option<(Self, std::io::Result<()>)> { + let lines = Arc::new(Lines { socket: out.try_clone().ok()?, turn: Mutex::new(()) }); + let stop = Arc::new(AtomicBool::new(false)); + let seat = Arc::new(Seat::new(counted)); + let first = lines.turn.lock().unwrap_or_else(PoisonError::into_inner); + let (l, s, watch_seat) = (lines.clone(), stop.clone(), Arc::clone(&seat)); + // The connection's seat on the watch rides the events thread: it leaves when the thread + // does, so the next failed write to a closed client retires it even while the connection's + // own thread is slow to see the end of the stream (#7226). The connection's own thread + // leaves it too, so a closed connection stops counting at once (#8315). + std::thread::Builder::new().stack_size(64 * 1024).spawn(move || send_events(watch, event, &l, &s, seen, watch_seat)).ok()?; + let sent = send(&mut &lines.socket, reply); + drop(first); + Some((Subscription { lines, stop, seat }, sent)) + } +} + +/// The design a `control.subscribe` reply names. +fn document_of(reply: &Value) -> Option<(String, u64)> { + let result = reply.get("result")?; + Some((result.get("document_id")?.as_str()?.to_string(), result.get("revision")?.as_u64()?)) +} + +impl Drop for Subscription { + fn drop(&mut self) { + self.stop.store(true, Ordering::Relaxed); + // The seat on the watch goes now: the connection is over, and nothing counts it any more + // (#8315). + self.seat.release(); + // The socket closes now, not when the thread next looks; a line blocked on a client that + // stopped reading ends too. + let _ = self.lines.socket.shutdown(std::net::Shutdown::Both); + } +} + +/// The `design.changed` lines of a subscribed connection, each time the design differs from the +/// last one it was told of (`seen`), until `stop`. Holds the connection's seat on the watch +/// (`seat`), which it leaves when the thread ends: on `stop`, or on the first write a closed +/// client refuses (#7226). +fn send_events(watch: &DocumentWatch, event: &str, lines: &Lines, stop: &AtomicBool, mut seen: (String, u64), seat: Arc) { + while !stop.load(Ordering::Relaxed) { + let Some(now) = watch.wait(&seen, stop, Duration::from_secs(1)).filter(|now| *now != seen) else { continue }; + if lines.send(&json!({"event": event, "params": {"document_id": now.0, "revision": now.1}})).is_err() { + break; + } + seen = now; + std::thread::sleep(EVENT_GAP); + } + // The client is gone: it is not counted on the watch any more, whatever the connection's own + // thread still has to notice (#8315). + seat.release(); +} + +/// Write `reply` on the connection: through its subscription once it has one. +fn answer(out: &mut TcpStream, events: Option<&Subscription>, reply: &Value) -> std::io::Result<()> { + match events { + Some(e) => e.lines.send(reply), + None => send(out, reply), + } +} + +impl Hub { + /// Serve one connection until it closes: the token first, then one request per line, each + /// sent to the window on `requests` and its reply written back. + pub fn serve(&self, stream: TcpStream, token: &str, requests: Sender, patient: bool) { + let (waits, running, documents, tx) = (self.waits, &self.running, self.watch, requests); + let connection = running.connections.fetch_add(1, Ordering::Relaxed) + 1; + let Ok(read) = stream.try_clone() else { return }; + let mut out = stream; + let mut reader = BufReader::new(ClientInput { socket: read, deadline: Some(Instant::now() + waits.token) }); + let mut bad = 0usize; + let mut authenticated = false; + let mut pending: Option = None; + let mut events: Option = None; + loop { + // A first `control.subscribe` on its way to the window (#6985). + let mut joining = None; + let line = match read_line(&mut reader, self.max_line) { + Ok(Some(l)) => String::from_utf8_lossy(&l).into_owned(), + Ok(None) => break, + // Closed without a word: a client that connected early and only now sends its first + // request would read a line here as the reply to it. It finds the connection closed + // and opens another. + Err(e) if !authenticated && matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock) => break, + Err(e) => { + let _ = answer(&mut out, events.as_ref(), &json!({"ok": false, "error": e.to_string()})); + break; + } + }; + if line.trim().is_empty() { + continue; + } + // The first request must carry the token; later ones on the same connection may. + if !authenticated { + match token_of(&line) { + Some(t) if tokens_match(&t, token) => { + if reader.get_mut().admit().is_err() { + break; + } + authenticated = true; + } + _ => { + refuse(&mut out, &mut reader, self.protocol.docs()); + break; + } + } + } + let reply = match serde_json::from_str::(&line) { + Ok(msg) => { + let id = msg.get("id").cloned().unwrap_or(Value::Null); + let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string(); + let params = msg.get("params").cloned().unwrap_or(json!({})); + // A request that timed out may have finished since (#1618). `control.running` and + // `control.capabilities` only look, so they leave that reply where it is. + let late = pending.as_ref().filter(|_| !matches!(method.as_str(), "control.running" | "control.capabilities")).and_then(|p| { + match p.reply.try_recv() { + Ok(reply) => Some(reply), + Err(TryRecvError::Disconnected) => Some(json!({"ok": false, "error": "the app ended the request without a reply"})), + Err(TryRecvError::Empty) => None, + } + }); + let mut r = if method == "control.running" { + json!({"ok": true, "result": running.result(connection)}) + } else if method == "control.capabilities" { + json!({"ok": true, "result": self.protocol.capabilities()}) + } else if method == "control.cancel" { + let r = cancel(&mut pending, late, params.get("id"), self.protocol.document()); + (self.wake)(); + r + } else if method == "control.pending" { + match (pending.take(), late) { + (None, _) => json!({"ok": true, "result": {"pending": null}}), + (Some(p), Some(reply)) => { + json!({"ok": true, "result": {"pending": p.id, "method": p.method, "done": true, "reply": reply}}) + } + (Some(p), None) => { + let r = json!({"ok": true, "result": {"pending": p.id, "method": p.method, "done": false}}); + pending = Some(p); + r + } + } + } else if let Some(p) = pending.as_ref().filter(|_| late.is_none()) { + // Nothing more is queued behind it, to run unseen when it ends. + json!({"ok": false, "busy": true, "error": busy_message(p, &method)}) + } else if method == "control.subscribe" && events.is_some() { + if late.is_some() { + // As below: finished, and the client moved on without asking. + pending = None; + } + // Its events already say how the design moves; a second answer could only + // arrive behind a newer one. + { + let (event, revision, document) = + (self.protocol.change_event(), self.protocol.revision_method(), self.protocol.document()); + json!({"ok": false, "error": format!("already subscribed: this connection is sent {event} lines; ask {revision} for the {document} now")}) + } + } else { + if late.is_some() { + // Finished, and the client moved on without asking: it is dropped. + pending = None; + } + // A batch replaces many short requests: the whole of it may take long (#743). + let Summary { what, effect, command, steps, long } = self.protocol.summary(&method, ¶ms); + let wait = if patient || long { waits.long } else { waits.short }; + // Counted before the window answers, so it publishes every change from its + // answer on. + joining = (method == "control.subscribe").then(|| documents.join()); + let (req, rrx) = ControlRequest::new(method.clone(), params); + let job = Arc::clone(&req.ticket); + let ticket = running.add(connection, &id, &req, command, steps); + if tx.send(req).is_err() { + break; + } + (self.wake)(); + match rrx.recv_timeout(wait) { + Ok(r) => r, + Err(_) => { + let r = json!({"ok": false, "running": true, "error": timeout_message(&what, effect, self.protocol.document())}); + pending = Some(Pending { id: id.clone(), method, what, reply: watch(rrx, ticket), job }); + r + } + } + }; + if let Some(o) = r.as_object_mut() { + o.insert("id".into(), id); + } + r + } + Err(e) => { + bad += 1; + json!({"ok": false, "error": format!("bad JSON: {e}")}) + } + }; + // Subscribed once the window named the design (not when it refused or timed out). + let sent = match joining.zip(document_of(&reply)) { + Some((counted, seen)) => match Subscription::start(documents, self.protocol.change_event(), counted, &out, seen, &reply) { + Some((subscription, sent)) => { + events = Some(subscription); + sent + } + None => send( + &mut out, + &json!({"id": reply.get("id"), "ok": false, "error": "not subscribed: no thread or socket left for the events"}), + ), + }, + None => answer(&mut out, events.as_ref(), &reply), + }; + if sent.is_err() { + break; + } + if bad >= MAX_BAD_LINES { + break; + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The longest request line, as an app might accept. + const MAX_LINE: usize = 16 << 20; + + /// An app whose requests all may change its document and take the short wait. + struct Plain; + + impl Protocol for Plain { + fn summary(&self, method: &str, _: &Value) -> Summary { + Summary { what: format!("`{method}`"), effect: Effect::Changes, command: None, steps: None, long: false } + } + fn document(&self) -> &'static str { + "design" + } + fn change_event(&self) -> &'static str { + "design.changed" + } + fn revision_method(&self) -> &'static str { + "document.revision" + } + fn docs(&self) -> &'static str { + "docs/control-protocol.md" + } + fn capabilities(&self) -> Value { + json!({}) + } + } + + /// A server for [`Plain`] with these waits and this document watch. + fn hub(waits: Waits, watch: &'static DocumentWatch) -> Arc { + Arc::new(Hub::new(Plain, || {}, MAX_LINE).with_waits(waits).with_watch(watch)) + } + + /// The channel never goes on the network: a listener on any address but loopback is refused, + /// and nothing is served on it. + #[test] + fn a_listener_off_loopback_is_refused() { + let (tx, _rx) = channel::(); + let open = TcpListener::bind("0.0.0.0:0").unwrap(); + let e = hub(Waits::default(), &DOCUMENT_WATCH).listen(open, "0123456789abcdef0123456789abcdef".into(), tx.clone(), false).unwrap_err(); + assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput, "{e}"); + assert!(e.to_string().contains("loopback only"), "{e}"); + let local = TcpListener::bind("127.0.0.1:0").unwrap(); + assert!(hub(Waits::default(), &DOCUMENT_WATCH).listen(local, "0123456789abcdef0123456789abcdef".into(), tx, false).is_ok()); + } + + /// A connection that doesn't show the token in time is closed without a word, however it fills + /// the time. One that does keeps working when it idles for longer than that. + #[test] + fn the_token_has_a_time_limit_and_an_authenticated_client_may_idle() { + use std::io::Read; + let token = "0123456789abcdef0123456789abcdef"; + let limit = Duration::from_millis(500); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + std::thread::spawn(move || { + for req in rx { + let _ = req.reply.send(json!({"ok": true, "result": req.method})); + } + }); + let waits = Waits { short: Duration::from_secs(10), long: Duration::from_secs(10), token: limit }; + let hub = hub(waits, &DOCUMENT_WATCH); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let (tx, hub) = (tx.clone(), Arc::clone(&hub)); + std::thread::spawn(move || hub.serve(stream, token, tx, false)); + } + }); + let open = || { + let s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(20))).unwrap(); + s + }; + // Sends `drip` every 50 ms until the server closes; what it read, and when it closed. + let closed_after = |drip: &[u8]| { + let mut s = open(); + let start = Instant::now(); + s.set_read_timeout(Some(Duration::from_millis(50))).unwrap(); + let mut got = Vec::new(); + loop { + let mut buf = [0u8; 256]; + match s.read(&mut buf) { + Ok(0) => break, + Ok(n) => got.extend_from_slice(buf.get(..n).unwrap()), + Err(e) if matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut) => { + assert!(start.elapsed() < Duration::from_secs(20), "still open"); + let _ = s.write_all(drip); + } + Err(_) => break, + } + } + (got, start.elapsed()) + }; + for drip in [&b""[..], b"\n", b"\r\n \n", b" ", b"{"] { + let (got, took) = closed_after(drip); + assert!(got.is_empty(), "{drip:?} was answered: {}", String::from_utf8_lossy(&got)); + assert!(took >= limit.saturating_sub(Duration::from_millis(50)), "{drip:?} closed after {took:?}"); + } + + let mut s = open(); + let mut lines = BufReader::new(s.try_clone().unwrap()); + let mut ask = |line: Value| -> Value { + writeln!(s, "{line}").unwrap(); + let mut l = String::new(); + lines.read_line(&mut l).unwrap(); + serde_json::from_str(&l).unwrap() + }; + assert_eq!(ask(json!({"id": 1, "method": "ui.view", "token": token}))["ok"], true); + std::thread::sleep(limit * 3); + assert_eq!(ask(json!({"id": 2, "method": "ui.view"}))["ok"], true, "idle for longer than the limit"); + + // A wrong token is still answered, at once. + let mut wrong = open(); + writeln!(wrong, "{}", json!({"id": 1, "method": "ui.view", "token": "nope"})).unwrap(); + let mut l = String::new(); + BufReader::new(wrong).read_line(&mut l).unwrap(); + assert!(l.contains("unauthorized"), "{l}"); + } + + /// #1989: `control.cancel` cancels the connection's own outstanding request. One still + /// waiting its turn is removed and never runs, and the connection is free at once; one that + /// already runs in the app can't be, and says so; another connection's request is refused; + /// with nothing outstanding there is nothing to cancel. + #[test] + fn control_cancel_cancels_only_this_connections_request() { + let token = "0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + let (gate_tx, gate_rx) = channel::<()>(); + let ran = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let seen = ran.clone(); + // A stand-in for the window thread: `queued` waits behind the others (never taken until + // `slow` is done), `slow` runs until the gate opens. + std::thread::spawn(move || { + let mut held = Vec::new(); + for req in rx { + if req.method == "queued" { + held.push(req); + continue; + } + if !req.ticket.take() { + continue; + } + seen.lock().unwrap().push(req.method.clone()); + if req.method == "slow" { + let _ = gate_rx.recv(); + } + let _ = req.reply.send(json!({"ok": true, "result": req.method})); + if req.method == "slow" { + for req in held.drain(..) { + if req.ticket.take() { + seen.lock().unwrap().push(req.method.clone()); + } + } + } + } + }); + let waits = Waits { short: Duration::from_millis(100), long: Duration::from_millis(100), token: TOKEN_WAIT }; + let hub = hub(waits, &DOCUMENT_WATCH); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let (tx, hub) = (tx.clone(), Arc::clone(&hub)); + std::thread::spawn(move || hub.serve(stream, token, tx, false)); + } + }); + let connect = || { + let s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); + let out = s.try_clone().unwrap(); + (out, BufReader::new(s)) + }; + let ask = |(out, lines): &mut (TcpStream, BufReader), v: Value| -> Value { + writeln!(out, "{v}").unwrap(); + let mut l = String::new(); + lines.read_line(&mut l).unwrap(); + serde_json::from_str(&l).unwrap() + }; + let mut a = connect(); + let mut b = connect(); + let r = ask(&mut b, json!({"id": 1, "method": "control.cancel", "token": token})); + assert!(r["ok"] == true && r["result"]["cancelled"].is_null(), "nothing to cancel: {r}"); + + // Waiting its turn: removed, never run, and the connection is free. + let r = ask(&mut a, json!({"id": 1, "method": "queued", "token": token})); + assert_eq!(r["running"], true, "{r}"); + let r = ask(&mut a, json!({"id": 2, "method": "control.cancel", "params": {"id": 1}})); + assert_eq!((&r["ok"], &r["result"]["cancelled"], &r["result"]["outcome"]), (&json!(true), &json!(1), &json!("removed")), "{r}"); + assert!(r["result"]["note"].as_str().is_some_and(|n| n.contains("will not run")), "{r}"); + let r = ask(&mut a, json!({"id": 3, "method": "control.pending"})); + assert_eq!(r["result"]["pending"], Value::Null, "{r}"); + + // Already running in the app: it can't be, and it still answers. + let r = ask(&mut a, json!({"id": 4, "method": "slow"})); + assert_eq!(r["running"], true, "{r}"); + let r = ask(&mut b, json!({"id": 2, "method": "control.cancel", "params": {"id": 4}})); + assert!(r["ok"] == false && r["error"].as_str().is_some_and(|e| e.contains("its own requests")), "another connection's: {r}"); + let r = ask(&mut a, json!({"id": 5, "method": "control.cancel", "params": {"id": 99}})); + assert!(r["ok"] == false && r["error"].as_str().is_some_and(|e| e.starts_with("nothing to cancel")), "{r}"); + let r = ask(&mut a, json!({"id": 6, "method": "control.cancel"})); + assert!(r["ok"] == false && r["cancellable"] == false, "{r}"); + assert!(r["error"].as_str().is_some_and(|e| e.starts_with("cannot be cancelled") && e.contains("control.pending")), "{r}"); + gate_tx.send(()).unwrap(); + let deadline = Instant::now() + Duration::from_secs(10); + let r = loop { + let r = ask(&mut a, json!({"id": 7, "method": "control.pending"})); + if r["result"]["done"] == true || Instant::now() > deadline { + break r; + } + std::thread::sleep(Duration::from_millis(10)); + }; + assert_eq!(r["result"]["reply"]["result"], "slow", "{r}"); + let r = ask(&mut a, json!({"id": 8, "method": "control.cancel"})); + assert!(r["ok"] == true && r["result"]["cancelled"].is_null(), "{r}"); + assert_eq!(*ran.lock().unwrap(), ["slow"], "the cancelled request never ran"); + } + + /// #1618: a request that outlasts its wait says it is still running; the connection keeps its + /// reply for `control.pending`, and refuses other requests as `busy` until it lands, so none + /// is queued to run unseen behind it. + #[test] + fn a_timed_out_request_can_be_asked_for_later() { + let token = "0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + // The slow request is held until the test has seen it time out, so the test does not + // race the clock on a loaded machine. + let (gate_tx, gate_rx) = channel::<()>(); + let ran = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let seen = ran.clone(); + std::thread::spawn(move || { + for req in rx { + seen.lock().unwrap().push(req.method.clone()); + if req.method == "slow" { + let _ = gate_rx.recv(); + } + let _ = req.reply.send(json!({"ok": true, "result": req.method})); + } + }); + let waits = Waits { short: Duration::from_millis(100), long: Duration::from_millis(100), token: TOKEN_WAIT }; + let hub = hub(waits, &DOCUMENT_WATCH); + std::thread::spawn(move || { + if let Some(stream) = listener.incoming().flatten().next() { + hub.serve(stream, token, tx, false); + } + }); + let s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); + let mut out = s.try_clone().unwrap(); + let mut lines = BufReader::new(s); + let mut ask = |v: Value| -> Value { + writeln!(out, "{v}").unwrap(); + let mut l = String::new(); + lines.read_line(&mut l).unwrap(); + serde_json::from_str(&l).unwrap() + }; + let r = ask(json!({"id": 1, "method": "slow", "token": token})); + assert!(r["ok"] == false && r["running"] == true && r["error"].as_str().is_some_and(|e| e.starts_with("timeout")), "{r}"); + let r = ask(json!({"id": 2, "method": "ui.view"})); + assert!(r["busy"] == true && r["id"] == 2, "{r}"); + let r = ask(json!({"id": 3, "method": "control.pending"})); + assert_eq!((&r["result"]["pending"], &r["result"]["done"]), (&json!(1), &json!(false)), "{r}"); + gate_tx.send(()).unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(10); + let r = loop { + let r = ask(json!({"id": 4, "method": "control.pending"})); + if r["result"]["done"] == true || std::time::Instant::now() > deadline { + break r; + } + std::thread::sleep(Duration::from_millis(10)); + }; + assert_eq!( + (&r["result"]["pending"], &r["result"]["done"], &r["result"]["reply"]["result"]), + (&json!(1), &json!(true), &json!("slow")), + "{r}" + ); + let r = ask(json!({"id": 5, "method": "control.pending"})); + assert_eq!(r["result"]["pending"], Value::Null, "{r}"); + let r = ask(json!({"id": 6, "method": "ui.view"})); + assert_eq!((&r["ok"], &r["result"]), (&json!(true), &json!("ui.view")), "{r}"); + assert_eq!(*ran.lock().unwrap(), ["slow", "ui.view"], "the busy request never reached the app"); + } + + /// #6985: a connection that sends `control.subscribe` gets the design in the reply, then a + /// `design.changed` line each time the window publishes another: never before the reply, + /// none for the same design, the latest after a burst, each line whole between its replies. + /// A second subscribe is refused without reaching the window. A connection that did not + /// subscribe gets no events, and one that closed is no longer counted. + #[test] + fn subscribed_connections_are_told_of_design_changes() { + static WATCH: DocumentWatch = DocumentWatch::new(); + let token = "0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + // The window: it answers `control.subscribe` as the app does, publishing the design first; + // then the design changes at once, before the reply may have been written. + std::thread::spawn(move || { + for req in rx { + if req.method == "control.subscribe" { + WATCH.publish("d-1", 3); + let _ = req.reply.send(json!({"ok": true, "result": {"document_id": "d-1", "revision": 3}})); + WATCH.publish("d-1", 4); + } else { + let _ = req.reply.send(json!({"ok": true, "result": req.method})); + } + } + }); + let hub = hub(Waits::default(), &WATCH); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let (tx, hub) = (tx.clone(), Arc::clone(&hub)); + std::thread::spawn(move || hub.serve(stream, token, tx, false)); + } + }); + let open = || { + let s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); + (s.try_clone().unwrap(), BufReader::new(s)) + }; + let next = |lines: &mut BufReader| -> Value { + let mut l = String::new(); + lines.read_line(&mut l).unwrap(); + serde_json::from_str(&l).unwrap() + }; + let changed = |id: &str, revision: u64| json!({"event": "design.changed", "params": {"document_id": id, "revision": revision}}); + + let (mut quiet_out, mut quiet) = open(); + writeln!(quiet_out, "{}", json!({"id": 1, "method": "ui.view", "token": token})).unwrap(); + assert_eq!(next(&mut quiet)["id"], 1); + + let (mut out, mut lines) = open(); + writeln!(out, "{}", json!({"id": 1, "method": "control.subscribe", "token": token})).unwrap(); + assert_eq!(next(&mut lines), json!({"id": 1, "ok": true, "result": {"document_id": "d-1", "revision": 3}})); + assert_eq!(next(&mut lines), changed("d-1", 4), "the change after the window's answer comes after the reply"); + assert!(WATCH.subscribed()); + // The design it was told of is no news; another revision is. + WATCH.publish("d-1", 4); + WATCH.publish("d-1", 5); + assert_eq!(next(&mut lines), changed("d-1", 5)); + // A burst ends with its latest. + for revision in 6..=10 { + WATCH.publish("d-1", revision); + } + let mut last = 5; + while last < 10 { + let e = next(&mut lines); + let revision = e["params"]["revision"].as_u64().unwrap(); + assert!(e["event"] == "design.changed" && revision > last, "{e} after {last}"); + last = revision; + } + // Another tab: another id, whatever its revision. + WATCH.publish("d-2", 2); + assert_eq!(next(&mut lines), changed("d-2", 2)); + // Subscribed already: refused here (the window would have published d-1 again). + writeln!(out, "{}", json!({"id": 2, "method": "control.subscribe"})).unwrap(); + let r = next(&mut lines); + assert!(r["id"] == 2 && r["ok"] == false && r["error"].as_str().is_some_and(|e| e.starts_with("already subscribed")), "{r}"); + // Replies still come, whole, and nothing else was sent meanwhile. + writeln!(out, "{}", json!({"id": 3, "method": "ui.view"})).unwrap(); + assert_eq!(next(&mut lines), json!({"id": 3, "ok": true, "result": "ui.view"})); + writeln!(quiet_out, "{}", json!({"id": 2, "method": "ui.view"})).unwrap(); + assert_eq!(next(&mut quiet), json!({"id": 2, "ok": true, "result": "ui.view"}), "no events without subscribing"); + // Closed, it is no longer counted. Each publish asks the events thread for one more line, + // and its write to the closed connection is what retires the seat; that keeps the wait off + // the connection's own thread, which anything else this process is doing may hold up past + // the deadline (#7226). + drop((out, lines)); + let mut revision = 11; + let deadline = Instant::now() + Duration::from_secs(10); + while WATCH.subscribed() && Instant::now() < deadline { + WATCH.publish("d-1", revision); + revision += 1; + std::thread::sleep(Duration::from_millis(20)); + } + assert!(!WATCH.subscribed()); + } + + /// #8315: a closed connection leaves the watch at once. The seat of a subscribed connection + /// goes when the connection's own thread sees the end of the stream, not only when the + /// events thread notices: that thread waits for the design to move, so a connection that + /// closed while the design stood still kept the watch counting it for a second or more. + #[test] + fn a_closed_connection_leaves_the_watch_at_once() { + static WATCH: DocumentWatch = DocumentWatch::new(); + let token = "0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + // The window names the design once and then leaves it alone: nothing moves it, so the + // events thread has nothing to write and only the end of the connection can retire it. + std::thread::spawn(move || { + for req in rx { + if req.method == "control.subscribe" { + WATCH.publish("d-1", 3); + let _ = req.reply.send(json!({"ok": true, "result": {"document_id": "d-1", "revision": 3}})); + } else { + let _ = req.reply.send(json!({"ok": true, "result": req.method})); + } + } + }); + let hub = hub(Waits::default(), &WATCH); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let (tx, hub) = (tx.clone(), Arc::clone(&hub)); + std::thread::spawn(move || hub.serve(stream, token, tx, false)); + } + }); + let s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); + let mut out = s.try_clone().unwrap(); + let mut lines = BufReader::new(s); + writeln!(out, "{}", json!({"id": 1, "method": "control.subscribe", "token": token})).unwrap(); + let mut l = String::new(); + lines.read_line(&mut l).unwrap(); + let reply: Value = serde_json::from_str(&l).unwrap(); + assert!(reply["ok"] == true, "{reply}"); + assert!(WATCH.subscribed(), "the connection is subscribed"); + // Let the events thread settle into its wait: the design stands still, so it has nothing + // to write, and only the end of the connection can retire it. + std::thread::sleep(Duration::from_millis(200)); + // The client goes away without asking anything else. + drop((out, lines)); + let deadline = Instant::now() + Duration::from_millis(400); + while WATCH.subscribed() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(!WATCH.subscribed(), "a closed connection is not counted on the watch"); + } + + /// The token is read from a request object without building the rest of it: other fields, + /// however large or nested, are only skipped. Anything but an object with a string token is + /// no token. + #[test] + fn token_is_read_without_parsing_the_request() { + assert_eq!(token_of(r#"{"id":1,"token":"abc","params":{"a":[1,[2,{"b":null}]]}}"#).as_deref(), Some("abc")); + let flood = format!(r#"{{"params":[{}0],"token":"t"}}"#, "0,".repeat(1 << 20)); + assert_eq!(token_of(&flood).as_deref(), Some("t")); + for no in [r#"["abc"]"#, r#"{"token":5}"#, r#"{"token":"a"} x"#, r#"{"id":1}"#, "GET / HTTP/1.1", r#""token""#] { + assert_eq!(token_of(no), None, "{no}"); + } + } + + /// #205: a connection runs nothing until a request carries the launch's token; a wrong or + /// missing token, or garbage, is refused and the connection closed. Once shown, the token + /// need not be repeated on that connection. + #[test] + fn requests_run_only_after_the_token() { + use std::io::Read; + let token = "0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + let ran = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let seen = ran.clone(); + std::thread::spawn(move || { + for req in rx { + seen.lock().unwrap().push(req.method.clone()); + let _ = req.reply.send(json!({"ok": true, "result": null})); + } + }); + let hub = hub(Waits::default(), &DOCUMENT_WATCH); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let (tx, hub) = (tx.clone(), Arc::clone(&hub)); + std::thread::spawn(move || hub.serve(stream, token, tx, false)); + } + }); + let session = |lines: &[String]| { + let mut s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); + for l in lines { + let _ = writeln!(s, "{l}"); + } + let _ = s.shutdown(std::net::Shutdown::Write); + let mut text = String::new(); + let _ = s.read_to_string(&mut text); + text.lines().map(|l| serde_json::from_str::(l).unwrap()).collect::>() + }; + for first in [ + json!({"id": 1, "method": "app.quit", "params": {"force": true}}).to_string(), + json!({"id": 1, "method": "app.quit", "token": "0123456789abcdef0123456789abcdee"}).to_string(), + json!({"id": 1, "method": "app.quit", "token": "0123"}).to_string(), + json!({"id": 1, "method": "app.quit", "token": 7}).to_string(), + "not json".to_string(), + ] { + let replies = session(&[first.clone(), json!({"id": 2, "method": "app.quit", "token": token}).to_string()]); + assert_eq!(replies.len(), 1, "{first}: closed after refusing: {replies:?}"); + assert!(replies[0]["error"].as_str().is_some_and(|e| e.starts_with("unauthorized")), "{first}: {replies:?}"); + } + assert!(ran.lock().unwrap().is_empty(), "nothing ran without the token"); + let replies = session(&[ + json!({"id": 1, "method": "ui.inspect", "token": token}).to_string(), + json!({"id": 2, "method": "engine.commands"}).to_string(), + ]); + assert_eq!(replies.len(), 2, "{replies:?}"); + assert!(replies.iter().all(|r| r["ok"] == true), "{replies:?}"); + assert_eq!(*ran.lock().unwrap(), ["ui.inspect", "engine.commands"]); + } + + /// A request longer than the cap is refused instead of allocated. + #[test] + fn lines_past_the_cap_are_refused() { + let flood = vec![b'A'; MAX_LINE + 2]; + let mut r = BufReader::new(flood.as_slice()); + let e = read_line(&mut r, MAX_LINE).unwrap_err(); + assert_eq!(e.kind(), std::io::ErrorKind::InvalidData); + assert!(e.to_string().contains("request too large"), "{e}"); + } + + /// Ordinary framing: lines split on newlines, blank lines kept, no trailing newline needed. + #[test] + fn lines_split_on_newlines() { + let text = b"{\"a\":1}\n\n{\"b\":2}\nlast".to_vec(); + let mut r = BufReader::new(text.as_slice()); + let mut got = Vec::new(); + while let Some(line) = read_line(&mut r, MAX_LINE).unwrap() { + got.push(String::from_utf8(line).unwrap()); + } + assert_eq!(got, vec!["{\"a\":1}", "", "{\"b\":2}", "last"]); + assert!(read_line(&mut r, MAX_LINE).unwrap().is_none()); + } + + /// A line that spans several reads of the underlying stream still arrives whole. + #[test] + fn lines_longer_than_one_buffer_arrive_whole() { + let long = "x".repeat(200_000); + let text = format!("{long}\n{long}\n").into_bytes(); + let mut r = BufReader::with_capacity(64, text.as_slice()); + assert_eq!(read_line(&mut r, MAX_LINE).unwrap().unwrap().len(), long.len()); + assert_eq!(read_line(&mut r, MAX_LINE).unwrap().unwrap().len(), long.len()); + assert!(read_line(&mut r, MAX_LINE).unwrap().is_none()); + // The same line against a smaller cap is refused. + let mut r = BufReader::with_capacity(64, text.as_slice()); + assert!(read_line(&mut r, 1000).is_err()); + } + + /// A large reply reaches the socket in one write, not one per JSON piece (a component tree of + /// thousands of components took half a second to send), and reads back as it was. + #[test] + fn a_large_reply_is_written_at_once() { + struct Counted { + writes: usize, + bytes: Vec, + } + impl Write for Counted { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.writes += 1; + self.bytes.extend_from_slice(buf); + Ok(buf.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let components: Vec = (0..2000).map(|i| json!({"id": i, "name": format!("Part {i}"), "bodies": ["a", "b"], "parent": 0})).collect(); + let reply = json!({"ok": true, "id": 7, "result": {"components": components}}); + let mut out = Counted { writes: 0, bytes: Vec::new() }; + send(&mut out, &reply).unwrap(); + assert_eq!(out.writes, 1); + assert_eq!(out.bytes.last(), Some(&b'\n')); + assert_eq!(serde_json::from_slice::(&out.bytes).unwrap(), reply); + } + + /// A stopped listener frees its port at once and ends the connections it had, so a channel + /// turned off in the settings leaves nothing listening and can be turned on again on the + /// same port. + #[test] + fn a_stopped_listener_closes_its_port_and_its_connections() { + use std::io::Read; + let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let port = listener.local_addr().unwrap().port(); + let (tx, _rx) = channel(); + let stop = hub(Waits::default(), &DOCUMENT_WATCH).listen_stoppable(listener, "0123456789abcdef0123456789abcdef".into(), tx, false).unwrap(); + assert_eq!(stop.port(), port); + let mut client = TcpStream::connect(("127.0.0.1", port)).unwrap(); + client.set_read_timeout(Some(Duration::from_secs(4))).unwrap(); + // Wait until the server holds the connection, so stopping has one to close. + let held = Instant::now(); + while stop.shared.lock().is_empty() && held.elapsed() < Duration::from_secs(4) { + std::thread::sleep(Duration::from_millis(5)); + } + let started = Instant::now(); + stop.stop(); + let mut byte = [0u8; 1]; + match client.read(&mut byte) { + Ok(n) => assert_eq!(n, 0, "the connection ends"), + Err(e) => { + assert!(!matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut), "the connection ends, not waits: {e}") + } + } + assert!(started.elapsed() < Duration::from_secs(3), "before the token wait runs out"); + // The listener thread closes its socket when it notices the stop; on a loaded machine that can + // take a moment after `stop()` returns. Wait for the port to be free, not for one instant. + let freed = Instant::now(); + let mut bound = TcpListener::bind(("127.0.0.1", port)); + while bound.is_err() && freed.elapsed() < Duration::from_secs(2) { + std::thread::sleep(Duration::from_millis(10)); + bound = TcpListener::bind(("127.0.0.1", port)); + } + assert!(bound.is_ok(), "the port is free again: {:?}", bound.err()); + } +} From bea91b1861eea3889bec9d26338076f2ff31c4ac Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:55:38 +0100 Subject: [PATCH 322/471] raw: charge each DNG chunk's own output allocation against the aggregate budget (craft-libs #778) (#1884) A lossy JPEG smaller than its tile is now budgeted by its frame, but decoding still fills the whole tile, so the chunk's w x h x spp output is charged as well (and refused over MAX_SAMPLES). Regression test: a 256x256 JPEG padded with comments in a 16384x16384 tile is TooLarge instead of allocating ~1.5 GiB. --- crates/raw/src/dng_lines.rs | 8 +++++++- crates/raw/tests/it/dng_lines.rs | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index 83877492e..8da1f45d8 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -342,7 +342,13 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { for c in &chunks { let n = c.w.checked_mul(c.h).and_then(|v| v.checked_mul(spp)).ok_or(Error::TooLarge)?; let budget = decode_budget(c, &l).filter(|&b| b <= MAX_SAMPLES).ok_or(Error::TooLarge)?; - decoded_samples = decoded_samples.checked_add(budget).ok_or(Error::TooLarge)?; + // the chunk's own `w` × `h` × `spp` output is allocated whatever its coded frame declares + // (a lossy JPEG smaller than its tile still fills a whole tile), so it is charged too + let alloc = budget.max(n); + if alloc > MAX_SAMPLES { + return Err(Error::TooLarge); + } + decoded_samples = decoded_samples.checked_add(alloc).ok_or(Error::TooLarge)?; if decoded_samples > MAX_DECODED_TILE_SAMPLES { return Err(Error::TooLarge); } diff --git a/crates/raw/tests/it/dng_lines.rs b/crates/raw/tests/it/dng_lines.rs index 1cf266d67..dd10c763c 100644 --- a/crates/raw/tests/it/dng_lines.rs +++ b/crates/raw/tests/it/dng_lines.rs @@ -277,3 +277,25 @@ fn short_lossy_jpeg_strips_are_charged_their_own_rows() { let band = w * rows * 3; assert_eq!(&img.data[..band], &img.data[img.data.len() - band..]); } + +#[test] +fn a_small_lossy_jpeg_in_a_huge_tile_is_charged_the_whole_tile() { + // a 256 x 256 RGB image in one 16384 x 16384 tile whose JPEG is only 256 x 256 (padded with + // comments to pass the bytes-per-sample guard): the JPEG's frame is small, but decoding fills + // the whole tile (about 1.5 GiB of samples), so it is refused before anything is allocated + let (w, tile) = (256usize, 16384usize); + let px: Vec = (0..w * w).flat_map(|i| [(i % 251) as u8, 90, 160]).collect(); + let mut jpeg = Vec::new(); + jpeg_encoder::Encoder::new(&mut jpeg, 90).encode(&px, w as u16, w as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + let mut padded = jpeg[..2].to_vec(); + let need = tile * tile * 3 / 768 + 1; + while padded.len() < need { + padded.extend_from_slice(&[0xFF, 0xFE, 0xFF, 0xFF]); + padded.extend(std::iter::repeat_n(0u8, 0xFFFF - 2)); + } + padded.extend_from_slice(&jpeg[2..]); + let mut raw = base_ifd(w, w, 8, 3, false); + raw.set(t::COMPRESSION, Value::Short(vec![34892])); + raw.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles: vec![padded] }); + assert_eq!(lines(&dng(raw, ByteOrder::Little)).unwrap_err(), Error::TooLarge); +} From b774e4d1680a69ab0dc17bc437db93fe530cce36 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 09:55:44 -0500 Subject: [PATCH 323/471] psd: add bounded PackBits recovery decoding for app cutovers (#1885) * psd: expose bounded PackBits recovery row decoding * Clarify recovery row budget and partial-error contract --- crates/psd/src/compression.rs | 32 +++++++++++++++++++++++++---- crates/psd/src/compression/tests.rs | 22 ++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/psd/src/compression.rs b/crates/psd/src/compression.rs index b41936a04..76891a421 100644 --- a/crates/psd/src/compression.rs +++ b/crates/psd/src/compression.rs @@ -206,10 +206,34 @@ pub mod packbits { /// `-128` no-op bytes (which are always permitted). Errors, rather than /// overflowing, when `out.len() + expected` does not fit in `usize`. pub(crate) fn decode_into(src: &[u8], expected: usize, out: &mut Vec) -> Result<()> { + decode_row(src, expected, out, false) + } + + /// Appends a recovery row: complete oversized packets are clipped and a short + /// row is zero-padded. Truncated packet payloads still fail; trailing packets + /// after the row is full are ignored. Strict decoding remains the default. + /// + /// Appended row bytes are bounded by `max_row_bytes` and the PSD decoded-size + /// limit; callers enforce their aggregate image budget. Allocation is fallible. + /// Existing output is retained; an error may leave a partially appended row. + pub fn decode_padded_into(src: &[u8], expected: usize, max_row_bytes: usize, out: &mut Vec) -> Result<()> { + if expected > max_row_bytes || expected as u64 > super::MAX_DECODED_BYTES { + return Err(PsdError::LimitExceeded("PackBits recovery row exceeds limit")); + } + out.len().checked_add(expected).ok_or(PsdError::LimitExceeded("PackBits output length overflows usize"))?; + out.try_reserve_exact(expected).map_err(|_| PsdError::OutOfMemory("PackBits recovery row allocation failed"))?; + decode_row(src, expected, out, true) + } + + fn decode_row(src: &[u8], expected: usize, out: &mut Vec, padded: bool) -> Result<()> { let target = out.len().checked_add(expected).ok_or(PsdError::LimitExceeded("PackBits output length overflows usize"))?; let mut i = 0; while out.len() < target { let Some(&h) = src.get(i) else { + if padded { + out.resize(target, 0); + break; + } return Err(PsdError::Decompress("PackBits row ended early".into())); }; i += 1; @@ -219,10 +243,10 @@ pub mod packbits { let Some(lit) = src.get(i..i + len) else { return Err(PsdError::Decompress("PackBits literal truncated".into())); }; - if len > target - out.len() { + if !padded && len > target - out.len() { return Err(PsdError::Decompress("PackBits literal overflows row".into())); } - out.extend_from_slice(lit); + out.extend_from_slice(&lit[..len.min(target - out.len())]); i += len; } else if h != -128 { let len = 1 - h as isize; @@ -231,10 +255,10 @@ pub mod packbits { return Err(PsdError::Decompress("PackBits run truncated".into())); }; i += 1; - if len > target - out.len() { + if !padded && len > target - out.len() { return Err(PsdError::Decompress("PackBits run overflows row".into())); } - out.extend(std::iter::repeat_n(b, len)); + out.extend(std::iter::repeat_n(b, len.min(target - out.len()))); } } Ok(()) diff --git a/crates/psd/src/compression/tests.rs b/crates/psd/src/compression/tests.rs index 6e0d86a7e..7cf876321 100644 --- a/crates/psd/src/compression/tests.rs +++ b/crates/psd/src/compression/tests.rs @@ -771,3 +771,25 @@ fn rle_chunks_use_the_callers_pool_and_report_panics() { assert!(map_chunks(2, vec![0, 1], |_| panic!("worker")).is_err()); }); } + +#[test] +fn padded_packbits_preserves_recovery_without_weakening_strict_decode() { + for (input, expected) in + [(&[0, 7][..], vec![7, 0, 0]), (&[3, 1, 2, 3, 4][..], vec![1, 2, 3]), (&[253, 9][..], vec![9, 9, 9]), (&[128][..], vec![0, 0, 0])] + { + let mut out = vec![42]; + super::packbits::decode_padded_into(input, 3, 3, &mut out).unwrap(); + assert_eq!(&out[1..], expected); + assert_eq!(out[0], 42); + assert!(super::packbits::decode(input, 3).is_err()); + } + for input in [&[3, 1][..], &[255][..]] { + assert!(super::packbits::decode_padded_into(input, 1, 1, &mut vec![]).is_err()); + } + let mut out = vec![42]; + assert!(super::packbits::decode_padded_into(&[], 4, 3, &mut out).is_err()); + assert_eq!(out, [42]); + super::packbits::decode_padded_into(&[255], 0, 0, &mut out).unwrap(); + super::packbits::decode_padded_into(&[0, 7, 255], 1, 1, &mut out).unwrap(); + assert_eq!(out, [42, 7]); +} From e44720af47b02670b3ec42da3b65473de77a78c1 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:02:40 +0100 Subject: [PATCH 324/471] media: refuse a truncated file on a FileReader cache hit file() only checked length when reopening after eviction, so a prefix read could still succeed if the handle was pooled. Check metadata on the cached handle too, and drop the entry if the file is shorter or no longer a regular file. MAX_OPEN_FILES is documented as a bound on retained pool entries, not in-flight Arc handles. --- crates/media/src/file.rs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/crates/media/src/file.rs b/crates/media/src/file.rs index d87b323c0..f343737b3 100644 --- a/crates/media/src/file.rs +++ b/crates/media/src/file.rs @@ -1,8 +1,9 @@ //! Pooled file [`ByteSource`]s: many clips can be open at once without holding a descriptor each. //! -//! Past [`MAX_OPEN_FILES`] the least recently read file is closed and reopened by path on demand, -//! so a project with hundreds of sources does not hit the process file-descriptor limit (often 256 -//! on macOS, 1024 on Linux). +//! Past [`MAX_OPEN_FILES`] retained pool entries, the least recently read file is closed and +//! reopened by path on demand, so a project with hundreds of sources does not hit the process +//! file-descriptor limit (often 256 on macOS, 1024 on Linux). The cap does not bound in-flight +//! `Arc` handles: a file another thread is mid-read stays open until that read ends. #![cfg(not(target_arch = "wasm32"))] @@ -63,8 +64,10 @@ fn not_a_file(path: &Path) -> io::Error { io::Error::new(io::ErrorKind::NotFound, format!("{} is not a file", path.display())) } -/// Files [`FileReader`]s keep open at most, together. Past this many, the least recently read file -/// is closed and its reader reopens it on demand. +/// Retained pool entries [`FileReader`]s keep open at most, together. Past this many, the least +/// recently read file is closed and its reader reopens it on demand. The cap does not bound +/// in-flight `Arc` handles: a file another thread is mid-read stays open until that read ends, +/// even if the pool has already evicted its entry. pub const MAX_OPEN_FILES: usize = 128; /// The open files of a set of [`FileReader`]s, least recently used first. @@ -172,7 +175,12 @@ impl FileReader { /// being written) is read up to the opened length. fn file(&self) -> io::Result> { if let Some(file) = self.pool.get(self.id) { - return Ok(file); + let m = file.metadata()?; + if m.is_file() && m.len() >= self.len { + return Ok(file); + } + self.pool.remove(self.id); + return Err(io::Error::other(format!("{} changed since it was opened", self.path.display()))); } let file = open_nonblocking(&self.path)?; let m = file.metadata()?; @@ -331,4 +339,23 @@ mod tests { ); std::fs::remove_dir_all(&dir).ok(); } + + /// A file truncated while its handle is still in the pool is refused, same as after eviction. + #[test] + fn a_truncated_file_is_refused_while_still_pooled() { + let dir = craft_media_testkit::private_dir("file-trunc-hit"); + let path = dir.join("clip.bin"); + std::fs::write(&path, b"0123456789").unwrap(); + let pool = Arc::new(FilePool::new(2)); + let reader = FileReader::open_in(&path, &pool).unwrap(); + assert_eq!(pool.open_count(), 1); + let mut b = [0u8; 5]; + reader.read_at(0, &mut b).unwrap(); + assert_eq!(&b, b"01234"); + std::fs::write(&path, b"short").unwrap(); + assert!(reader.read_at(0, &mut b).is_err(), "truncation must be refused on a cache hit too"); + drop(reader); + assert_eq!(pool.open_count(), 0); + std::fs::remove_dir_all(&dir).ok(); + } } From ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 10:06:40 -0500 Subject: [PATCH 325/471] stream-codecs: append TIFF PackBits rows with shared packet encoder --- crates/stream-codecs/src/runlength.rs | 39 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/crates/stream-codecs/src/runlength.rs b/crates/stream-codecs/src/runlength.rs index a6df9e19d..dceb78f66 100644 --- a/crates/stream-codecs/src/runlength.rs +++ b/crates/stream-codecs/src/runlength.rs @@ -43,6 +43,21 @@ pub fn decode(data: &[u8], max: usize) -> Step { pub fn encode(data: &[u8]) -> Vec { let mut out = Vec::with_capacity(data.len() + data.len() / 128 + 2); + encode_packets(data, &mut out, 2); + out.push(128); + out +} + +/// Append a TIFF PackBits row without an end-of-data marker. +/// +/// Runs of three or more bytes use repeat packets; pairs remain in literal packets. +/// Existing output is retained, permitting rows to be appended directly to a strip. +/// PostScript callers can append their own EOD byte after the final row. +pub fn encode_packbits_into(data: &[u8], out: &mut Vec) { + encode_packets(data, out, 3); +} + +fn encode_packets(data: &[u8], out: &mut Vec, repeat_min: usize) { let n = data.len(); let mut i = 0; while i < n { @@ -50,7 +65,7 @@ pub fn encode(data: &[u8]) -> Vec { while i + run < n && run < 128 && data[i + run] == data[i] { run += 1; } - if run >= 2 { + if run >= repeat_min { out.push((257 - run) as u8); out.push(data[i]); i += run; @@ -59,15 +74,14 @@ pub fn encode(data: &[u8]) -> Vec { // Literal: extend until a repeat begins or 128 bytes. let start = i; let mut j = i + 1; - while j < n && j - start < 128 && !(j + 1 < n && data[j] == data[j + 1]) { + while j < n && j - start < 128 && !data.get(j..).and_then(|rest| rest.get(..repeat_min)).is_some_and(|run| run.iter().all(|b| *b == data[j])) + { j += 1; } out.push((j - start - 1) as u8); out.extend_from_slice(&data[start..j]); i = j; } - out.push(128); - out } #[cfg(test)] @@ -114,4 +128,21 @@ mod tests { assert_eq!((e[0], e[129]), (127, 71)); assert_eq!(dec(&e).unwrap(), lit); } + #[test] + fn packbits_appends_rows_without_eod_and_keeps_pairs_literal() { + let mut out = vec![42]; + encode_packbits_into(b"aabbccc", &mut out); + assert_eq!(out, [42, 3, b'a', b'a', b'b', b'b', 254, b'c']); + let before = out.clone(); + encode_packbits_into(b"", &mut out); + assert_eq!(out, before); + assert_eq!(encode(b"aa"), [255, b'a', 128]); + for n in [1, 2, 3, 127, 128, 129, 255, 256, 300] { + for row in [vec![7; n], (0..n).map(|i| (i % 251) as u8).collect()] { + let mut encoded = Vec::new(); + encode_packbits_into(&row, &mut encoded); + assert_eq!(decode(&encoded, n).unwrap(), row); + } + } + } } From 9fc471fc5c18f467394a043adf17f021c2b27cae Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:09:54 +0100 Subject: [PATCH 326/471] psd: add Descriptor::parse_prefix for mid-stream reads (#1887) from_bytes still requires the slice to be fully consumed. parse_prefix returns the descriptor and the number of bytes used, matching VersionedDescriptor, so apps can read a descriptor that is followed by more data (layer effects, fill and adjustment blocks). Part of #778 and effectcraft#786. --- crates/psd/src/descriptor.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/psd/src/descriptor.rs b/crates/psd/src/descriptor.rs index f3afae64a..96ad47a7b 100644 --- a/crates/psd/src/descriptor.rs +++ b/crates/psd/src/descriptor.rs @@ -300,6 +300,15 @@ impl Descriptor { Ok(d) } + /// Parses a descriptor at the start of `data`, returning it and the number of bytes consumed + /// (trailing data is allowed). Mid-stream reads of layer effects, fill and adjustment + /// descriptors use this; [`from_bytes`] still requires the slice to be fully consumed. + pub fn parse_prefix(data: &[u8]) -> Result<(Self, usize)> { + let mut r = Reader::new(data); + let d = Self::read(&mut r)?; + Ok((d, r.pos())) + } + /// Serializes the descriptor. pub fn to_bytes(&self) -> Vec { let mut v = Vec::new(); @@ -705,6 +714,18 @@ mod tests { assert_eq!(VersionedDescriptor::from_bytes(&b).unwrap(), v); } + #[test] + fn parse_prefix() { + let d = sample(); + let mut b = d.to_bytes(); + let n = b.len(); + b.extend_from_slice(&[1, 2, 3]); + let (p, used) = Descriptor::parse_prefix(&b).unwrap(); + assert_eq!(p, d); + assert_eq!(used, n); + assert!(Descriptor::from_bytes(&b).is_err(), "from_bytes still refuses trailing bytes"); + } + #[test] fn versioned_parse_prefix() { let v = VersionedDescriptor::new(sample()); From c5f217e76518f15ae27ac5ec615ed5d0735d6a90 Mon Sep 17 00:00:00 2001 From: FredApps Date: Mon, 16 Nov 2026 16:10:03 +0100 Subject: [PATCH 327/471] craft-docx: a shape's VML fallback takes its anchor's relativeHeight Main's VML fallback (#1864) read the writer's z counter after the anchor was written. That counter now only counts objects without a stored order, so a stored order's fallback got the wrong z-index. The anchor's relativeHeight is now kept in anchor_z for the fallback, and the wps_fallback tests expect the written relativeHeight (one less than Float::z). --- crates/docx/src/write.rs | 3 +++ crates/docx/src/write/parts.rs | 1 + crates/docx/src/write/story/drawing.rs | 1 + crates/docx/src/write/story/object.rs | 5 +++-- crates/docx/tests/it/wps_fallback.rs | 6 ++++-- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/docx/src/write.rs b/crates/docx/src/write.rs index 634f5f833..0391ba615 100644 --- a/crates/docx/src/write.rs +++ b/crates/docx/src/write.rs @@ -120,6 +120,9 @@ pub(crate) struct Writer<'d> { /// Stored stacking orders written as other `relativeHeight`s (see [`stacking`]); empty: each /// as its own. zmap: BTreeMap, + /// The `relativeHeight` the last `wp:anchor` was written with, which a shape's VML fallback + /// gives as its `z-index`. + anchor_z: u32, /// Note part id → file note id, in first-reference order. footnotes: Vec, endnotes: Vec, diff --git a/crates/docx/src/write/parts.rs b/crates/docx/src/write/parts.rs index 00826f4b5..fa6d6b693 100644 --- a/crates/docx/src/write/parts.rs +++ b/crates/docx/src/write/parts.rs @@ -33,6 +33,7 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result { self.z = self.z.saturating_add(1); self.z }; + self.anchor_z = z; let behind = if float.wrap == Wrap::BehindText { "1" } else { "0" }; w.open( "wp:anchor", diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 4b0ae856a..3a42a8b04 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -398,8 +398,9 @@ impl Writer<'_> { } let float = &float; self.drawing_open(w, DrawingInfo { float, size: (*sw, *sh), docpr: &docpr, name: &name, alt: o.alt_text(), link: link.as_deref() }); - // This shape's stacking order: floating objects in its text box move `self.z` on. - let z = self.z; + // This shape's stacking order, as its anchor gave it: floating objects in its text box + // write anchors of their own. + let z = self.anchor_z; w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape")]); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index 9b209082b..6d3deddb1 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -107,7 +107,8 @@ fn the_fallback_keeps_order_visibility_and_direction() { // is copied into the outer one's text, so it comes after it). let tb = xml.find(r#""#).expect("the outer box's text, turned"); let rect = &xml[xml[..tb].rfind(" Date: Mon, 16 Nov 2026 10:21:41 -0500 Subject: [PATCH 328/471] test: report PackBits codec errors without requiring Failure Debug --- crates/stream-codecs/src/runlength.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/stream-codecs/src/runlength.rs b/crates/stream-codecs/src/runlength.rs index dceb78f66..a13a1d4fb 100644 --- a/crates/stream-codecs/src/runlength.rs +++ b/crates/stream-codecs/src/runlength.rs @@ -141,7 +141,7 @@ mod tests { for row in [vec![7; n], (0..n).map(|i| (i % 251) as u8).collect()] { let mut encoded = Vec::new(); encode_packbits_into(&row, &mut encoded); - assert_eq!(decode(&encoded, n).unwrap(), row); + assert_eq!(decode(&encoded, n).map_err(|failure| failure.error).unwrap(), row); } } } From 45ad9df6370213c1e1b0ad242d1c74c413963e15 Mon Sep 17 00:00:00 2001 From: FredApps Date: Mon, 16 Nov 2026 16:21:55 +0100 Subject: [PATCH 329/471] craft-xlsx: decode XLSB defined names after the workbook's tables are indexed A defined name's formula may use a structured reference (PtgList). Names were decoded inside xlsb::workbook, before Package::workbook indexed the table parts, so such a name was left out and the cells using it kept only their saved results. xlsb::workbook now returns the names undecoded (Names), and Package::workbook decodes them with xlsb::read_names after index_tables. Regression: a_name_using_a_structured_reference_is_decoded_after_tables_are_indexed fails without the reorder. --- crates/xlsx/src/package.rs | 4 +++- crates/xlsx/src/xlsb.rs | 28 +++++++++++++++++++++------- crates/xlsx/tests/xlsb.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/crates/xlsx/src/package.rs b/crates/xlsx/src/package.rs index ce7d7756f..a75874e71 100644 --- a/crates/xlsx/src/package.rs +++ b/crates/xlsx/src/package.rs @@ -149,10 +149,12 @@ impl<'a> Package<'a> { if self.binary.is_some() { let bytes = self.required(&self.workbook_part.clone())?; let rels = self.workbook_rels()?; - let (wb, mut links) = xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings)?; + let (mut wb, mut links, names) = xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings)?; if let Some(links) = links.as_mut() { self.index_tables(&wb, links)?; } + // Names after tables: a name's formula may use a structured reference. + xlsb::read_names(&mut wb, names, links.as_mut(), &self.limits, &self.budget, &mut self.warnings); self.binary_links = Some(links); return Ok(wb); } diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 7ec6be839..b45b48808 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -289,14 +289,16 @@ impl<'a, 'l> Records<'a, 'l> { /// The workbook part: BrtBeginBook, BrtWbProp, the BrtBundleSh list. /// The workbook part, and the tables its formulas refer to ([`Links`]): `None` when those can't -/// be read, so that the workbook still opens, with every formula kept as its saved result. +/// be read, so that the workbook still opens, with every formula kept as its saved result. Its +/// defined names come back undecoded ([`Names`]): [`read_names`] decodes them once the workbook's +/// tables are indexed, so a name's formula can use a structured reference. pub(crate) fn workbook( bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budget, warnings: &mut Warnings, -) -> Result<(Workbook, Option), Error> { +) -> Result<(Workbook, Option, Names), Error> { let mut records = Records::new(bytes, limits, budget); records.first(131, warnings)?; let mut wb = Workbook::default(); @@ -320,11 +322,7 @@ pub(crate) fn workbook( if wb.sheets.is_empty() { return Err(invalid("no supported worksheets")); } - match links.as_mut() { - Some(l) => wb.defined_names = defined_names(pending, &bundle_sheets, l, limits, budget, warnings), - None => warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"), - } - return Ok((wb, links)); + return Ok((wb, links, Names { pending, sheets: bundle_sheets })); } 39 | 355 | 357 | 358 | 362 | 667 => { if let Some(l) = links.as_mut() @@ -423,6 +421,22 @@ struct PendingName { help: Option, } +/// The workbook part's defined names, not yet decoded, and each BrtBundleSh's place in +/// [`Workbook::sheets`] (`None` for a skipped one). +pub(crate) struct Names { + pending: Vec, + sheets: Vec>, +} + +/// Decodes the workbook part's defined names `names` into `wb`, after the tables in `links` are +/// indexed. Without `links` none can be read, and formulas keep their saved results. +pub(crate) fn read_names(wb: &mut Workbook, names: Names, links: Option<&mut Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings) { + match links { + Some(l) => wb.defined_names = defined_names(names.pending, &names.sheets, l, limits, budget, warnings), + None => warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"), + } +} + /// The workbook's defined names, as `.xlsx` has them in `definedNames`: every `BrtName` but the /// hidden ones for newer functions and LET parameters, which Excel doesn't write there. `sheets` /// maps a name's `itab` (its sheet's place in `BrtBundleSh` order) to [`Workbook::sheets`]. diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 16220df9e..cd5494c03 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1803,6 +1803,42 @@ fn tables_of(bytes: &[u8]) -> (Vec
, Vec) { (tables, p.warnings().to_vec()) } +#[test] +fn a_name_using_a_structured_reference_is_decoded_after_tables_are_indexed() { + // `Vals` = `Sales[Val]`. Names are decoded after the workbook's tables are indexed, so + // the formula and a cell that uses it are kept, instead of only their saved results. + let name = brt_name(0, 0, WORKBOOK, "Vals", &ptg_list(0, 1, 1, 1, 1), None); + let list = [record(660, &1u32.to_le_bytes()), record(661, &wide("t1")), record(662, &[])]; + let mut data = row(0); + data.extend(formula_cell(1, &[0x23, 5, 0, 0, 0], &[])); // =Vals, after the four names of `book_with_links` + let types = { + let t = TYPES.trim_end_matches(""); + format!(r#"{t}"#) + }; + let sheet_rels = r#""#; + let (one, two, strings, table) = (worksheet(&data, &list.concat()), worksheet(&[], &[]), sst(), sales(&sales_filters())); + let book = { + let plain = book_with_links(&xti()); + let end = record(132, &[]); + [&plain[..plain.len() - end.len()], name.as_slice(), end.as_slice()].concat() + }; + let bytes = zip(&[ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", ROOT.as_bytes()), + ("records/book.bin", book.as_slice()), + ("records/_rels/book.bin.rels", RELS.as_bytes()), + ("data/one.bin", one.as_slice()), + ("data/_rels/one.bin.rels", sheet_rels.as_bytes()), + ("data/two.bin", two.as_slice()), + ("text/strings.bin", strings.as_slice()), + ("tables/t1.bin", table.as_slice()), + ]); + let names = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names; + assert_eq!(names.iter().find(|n| n.name == "Vals").map(|n| n.formula.as_str()), Some("Sales[Val]")); + let wb = read(&bytes).unwrap(); + assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), Some("Vals"), "{:?}", wb.warnings); +} + #[test] fn tables_are_read_as_their_xlsx_spelling() { // Spacing flags: 0x80 inside the outer brackets, 0x100 after commas. From dd3015c678052b9a1f1732e70f83576c0a72e75f Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:23:22 +0100 Subject: [PATCH 330/471] wordmodel: keep the kashida length of a justified paragraph (#1875) * wordmodel: keep the kashida length of a justified paragraph ST_Jc lowKashida, mediumKashida and highKashida were all read as Align::Justify, so the length was lost. ParaProps and ResolvedPara now keep it; a direct both/justify cancels an inherited length. The DOCX reader and writer round-trip the three values. * docx: keep a style's kashida length when writing justify w:jc encodes alignment and kashida together. A paragraph or style with Align::Justify and kashida unset still inherits a length from the style chain; writing "both" would read back as Some(Kashida::None) and cancel it. Use the resolved length in that case. --- crates/docx/src/read/props.rs | 11 +- crates/docx/src/write.rs | 5 + crates/docx/src/write/props.rs | 11 +- crates/docx/src/write/story/para.rs | 6 + crates/docx/src/write/styles.rs | 8 +- crates/docx/tests/it/kashida.rs | 136 +++++++++++++++++++++++ crates/docx/tests/it/main.rs | 1 + crates/docx/tests/it/roundtrip.rs | 5 +- crates/wordmodel/src/props.rs | 2 +- crates/wordmodel/src/props/para_props.rs | 45 +++++++- crates/wordmodel/src/resolve.rs | 35 +++++- 11 files changed, 254 insertions(+), 11 deletions(-) create mode 100644 crates/docx/tests/it/kashida.rs diff --git a/crates/docx/src/read/props.rs b/crates/docx/src/read/props.rs index dc61b3a28..4d86ae164 100644 --- a/crates/docx/src/read/props.rs +++ b/crates/docx/src/read/props.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use craft_wordmodel::para::cap_alt; use craft_wordmodel::props::{ - Align, Border, BorderStyle, Borders, CellProps, CharProps, CombineBrackets, EastAsianLayout, HeightRule, Highlight, LineSpacing, NumRef, + Align, Border, BorderStyle, Borders, CellProps, CharProps, CombineBrackets, EastAsianLayout, HeightRule, Highlight, Kashida, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, Rgb, RowProps, TabAlign, TabLeader, TabStop, TableFloat, TableLook, TableProps, TextAlignment, TextColor, TextDirection, Underline, VAlign, VMerge, VertAlign, }; @@ -465,7 +465,14 @@ impl PropCtx { _ => {} } } - "w:jc" => p.align = k.attr("w:val").map(align), + "w:jc" => { + if let Some(v) = k.attr("w:val") { + p.align = Some(align(v)); + if let Some(kashida) = Kashida::from_jc(v) { + p.kashida = Some(kashida); + } + } + } "w:outlineLvl" => p.outline_level = k.attr("w:val").and_then(u32_of).map(|v| v.min(9) as u8), "w:rPr" => mark = self.rpr(k), _ => {} diff --git a/crates/docx/src/write.rs b/crates/docx/src/write.rs index 1d9eb0818..4ba486a6c 100644 --- a/crates/docx/src/write.rs +++ b/crates/docx/src/write.rs @@ -151,6 +151,11 @@ pub fn write_as(doc: &Document, flavor: Flavor) -> Result, DocxError> { } impl Writer<'_> { + /// The document's styles, for writers that need the resolved style chain. + pub(crate) fn styles(&self) -> &craft_wordmodel::styles::StyleSheet { + &self.doc.styles + } + fn assign_media(&mut self) { let mut used: std::collections::HashSet = std::collections::HashSet::new(); for (key, bytes) in &self.doc.media { diff --git a/crates/docx/src/write/props.rs b/crates/docx/src/write/props.rs index 0b92ce191..fb76da8d8 100644 --- a/crates/docx/src/write/props.rs +++ b/crates/docx/src/write/props.rs @@ -1,8 +1,8 @@ //! Property writers, in ECMA-376 schema element order. use craft_wordmodel::props::{ - Align, Border, BorderStyle, Borders, CellProps, CharProps, EastAsianLayout, HeightRule, Highlight, LineSpacing, ParaProps, RowProps, TabAlign, - TabLeader, TableFloat, TableLook, TableProps, TextColor, VAlign, VMerge, VertAlign, + Align, Border, BorderStyle, Borders, CellProps, CharProps, EastAsianLayout, HeightRule, Highlight, Kashida, LineSpacing, ParaProps, RowProps, + TabAlign, TabLeader, TableFloat, TableLook, TableProps, TextColor, VAlign, VMerge, VertAlign, }; use crate::units::{n, twips}; @@ -201,6 +201,11 @@ pub fn align_val(a: Align) -> &'static str { } } +/// `w:jc` for a paragraph, including the kashida length of a justified one. +pub fn jc_val(a: Align, kashida: Option) -> &'static str { + if a == Align::Justify { kashida.unwrap_or(Kashida::None).jc() } else { align_val(a) } +} + /// A tab stop's or positional tab's `w:leader` value. pub fn tab_leader(l: TabLeader) -> &'static str { match l { @@ -287,7 +292,7 @@ pub fn ppr_inner(w: &mut W, p: &ParaProps, framed: bool, num_change: Option<&Cha toggle(w, "w:contextualSpacing", p.contextual_spacing); toggle(w, "w:mirrorIndents", p.mirror_indents); if let Some(a) = p.align { - w.val("w:jc", align_val(a)); + w.val("w:jc", jc_val(a, p.kashida)); } if let Some(direction) = p.text_direction { w.val("w:textDirection", direction.ooxml()); diff --git a/crates/docx/src/write/story/para.rs b/crates/docx/src/write/story/para.rs index e0242d8eb..aa973184e 100644 --- a/crates/docx/src/write/story/para.rs +++ b/crates/docx/src/write/story/para.rs @@ -76,6 +76,12 @@ impl Writer<'_> { a.push(("w:original", c.original.clone())); a }); + // `w:jc` encodes alignment and kashida length together. A justified paragraph + // whose kashida is unset (`None`) still inherits a length from its style; writing + // `both` would read back as `Some(Kashida::None)` and cancel that length. + if pp.align == Some(craft_wordmodel::props::Align::Justify) && pp.kashida.is_none() { + pp.kashida = Some(self.styles().resolve_para(&pp).kashida); + } ppr_inner(w, &pp, framed, num_change.as_ref()); if has_mark { self.write_mark_rpr(w, &p.mark, &mark_revs); diff --git a/crates/docx/src/write/styles.rs b/crates/docx/src/write/styles.rs index 3bcf9ab97..0fddf33b4 100644 --- a/crates/docx/src/write/styles.rs +++ b/crates/docx/src/write/styles.rs @@ -184,7 +184,13 @@ fn style_property_xml(wr: &mut Writer<'_>, w: &mut W, st: &Style) { let para_change = st.para_change.as_deref().filter(|ch| ch.old.formatting() != st.para.formatting()); if (!st.para.is_empty() || para_change.is_some()) && st.kind != StyleKind::Character { w.open("w:pPr", &[]); - props::ppr_inner(w, &st.para, false, None); + let mut ppr = st.para.clone(); + if ppr.align == Some(craft_wordmodel::props::Align::Justify) && ppr.kashida.is_none() { + let mut probe = ppr.clone(); + probe.style = Some(st.id.clone()); + ppr.kashida = Some(wr.styles().resolve_para(&probe).kashida); + } + props::ppr_inner(w, &ppr, false, None); if let Some(ch) = para_change { let a = wr.change_attrs(ch.rev); open_change(w, "w:pPrChange", &a); diff --git a/crates/docx/tests/it/kashida.rs b/crates/docx/tests/it/kashida.rs new file mode 100644 index 000000000..02eaf2a49 --- /dev/null +++ b/crates/docx/tests/it/kashida.rs @@ -0,0 +1,136 @@ +//! Justified paragraphs keep their kashida length (`ST_Jc` `lowKashida`, `mediumKashida`, +//! `highKashida`, and ordinary `both` / `justify`) through read and write. + +use std::io::{Read, Write}; + +use std::sync::Arc; + +use craft_wordmodel::props::{Align, Kashida, ParaProps}; +use craft_wordmodel::styles::{Style, StyleKind, StyleSheet}; +use craft_wordmodel::{Document, Paragraph, para_block}; + +fn zip(entries: &[(&str, &[u8])]) -> Vec { + let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for (name, bytes) in entries { + zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); + zw.write_all(bytes).unwrap(); + } + zw.finish().unwrap().into_inner() +} + +fn docx(body: &str) -> Vec { + let root = r#""#; + let doc = format!( + r#"{body}"# + ); + zip(&[("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())]) +} + +fn paras(d: &Document) -> Vec<&Paragraph> { + d.body.iter().filter_map(|b| b.as_para()).collect() +} + +fn document_xml(bytes: &[u8]) -> String { + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut s = String::new(); + z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); + s +} + +fn jc_para(body: &str, val: &str) -> String { + format!(r#"{body}"#) +} + +#[test] +fn kashida_jc_values_are_read() { + let body = [ + jc_para("both", "both"), + jc_para("justify", "justify"), + jc_para("low", "lowKashida"), + jc_para("medium", "mediumKashida"), + jc_para("high", "highKashida"), + jc_para("left", "left"), + ] + .join(""); + let d = craft_docx::read(&docx(&body)).unwrap(); + let ps = paras(&d); + let got: Vec<_> = ps.iter().map(|p| (p.props.align, p.props.kashida)).collect(); + assert_eq!( + got, + vec![ + (Some(Align::Justify), Some(Kashida::None)), + (Some(Align::Justify), Some(Kashida::None)), + (Some(Align::Justify), Some(Kashida::Short)), + (Some(Align::Justify), Some(Kashida::Medium)), + (Some(Align::Justify), Some(Kashida::Long)), + (Some(Align::Left), None), + ] + ); +} + +#[test] +fn kashida_length_round_trips() { + let cases = [ + (Some(Kashida::None), "w:val=\"both\""), + (Some(Kashida::Short), "w:val=\"lowKashida\""), + (Some(Kashida::Medium), "w:val=\"mediumKashida\""), + (Some(Kashida::Long), "w:val=\"highKashida\""), + ]; + for (kashida, token) in cases { + let mut p = Paragraph::with_text("مثال", Default::default()); + p.props = ParaProps { align: Some(Align::Justify), kashida, ..Default::default() }; + let mut d = Document::new(); + d.body = vec![para_block(p.clone())]; + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert!(xml.contains(token), "expected {token} in {xml}"); + let r = craft_docx::read(&bytes).unwrap(); + let q = paras(&r)[0]; + assert_eq!(q.props.align, Some(Align::Justify)); + assert_eq!(q.props.kashida, kashida, "round-trip of {token}"); + } +} + +fn body_style(kashida: Kashida) -> StyleSheet { + let mut sheet = StyleSheet::empty(); + sheet.styles.push(Style { + id: "Body".into(), + name: "Body".into(), + kind: StyleKind::Paragraph, + para: ParaProps { align: Some(Align::Justify), kashida: Some(kashida), ..ParaProps::default() }, + ..Style::default() + }); + sheet +} + +#[test] +fn style_kashida_length_survives_a_write_when_the_paragraph_has_no_direct_jc() { + let mut p = Paragraph::with_text("مثال", Default::default()); + p.props = ParaProps { style: Some("Body".into()), ..ParaProps::default() }; + let mut d = Document::new(); + d.styles = Arc::new(body_style(Kashida::Short)); + d.body = vec![para_block(p)]; + assert_eq!(d.styles.resolve_para(&d.body[0].as_para().unwrap().props).kashida, Kashida::Short); + let bytes = craft_docx::write(&d).unwrap(); + let r = craft_docx::read(&bytes).unwrap(); + let q = paras(&r)[0]; + assert_eq!(q.props.kashida, None, "no direct w:jc on the paragraph"); + assert_eq!(r.styles.resolve_para(&q.props).kashida, Kashida::Short); +} + +#[test] +fn writing_justify_without_a_local_kashida_keeps_the_style_length() { + let mut p = Paragraph::with_text("مثال", Default::default()); + p.props = ParaProps { style: Some("Body".into()), align: Some(Align::Justify), kashida: None, ..ParaProps::default() }; + let mut d = Document::new(); + d.styles = Arc::new(body_style(Kashida::Medium)); + d.body = vec![para_block(p)]; + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert!(xml.contains("w:val=\"mediumKashida\""), "expected mediumKashida in {xml}"); + let r = craft_docx::read(&bytes).unwrap(); + let q = paras(&r)[0]; + assert_eq!(q.props.align, Some(Align::Justify)); + assert_eq!(q.props.kashida, Some(Kashida::Medium)); + assert_eq!(r.styles.resolve_para(&q.props).kashida, Kashida::Medium); +} diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index 8696981c7..8d6be8a97 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -20,6 +20,7 @@ mod emphasis_marks; mod fixtures; mod glossary; mod heading_levels; +mod kashida; mod legacy_forms; mod ligatures; mod line_numbering; diff --git a/crates/docx/tests/it/roundtrip.rs b/crates/docx/tests/it/roundtrip.rs index 65a01e6b4..2037f9849 100644 --- a/crates/docx/tests/it/roundtrip.rs +++ b/crates/docx/tests/it/roundtrip.rs @@ -6,8 +6,8 @@ use std::sync::Arc; use craft_wordmodel::numbering::ListKind; use craft_wordmodel::para::{Anchor, Float, FloatAlign, NoteKind, ShapeKind, Wrap}; use craft_wordmodel::props::{ - Align, Border, BorderStyle, Borders, CharProps, HeightRule, Highlight, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, Rgb, RowProps, - TabAlign, TabLeader, TabStop, TableLook, TextAlignment, TextColor, TextDirection, Underline, VAlign, VMerge, VertAlign, + Align, Border, BorderStyle, Borders, CharProps, HeightRule, Highlight, Kashida, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, Rgb, + RowProps, TabAlign, TabLeader, TabStop, TableLook, TextAlignment, TextColor, TextDirection, Underline, VAlign, VMerge, VertAlign, }; use craft_wordmodel::section::{Columns, DocGrid, DocGridType, LineNumberRestart, LineNumbering, NumFormat, PageVAlign, SectionProps, SectionStart}; use craft_wordmodel::styles::{Style, StyleKind}; @@ -163,6 +163,7 @@ fn every_para_prop_round_trips() { let pp = ParaProps { style: Some("Heading1".into()), align: Some(Align::Justify), + kashida: Some(Kashida::Medium), indent_left: Some(36.0), indent_right: Some(18.0), indent_first: Some(-18.0), diff --git a/crates/wordmodel/src/props.rs b/crates/wordmodel/src/props.rs index 65a59592c..318779acb 100644 --- a/crates/wordmodel/src/props.rs +++ b/crates/wordmodel/src/props.rs @@ -27,7 +27,7 @@ pub use char_props::{ }; pub use color::{Rgb, TextColor}; pub use para_props::{ - Align, Border, BorderStyle, Borders, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, TAB_POS_EPS, TabAlign, TabLeader, TabStop, + Align, Border, BorderStyle, Borders, Kashida, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, TAB_POS_EPS, TabAlign, TabLeader, TabStop, TextAlignment, TextFrame, }; pub use table_props::{ diff --git a/crates/wordmodel/src/props/para_props.rs b/crates/wordmodel/src/props/para_props.rs index a48540248..b3dec00ad 100644 --- a/crates/wordmodel/src/props/para_props.rs +++ b/crates/wordmodel/src/props/para_props.rs @@ -26,6 +26,45 @@ impl Align { } } +/// The kashida (tatweel) length a justified paragraph uses (`ST_Jc` in ECMA-376 §17.18.44). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)] +#[serde(rename_all = "camelCase")] +pub enum Kashida { + /// No kashidas: justified lines widen their word spaces (`both`, `justify`). + #[default] + None, + /// `lowKashida`. + Short, + /// `mediumKashida`. + Medium, + /// `highKashida`. + Long, +} + +impl Kashida { + /// The length named by a `w:jc` value, if that value is a kashida or ordinary justify form. + /// Other alignments return `None` so they leave an inherited length. + pub fn from_jc(value: &str) -> Option { + match value { + "both" | "justify" => Some(Kashida::None), + "lowKashida" => Some(Kashida::Short), + "mediumKashida" => Some(Kashida::Medium), + "highKashida" => Some(Kashida::Long), + _ => None, + } + } + + /// The `w:jc` value for a justified paragraph of this length. + pub fn jc(self) -> &'static str { + match self { + Kashida::None => "both", + Kashida::Short => "lowKashida", + Kashida::Medium => "mediumKashida", + Kashida::Long => "highKashida", + } + } +} + /// Paragraph-level text flow (`w:pPr/w:textDirection`, ECMA-376 §17.3.1.41). /// /// Kept separate from [`super::TextDirection`], whose three values model the table-cell direction @@ -383,6 +422,10 @@ pub struct ParaProps { pub style: Option, #[serde(skip_serializing_if = "Option::is_none")] pub align: Option, + /// Kashida length for a justified paragraph. `None` inherits; `Some(Kashida::None)` is + /// ordinary justification (`both` / `justify`) and cancels an inherited length. + #[serde(skip_serializing_if = "Option::is_none")] + pub kashida: Option, /// Left indent, points. #[serde(skip_serializing_if = "Option::is_none")] pub indent_left: Option, @@ -516,7 +559,7 @@ impl ParaProps { } fn overlay_layout(&mut self, patch: &ParaProps) { - overlay_fields!(self, patch; style, align, indent_left, indent_right, indent_first, indent_left_chars, indent_right_chars, indent_first_chars, + overlay_fields!(self, patch; style, align, kashida, indent_left, indent_right, indent_first, indent_left_chars, indent_right_chars, indent_first_chars, space_before, space_after, space_before_auto, space_after_auto, line_spacing, contextual_spacing, mirror_indents, adjust_right_indent, tabs, shading, borders); } diff --git a/crates/wordmodel/src/resolve.rs b/crates/wordmodel/src/resolve.rs index 7ef12ccdc..85a7a1550 100644 --- a/crates/wordmodel/src/resolve.rs +++ b/crates/wordmodel/src/resolve.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::props::{ - Align, Border, Borders, CharProps, Highlight, LineSpacing, NumRef, ParaProps, Rgb, TabAlign, TabStop, TextColor, Underline, VertAlign, + Align, Border, Borders, CharProps, Highlight, Kashida, LineSpacing, NumRef, ParaProps, Rgb, TabAlign, TabStop, TextColor, Underline, VertAlign, }; use crate::styles::{StyleKind, StyleSheet}; @@ -173,6 +173,9 @@ fn default_adjust_right_indent() -> bool { pub struct ResolvedPara { pub style: String, pub align: Align, + /// Kashida length when `align` is Justify; ignored for other alignments. + #[serde(default)] + pub kashida: Kashida, pub indent_left: f32, pub indent_right: f32, pub indent_first: f32, @@ -268,6 +271,7 @@ impl ResolvedPara { ResolvedPara { style: style.to_string(), align: p.align.unwrap_or_default(), + kashida: p.kashida.unwrap_or_default(), indent_left: clampi(p.indent_left), indent_right: clampi(p.indent_right), indent_first: clampi(p.indent_first), @@ -634,4 +638,33 @@ mod tests { assert_eq!(back.padding, [0.0; 4]); assert_eq!(back, ResolvedPara { padding: [0.0; 4], ..p }); } + + /// A style's kashida length is inherited, and a direct `both` / `justify` cancels it. + #[test] + fn kashida_length_follows_the_style_chain_and_direct_formatting() { + use crate::styles::{Style, StyleKind}; + let mut styles = StyleSheet::builtin(); + styles.upsert(Style { + id: "Body".into(), + name: "Body".into(), + kind: StyleKind::Paragraph, + based_on: Some("Normal".into()), + para: ParaProps { align: Some(Align::Justify), kashida: Some(Kashida::Short), ..Default::default() }, + ..Default::default() + }); + let inherited = styles.resolve_para(&ParaProps { style: Some("Body".into()), ..Default::default() }); + assert_eq!(inherited.align, Align::Justify); + assert_eq!(inherited.kashida, Kashida::Short); + + let cleared = styles.resolve_para(&ParaProps { + style: Some("Body".into()), + align: Some(Align::Justify), + kashida: Some(Kashida::None), + ..Default::default() + }); + assert_eq!(cleared.kashida, Kashida::None); + + let longer = styles.resolve_para(&ParaProps { style: Some("Body".into()), kashida: Some(Kashida::Long), ..Default::default() }); + assert_eq!(longer.kashida, Kashida::Long); + } } From a58989cae9173c7402a125bdd42e48fb5c878e09 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:26:36 +0100 Subject: [PATCH 331/471] image-codecs: skip a SubIFDs array whose offset is cut off (#1889) camera_raw_mode read a missing SubIFDs offset as 0, so it walked the TIFF header as the pointer array and could classify a malformed TIFF as RAW. Skip the entry instead, matching LightCraft's classifier. --- crates/image-codecs/src/codecs/tiff.rs | 6 ++++-- crates/image-codecs/tests/legacy_tiff_raw.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/image-codecs/src/codecs/tiff.rs b/crates/image-codecs/src/codecs/tiff.rs index bf59157eb..0f53bb3da 100644 --- a/crates/image-codecs/src/codecs/tiff.rs +++ b/crates/image-codecs/src/codecs/tiff.rs @@ -207,8 +207,10 @@ fn camera_raw_mode(b: &[u8], legacy: bool) -> bool { } TAG_SUB_IFDS if count == 1 => queue.extend(value(e)), TAG_SUB_IFDS => { - // More than one offset: they are stored at the entry's offset. - let at = u32_at(e.saturating_add(8)).unwrap_or(0) as usize; + // More than one offset: they are stored at the entry's offset. A cut-off + // offset field means no array, not one at the start of the file. + let Some(at) = u32_at(e.saturating_add(8)) else { continue }; + let at = at as usize; for k in 0..count.min(8) as usize { queue.extend(u32_at(at.saturating_add(4 * k))); } diff --git a/crates/image-codecs/tests/legacy_tiff_raw.rs b/crates/image-codecs/tests/legacy_tiff_raw.rs index 770801734..27cf6b881 100644 --- a/crates/image-codecs/tests/legacy_tiff_raw.rs +++ b/crates/image-codecs/tests/legacy_tiff_raw.rs @@ -63,3 +63,23 @@ fn oversized_or_cut_directories_are_skipped() { cut.truncate(20); assert!(!legacy_tiff_raw_hint(&cut)); } + +#[test] +fn a_subifds_array_without_its_offset_is_skipped() { + // IFD0 at 64 holds one SubIFDs entry (LONG, count 3) whose offset field is cut off. A DNG + // directory sits at 32, and the bytes at 8 point to it. Reading the missing offset as 0 + // would treat the TIFF header as the pointer array and follow 8 -> 32. + let mut b = vec![0u8; 64]; + b[..8].copy_from_slice(b"II\x2a\0\x40\0\0\0"); + b[8..12].copy_from_slice(&32u32.to_le_bytes()); + b[32..34].copy_from_slice(&1u16.to_le_bytes()); + b[34..46].copy_from_slice(&[0x12, 0xc6, 1, 0, 4, 0, 0, 0, 1, 4, 0, 0]); + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&[0x4a, 0x01, 4, 0, 3, 0, 0, 0]); + assert_eq!(b.len(), 74); + assert!(!legacy_tiff_raw_hint(&b)); + // With the offset present (the array at 8), the same SubIFD is followed. + let mut whole = b.clone(); + whole.extend_from_slice(&8u32.to_le_bytes()); + assert!(legacy_tiff_raw_hint(&whole)); +} From fb2b27b1f02b603d696480d03345827e8b44124a Mon Sep 17 00:00:00 2001 From: FredApps Date: Mon, 16 Nov 2026 16:50:42 +0100 Subject: [PATCH 332/471] craft-xlsx: preserve spacing in simple structured references --- crates/xlsx/src/xlsb/formula.rs | 6 +++++- crates/xlsx/tests/xlsb.rs | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 5d4cfa619..40d461db1 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -681,17 +681,22 @@ fn list(t: &mut Bytes<'_>, links: &Links) -> Result { 2 if first <= last => Some((column(first)?, Some(column(last)?))), _ => return Err(Unsupported("invalid structured reference columns")), }; + let pad = if bits & 0x80 != 0 { " " } else { "" }; let mut s = table.name.clone(); match (rows, &columns) { ([], None) => s.push_str("[]"), ([], Some((c, None))) => { s.push('['); + s.push_str(pad); s.push_str(c); + s.push_str(pad); s.push(']'); } ([row], None) => { s.push('['); + s.push_str(pad); s.push_str(row); + s.push_str(pad); s.push(']'); } _ => { @@ -701,7 +706,6 @@ fn list(t: &mut Bytes<'_>, links: &Links) -> Result { Some((a, None)) => items.push(format!("[{a}]")), None => {} } - let pad = if bits & 0x80 != 0 { " " } else { "" }; let comma = if bits & 0x100 != 0 { ", " } else { "," }; s.push('['); s.push_str(pad); diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index cd5494c03..a7253df22 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1839,6 +1839,27 @@ fn a_name_using_a_structured_reference_is_decoded_after_tables_are_indexed() { assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), Some("Vals"), "{:?}", wb.warnings); } +#[test] +fn simple_table_references_preserve_square_bracket_spacing() { + let cases = [ + (1, "Sales[Val]"), + (1 | 0x80, "Sales[ Val ]"), + (rows(1), "Sales[#All]"), + (rows(1) | 0x80, "Sales[ #All ]"), + (1 | 0x100, "Sales[Val]"), + (rows(1) | 0x100, "Sales[#All]"), + ]; + let mut data = row(0); + for (i, (bits, _)) in cases.iter().enumerate() { + data.extend(formula_cell(i as u32 + 1, &ptg_list(0, *bits, 1, 1, 1), &[])); + } + let bytes = book_with_tables(&data, &[sales(&sales_filters())]); + let wb = read(&bytes).unwrap(); + for (i, (_, want)) in cases.iter().enumerate() { + assert_eq!(wb.sheets[0].formula(CellRef::new(0, i as u32 + 1)), Some(*want), "{:?}", wb.warnings); + } +} + #[test] fn tables_are_read_as_their_xlsx_spelling() { // Spacing flags: 0x80 inside the outer brackets, 0x100 after commas. From b84343883d546731e658df8a9a8b77b71ffa1489 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:04:27 +0100 Subject: [PATCH 333/471] Validate compositor frame inputs and return allocation errors --- crates/color/src/lib.rs | 45 ++++++-- crates/frame/src/lib.rs | 226 +++++++++++++++++++++++++++++++--------- 2 files changed, 211 insertions(+), 60 deletions(-) diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs index 5bf6426b8..01fbf3b18 100644 --- a/crates/color/src/lib.rs +++ b/crates/color/src/lib.rs @@ -154,24 +154,41 @@ pub fn rgb_to_ycbcr(r: f32, g: f32, b: f32, m: Matrix) -> [f32; 3] { [y, (b - y) / (2.0 * (1.0 - kb)), (r - y) / (2.0 * (1.0 - kr))] } +/// Unsupported integer sample depth. Normalization supports 8 through 16 bits. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct InvalidBitDepth(pub u32); + +impl std::fmt::Display for InvalidBitDepth { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "unsupported sample bit depth {}; expected 8..=16", self.0) + } +} +impl std::error::Error for InvalidBitDepth {} + /// Normalise an integer luma code value to 0..1 given bit depth and range. #[inline] -pub fn normalize_y(v: u32, bits: u32, range: Range) -> f32 { +pub fn normalize_y(v: u32, bits: u32, range: Range) -> Result { + if !(8..=16).contains(&bits) { + return Err(InvalidBitDepth(bits)); + } let scale = (1u32 << (bits - 8)) as f32; - match range { + Ok(match range { Range::Limited => (v as f32 - 16.0 * scale) / (219.0 * scale), Range::Full => v as f32 / ((1u32 << bits) - 1) as f32, - } + }) } /// Normalise an integer chroma code value to −0.5..0.5 given bit depth and range. #[inline] -pub fn normalize_c(v: u32, bits: u32, range: Range) -> f32 { +pub fn normalize_c(v: u32, bits: u32, range: Range) -> Result { + if !(8..=16).contains(&bits) { + return Err(InvalidBitDepth(bits)); + } let scale = (1u32 << (bits - 8)) as f32; - match range { + Ok(match range { Range::Limited => (v as f32 - 128.0 * scale) / (224.0 * scale), Range::Full => (v as f32 - (1u32 << (bits - 1)) as f32) / ((1u32 << bits) - 1) as f32, - } + }) } /// sRGB electro-optical transfer (encoded → linear). @@ -355,12 +372,20 @@ mod tests { } } + #[test] + fn invalid_bit_depths_are_errors_not_shifts() { + for bits in [0, 1, 7, 17, 31, 32, u32::MAX] { + assert_eq!(normalize_y(0, bits, Range::Full), Err(InvalidBitDepth(bits))); + assert_eq!(normalize_c(0, bits, Range::Limited), Err(InvalidBitDepth(bits))); + } + } + #[test] fn limited_range_levels() { - assert_eq!(normalize_y(16, 8, Range::Limited), 0.0); - assert_eq!(normalize_y(235, 8, Range::Limited), 1.0); - assert_eq!(normalize_y(940, 10, Range::Limited), 1.0); - assert_eq!(normalize_c(128, 8, Range::Limited), 0.0); + assert_eq!(normalize_y(16, 8, Range::Limited).unwrap(), 0.0); + assert_eq!(normalize_y(235, 8, Range::Limited).unwrap(), 1.0); + assert_eq!(normalize_y(940, 10, Range::Limited).unwrap(), 1.0); + assert_eq!(normalize_c(128, 8, Range::Limited).unwrap(), 0.0); } #[test] diff --git a/crates/frame/src/lib.rs b/crates/frame/src/lib.rs index 09f95e0b8..ad294b10f 100644 --- a/crates/frame/src/lib.rs +++ b/crates/frame/src/lib.rs @@ -23,6 +23,53 @@ use craft_time::Tick; use rayon::prelude::*; use serde::{Deserialize, Serialize}; +/// Default per-buffer allocation ceiling (1 GiB), permitting an 8K RGBA-f32 frame. +/// Callers with a smaller budget should use the `*_with_limit` conversions. +pub const DEFAULT_MAX_FRAME_BYTES: usize = 1 << 30; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FrameError { + InvalidLayout, + InvalidBitDepth, + TooLarge, + Allocation, +} +impl std::fmt::Display for FrameError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::InvalidLayout => "frame storage does not match its dimensions", + Self::InvalidBitDepth => "unsupported frame sample bit depth", + Self::TooLarge => "frame exceeds the allocation limit", + Self::Allocation => "unable to allocate frame buffer", + }) + } +} +impl std::error::Error for FrameError {} +impl From for FrameError { + fn from(_: craft_color::InvalidBitDepth) -> Self { + Self::InvalidBitDepth + } +} +pub type Result = std::result::Result; + +fn elements(width: u32, height: u32, channels: usize) -> Result { + usize::try_from(width) + .ok() + .and_then(|w| usize::try_from(height).ok().and_then(|h| w.checked_mul(h))) + .and_then(|n| n.checked_mul(channels)) + .ok_or(FrameError::TooLarge) +} + +fn buffer(len: usize, max_bytes: usize) -> Result> { + if len.checked_mul(std::mem::size_of::()).is_none_or(|bytes| bytes > max_bytes || bytes > isize::MAX as usize) { + return Err(FrameError::TooLarge); + } + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|_| FrameError::Allocation)?; + out.resize(len, T::default()); + Ok(out) +} + /// Chroma subsampling of planar Y'CbCr. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Chroma { @@ -103,20 +150,51 @@ impl Region { impl VideoFrame { /// An sRGB-full, 1:1, zero-pts RGBA8 frame. `data` is `width × height × 4` bytes. - pub fn rgba8(width: u32, height: u32, data: Vec) -> Self { - debug_assert_eq!(data.len(), (width * height * 4) as usize); - Self { width, height, data: PixelData::Rgba8(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO } + pub fn rgba8(width: u32, height: u32, data: Vec) -> Result { + let frame = Self { width, height, data: PixelData::Rgba8(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO }; + frame.validate()?; + Ok(frame) } /// A linear premultiplied RGBA f32 frame, sRGB-full tags, 1:1, zero pts. - pub fn rgba_f32(width: u32, height: u32, data: Vec) -> Self { - debug_assert_eq!(data.len(), (width * height * 4) as usize); - Self { width, height, data: PixelData::RgbaF32(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO } + pub fn rgba_f32(width: u32, height: u32, data: Vec) -> Result { + let frame = Self { width, height, data: PixelData::RgbaF32(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO }; + frame.validate()?; + Ok(frame) } /// A transparent linear RGBA f32 frame of the given size. - pub fn transparent_f32(width: u32, height: u32) -> Self { - Self::rgba_f32(width, height, vec![0.0; (width * height * 4) as usize]) + pub fn transparent_f32(width: u32, height: u32) -> Result { + Self::rgba_f32(width, height, buffer(elements(width, height, 4)?, DEFAULT_MAX_FRAME_BYTES)?) + } + + /// Validate all public fields before indexing pixel storage. Rechecked by conversions because + /// callers can mutate these fields after construction. Padding beyond each required plane is allowed. + pub fn validate(&self) -> Result<()> { + let pixels = elements(self.width, self.height, 1)?; + let rgba = pixels.checked_mul(4).ok_or(FrameError::TooLarge)?; + let planar = |lengths: [usize; 3], chroma: Chroma, alpha: Option| { + let (sx, sy) = chroma.shifts(); + let cw = (self.width as usize).div_ceil(1 << sx); + let ch = (self.height as usize).div_ceil(1 << sy); + let cp = cw.checked_mul(ch).ok_or(FrameError::TooLarge)?; + if lengths[0] < pixels || lengths[1] < cp || lengths[2] < cp || alpha.is_some_and(|n| n < pixels) { + return Err(FrameError::InvalidLayout); + } + Ok(()) + }; + match &self.data { + PixelData::Rgba8(d) if d.len() == rgba => Ok(()), + PixelData::RgbaF32(d) if d.len() == rgba => Ok(()), + PixelData::Yuv8 { planes, chroma, alpha } => planar(planes.each_ref().map(|p| p.len()), *chroma, alpha.as_ref().map(|p| p.len())), + PixelData::Yuv16 { planes, chroma, bits, alpha } => { + if !(9..=16).contains(bits) { + return Err(FrameError::InvalidBitDepth); + } + planar(planes.each_ref().map(|p| p.len()), *chroma, alpha.as_ref().map(|p| p.len())) + } + _ => Err(FrameError::InvalidLayout), + } } /// Sets the presentation timestamp and returns the frame. @@ -139,18 +217,27 @@ impl VideoFrame { pub fn byte_size(&self) -> usize { match &self.data { PixelData::Rgba8(d) => d.len(), - PixelData::RgbaF32(d) => d.len() * 4, - PixelData::Yuv8 { planes, alpha, .. } => planes.iter().map(|p| p.len()).sum::() + alpha.as_ref().map_or(0, |a| a.len()), - PixelData::Yuv16 { planes, alpha, .. } => (planes.iter().map(|p| p.len()).sum::() + alpha.as_ref().map_or(0, |a| a.len())) * 2, + PixelData::RgbaF32(d) => d.len().saturating_mul(4), + PixelData::Yuv8 { planes, alpha, .. } => { + planes.iter().fold(0usize, |n, p| n.saturating_add(p.len())).saturating_add(alpha.as_ref().map_or(0, |a| a.len())) + } + PixelData::Yuv16 { planes, alpha, .. } => { + planes.iter().fold(0usize, |n, p| n.saturating_add(p.len())).saturating_add(alpha.as_ref().map_or(0, |a| a.len())).saturating_mul(2) + } } } /// Premultiplied linear-light RGBA f32, one pixel per source pixel, row-major, 4 samples each. - pub fn to_linear_f32(&self) -> Vec { + pub fn to_linear_f32(&self) -> Result> { + self.to_linear_f32_with_limit(DEFAULT_MAX_FRAME_BYTES) + } + + pub fn to_linear_f32_with_limit(&self, max_bytes: usize) -> Result> { + self.validate()?; let (w, h) = (self.width as usize, self.height as usize); - let mut out = vec![0.0; w.saturating_mul(h).saturating_mul(4)]; + let mut out = buffer::(elements(self.width, self.height, 4)?, max_bytes)?; if w == 0 || h == 0 { - return out; + return Ok(out); } match &self.data { PixelData::RgbaF32(d) => { @@ -173,8 +260,8 @@ impl VideoFrame { let info = self.color; let lin: Vec = (0..4096).map(|i| to_linear(i as f32 / 4095.0, info.transfer)).collect(); let q = |v: f32| lin[(v.clamp(0.0, 1.0) * 4095.0 + 0.5) as usize]; - let ytab: Vec = (0..256).map(|v| normalize_y(v, 8, info.range)).collect(); - let ctab: Vec = (0..256).map(|v| normalize_c(v, 8, info.range)).collect(); + let ytab: Vec = (0..256).map(|v| normalize_y(v, 8, info.range)).collect::>()?; + let ctab: Vec = (0..256).map(|v| normalize_c(v, 8, info.range)).collect::>()?; let rgb_of = |y: usize, x: usize| { let yy = ytab[planes[0][y * w + x] as usize]; let u = ctab[planes[1][(y >> sy) * cw + (x >> sx)] as usize]; @@ -200,9 +287,9 @@ impl VideoFrame { let q = |v: f32| lin[(v.clamp(0.0, 1.0) * 4095.0 + 0.5) as usize]; for y in 0..h { for x in 0..w { - let yy = normalize_y(planes[0][y * w + x] as u32, bits, info.range); - let u = normalize_c(planes[1][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range); - let v = normalize_c(planes[2][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range); + let yy = normalize_y(planes[0][y * w + x] as u32, bits, info.range)?; + let u = normalize_c(planes[1][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range)?; + let v = normalize_c(planes[2][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range)?; let rgb = ycbcr_to_rgb(yy, u, v, info.matrix); let a = alpha.as_ref().map_or(1.0, |al| al[y * w + x] as f32 / amax); let o = (y * w + x) * 4; @@ -214,19 +301,28 @@ impl VideoFrame { } } } - out + Ok(out) } /// Convert to straight-alpha sRGB RGBA8 for display (fast path for 8-bit Rec.709/sRGB Y'CbCr /// without alpha, and a copy for RGBA8). - pub fn to_rgba8(&self) -> Vec { + pub fn to_rgba8(&self) -> Result> { + self.to_rgba8_with_limit(DEFAULT_MAX_FRAME_BYTES) + } + + pub fn to_rgba8_with_limit(&self, max_bytes: usize) -> Result> { + self.validate()?; let (w, h) = (self.width as usize, self.height as usize); // Empty pictures have no rows; Rayon rejects a zero-sized row chunk. if w == 0 || h == 0 { - return Vec::new(); + return Ok(Vec::new()); } match &self.data { - PixelData::Rgba8(d) => d.as_ref().clone(), + PixelData::Rgba8(d) => { + let mut out = buffer::(d.len(), max_bytes)?; + out.copy_from_slice(d); + Ok(out) + } PixelData::Yuv8 { planes, chroma, alpha: None } if matches!(self.color.transfer, Transfer::Bt709 | Transfer::Srgb) => { let (sx, sy) = chroma.shifts(); let cw = w.div_ceil(1 << sx); @@ -238,7 +334,7 @@ impl VideoFrame { }; let (crr, cbb) = (2.0 * (1.0 - kr) * cs, 2.0 * (1.0 - kb) * cs); let (cgr, cgb) = (crr * kr / kg, cbb * kb / kg); - let mut out = vec![0u8; w * h * 4]; + let mut out = buffer::(elements(self.width, self.height, 4)?, max_bytes)?; out.par_chunks_mut(w * 4).enumerate().for_each(|(y, row)| { let cy = y >> sy; let yrow = &planes[0][y * w..y * w + w]; @@ -255,30 +351,33 @@ impl VideoFrame { o[3] = 255; } }); - out + Ok(out) } _ => { - let lin = self.to_linear_f32(); - let mut out = vec![0u8; w * h * 4]; + let lin = self.to_linear_f32_with_limit(max_bytes)?; + let mut out = buffer::(elements(self.width, self.height, 4)?, max_bytes)?; out.par_chunks_mut(w * 4).zip(lin.par_chunks(w * 4)).for_each(|(o, s)| linear_premul_to_srgb8(s, o)); - out + Ok(out) } } } /// Luma plane (8-bit, for scopes and thumbnails). Y'CbCr 8-bit returns the Y plane; others go /// through [`Self::to_rgba8`] and Rec.709 luma of the encoded RGB. - pub fn luma8(&self) -> Vec { + pub fn luma8(&self) -> Result> { + self.validate()?; + let pixels = elements(self.width, self.height, 1)?; + let mut out = buffer::(pixels, DEFAULT_MAX_FRAME_BYTES)?; match &self.data { - PixelData::Yuv8 { planes, .. } => planes[0].as_ref().clone(), - _ => self - .to_rgba8() - .as_chunks::<4>() - .0 - .iter() - .map(|p| (0.2126 * p[0] as f32 + 0.7152 * p[1] as f32 + 0.0722 * p[2] as f32) as u8) - .collect(), + PixelData::Yuv8 { planes, .. } => out.copy_from_slice(&planes[0][..pixels]), + _ => { + let rgba = self.to_rgba8()?; + for (o, p) in out.iter_mut().zip(rgba.as_chunks::<4>().0) { + *o = (0.2126 * p[0] as f32 + 0.7152 * p[1] as f32 + 0.0722 * p[2] as f32) as u8; + } + } } + Ok(out) } } @@ -307,22 +406,49 @@ pub fn default_matrix(width: u32, height: u32) -> Matrix { mod tests { use super::*; + #[test] + fn malformed_storage_and_bit_depth_are_errors() { + let make = |data| VideoFrame { width: 2, height: 2, data, color: ColorInfo::REC709, par: (1, 1), pts: Tick::ZERO }; + let short = make(PixelData::Yuv8 { planes: std::array::from_fn(|_| Arc::new(vec![0])), chroma: Chroma::C420, alpha: None }); + assert_eq!(short.to_rgba8(), Err(FrameError::InvalidLayout)); + assert_eq!(short.to_linear_f32(), Err(FrameError::InvalidLayout)); + assert_eq!(short.luma8(), Err(FrameError::InvalidLayout)); + for bits in [0, 8, 17, 32, u32::MAX] { + let frame = make(PixelData::Yuv16 { planes: std::array::from_fn(|_| Arc::new(vec![0; 4])), chroma: Chroma::C444, bits, alpha: None }); + assert_eq!(frame.to_linear_f32(), Err(FrameError::InvalidBitDepth)); + } + assert!(VideoFrame::rgba8(2, 2, vec![0; 15]).is_err()); + assert!(VideoFrame::rgba_f32(2, 2, vec![0.0; 15]).is_err()); + let mut valid = VideoFrame::rgba8(1, 1, vec![0; 4]).unwrap(); + valid.width = 2; // public mutations must be checked again, not just by constructors. + assert_eq!(valid.to_linear_f32(), Err(FrameError::InvalidLayout)); + } + + #[test] + fn frame_allocations_are_checked_before_reserving() { + assert!(matches!(VideoFrame::transparent_f32(u32::MAX, u32::MAX), Err(FrameError::TooLarge))); + let frame = VideoFrame::rgba8(1, 1, vec![0; 4]).unwrap(); + assert_eq!(frame.to_rgba8_with_limit(3), Err(FrameError::TooLarge)); + assert_eq!(frame.to_linear_f32_with_limit(15), Err(FrameError::TooLarge)); + assert_eq!(frame.to_linear_f32_with_limit(16).unwrap(), vec![0.0; 4]); + } + #[test] fn empty_frames_convert_without_zero_sized_chunks() { for (w, h) in [(0, 0), (0, 1), (1, 0)] { - let frame = VideoFrame::transparent_f32(w, h); - assert!(frame.to_rgba8().is_empty()); - assert!(frame.to_linear_f32().is_empty()); - assert!(frame.luma8().is_empty()); + let frame = VideoFrame::transparent_f32(w, h).unwrap(); + assert!(frame.to_rgba8().unwrap().is_empty()); + assert!(frame.to_linear_f32().unwrap().is_empty()); + assert!(frame.luma8().unwrap().is_empty()); } } #[test] fn rgba8_roundtrips_through_to_rgba8() { let px = vec![10u8, 20, 30, 255, 0, 0, 0, 0]; - let f = VideoFrame::rgba8(2, 1, px.clone()).with_pts(Tick(42)); + let f = VideoFrame::rgba8(2, 1, px.clone()).unwrap().with_pts(Tick(42)); assert_eq!(f.pts, Tick(42)); - assert_eq!(f.to_rgba8(), px); + assert_eq!(f.to_rgba8().unwrap(), px); assert_eq!(f.format_label(), "RGBA 8-bit"); assert_eq!(f.byte_size(), 8); assert_eq!(f.color, ColorInfo::SRGB_FULL); @@ -331,14 +457,14 @@ mod tests { #[test] fn rgba8_to_linear_premultiplies() { // 50% red, 50% alpha, sRGB encoded. Linear of 128/255, times 0.5 alpha. - let f = VideoFrame::rgba8(1, 1, vec![128, 0, 0, 128]); - let lin = f.to_linear_f32(); + let f = VideoFrame::rgba8(1, 1, vec![128, 0, 0, 128]).unwrap(); + let lin = f.to_linear_f32().unwrap(); let a = 128.0 / 255.0; let r = srgb_u8_to_linear_table()[128] * a; assert!((lin[0] - r).abs() < 1e-6); assert_eq!(lin[1], 0.0); assert!((lin[3] - a).abs() < 1e-6); - let back = f.to_rgba8(); + let back = f.to_rgba8().unwrap(); assert_eq!(back[0], 128); assert_eq!(back[3], 128); } @@ -356,14 +482,14 @@ mod tests { par: (1, 1), pts: Tick::ZERO, }; - let rgba = f.to_rgba8(); + let rgba = f.to_rgba8().unwrap(); for p in rgba.as_chunks::<4>().0 { assert!((p[0] as i16 - p[1] as i16).abs() <= 1, "{p:?}"); assert!((p[1] as i16 - p[2] as i16).abs() <= 1, "{p:?}"); assert!(p[0] > 90 && p[0] < 160, "{p:?}"); assert_eq!(p[3], 255); } - assert_eq!(f.luma8(), vec![126, 126, 126, 126]); + assert_eq!(f.luma8().unwrap(), vec![126, 126, 126, 126]); assert_eq!(f.format_label(), "YUV 4:4:4 8-bit"); } @@ -383,8 +509,8 @@ mod tests { #[test] fn transparent_float_encodes_as_zero_rgba8() { - let f = VideoFrame::transparent_f32(2, 1); - assert_eq!(f.to_rgba8(), vec![0; 8]); + let f = VideoFrame::transparent_f32(2, 1).unwrap(); + assert_eq!(f.to_rgba8().unwrap(), vec![0; 8]); assert_eq!(f.byte_size(), 32); // 2 pixels × 4 f32 channels × 4 bytes } } From d839db7d4a1a24cb21bcd44162aba1a5055ad2f0 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:06:42 +0100 Subject: [PATCH 334/471] Test odd planar layouts and independent storage validation --- crates/frame/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crates/frame/src/lib.rs b/crates/frame/src/lib.rs index ad294b10f..7f3a993c7 100644 --- a/crates/frame/src/lib.rs +++ b/crates/frame/src/lib.rs @@ -424,6 +424,52 @@ mod tests { assert_eq!(valid.to_linear_f32(), Err(FrameError::InvalidLayout)); } + #[test] + fn odd_subsampled_frames_validate_chroma_and_alpha_independently() { + let make = |bits| VideoFrame { + width: 3, + height: 3, + data: PixelData::Yuv16 { + planes: [Arc::new(vec![128; 9]), Arc::new(vec![128; 4]), Arc::new(vec![128; 4])], + chroma: Chroma::C420, + bits, + alpha: Some(Arc::new(vec![255; 9])), + }, + color: ColorInfo::REC709, + par: (1, 1), + pts: Tick::ZERO, + }; + for bits in [9, 16] { + let mut valid = make(bits); + assert_eq!(valid.to_linear_f32().unwrap().len(), 36); + assert_eq!(valid.to_rgba8().unwrap().len(), 36); + if let PixelData::Yuv16 { planes, .. } = &mut valid.data { + planes[1] = Arc::new(vec![128; 3]); + } + assert_eq!(valid.to_linear_f32(), Err(FrameError::InvalidLayout)); + let mut valid = make(bits); + if let PixelData::Yuv16 { alpha, .. } = &mut valid.data { + *alpha = Some(Arc::new(vec![255; 8])); + } + assert_eq!(valid.to_rgba8(), Err(FrameError::InvalidLayout)); + } + let mut fast = VideoFrame { + width: 3, + height: 3, + data: PixelData::Yuv8 { + planes: [Arc::new(vec![128; 9]), Arc::new(vec![128; 4]), Arc::new(vec![128; 4])], + chroma: Chroma::C420, + alpha: None, + }, + color: ColorInfo::REC709, + par: (1, 1), + pts: Tick::ZERO, + }; + assert_eq!(fast.to_rgba8().unwrap().len(), 36); + fast.height = 4; + assert_eq!(fast.to_rgba8(), Err(FrameError::InvalidLayout)); + } + #[test] fn frame_allocations_are_checked_before_reserving() { assert!(matches!(VideoFrame::transparent_f32(u32::MAX, u32::MAX), Err(FrameError::TooLarge))); From 10a585c3e2f7486b8bbd179edb6dc167e0eaea99 Mon Sep 17 00:00:00 2001 From: FredApps Date: Mon, 16 Nov 2026 17:14:31 +0100 Subject: [PATCH 335/471] fix(psd): preserve consumer resource limits (#786) --- crates/psd/src/descriptor.rs | 60 ++++++++++++++++++----- crates/psd/src/float.rs | 28 ++++++++--- crates/psd/src/smart.rs | 7 ++- crates/psd/tests/resource_limits.rs | 76 +++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 18 deletions(-) create mode 100644 crates/psd/tests/resource_limits.rs diff --git a/crates/psd/src/descriptor.rs b/crates/psd/src/descriptor.rs index 96ad47a7b..1a6f2000e 100644 --- a/crates/psd/src/descriptor.rs +++ b/crates/psd/src/descriptor.rs @@ -12,6 +12,26 @@ mod lookup; /// Maximum nesting depth accepted when parsing (fuzz safety). pub const MAX_DEPTH: usize = 64; +/// Per-container limits for parsing descriptor trees. +/// +/// Limits apply to every nested descriptor and `VlLs` list, before allocation. +/// They are not a total memory budget; byte strings and other value types retain +/// their existing input-length checks. Defaults preserve the unrestricted counts +/// accepted by the original API. Nesting remains bounded by [`MAX_DEPTH`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ParseLimits { + /// Maximum key/value pairs in each descriptor. + pub max_descriptor_items: u32, + /// Maximum values in each `VlLs` list. + pub max_list_items: u32, +} + +impl Default for ParseLimits { + fn default() -> Self { + Self { max_descriptor_items: u32::MAX, max_list_items: u32::MAX } + } +} + /// A Photoshop Unicode string: UTF-16 code units, preserved exactly /// (including any trailing NUL and unpaired surrogates). #[derive(Debug, Clone, PartialEq, Eq, Default, Hash)] @@ -309,6 +329,14 @@ impl Descriptor { Ok((d, r.pos())) } + /// Parses a descriptor prefix with explicit per-container limits. + /// Returns the descriptor and bytes consumed, allowing trailing data. + pub fn parse_prefix_with_limits(data: &[u8], limits: ParseLimits) -> Result<(Self, usize)> { + let mut r = Reader::new(data); + let d = Self::read_with_limits(&mut r, limits)?; + Ok((d, r.pos())) + } + /// Serializes the descriptor. pub fn to_bytes(&self) -> Vec { let mut v = Vec::new(); @@ -317,7 +345,11 @@ impl Descriptor { } pub(crate) fn read(r: &mut Reader<'_>) -> Result { - read_descriptor(r, 0) + Self::read_with_limits(r, ParseLimits::default()) + } + + pub(crate) fn read_with_limits(r: &mut Reader<'_>, limits: ParseLimits) -> Result { + read_descriptor(r, 0, limits) } pub(crate) fn write(&self, out: &mut Vec) { @@ -391,33 +423,36 @@ pub(crate) fn vec_for_count(n: u32) -> Vec { Vec::with_capacity((n as usize).min(MAX_PREALLOC)) } -fn read_descriptor(r: &mut Reader<'_>, depth: usize) -> Result { +fn read_descriptor(r: &mut Reader<'_>, depth: usize, limits: ParseLimits) -> Result { if depth > MAX_DEPTH { return Err(PsdError::LimitExceeded("descriptor nesting too deep")); } let name = UnicodeString::read(r)?; let class_id = Id::read(r)?; let n = r.u32()?; + if n > limits.max_descriptor_items { + return Err(PsdError::LimitExceeded("too many descriptor items")); + } // Each item needs at least 5 (1-byte string key) + 4 (type) + 1 (bool). r.check_count(u64::from(n), 10)?; let mut items = vec_for_count(n); for _ in 0..n { let key = Id::read(r)?; - let v = read_value(r, depth + 1)?; + let v = read_value(r, depth + 1, limits)?; items.push((key, v)); } Ok(Descriptor { name, class_id, items }) } -fn read_value(r: &mut Reader<'_>, depth: usize) -> Result { +fn read_value(r: &mut Reader<'_>, depth: usize, limits: ParseLimits) -> Result { if depth > MAX_DEPTH { return Err(PsdError::LimitExceeded("descriptor nesting too deep")); } let ty = r.array::<4>()?; - read_typed_value(r, &ty, depth) + read_typed_value(r, &ty, depth, limits) } -fn read_typed_value(r: &mut Reader<'_>, ty: &[u8; 4], depth: usize) -> Result { +fn read_typed_value(r: &mut Reader<'_>, ty: &[u8; 4], depth: usize, limits: ParseLimits) -> Result { Ok(match ty { b"obj " => { let n = r.u32()?; @@ -428,14 +463,17 @@ fn read_typed_value(r: &mut Reader<'_>, ty: &[u8; 4], depth: usize) -> Result Value::Descriptor(read_descriptor(r, depth + 1)?), - b"GlbO" => Value::GlobalObject(read_descriptor(r, depth + 1)?), + b"Objc" => Value::Descriptor(read_descriptor(r, depth + 1, limits)?), + b"GlbO" => Value::GlobalObject(read_descriptor(r, depth + 1, limits)?), b"VlLs" => { let n = r.u32()?; + if n > limits.max_list_items { + return Err(PsdError::LimitExceeded("too many descriptor list items")); + } r.check_count(u64::from(n), 4)?; let mut items = vec_for_count(n); for _ in 0..n { - items.push(read_value(r, depth + 1)?); + items.push(read_value(r, depth + 1, limits)?); } Value::List(items) } @@ -472,7 +510,7 @@ fn read_typed_value(r: &mut Reader<'_>, ty: &[u8; 4], depth: usize) -> Result { let prefix = r.u32()?; - Value::ObjectArray(ObjectArray { prefix, body: read_descriptor(r, depth + 1)? }) + Value::ObjectArray(ObjectArray { prefix, body: read_descriptor(r, depth + 1, limits)? }) } other => { return Err(PsdError::Unsupported(format!("descriptor OSType {:?}", String::from_utf8_lossy(other)))); @@ -625,7 +663,7 @@ mod tests { data.extend_from_slice(b"bool\x01"); } let mut r = Reader::new(&data); - match read_typed_value(&mut r, b"VlLs", 0).unwrap() { + match read_typed_value(&mut r, b"VlLs", 0, ParseLimits::default()).unwrap() { Value::List(v) => assert_eq!(v.len(), n as usize), other => panic!("expected list, got {other:?}"), } diff --git a/crates/psd/src/float.rs b/crates/psd/src/float.rs index 6e26cd75b..658538bc2 100644 --- a/crates/psd/src/float.rs +++ b/crates/psd/src/float.rs @@ -436,8 +436,22 @@ impl SmartObject { /// exceed [`MAX_BAKE_PIXELS`], or the mesh needs more rasterising work than eight times the /// output allows (a hostile mesh whose triangles each cover the whole output). pub fn render_placed(&self, content: &FloatImage, k: f64, size: Option<(u32, u32)>) -> Option<(FloatImage, [f64; 2])> { - let (cw, ch) = (f64::from(content.width), f64::from(content.height)); - if content.width == 0 || content.height == 0 { + self.render_placed_pixels(content.width, content.height, &content.data, k, size) + } + + /// Bakes borrowed straight RGBA pixels without copying the source image. + /// Uses the same placement, output and work limits as [`Self::render_placed`]. + /// Missing pixels in a short slice read as transparent; extra pixels are ignored. + pub fn render_placed_pixels( + &self, + width: u32, + height: u32, + data: &[[f32; 4]], + k: f64, + size: Option<(u32, u32)>, + ) -> Option<(FloatImage, [f64; 2])> { + let (cw, ch) = (f64::from(width), f64::from(height)); + if width == 0 || height == 0 { return None; } // The quad is solved once for the bounds, the size and the whole grid. @@ -464,7 +478,7 @@ impl SmartObject { let mut out: Vec<[f32; 4]> = Vec::new(); out.try_reserve_exact(count).ok()?; out.resize(count, [0.0; 4]); - let sampler = Sampler { content, cw, ch }; + let sampler = Sampler { width, height, data, cw, ch }; let idx = |i: usize, j: usize| j * (n + 1) + i; // A sane mesh's triangles each span about one grid cell (ow/n by oh/n), rounded out to // whole pixels. A thin output (16384×1) stacks every grid row onto the same pixel row, so @@ -498,7 +512,9 @@ impl SmartObject { /// Bilinear, premultiplied sampling of the content at content-pixel coordinates. struct Sampler<'a> { - content: &'a FloatImage, + width: u32, + height: u32, + data: &'a [[f32; 4]], cw: f64, ch: f64, } @@ -506,14 +522,14 @@ struct Sampler<'a> { impl Sampler<'_> { fn at(&self, x: usize, y: usize) -> [f32; 4] { // `data` is a public field: a short one reads as transparent. - let p = self.content.data.get(y * self.content.width as usize + x).copied().unwrap_or([0.0; 4]); + let p = y.checked_mul(self.width as usize).and_then(|row| row.checked_add(x)).and_then(|i| self.data.get(i)).copied().unwrap_or([0.0; 4]); [p[0] * p[3], p[1] * p[3], p[2] * p[3], p[3]] } fn sample(&self, x: f64, y: f64) -> [f32; 4] { let (fx, fy) = ((x - 0.5).clamp(0.0, self.cw - 1.0), (y - 0.5).clamp(0.0, self.ch - 1.0)); let (x0, y0) = (fx.floor() as usize, fy.floor() as usize); - let (x1, y1) = ((x0 + 1).min(self.content.width as usize - 1), (y0 + 1).min(self.content.height as usize - 1)); + let (x1, y1) = ((x0 + 1).min(self.width as usize - 1), (y0 + 1).min(self.height as usize - 1)); let (a, b) = ((fx - x0 as f64) as f32, (fy - y0 as f64) as f32); let (p00, p10, p01, p11) = (self.at(x0, y0), self.at(x1, y0), self.at(x0, y1), self.at(x1, y1)); std::array::from_fn(|c| (p00[c] * (1.0 - a) + p10[c] * a) * (1.0 - b) + (p01[c] * (1.0 - a) + p11[c] * a) * b) diff --git a/crates/psd/src/smart.rs b/crates/psd/src/smart.rs index a2195bf5d..d4d98c695 100644 --- a/crates/psd/src/smart.rs +++ b/crates/psd/src/smart.rs @@ -259,11 +259,16 @@ pub struct SmartObject { impl SmartObject { /// Reads the data of a `SoLd` / `SoLE` block. pub fn from_placed_layer_data(data: &[u8]) -> Result { + Self::from_placed_layer_data_with_limits(data, crate::descriptor::ParseLimits::default()) + } + + /// Reads a `SoLd` / `SoLE` block with explicit descriptor container limits. + pub fn from_placed_layer_data_with_limits(data: &[u8], limits: crate::descriptor::ParseLimits) -> Result { let mut r = Reader::new(data); let _kind = r.array::<4>()?; let _version = r.u32()?; let _descriptor_version = r.u32()?; - let d = Descriptor::read(&mut r)?; + let d = Descriptor::read_with_limits(&mut r, limits)?; let uuid = d.get_text("Idnt").unwrap_or_default(); let t = eight(&d, "Trnf").unwrap_or([0.0; 8]); // The non-affine transform when present (perspective), else the transform. diff --git a/crates/psd/tests/resource_limits.rs b/crates/psd/tests/resource_limits.rs new file mode 100644 index 000000000..624aaf287 --- /dev/null +++ b/crates/psd/tests/resource_limits.rs @@ -0,0 +1,76 @@ +use craft_psd::descriptor::{Descriptor, ObjectArray, ParseLimits, Value}; +use craft_psd::float::FloatImage; +use craft_psd::smart::SmartObject; + +#[test] +fn descriptor_limits_apply_at_boundary_and_through_every_nested_container() { + let limits = ParseLimits { max_descriptor_items: 1, max_list_items: 1 }; + let one = Descriptor::new("test").with("one", Value::Boolean(true)); + let two = one.clone().with("two", Value::Boolean(false)); + assert!(Descriptor::parse_prefix_with_limits(&one.to_bytes(), limits).is_ok()); + assert!(Descriptor::parse_prefix_with_limits(&two.to_bytes(), limits).is_err()); + assert!(Descriptor::parse_prefix(&two.to_bytes()).is_ok()); + for nested in [ + Value::Descriptor(two.clone()), + Value::GlobalObject(two.clone()), + Value::ObjectArray(ObjectArray { prefix: 0, body: two.clone() }), + Value::List(vec![Value::Descriptor(two.clone())]), + ] { + let bytes = Descriptor::new("test").with("nest", nested).to_bytes(); + assert!(Descriptor::parse_prefix_with_limits(&bytes, limits).is_err()); + assert!(Descriptor::parse_prefix(&bytes).is_ok()); + } + for n in [0, 1, 2] { + let d = Descriptor::new("test").with("list", Value::List(vec![Value::Boolean(true); n])); + assert_eq!(Descriptor::parse_prefix_with_limits(&d.to_bytes(), limits).is_ok(), n <= 1); + } + let nested_list = Descriptor::new("test").with("list", Value::List(vec![Value::List(vec![Value::Boolean(true); 2])])); + assert!(Descriptor::parse_prefix_with_limits(&nested_list.to_bytes(), limits).is_err()); + let mut bytes = one.to_bytes(); + let used = bytes.len(); + bytes.extend_from_slice(b"tail"); + assert_eq!(Descriptor::parse_prefix_with_limits(&bytes, limits).unwrap().1, used); + let zero = ParseLimits { max_descriptor_items: 0, max_list_items: 0 }; + assert!(Descriptor::parse_prefix_with_limits(&Descriptor::new("test").to_bytes(), zero).is_ok()); + assert!(Descriptor::parse_prefix_with_limits(&one.to_bytes(), zero).is_err()); +} + +#[test] +fn count_limits_reject_before_reading_declared_elements() { + let limits = ParseLimits { max_descriptor_items: 1, max_list_items: 1 }; + let mut bytes = Descriptor::new("test").to_bytes(); + let end = bytes.len(); + bytes[end - 4..].copy_from_slice(&2u32.to_be_bytes()); + assert!(matches!(Descriptor::parse_prefix_with_limits(&bytes, limits), Err(craft_psd::PsdError::LimitExceeded(_)))); + let mut bytes = Descriptor::new("test").with("list", Value::List(vec![])).to_bytes(); + let end = bytes.len(); + bytes[end - 4..].copy_from_slice(&2u32.to_be_bytes()); + assert!(matches!(Descriptor::parse_prefix_with_limits(&bytes, limits), Err(craft_psd::PsdError::LimitExceeded(_)))); +} + +#[test] +fn placed_layer_data_honors_descriptor_limits() { + let d = Descriptor::new("test").with("one", Value::Boolean(true)).with("two", Value::Boolean(false)); + let mut bytes = b"soLD".to_vec(); + bytes.extend_from_slice(&4u32.to_be_bytes()); + bytes.extend_from_slice(&16u32.to_be_bytes()); + bytes.extend_from_slice(&d.to_bytes()); + assert!(SmartObject::from_placed_layer_data(&bytes).is_ok()); + assert!(SmartObject::from_placed_layer_data_with_limits(&bytes, ParseLimits { max_descriptor_items: 1, max_list_items: 1 }).is_err()); +} + +#[test] +fn borrowed_pixels_match_owned_render_and_preserve_short_slice_behavior() { + let smart = SmartObject { uuid: String::new(), quad: [[0.0, 0.0], [2.0, 0.0], [2.0, 2.0], [0.0, 2.0]], size: None, warp: None }; + for data in [vec![], vec![[1.0, 0.5, 0.25, 0.5]], vec![[1.0, 0.5, 0.25, 0.5]; 4]] { + let image = FloatImage { width: 2, height: 2, data }; + let owned = smart.render_placed(&image, 1.0, Some((2, 2))); + let borrowed = smart.render_placed_pixels(2, 2, &image.data, 1.0, Some((2, 2))); + assert!(owned.is_some()); + assert_eq!(owned, borrowed); + } + assert!(smart.render_placed_pixels(0, 2, &[], 1.0, None).is_none()); + assert!(smart.render_placed_pixels(u32::MAX, u32::MAX, &[], 1.0, Some((u32::MAX, u32::MAX))).is_none()); + // A short source remains safely transparent even with enormous declared dimensions. + assert!(smart.render_placed_pixels(u32::MAX, u32::MAX, &[], 1.0, Some((1, 1))).is_some()); +} From ecb92f4554042b2a6a5e7fb07b9b56be8fc67a31 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 16 Nov 2026 11:24:32 -0500 Subject: [PATCH 336/471] timeline: extract attributed allocation-free XML and JSON preflight scans --- crates/timeline/src/lib.rs | 1 + crates/timeline/src/preflight.rs | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 crates/timeline/src/preflight.rs diff --git a/crates/timeline/src/lib.rs b/crates/timeline/src/lib.rs index b638fd3ec..8c409120e 100644 --- a/crates/timeline/src/lib.rs +++ b/crates/timeline/src/lib.rs @@ -34,6 +34,7 @@ pub mod model; #[cfg(feature = "otio")] pub mod otio; pub mod path; +pub mod preflight; pub mod rate; pub mod report; pub mod time; diff --git a/crates/timeline/src/preflight.rs b/crates/timeline/src/preflight.rs new file mode 100644 index 000000000..49995667c --- /dev/null +++ b/crates/timeline/src/preflight.rs @@ -0,0 +1,82 @@ +//! Allocation-free lexical nesting preflight for timeline XML and JSON. +//! +//! Extracted from EffectCraft's interchange reader, originally by Brandon Thomas. +//! These functions preserve its lightweight byte-scanner behavior. They do not validate +//! syntax, expand entities, decode encodings, or replace a bounded format parser. In +//! particular XML self-closing elements do not increase the count, and declarations are +//! skipped lexically rather than parsed as DTDs. Apply document/encoding/entity policy +//! separately; callers choose the maximum acceptable nesting for their parser. + +/// The deepest element nesting of an XML document (a quick scan that skips comments, CDATA, +/// processing instructions, declarations and quoted attribute values). +pub fn xml_depth(b: &[u8]) -> usize { + let (mut i, mut depth, mut max) = (0usize, 0usize, 0usize); + let skip_to = + |from: usize, end: &[u8]| b.get(from..).and_then(|r| r.windows(end.len()).position(|w| w == end)).map_or(b.len(), |p| from + p + end.len()); + while let Some(&c) = b.get(i) { + if c != b'<' { + i += 1; + continue; + } + let rest = b.get(i..).unwrap_or_default(); + if rest.starts_with(b""); + } else if rest.starts_with(b""); + } else if rest.starts_with(b""); + } else if rest.starts_with(b""); + } else if rest.starts_with(b""); + } else { + // A start tag: find its end outside quotes; `/>` closes it at once. + let mut j = i + 1; + let mut quote = None; + while let Some(&c) = b.get(j) { + match quote { + Some(q) if c == q => quote = None, + Some(_) => {} + None if c == b'"' || c == b'\'' => quote = Some(c), + None if c == b'>' => break, + None => {} + } + j += 1; + } + if b.get(j.wrapping_sub(1)) != Some(&b'/') { + depth += 1; + max = max.max(depth); + } + i = j + 1; + } + } + max +} + +/// The deepest array / object nesting of a JSON document (strings skipped). +pub fn json_depth(b: &[u8]) -> usize { + let (mut depth, mut max, mut in_str, mut esc) = (0usize, 0usize, false, false); + for &c in b { + if in_str { + if esc { + esc = false; + } else if c == b'\\' { + esc = true; + } else if c == b'"' { + in_str = false; + } + continue; + } + match c { + b'"' => in_str = true, + b'[' | b'{' => { + depth += 1; + max = max.max(depth); + } + b']' | b'}' => depth = depth.saturating_sub(1), + _ => {} + } + } + max +} From a2560df05b4c58631f53a57e65fb05f0e0e6cf76 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 11:25:03 -0500 Subject: [PATCH 337/471] timeline: cover lexical preflight compatibility and deep inputs --- crates/timeline/tests/preflight.rs | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 crates/timeline/tests/preflight.rs diff --git a/crates/timeline/tests/preflight.rs b/crates/timeline/tests/preflight.rs new file mode 100644 index 000000000..bbe485d67 --- /dev/null +++ b/crates/timeline/tests/preflight.rs @@ -0,0 +1,31 @@ +use craft_timeline::preflight::{json_depth, xml_depth}; + +#[test] +fn xml_skips_markup_inside_quotes_comments_and_cdata() { + let input = br#"]]>"#; + assert_eq!(xml_depth(input), 2); + assert_eq!(xml_depth(b""), 0); +} + +#[test] +fn lexical_scans_do_not_claim_to_validate_malformed_documents() { + assert_eq!(xml_depth(b""), 1); + assert_eq!(xml_depth(b"

"#; + let expected = br#"

"#; + assert_eq!(remap_orders(xml, |n| n.saturating_sub(1)).unwrap().as_ref(), expected); + assert!(matches!(remap_orders(xml, |n| n).unwrap(), std::borrow::Cow::Borrowed(_))); + let late = format!("", (0..128).map(|i| format!("a{i}='x'")).collect::>().join(" ")); + assert_eq!(remap_orders(late.as_bytes(), |n| n - 1).unwrap().as_ref(), late.as_bytes()); + for broken in ["=bad", "x=bad", "x==bad", "x", "x='a' x", "x=\"bad\"", "'odd'", "/odd", "x='a>b'"] { + let input = format!(""); + let mut seen = false; + events(input.as_bytes(), |e| { + if let Ev::Start(_, a) = e { + seen |= get(&a, "draw:z-index") == Some("99"); + } + }); + if seen { + let changed = remap_orders(input.as_bytes(), |n| n - 1).unwrap_or_else(|e| panic!("{input}: {e}")); + let mut found = false; + events(&changed, |e| { + if let Ev::Start(_, a) = e { + found |= get(&a, "draw:z-index") == Some("98"); + } + }); + assert!(found, "{input}"); + } + } + // Lenient malformed attributes are skipped just as in the reader. + let malformed = b""; + assert_eq!(remap_orders(malformed, |n| n - 1).unwrap().as_ref(), b""); + } +} From dca3593452635b2e8c75da2795fda830c3be451f Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan <126534658+scks1@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:37:17 -0500 Subject: [PATCH 350/471] craft-latex: skip the blanks after a control space TeX skips the blanks after a control space, `\ ` or `\` and a tab, as it does after `\` at a line end. The lexer left them to the whitespace branch, which added a second space, so text taken as written, such as a reference or an `\item` label, read `one two`. The control-space branch now skips them the way the line-end branch does, and a blank line after one still ends the paragraph. --- crates/latex/src/lex.rs | 10 ++++++++-- crates/latex/src/tests.rs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/latex/src/lex.rs b/crates/latex/src/lex.rs index 82eabe15f..5040f9d9f 100644 --- a/crates/latex/src/lex.rs +++ b/crates/latex/src/lex.rs @@ -151,8 +151,14 @@ impl Lexer<'_> { } let Some(sym) = self.bump() else { break }; match sym { - // `\^^I`, likewise. - '\t' => self.out.push(Tok::Cmd(" ".to_string())), + // `\ ` and `\^^I` (a tab) are control spaces too. TeX skips the blanks + // after them, but a blank line after one still ends the paragraph. + ' ' | '\t' => { + self.out.push(Tok::Cmd(" ".to_string())); + if self.blanks() >= 2 { + self.out.push(Tok::Par); + } + } '(' => { let m = self.until("\\)"); self.out.push(Tok::Math(m, false)); diff --git a/crates/latex/src/tests.rs b/crates/latex/src/tests.rs index 5f6311094..85fee24e4 100644 --- a/crates/latex/src/tests.rs +++ b/crates/latex/src/tests.rs @@ -304,6 +304,25 @@ fn a_control_space_at_a_line_end_adds_no_second_space() { assert_eq!(texts("\\begin{document}\\ref{one\\\r\n two}\\end{document}"), vec!["one two".to_string()]); } +#[test] +fn blanks_after_a_control_space_add_no_second_space() { + // TeX skips the blanks after `\ ` and after `\` and a tab, but they were read as a space of + // their own, so text taken as written had two spaces. + for cs in ["\\ ", "\\\t"] { + assert_eq!(tokenize(&format!("a{cs} \tb")), vec![Tok::Text("a".into()), Tok::Cmd(" ".into()), Tok::Text("b".into())], "{cs:?}"); + assert_eq!(texts(&format!("\\begin{{document}}\\ref{{one{cs}\n two}}\\end{{document}}")), vec!["one two".to_string()], "{cs:?}"); + assert_eq!(texts(&format!("\\begin{{document}}\\ref{{one{cs} two}}\\end{{document}}")), vec!["one two".to_string()], "{cs:?}"); + let t = texts(&format!("\\begin{{document}}\\begin{{description}}\\item[one{cs} two] x\\end{{description}}\\end{{document}}")); + assert_eq!(t.iter().map(|s| s.trim()).collect::>(), vec!["one two x"], "{cs:?}"); + // A blank line after it still ends the paragraph. + assert_eq!( + texts(&format!("\\begin{{document}}first{cs}\n\nsecond\\end{{document}}")), + vec!["first".to_string(), "second".to_string()], + "{cs:?}" + ); + } +} + #[test] fn a_control_space_in_an_item_label_is_a_space() { // The optional argument kept a control symbol as written, so the label read `one\ two`. From 01fcf22cb019c5f1be92a6d970af038ef84d9afb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 11:23:53 -0800 Subject: [PATCH 351/471] craft-svg: comment markers in a declaration's literal hide no later declaration declared() skipped a comment or CDATA section wherever `"), + _ if rest.starts_with(b" past(b, i + 2, b"?>"), + _ => i + 1, + }; + } + b.len() +} + +/// Past the comment, character data or instruction that starts at `at` inside a declaration's +/// literal ending at `end`, when it closes before `end`. `missed` holds, for each kind, the literal +/// end at which its close was last not found, so a literal full of openers is searched once. +fn closed_in_literal(b: &[u8], at: usize, end: usize, missed: &mut [usize; 3]) -> Option { + const KINDS: [(&[u8], &[u8]); 3] = [(b""), (b""), (b"")]; + let rest = b.get(at..end)?; + let (kind, (open, close)) = KINDS.iter().enumerate().find(|(_, (open, _))| rest.starts_with(open))?; + let miss = missed.get_mut(kind)?; + if *miss == end { + return None; + } + let found = rest.get(open.len()..).and_then(|r| r.windows(close.len()).position(|w| w == *close)).map(|n| at + open.len() + n + close.len()); + if found.is_none() { + *miss = end; + } + found +} + /// The internal general entities declared in `svg`: name → every value declared for it. Text that /// only looks like a declaration in another entity's value is kept too: the largest value counts, /// so it can only make the estimate larger. Comments and CDATA are skipped, so markers there are -/// not treated as declarations. +/// not treated as declarations. A `") { - Some(n) => at + 1 + 3 + n + 3, - None => svg.len(), - }; - continue; - } - if let Some(body) = after.strip_prefix("![CDATA[") { - i = match body.find("]]>") { - Some(n) => at + 1 + 8 + n + 3, - None => svg.len(), - }; - continue; + if at >= literal_end { + if let Some(body) = after.strip_prefix("!--") { + i = match body.find("-->") { + Some(n) => at + 1 + 3 + n + 3, + None => svg.len(), + }; + continue; + } + if let Some(body) = after.strip_prefix("![CDATA[") { + i = match body.find("]]>") { + Some(n) => at + 1 + 8 + n + 3, + None => svg.len(), + }; + continue; + } + if after.starts_with('?') { + literal_end = past(b, at + 2, b"?>"); + } else if after.starts_with('!') { + literal_end = declaration_end(b, at); + } } if let Some(rest) = after.strip_prefix("!ENTITY") { let rest = rest.trim_start(); @@ -199,11 +266,10 @@ pub fn too_wide(svg: &str, most: usize, most_work: usize) -> bool { } else if rest.starts_with(b""); } else if rest.starts_with(b"` closing its subset (the first `>` when it has none). - let end = match (rest.iter().position(|b| *b == b'['), rest.iter().position(|b| *b == b'>')) { - (Some(o), Some(g)) if o < g => find(i, b"]>"), - _ => find(i, b">"), - }; + // To the `>` after the `]` closing its subset (its first `>` when it has none), + // quoted literals, comments and instructions skipped. + let head = declaration_end(data, i); + let end = if data.get(head.wrapping_sub(1)) == Some(&b'[') { find(subset_end(data, head), b">") } else { head }; let quotes = data.get(i..end).unwrap_or(&[]).iter().filter(|b| matches!(**b, b'"' | b'\'')).count(); if over(quotes / 2) { return true; @@ -318,24 +384,40 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { // (now, deepest) of the elements, and of the markup inside the document type. let (mut elements, mut entities, mut open_depth) = ((0usize, 0usize), (0usize, 0usize), 0usize); let mut doctype_end = 0; + // The end of the declaration the scan is in: a `").unwrap_or(b.len()) - } else if rest.starts_with(b"").unwrap_or(b.len()) - } else if rest.starts_with(b"").unwrap_or(b.len()) - } else if rest.starts_with(b"` that ends it: after its internal subset (`[` … `]`) when it has one. - let plain = tag_end(at, false).0; - let subset = b.get(at..plain).is_some_and(|head| head.contains(&b'[')); - doctype_end = if subset { find(at, b"]>").unwrap_or(b.len()) } else { plain }; + } else if let Some(end) = (!open).then(|| closed_in_literal(b, at, literal_end, &mut missed)).flatten() { + // A comment, character data or instruction that closes inside the literal holds no + // element of the entity's markup. + end + } else if open && rest.starts_with(b"`. + let head = declaration_end(b, at); + literal_end = head; + doctype_end = if b.get(head.wrapping_sub(1)) == Some(&b'[') { subset_end(b, head) } else { head }; at + 2 } else if rest.starts_with(b""); + for hidden in [ + format!("{large}]>"), + format!("{large}]>"), + format!(""), + format!("{large}]>"), + format!(""), + format!("{large}]>"), + ] { + assert!(expansion_too_large(&format!("{hidden}"))); + } + assert!(entity_holds_markup("'>]>")); + // A real comment still hides a declaration-like text. + assert!(!entity_holds_markup("'> -->]>")); + } + + #[test] + fn markers_in_a_literal_hide_no_markup_from_the_depth_or_attribute_counts() { + let tenth = DEPTH / 10 + 1; + let nested = format!("{}{}", "".repeat(tenth), "".repeat(tenth)); + for bait in ["", "", "\">"] { + assert!(too_deep(&format!("]>&a;")), "{bait}"); + } + // Comments and instructions in a value open and close no element. + let quiet = format!("", "".repeat(DEPTH)); + assert!(!too_deep(&format!("{}", svg(&groups(8))))); + let closers = format!("", "?></g>".repeat(tenth)); + assert!(too_deep(&format!("&a;"))); + // Openers that never close in the value are read once each. + let openers = format!("", ""), _ if rest.starts_with(b" past(b, i + 2, b"?>"), _ => i + 1, @@ -552,6 +563,7 @@ mod tests { format!("{large}]>"), format!(""), format!("{large}]>"), + format!("{large}]>"), ] { assert!(expansion_too_large(&format!("{hidden}"))); } @@ -564,7 +576,7 @@ mod tests { fn markers_in_a_literal_hide_no_markup_from_the_depth_or_attribute_counts() { let tenth = DEPTH / 10 + 1; let nested = format!("{}{}", "".repeat(tenth), "".repeat(tenth)); - for bait in ["", "", "\">"] { + for bait in ["", "", "\">", ""] { assert!(too_deep(&format!("]>&a;")), "{bait}"); } // Comments and instructions in a value open and close no element. @@ -577,6 +589,8 @@ mod tests { assert!(!too_deep(&format!("{}", svg(&groups(8))))); let many = (0..5000).map(|i| format!(" a{i}=\"1\"")).collect::(); assert!(wide(&format!("\">'>]>&e;"))); + // An element declaration ends at its first `>`, so a `]` inside it does not end the subset. + assert!(wide(&format!("'>]>&e;"))); } #[test] From 227ea620b1388a8e7ac62fcb3b2051af2fc152e2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 12:05:10 -0800 Subject: [PATCH 355/471] craft-metafile: count refused bitmaps and lost pen textures, empty line clips - A bitmap that the item or byte limit refuses is counted in dropped_images once, however many pieces a clip cuts it into. - An EMF+ pen whose texture brush was not read is counted in dropped_images, as a lost texture brush object is. - An EMF+ clip path or region path whose figures all lie on a line encloses no area, so it gives an empty area instead of its bounding rectangle. --- crates/metafile/README.md | 2 +- crates/metafile/src/canvas.rs | 5 +++++ crates/metafile/src/emfplus.rs | 16 ++++++++++++---- crates/metafile/src/lib.rs | 8 ++++---- crates/metafile/src/tests.rs | 28 ++++++++++++++++++++++++++++ crates/metafile/src/tests/model.rs | 30 ++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 9 deletions(-) diff --git a/crates/metafile/README.md b/crates/metafile/README.md index 4e246de51..226c956ad 100644 --- a/crates/metafile/README.md +++ b/crates/metafile/README.md @@ -39,7 +39,7 @@ differently: | Field | Reports | |---|---| | `truncated` | The parse stopped at a limit that ends it (items, bytes kept, `SAVEDC` depth, clip paths), in the file or in an EMF+ metafile image drawn in it. | -| `dropped_images` | Images that were not read: bitmaps past a size, byte or decode limit or not decodable, EMF+ metafile images that do not parse or are nested too deep, and pattern or texture brushes whose bitmap was not read (counted when the brush is created). An image with no pixels, or drawn from a source rectangle that selects none, is not counted. | +| `dropped_images` | Images that were not read: bitmaps past a size, item, byte or decode limit or not decodable, EMF+ metafile images that do not parse or are nested too deep, and pattern or texture brushes whose bitmap was not read, a pen's brush included (counted when the brush or pen is created). An image with no pixels, or drawn from a source rectangle that selects none, is not counted. | | `ignored_clips` | Clip changes not applied as the file asks: `SELECTCLIPPATH` with `RGN_OR`, `RGN_XOR` or `RGN_DIFF`, EMF+ clips combined with XOR, exclude or complement, EMF+ unions drawn as the rectangle around both areas, and EMF+ clips whose area stands in for the one the file gives (a path that is not one convex figure, drawn as its rectangle, or a region built with XOR, exclude or complement). A change that cannot alter the clip is not counted: an area whose figures all lie on a line (except in an EMF+ complement of a clip that encloses some area, which would empty it), `RGN_OR` on a clip that is still the whole surface, and a union whose rectangle is no larger than the union. | | `unplayed_raster_ops` | Raster operations not played: bitmaps drawn as plain copies because their operation is not one of `SRCCOPY`, `SRCAND`, `SRCPAINT` and `SRCINVERT`, and operations without a source (such as `DSTINVERT` or `PATINVERT`) that are left out. | diff --git a/crates/metafile/src/canvas.rs b/crates/metafile/src/canvas.rs index 67cc43f0f..916e8ac9a 100644 --- a/crates/metafile/src/canvas.rs +++ b/crates/metafile/src/canvas.rs @@ -1335,7 +1335,12 @@ impl Canvas { let (x0, x1) = (xs.iter().copied().fold(f64::INFINITY, f64::min), xs.iter().copied().fold(f64::NEG_INFINITY, f64::max)); let (y0, y1) = (ys.iter().copied().fold(f64::INFINITY, f64::min), ys.iter().copied().fold(f64::NEG_INFINITY, f64::max)); let dev = if xf[1] == 0.0 && xf[2] == 0.0 { dev } else { [x0, y0, x1 - x0, y1 - y0] }; + let was_full = self.full; self.push(Raw::Bitmap { rect: dev, xf, img, region: RawClip::default() }); + // A bitmap the item or byte limit refuses is counted once, however many pieces a clip cuts it into. + if self.full && !was_full { + self.drop_image(); + } } /// Draws a bitmap that a record asked to combine with raster operation `rop` (already applied by diff --git a/crates/metafile/src/emfplus.rs b/crates/metafile/src/emfplus.rs index 32786bc5f..505149371 100644 --- a/crates/metafile/src/emfplus.rs +++ b/crates/metafile/src/emfplus.rs @@ -1181,7 +1181,11 @@ fn pen(r: Bytes) -> Option { off += 4 + (r.u32(off)? as usize).min(1 << 20); } } - let color = brush(r, off)?.color(); + // A pen whose texture brush was not read is counted like a lost brush object. + let Some(fill) = brush(r, off) else { + return lost_texture(r.tail(off)?).then_some(Obj::LostBrush); + }; + let color = fill.color(); Some(Obj::Pen { color, width: if width.is_finite() { width.clamp(0.0, 1.0e5) } else { 0.0 } }) } @@ -1290,16 +1294,20 @@ fn region_node(r: Bytes, off: usize, depth: usize, work: &mut usize) -> Option<( } } -/// The area a path encloses: its outline when that is one convex figure, else the rectangle -/// around it. The flag is set when the rectangle stands in for a path that encloses some area. +/// The area a path encloses: its outline when that is one convex figure, none when its figures all +/// lie on a line, else the rectangle around it. The flag is set when the rectangle stands in for +/// the path. fn area_of_ops(ops: &[Op]) -> (Vec

, bool) { + if !crate::geom::encloses_area(ops) { + return (Vec::new(), false); + } let subs = flatten(ops); if let [(pts, _)] = subs.as_slice() && let Some(c) = convex(pts) { return (c, false); } - (ops_bbox(ops).map(rect_poly).unwrap_or_default(), crate::geom::encloses_area(ops)) + (ops_bbox(ops).map(rect_poly).unwrap_or_default(), true) } /// Two areas' intersection, and whether it ran out of work and kept the first area instead. diff --git a/crates/metafile/src/lib.rs b/crates/metafile/src/lib.rs index 6a2338a78..26a406d31 100644 --- a/crates/metafile/src/lib.rs +++ b/crates/metafile/src/lib.rs @@ -78,10 +78,10 @@ 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, - /// Images the file asks to draw that the reader did not read: bitmaps past a size, 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 (counted - /// when the brush is created). An image whose source rectangle selects none of its pixels, or that + /// 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 + /// pen's brush included (counted when the brush or pen is created). An image whose source rectangle selects none of its pixels, or that /// has no pixels, draws nothing and is not counted. Bitmaps left out while clipping to an EMF+ /// clip (see the README's limits) are not counted here. pub dropped_images: u32, diff --git a/crates/metafile/src/tests.rs b/crates/metafile/src/tests.rs index fa1cbb33a..9f929108b 100644 --- a/crates/metafile/src/tests.rs +++ b/crates/metafile/src/tests.rs @@ -486,6 +486,34 @@ fn canvas_stops_at_the_byte_budget() { assert_eq!(cv.items.len(), 2); } +#[test] +fn a_bitmap_the_byte_budget_refuses_is_counted_once() { + let img = || Rgba { w: 4, h: 4, px: vec![0u8; 64] }; + let diamond = || Some(vec![(2.0, 0.0), (4.0, 2.0), (2.0, 4.0), (0.0, 2.0)]); + let mut cv = Canvas::new(); + cv.set_clip(diamond()); + cv.bitmap([0.0, 0.0, 4.0, 4.0], img()); + assert!(!cv.full() && !cv.items.is_empty() && cv.dropped_images == 0, "a bitmap that is kept"); + // The clip cuts the bitmap into pieces, and none of them fits: one image is counted. + let mut cv = Canvas::new(); + cv.hold(MAX_BYTES - 1); + cv.set_clip(diamond()); + cv.bitmap([0.0, 0.0, 4.0, 4.0], img()); + assert!(cv.full() && cv.items.is_empty()); + assert_eq!(cv.dropped_images, 1); + // A clip that leaves none of the bitmap refuses nothing. + let mut cv = Canvas::new(); + cv.hold(MAX_BYTES - 1); + cv.set_clip(Some(vec![(100.0, 100.0), (110.0, 100.0), (110.0, 110.0)])); + cv.bitmap([0.0, 0.0, 4.0, 4.0], img()); + assert!(!cv.full() && cv.dropped_images == 0); + // A bitmap drawn with a raster operation is counted the same way. + let mut cv = Canvas::new(); + cv.hold(MAX_BYTES - 1); + cv.bitmap_rop([0.0, 0.0, 4.0, 4.0], img(), 0x00CC_0020); + assert!(cv.full() && cv.dropped_images == 1 && cv.unplayed_raster_ops == 0); +} + #[test] fn oversized_dib_header_size_is_rejected_without_overflow() { let mut info = info_header(2, 2, 24, 16); diff --git a/crates/metafile/src/tests/model.rs b/crates/metafile/src/tests/model.rs index 73186cb4c..5053606c4 100644 --- a/crates/metafile/src/tests/model.rs +++ b/crates/metafile/src/tests/model.rs @@ -485,6 +485,17 @@ fn pattern_and_texture_brushes_that_are_not_read_are_counted() { let none = cat(vec![h32(0xDBC0_1002), h32(1), h32(0), h32(4), h32(16), h32(0x0026_200A), h32(0)]); assert_eq!(parse(&texture(none)).unwrap().dropped_images, 0); assert_eq!(parse(&texture(bitmap_2x2())).unwrap().dropped_images, 0); + // A pen whose brush is such a texture brush. + let pen = |image: Vec| { + let brush = cat(vec![h32(0xDBC0_1002), h32(2), h32(0), h32(0), image]); + let pen = cat(vec![h32(0xDBC0_1002), h32(0), h32(0), h32(0), f32s(&[1.0]), brush]); + emf(&[plus_comment(&[plus_header(false), prec(0x4008, 0x0200 | 5, &pen)])], [0; 4], [0; 4]) + }; + let short = cat(vec![h32(0xDBC0_1002), h32(1), h32(4), h32(4), h32(16), h32(0x0026_200A), h32(0), vec![1, 2, 3]]); + assert_eq!(parse(&pen(short)).unwrap().dropped_images, 1); + let none = cat(vec![h32(0xDBC0_1002), h32(1), h32(0), h32(4), h32(16), h32(0x0026_200A), h32(0)]); + assert_eq!(parse(&pen(none)).unwrap().dropped_images, 0); + assert_eq!(parse(&pen(bitmap_2x2())).unwrap().dropped_images, 0); } #[test] @@ -643,6 +654,25 @@ fn emfplus_clips_that_are_not_applied_are_counted() { assert_eq!(plus(vec![clip(1, [0.0, 0.0, 50.0, 50.0]), clip(0, [1.0, 1.0, 5.0, 5.0])]), 0, "intersect and replace are applied"); } +#[test] +fn an_emfplus_clip_on_a_diagonal_line_encloses_nothing() { + let fill = prec(0x400A, 0x8000, &cat(vec![h32(0xFF00_0000), h32(1), f32s(&[0.0, 0.0, 100.0, 100.0])])); + let drawn = |clip: Vec>| { + let mut all = vec![plus_header(false)]; + all.extend(clip); + all.push(fill.clone()); + let p = parse(&emf(&[plus_comment(&all)], [0; 4], [0; 4])).unwrap(); + (p.items.len(), p.ignored_clips) + }; + let line = cat(vec![h32(0xDBC0_1002), h32(2), h32(0), f32s(&[0.0, 0.0, 10.0, 10.0]), vec![0, 1]]); + // SetClipPath with the line replaces the clip with nothing. + assert_eq!(drawn(vec![prec(0x4008, 0x0300 | 9, &line), prec(0x4033, 9, &[])]), (0, 0)); + // A region whose one node is the line. + let node = cat(vec![h32(0x1000_0001), h32(line.len() as u32), line]); + let region = cat(vec![h32(0xDBC0_1002), h32(1), node]); + assert_eq!(drawn(vec![prec(0x4008, 0x0400 | 6, ®ion), prec(0x4034, 6, &[])]), (0, 0)); +} + #[test] fn a_parse_stopped_at_a_limit_is_truncated() { assert!(!parse_emf(&[rect(0, 0, 10, 10)]).truncated); From 443a2b05ffa2588b7737f70b913126c7ad1899ea Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 15:51:23 -0500 Subject: [PATCH 356/471] docx: preserve partial table margins in current and tracked properties (#1924) --- crates/docx/src/read/props.rs | 11 ++++++ crates/docx/src/read/story/blocks.rs | 27 +++++++++++++- .../docx/tests/it/cell_margin_inheritance.rs | 15 ++++++++ crates/docx/tests/it/main.rs | 1 + crates/docx/tests/it/partial_table_margins.rs | 36 +++++++++++++++++++ 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 crates/docx/tests/it/partial_table_margins.rs diff --git a/crates/docx/src/read/props.rs b/crates/docx/src/read/props.rs index 4d86ae164..618ef8c10 100644 --- a/crates/docx/src/read/props.rs +++ b/crates/docx/src/read/props.rs @@ -562,6 +562,17 @@ pub fn margins(e: &El) -> [f32; 4] { margin_sides(e).map(|side| side.unwrap_or(0.0)) } +/// Word's own cell margins (the Normal Table style): 0.08" left and right, none above or below. +pub const WORD_CELL_MARGINS: [f32; 4] = [0.0, 5.4, 0.0, 5.4]; + +/// A table's `w:tblCellMar` over the margins it would have without one (its +/// style's, else Word's). A side the element leaves out keeps the underlying margin, as in Word: +/// a form table that sets only `w:left` keeps its style's right margin, not none. +pub fn margins_over(e: &El, base: [f32; 4]) -> [f32; 4] { + let sides = margin_sides(e); + std::array::from_fn(|i| sides.get(i).copied().flatten().or_else(|| base.get(i).copied()).unwrap_or(0.0)) +} + fn margin_sides(e: &El) -> [Option; 4] { let get = |names: &[&str]| names.iter().find_map(|n| e.child(n)).and_then(|c| tw(c, "w:w")).map(|v| v.clamp(0.0, 1584.0)); [get(&["w:top"]), get(&["w:left", "w:start"]), get(&["w:bottom"]), get(&["w:right", "w:end"])] diff --git a/crates/docx/src/read/story/blocks.rs b/crates/docx/src/read/story/blocks.rs index 878429454..91c54714f 100644 --- a/crates/docx/src/read/story/blocks.rs +++ b/crates/docx/src/read/story/blocks.rs @@ -333,12 +333,37 @@ impl Reader<'_> { } } + /// Omitted table-margin sides inherit from that property set's style. + fn table_margin_base(&self, style: Option<&str>) -> [f32; 4] { + style.and_then(|id| self.doc.styles.table_style(id)).and_then(|st| st.parts.cell_margins).unwrap_or(if self.no_default_table_style { + [0.0; 4] + } else { + crate::read::props::WORD_CELL_MARGINS + }) + } + fn read_table(&mut self, sc: &mut StoryCtx, t: &El, rels: &Rels, depth: usize) -> Option

{ let mut table = Table::default(); if let Some(p) = t.child("w:tblPr") { table.props = self.pc.tblpr(p); + // Sides `w:tblCellMar` leaves out keep the style's margins; without a style, Word's + // 0.08" left and right, or none in a file with no default table style. + if let Some(m) = p.child("w:tblCellMar") { + let base = self.table_margin_base(table.props.style.as_deref()); + table.props.cell_margins = Some(crate::read::props::margins_over(m, base)); + } if let Some(ch) = p.child("w:tblPrChange") { - let mut old = ch.child("w:tblPr").map(|o| self.pc.tblpr(o)).unwrap_or_default(); + let mut old = ch + .child("w:tblPr") + .map(|o| { + let mut props = self.pc.tblpr(o); + if let Some(m) = o.child("w:tblCellMar") { + let base = self.table_margin_base(props.style.as_deref()); + props.cell_margins = Some(crate::read::props::margins_over(m, base)); + } + props + }) + .unwrap_or_default(); // Rejecting the change brings back these properties: with the same margins. self.no_default_margins(&mut old); table.props.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, ch), old); diff --git a/crates/docx/tests/it/cell_margin_inheritance.rs b/crates/docx/tests/it/cell_margin_inheritance.rs index 83aafc7d0..dc5cba644 100644 --- a/crates/docx/tests/it/cell_margin_inheritance.rs +++ b/crates/docx/tests/it/cell_margin_inheritance.rs @@ -98,3 +98,18 @@ fn no_default_table_style_means_no_cell_margins() { let reopened = craft_docx::read(&bytes).unwrap(); assert_eq!(reopened.body[0].as_table().unwrap().props.cell_margins, Some([0.0; 4])); } + +#[test] +fn tracked_partial_table_margins_keep_the_old_styles_other_sides() { + let body = r#""#; + let styles = r#""#; + let doc = craft_docx::read(&package(body, Some(styles))).unwrap(); + let table = doc.body[0].as_table().unwrap(); + assert_eq!(table.props.cell_margins, Some([3.0, 5.0, 0.0, 9.0])); + let old = &table.props.fmt_change.as_ref().unwrap().old; + assert_eq!( + old.cell_margins, + Some([1.0, 0.0, 2.0, 8.0]), + "explicit zero overrides left, omitted sides come from the old style, not the current one" + ); +} diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index 8d6be8a97..6d521effb 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -29,6 +29,7 @@ mod malformed; mod missing_pictures; mod nested_block_controls; mod note_comments; +mod partial_table_margins; mod passthrough; mod picture_links; mod positional_tabs; diff --git a/crates/docx/tests/it/partial_table_margins.rs b/crates/docx/tests/it/partial_table_margins.rs new file mode 100644 index 000000000..7cf3c5f49 --- /dev/null +++ b/crates/docx/tests/it/partial_table_margins.rs @@ -0,0 +1,36 @@ +//! a table's partial cell margins keep the other sides. + +use std::io::Write; + +#[test] +fn a_left_only_table_margin_keeps_the_other_sides() { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; + let root = r#""#; + let rels = r#""#; + let styles = format!( + r#""# + ); + let table = |style: &str| { + format!( + r#"{style}Cell"# + ) + }; + let document = format!(r#"{}{}"#, table(r#""#), table("")); + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for (name, data) in [ + ("_rels/.rels", root), + ("word/_rels/document.xml.rels", rels), + ("word/document.xml", document.as_str()), + ("word/styles.xml", styles.as_str()), + ] { + zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + zip.write_all(data.as_bytes()).unwrap(); + } + let doc = craft_docx::read(&zip.finish().unwrap().into_inner()).unwrap(); + let margins: Vec<_> = doc.body.iter().filter_map(|b| b.as_table()).map(|t| t.props.cell_margins).collect(); + assert_eq!( + margins, + vec![Some([1.0, 5.35, 2.0, 8.0]), Some([0.0, 5.35, 0.0, 0.0])], + "the left side is the table's, the rest the style's (none without one: the file has no default table style)" + ); +} From 20220807a4c33a35a72c7b4dfad90edd29205e34 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 15:51:27 -0500 Subject: [PATCH 357/471] psd: preserve PhotoCraft lenient TySh prefix parsing (#1923) --- crates/psd/src/text/envelope.rs | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/crates/psd/src/text/envelope.rs b/crates/psd/src/text/envelope.rs index 73345fa83..16fac8737 100644 --- a/crates/psd/src/text/envelope.rs +++ b/crates/psd/src/text/envelope.rs @@ -29,6 +29,33 @@ pub fn transform_is_safe(t: &[f64; 6]) -> bool { t.iter().all(|v| v.is_finite() && v.abs() <= f64::from(i32::MAX)) } +/// Recovers the text and raw transform of a `TySh` prefix, even if its descriptor is damaged. +/// +/// This is PhotoCraft's preservation fallback, not a rendering-ready type layer. It keeps +/// line endings, the trailing paragraph terminator and non-finite transform values verbatim. +/// Callers must validate geometry before rendering. A complete 52-byte prefix is required; +/// a missing, unsupported or malformed text descriptor produces empty text. Warp and bounds +/// are not interpreted. Above [`MAX_BYTES`], the descriptor is not parsed and text is empty, +/// retaining the fixed transform so consumers can preserve the original block without decoding +/// an oversized descriptor. Inputs within that cap retain the shared parser's allocation limits. +pub fn parse_tysh_text_prefix(data: &[u8]) -> Option<(String, [f64; 6])> { + let f = |at: usize| data.get(at..at + 8).map(|b| f64::from_be_bytes(b.try_into().unwrap_or([0; 8]))); + let mut m = [0.0; 6]; + for (i, v) in m.iter_mut().enumerate() { + *v = f(2 + i * 8)?; + } + // text version (2) at 50, descriptor version (4) at 52, descriptor at 56. + let rest = data.get(52..)?; + if data.len() > MAX_BYTES { + return Some((String::new(), m)); + } + let text = VersionedDescriptor::parse_prefix(rest).ok().and_then(|(d, _)| match d.descriptor.get("Txt ") { + Some(D::Text(t)) => Some(t.to_string_lossy()), + _ => None, + }); + Some((text.unwrap_or_default(), m)) +} + /// Parses a `TySh` block. pub fn parse_tysh(data: &[u8]) -> Option { if data.len() > MAX_BYTES { @@ -246,6 +273,54 @@ mod tests { bounds: [-1.0, 2.0, 30.0, 40.0], } } + #[test] + fn recovery_prefix_preserves_text_and_ignores_damaged_tail() { + let mut t = sample(); + t.text = t.text.with("Txt ", D::Text(UnicodeString::new_nul("Hello\r\nWorld\r"))); + let mut bytes = write_tysh(&t).unwrap(); + let (_, n) = VersionedDescriptor::parse_prefix(&bytes[52..]).unwrap(); + bytes.truncate(52 + n); + bytes.extend_from_slice(b"broken warp"); + assert_eq!(parse_tysh_text_prefix(&bytes), Some(("Hello\r\nWorld\r".into(), t.transform))); + } + + #[test] + fn recovery_prefix_needs_only_complete_fixed_header() { + let bytes = write_tysh(&sample()).unwrap(); + for cut in 0..52 { + assert!(parse_tysh_text_prefix(&bytes[..cut]).is_none(), "{cut}"); + } + for cut in 52..56 { + assert_eq!(parse_tysh_text_prefix(&bytes[..cut]), Some((String::new(), sample().transform))); + } + let mut unsupported = bytes.clone(); + unsupported[52..56].copy_from_slice(&99u32.to_be_bytes()); + assert_eq!(parse_tysh_text_prefix(&unsupported), Some((String::new(), sample().transform))); + assert_eq!(parse_tysh_text_prefix(&bytes), Some((String::new(), sample().transform))); + } + + #[test] + fn recovery_prefix_bounds_descriptor_work_without_losing_preservation_geometry() { + let mut t = sample(); + t.text = t.text.with("Txt ", D::Text(UnicodeString::new_nul("kept"))); + let mut bytes = write_tysh(&t).unwrap(); + bytes.resize(MAX_BYTES, 0); + assert_eq!(parse_tysh_text_prefix(&bytes), Some(("kept".into(), t.transform))); + bytes.push(0); + assert_eq!(parse_tysh_text_prefix(&bytes), Some((String::new(), t.transform))); + assert!(parse_tysh(&bytes).is_none()); + } + + #[test] + fn recovery_prefix_retains_geometry_but_is_not_render_validation() { + let mut bytes = write_tysh(&sample()).unwrap(); + bytes[2..10].copy_from_slice(&f64::NAN.to_be_bytes()); + let (_, transform) = parse_tysh_text_prefix(&bytes).unwrap(); + assert!(transform[0].is_nan()); + assert!(!transform_is_safe(&transform)); + assert!(parse_tysh(&bytes).is_none()); + } + #[test] fn roundtrip_preserves_descriptors_transform_and_bounds() { let t = sample(); From 617a3cea3ba6a7fbad366b7e834109d9648376cc Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:51:32 +0100 Subject: [PATCH 358/471] Add craft-media-source: MediaSource, open_bytes, and open_reader (#1891) --- Cargo.lock | 14 + Cargo.toml | 1 + crates/media-source/Cargo.toml | 23 ++ crates/media-source/src/lib.rs | 563 +++++++++++++++++++++++++++++++++ 4 files changed, 601 insertions(+) create mode 100644 crates/media-source/Cargo.toml create mode 100644 crates/media-source/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9275d4646..8679e4eac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2281,6 +2281,20 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-media-source" +version = "0.1.0" +dependencies = [ + "craft-audio", + "craft-color", + "craft-frame", + "craft-media", + "craft-time", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-media-testkit" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index bdcb8201d..c8ae36287 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -119,6 +119,7 @@ craft-macho = { path = "crates/macho" } craft-math = { path = "crates/math" } # No default features: craft-audio turns on the craft-media modules each of its features needs. craft-media = { path = "crates/media", default-features = false } +craft-media-source = { path = "crates/media-source" } craft-materialx = { path = "crates/materialx" } craft-media-testkit = { path = "crates/media-testkit" } craft-midi = { path = "crates/midi" } diff --git a/crates/media-source/Cargo.toml b/crates/media-source/Cargo.toml new file mode 100644 index 000000000..3edda0b2e --- /dev/null +++ b/crates/media-source/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "craft-media-source" +description = "Compositor MediaSource trait and a pluggable open_bytes / open_reader registry" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-audio = { workspace = true } +craft-color = { workspace = true } +craft-frame = { workspace = true } +craft-media = { workspace = true } +craft-time = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/media-source/src/lib.rs b/crates/media-source/src/lib.rs new file mode 100644 index 000000000..c27aea688 --- /dev/null +++ b/crates/media-source/src/lib.rs @@ -0,0 +1,563 @@ +//! Compositor media sources and a pluggable opener registry. +//! +//! This crate is the EffectCraft / FilmCraft opener hub: a [`MediaSource`] yields compositor +//! [`VideoFrame`]s and [`AudioBuffer`]s, and [`open_bytes`] / [`open_reader`] try caller-supplied +//! openers in order. It lives in its own crate rather than `craft-media` so it can depend on +//! `craft-audio` without a cycle (`craft-audio` already depends on `craft-media`). +//! +//! Built-in still-image, WAV, and generator openers stay in the apps. [`open_bytes`] only runs the +//! `extra` openers; [`open_reader`] tries `reader_openers` and otherwise reads the file whole +//! (up to `max_whole`) and falls back to [`open_bytes`]. Container sources register themselves +//! from later PRs. +//! +//! [`SharedSource`] is `Arc`. It is not `craft_audio::SharedSource`, which is a +//! `ByteSource` marker used by the audio decoder. + +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] + +use std::io; +use std::path::Path; +use std::sync::Arc; + +use craft_audio::AudioBuffer; +use craft_color::{ColorInfo, HdrMetadata}; +use craft_frame::VideoFrame; +use craft_media::ByteSource; +use craft_time::{FrameRate, Tick}; +use serde::{Deserialize, Serialize}; + +/// Errors from opening or reading a media source. +#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)] +pub enum MediaError { + #[error("unsupported media: {0}")] + Unsupported(String), + #[error("I/O error: {0}")] + Io(String), + #[error("decode error: {0}")] + Decode(String), + #[error("no {0} stream")] + NoStream(&'static str), + #[error("media offline: {0}")] + Offline(String), + /// The request was cancelled. + #[error("cancelled")] + Cancelled, +} + +impl From for MediaError { + fn from(e: io::Error) -> Self { + MediaError::Io(e.to_string()) + } +} + +pub type Result = std::result::Result; + +/// Kind of media a source reports. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum MediaKind { + /// Video, possibly with audio. + Movie, + AudioOnly, + Still, + ImageSequence, + Synthetic, +} + +/// Video stream metadata a source reports. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct VideoStreamInfo { + pub width: u32, + pub height: u32, + pub frame_rate: FrameRate, + pub par: (u32, u32), + pub codec: String, + pub pixel_format: String, + pub color: ColorInfo, + pub has_alpha: bool, + /// Bitrate in bits/s when known. + pub bitrate: Option, + /// HDR static metadata when the file carries it. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub hdr: Option, +} + +/// Most audio streams a source reports (the count comes from the file: never trusted). +pub const MAX_AUDIO_STREAMS: usize = 64; + +/// Audio stream metadata a source reports. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct AudioStreamInfo { + pub sample_rate: u32, + pub channels: u32, + pub codec: String, + pub bits_per_sample: Option, +} + +/// Identity and streams of an open source. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct MediaInfo { + pub name: String, + pub kind: MediaKind, + /// Media duration. Stills typically report a default duration chosen by the app. + pub duration: Tick, + pub video: Option, + /// Every audio stream the container carries, in file order. Stream 0 is the one + /// [`MediaSource::audio`] reads; the others are read with [`MediaSource::audio_stream`]. + /// Project files written before multi-stream support hold a single `audio` object (or + /// `null`): both still load. + #[serde(default, alias = "audio", deserialize_with = "deserialize_audio_streams")] + pub audio_streams: Vec, + pub container: String, + /// Timecode of the first frame (in frames at `video.frame_rate`), if the file carries one. + pub start_timecode: Option, + pub file_size: Option, +} + +/// Reads `audio_streams` from a list, or from the single optional stream older project files wrote. +fn deserialize_audio_streams<'de, D: serde::Deserializer<'de>>(d: D) -> std::result::Result, D::Error> { + struct StreamsVisitor; + struct RejectExtra; + impl<'de> serde::de::DeserializeSeed<'de> for RejectExtra { + type Value = (); + fn deserialize>(self, _d: D) -> std::result::Result<(), D::Error> { + Err(serde::de::Error::custom("audio stream count exceeds MAX_AUDIO_STREAMS")) + } + } + impl<'de> serde::de::Visitor<'de> for StreamsVisitor { + type Value = Vec; + fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("at most 64 audio streams, a legacy audio object, or null") + } + fn visit_unit(self) -> std::result::Result { + Ok(Vec::new()) + } + fn visit_none(self) -> std::result::Result { + Ok(Vec::new()) + } + fn visit_map>(self, map: A) -> std::result::Result { + AudioStreamInfo::deserialize(serde::de::value::MapAccessDeserializer::new(map)).map(|stream| vec![stream]) + } + fn visit_seq>(self, mut seq: A) -> std::result::Result { + let mut streams = Vec::new(); + // Do not reserve from an untrusted size hint or buffer an untagged sequence. + while streams.len() < MAX_AUDIO_STREAMS { + let Some(stream) = seq.next_element()? else { return Ok(streams) }; + streams.push(stream); + } + // Detect an excess entry without deserializing or allocating its contents. + seq.next_element_seed(RejectExtra)?; + Ok(streams) + } + } + d.deserialize_any(StreamsVisitor) +} + +impl MediaInfo { + pub fn frame_rate(&self) -> FrameRate { + self.video.as_ref().map(|v| v.frame_rate).unwrap_or_default() + } + pub fn has_video(&self) -> bool { + self.video.is_some() + } + pub fn has_audio(&self) -> bool { + !self.audio_streams.is_empty() + } + /// The primary audio stream (stream 0), the one [`MediaSource::audio`] reads. + pub fn audio(&self) -> Option<&AudioStreamInfo> { + self.audio_streams.first() + } +} + +/// A request for a video frame. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct FrameRequest { + /// Media time. + pub time: Tick, + /// Desired scale relative to full resolution (1.0 = full). Sources may return a larger frame. + pub scale: f32, +} + +impl FrameRequest { + pub fn full(time: Tick) -> Self { + Self { time, scale: 1.0 } + } +} + +/// A decoded or generated media source that can yield compositor frames and audio. +pub trait MediaSource: Send + Sync { + fn info(&self) -> &MediaInfo; + /// The frame displayed at media time `req.time`. + fn video_frame(&self, req: FrameRequest) -> Result>; + /// `frames` audio frames starting at sample index `start` (at `sample_rate`, media time). + fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> Result; + /// Like [`audio`](Self::audio) for audio stream `stream` (an index into + /// [`MediaInfo::audio_streams`]). Sources with a single audio stream need not override this: + /// stream 0 is `audio`, any other index is [`MediaError::NoStream`]. + fn audio_stream(&self, stream: usize, start: i64, frames: usize, sample_rate: u32) -> Result { + if stream == 0 { self.audio(start, frames, sample_rate) } else { Err(MediaError::NoStream("audio")) } + } +} + +/// Shared compositor source. Distinct from [`craft_audio::SharedSource`], a `ByteSource` marker. +pub type SharedSource = Arc; + +/// Random-access bytes used by [`open_reader`]. +pub type SharedBytes = Arc; + +/// File extensions recognised at import (the media browser filters on these). +pub const STILL_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "tif", "tiff", "bmp", "tga", "dpx", "exr", "hdr", "dng"]; +pub const AUDIO_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "mp3", "mp2", "m4a", "aac", "flac", "ogg", "opus"]; +pub const VIDEO_EXTENSIONS: &[&str] = &[ + "mp4", "m4v", "mov", "mkv", "webm", "avi", "mxf", "mts", "m2ts", "m2t", "ts", "mpg", "mpeg", "vob", "mod", "tod", "m2v", "m1v", "mpv", "3gp", + "y4m", "apv", +]; + +/// True when `path` has a still, audio, or video extension we recognise, ignoring case. +pub fn is_importable(path: &Path) -> bool { + let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); + STILL_EXTENSIONS.contains(&ext.as_str()) || AUDIO_EXTENSIONS.contains(&ext.as_str()) || VIDEO_EXTENSIONS.contains(&ext.as_str()) +} + +/// A pluggable in-memory opener. Returns `None` when the format is not this opener's. +pub type Opener = fn(name: &str, bytes: Arc<[u8]>) -> Option>; + +/// Opens a container from a reader: `head` is the first bytes of the file (for sniffing). +/// Returns `None` when the format is not this opener's. +pub type ReaderOpener = fn(name: &str, head: &[u8], reader: &SharedBytes) -> Option>; + +/// Bytes sniffed by [`open_reader`]. +pub const HEAD_LEN: usize = 64 * 1024; + +/// Default ceiling for a whole-file fallback or an offline output buffer (256 MiB). +/// Streaming openers are tried first. Use open_reader_within for an explicit different file budget. +pub const DEFAULT_MAX_MEDIA_BYTES: usize = 256 << 20; + +fn allocate(len: usize, max_bytes: usize) -> io::Result> { + if len.checked_mul(std::mem::size_of::()).is_none_or(|bytes| bytes > max_bytes || bytes > isize::MAX as usize) { + return Err(io::Error::new(io::ErrorKind::InvalidData, "media buffer exceeds allocation limit")); + } + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|e| io::Error::other(format!("cannot allocate media buffer: {e}")))?; + out.resize(len, T::default()); + Ok(out) +} + +fn io_named(name: &str, e: io::Error) -> MediaError { + MediaError::Io(format!("{name}: {e}")) +} + +/// Read `len` bytes at `offset` (fewer at the end of the source). +pub fn read_range(r: &dyn ByteSource, offset: u64, len: usize) -> io::Result> { + read_range_within(r, offset, len, DEFAULT_MAX_MEDIA_BYTES) +} + +fn read_range_within(r: &dyn ByteSource, offset: u64, len: usize, max_bytes: usize) -> io::Result> { + let n = usize::try_from(r.len().saturating_sub(offset).min(len as u64)) + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "media length is not representable"))?; + let mut v = allocate(n, max_bytes)?; + r.read_at(offset, &mut v)?; + Ok(v) +} + +/// Open media from bytes by trying `extra` openers in order. There is no built-in fallback: +/// stills, WAV, and generators stay in the apps and register here. +pub fn open_bytes(name: &str, bytes: Arc<[u8]>, extra: &[Opener]) -> Result { + for o in extra { + if let Some(r) = o(name, bytes.clone()) { + return r; + } + } + Err(MediaError::Unsupported(format!("{name}: unrecognised format"))) +} + +/// Open media from a reader: `reader_openers` first; otherwise read the whole file (if it fits in +/// `max_whole`) and use [`open_bytes`] with `extra`. +pub fn open_reader(name: &str, reader: SharedBytes, reader_openers: &[ReaderOpener], extra: &[Opener]) -> Result { + open_reader_within(name, reader, reader_openers, extra, DEFAULT_MAX_MEDIA_BYTES as u64) +} + +/// Like [`open_reader`], but a file no reader opener takes is read whole only up to `max_whole` +/// bytes; a larger one is refused instead. For looking at files rather than importing them: +/// reading a multi-gigabyte file into memory to show its duration, or to find out it isn't +/// supported, thrashes the disk. +pub fn open_reader_within( + name: &str, + reader: SharedBytes, + reader_openers: &[ReaderOpener], + extra: &[Opener], + max_whole: u64, +) -> Result { + let head = read_range(&*reader, 0, HEAD_LEN).map_err(|e| io_named(name, e))?; + for o in reader_openers { + if let Some(r) = o(name, &head, &reader) { + return r; + } + } + if extra.is_empty() { + return Err(MediaError::Unsupported(format!("{name}: unrecognised format"))); + } + if reader.len() > max_whole { + return Err(MediaError::Unsupported(format!("{name}: no streaming reader for this format, and it is too large to read whole here"))); + } + let len = usize::try_from(reader.len()).map_err(|_| MediaError::Unsupported(format!("{name}: media length is not representable")))?; + let budget = usize::try_from(max_whole).unwrap_or(usize::MAX); + let all = read_range_within(&*reader, 0, len, budget).map_err(|e| io_named(name, e))?; + open_bytes(name, all.into(), extra) +} + +/// Open a regular file through the shared pooled FileReader. Non-files (including FIFOs) +/// are refused without a blocking open, using the same contract as other media readers. +#[cfg(not(target_arch = "wasm32"))] +pub fn open_path(path: &Path, reader_openers: &[ReaderOpener], extra: &[Opener]) -> Result { + let file = craft_media::FileReader::open(path).map_err(|e| MediaError::Io(format!("{}: {e}", path.display())))?; + let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + open_reader(&name, Arc::new(file), reader_openers, extra) +} + +fn audio_channels(info: &MediaInfo) -> u16 { + info.audio().map_or(2, |a| u16::try_from(a.channels).unwrap_or(u16::MAX).max(1)) +} + +/// A source that renders a solid slate (used for offline media). +pub struct OfflineSource { + pub info: MediaInfo, +} + +impl MediaSource for OfflineSource { + fn info(&self) -> &MediaInfo { + &self.info + } + fn video_frame(&self, req: FrameRequest) -> Result> { + let v = self.info.video.as_ref().ok_or(MediaError::NoStream("video"))?; + if !req.scale.is_finite() || req.scale <= 0.0 || v.width == 0 || v.height == 0 { + return Err(MediaError::Decode("invalid offline frame dimensions or scale".into())); + } + let s = req.scale.clamp(0.02, 1.0); + let (w, h) = (((v.width as f32 * s) as u32).max(2), ((v.height as f32 * s) as u32).max(2)); + let samples = usize::try_from(w) + .ok() + .and_then(|w| usize::try_from(h).ok().and_then(|h| w.checked_mul(h))) + .and_then(|pixels| pixels.checked_mul(4)) + .ok_or_else(|| MediaError::Decode("offline frame dimensions overflow".into()))?; + let mut px = allocate::(samples, DEFAULT_MAX_MEDIA_BYTES)?; + for p in px.as_chunks_mut::<4>().0 { + p.copy_from_slice(&[140, 16, 16, 255]); + } + Ok(Arc::new(VideoFrame::rgba8(w, h, px).map_err(|e| MediaError::Decode(e.to_string()))?.with_pts(req.time))) + } + fn audio(&self, _start: i64, frames: usize, sample_rate: u32) -> Result { + let channels = usize::from(audio_channels(&self.info)); + let bytes = frames + .checked_mul(channels) + .and_then(|n| n.checked_mul(std::mem::size_of::())) + .ok_or_else(|| MediaError::Decode("offline audio dimensions overflow".into()))?; + if bytes > DEFAULT_MAX_MEDIA_BYTES { + return Err(MediaError::Decode("offline audio exceeds allocation limit".into())); + } + let mut planes = Vec::new(); + planes.try_reserve_exact(channels).map_err(|e| MediaError::Io(e.to_string()))?; + for _ in 0..channels { + planes.push(allocate::(frames, DEFAULT_MAX_MEDIA_BYTES)?); + } + Ok(AudioBuffer { sample_rate, channels: planes }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn silent(name: &str, kind: MediaKind) -> OfflineSource { + OfflineSource { + info: MediaInfo { + name: name.into(), + kind, + duration: Tick::from_seconds_f64(1.0), + video: Some(VideoStreamInfo { + width: 16, + height: 8, + frame_rate: FrameRate::FPS_24, + par: (1, 1), + codec: "slate".into(), + pixel_format: "rgba8".into(), + color: ColorInfo::default(), + has_alpha: false, + bitrate: None, + hdr: None, + }), + audio_streams: vec![AudioStreamInfo { sample_rate: 48000, channels: 2, codec: "pcm".into(), bits_per_sample: Some(32) }], + container: "none".into(), + start_timecode: None, + file_size: None, + }, + } + } + + fn accept_ok(name: &str, _bytes: Arc<[u8]>) -> Option> { + if name.ends_with(".ok") { Some(Ok(Arc::new(silent(name, MediaKind::Synthetic)))) } else { None } + } + + fn reject_all(_name: &str, _bytes: Arc<[u8]>) -> Option> { + None + } + + fn boom(name: &str, _bytes: Arc<[u8]>) -> Option> { + Some(Err(MediaError::Decode(format!("{name}: boom")))) + } + + fn reader_ok(name: &str, head: &[u8], _reader: &SharedBytes) -> Option> { + if head.starts_with(b"OK") { Some(Ok(Arc::new(silent(name, MediaKind::Movie)))) } else { None } + } + + struct HugeSource; + impl ByteSource for HugeSource { + fn len(&self) -> u64 { + u64::MAX + } + fn read_at(&self, _offset: u64, buf: &mut [u8]) -> io::Result<()> { + assert!(buf.len() <= HEAD_LEN, "must reject whole-file allocation before reading"); + buf.fill(0); + Ok(()) + } + } + + #[test] + fn whole_file_fallback_and_explicit_ranges_are_bounded() { + let reader: SharedBytes = Arc::new(HugeSource); + assert!(open_reader("large.ok", reader.clone(), &[], &[accept_ok]).is_err()); + assert!(open_reader_within("large.ok", reader.clone(), &[], &[accept_ok], u64::MAX).is_err()); + assert!(read_range(&HugeSource, 0, usize::MAX).is_err()); + assert!(read_range_within(&HugeSource, 0, 10, 9).is_err()); + assert_eq!(read_range_within(&HugeSource, 0, 10, 10).unwrap(), vec![0; 10]); + } + + struct SniffOnlySource(std::sync::atomic::AtomicUsize); + impl ByteSource for SniffOnlySource { + fn len(&self) -> u64 { + u64::MAX + } + fn read_at(&self, offset: u64, buf: &mut [u8]) -> io::Result<()> { + assert_eq!(offset, 0); + assert_eq!(buf.len(), HEAD_LEN); + assert_eq!(self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst), 0, "must only sniff once"); + buf.fill(0); + buf[..2].copy_from_slice(b"OK"); + Ok(()) + } + } + + #[test] + fn no_fallback_stops_after_sniff_and_streaming_opener_precedes_size_cap() { + let reader = Arc::new(SniffOnlySource(std::sync::atomic::AtomicUsize::new(0))); + let error = open_reader("unknown", reader.clone(), &[], &[]).err().unwrap(); + assert!(matches!(error, MediaError::Unsupported(_))); + assert_eq!(reader.0.load(std::sync::atomic::Ordering::SeqCst), 1); + let reader = Arc::new(SniffOnlySource(std::sync::atomic::AtomicUsize::new(0))); + let source = open_reader_within("large.mov", reader.clone(), &[reader_ok], &[accept_ok], 0).unwrap(); + assert_eq!(source.info().kind, MediaKind::Movie); + assert_eq!(reader.0.load(std::sync::atomic::Ordering::SeqCst), 1); + } + + #[test] + fn hostile_offline_metadata_and_requests_are_errors() { + let mut source = silent("bad", MediaKind::Synthetic); + source.info.video.as_mut().unwrap().width = u32::MAX; + source.info.video.as_mut().unwrap().height = u32::MAX; + assert!(source.video_frame(FrameRequest::full(Tick::ZERO)).is_err()); + assert!(source.audio(0, usize::MAX, 48000).is_err()); + let source = silent("bad-scale", MediaKind::Synthetic); + for scale in [f32::NAN, f32::INFINITY, 0.0, -1.0] { + assert!(source.video_frame(FrameRequest { time: Tick::ZERO, scale }).is_err()); + } + } + + #[cfg(unix)] + #[test] + fn open_path_refuses_a_fifo_without_waiting_for_a_writer() { + let root = std::env::temp_dir().join(format!("craft-media-source-fifo-{}", std::process::id())); + std::fs::create_dir(&root).unwrap(); + let path = root.join("fifo"); + if std::process::Command::new("mkfifo").arg(&path).status().is_ok_and(|s| s.success()) { + assert!(open_path(&path, &[], &[]).is_err()); + std::fs::remove_file(&path).unwrap(); + } + std::fs::remove_dir(&root).unwrap(); + } + + #[test] + fn importable_extensions_match_regardless_of_case() { + for name in ["clip.mov", "clip.MOV", "clip.Mov", "shot.MP4", "IMG_0001.JPG", "sound.WAV", "take.M2TS"] { + assert!(is_importable(Path::new(name)), "{name}"); + } + assert!(!is_importable(Path::new("notes.txt"))); + } + + #[test] + fn open_bytes_tries_extra_in_order_and_otherwise_is_unsupported() { + let bytes: Arc<[u8]> = Arc::from(&b"xxxx"[..]); + let err = open_bytes("a.bin", bytes.clone(), &[]).err().expect("expected error"); + assert!(matches!(err, MediaError::Unsupported(_))); + let err = open_bytes("a.bin", bytes.clone(), &[reject_all, accept_ok]).err().expect("expected error"); + assert!(matches!(err, MediaError::Unsupported(_))); + let src = open_bytes("clip.ok", bytes.clone(), &[reject_all, accept_ok]).unwrap(); + assert_eq!(src.info().name, "clip.ok"); + let err = open_bytes("clip.ok", bytes, &[boom, accept_ok]).err().expect("expected error"); + assert!(matches!(err, MediaError::Decode(_))); + } + + #[test] + fn open_reader_uses_reader_openers_then_falls_back_within_max_whole() { + let small: SharedBytes = Arc::new(b"HELLO".to_vec()); + let err = open_reader("a.bin", small.clone(), &[], &[]).err().expect("expected error"); + assert!(matches!(err, MediaError::Unsupported(_))); + + let ok: SharedBytes = Arc::new(b"OK-file".to_vec()); + let src = open_reader("clip.mov", ok, &[reader_ok], &[]).unwrap(); + assert_eq!(src.info().kind, MediaKind::Movie); + + let tagged: SharedBytes = Arc::new(b"xxxx".to_vec()); + let src = open_reader("clip.ok", tagged.clone(), &[], &[accept_ok]).unwrap(); + assert_eq!(src.info().name, "clip.ok"); + + let big = vec![0u8; HEAD_LEN + 8]; + let r: SharedBytes = Arc::new(big); + let e = open_reader_within("a.avi", r, &[], &[accept_ok], HEAD_LEN as u64).err().expect("expected error"); + assert!(matches!(e, MediaError::Unsupported(s) if s.contains("too large"))); + } + + #[test] + fn audio_streams_serde_accepts_legacy_single_object() { + let stream = r#"{"sample_rate":48000,"channels":2,"codec":"pcm","bits_per_sample":null}"#; + let prefix = r#"{"name":"a","kind":"Movie","duration":0,"video":null,"audio_streams":["#; + let suffix = r#"],"container":"wav","start_timecode":null,"file_size":null}"#; + let full = std::iter::repeat_n(stream, MAX_AUDIO_STREAMS).collect::>().join(","); + let info: MediaInfo = serde_json::from_str(&format!("{prefix}{full}{suffix}")).unwrap(); + assert_eq!(info.audio_streams.len(), MAX_AUDIO_STREAMS); + for extra in [stream, r#"{"malformed":"not an audio stream"}"#] { + let error = serde_json::from_str::(&format!("{prefix}{full},{extra}{suffix}")).unwrap_err(); + assert!(error.to_string().contains("MAX_AUDIO_STREAMS"), "{error}"); + } + let many = r#"{"name":"a","kind":"Movie","duration":0,"video":null,"audio_streams":[{"sample_rate":48000,"channels":2,"codec":"pcm","bits_per_sample":null}],"container":"wav","start_timecode":null,"file_size":null}"#; + let info: MediaInfo = serde_json::from_str(many).unwrap(); + assert_eq!(info.audio_streams.len(), 1); + let one = r#"{"name":"a","kind":"AudioOnly","duration":0,"video":null,"audio":{"sample_rate":44100,"channels":1,"codec":"pcm","bits_per_sample":16},"container":"wav","start_timecode":null,"file_size":null}"#; + let info: MediaInfo = serde_json::from_str(one).unwrap(); + assert_eq!(info.audio().map(|a| a.sample_rate), Some(44100)); + let none = r#"{"name":"a","kind":"Still","duration":0,"video":null,"audio":null,"container":"png","start_timecode":null,"file_size":null}"#; + let info: MediaInfo = serde_json::from_str(none).unwrap(); + assert!(info.audio_streams.is_empty()); + } + + #[test] + fn offline_source_returns_slate_and_silence() { + let src = silent("off", MediaKind::Synthetic); + let frame = src.video_frame(FrameRequest::full(Tick::ZERO)).unwrap(); + assert_eq!((frame.width, frame.height), (16, 8)); + assert_eq!(frame.pts, Tick::ZERO); + let audio = src.audio(0, 8, 48000).unwrap(); + assert_eq!(audio.sample_rate, 48000); + assert_eq!(audio.num_channels(), 2); + assert_eq!(audio.frames(), 8); + assert!(src.audio_stream(1, 0, 8, 48000).is_err()); + } +} From f200f1c91df2c8b5ba8ef3c3c853382ced75df1f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 21:53:12 +0100 Subject: [PATCH 359/471] dds: 16-bit input and output (R16, R16G16, R16G16B16A16; BC4/BC5 fitted to 16-bit values) encode16 writes 16-bit pixels uncompressed as R16_UNORM, R16G16_UNORM or R16G16B16A16_UNORM with the DX10 header, and fits BC4/BC5 endpoints and indices against the 16-bit values; BC1-BC3 round to 8 bits first. Texture::decode16 reads these formats (and legacy FourCC 36, wide channel masks, BC4/BC5 interpolated at 16 bits) without losing precision. --- crates/dds/README.md | 18 +- crates/dds/src/bc.rs | 120 ++++++++++- crates/dds/src/lib.rs | 453 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 545 insertions(+), 46 deletions(-) diff --git a/crates/dds/README.md b/crates/dds/README.md index a3f81e50b..7a25eb36e 100644 --- a/crates/dds/README.md +++ b/crates/dds/README.md @@ -11,22 +11,32 @@ craft-dds = { git = "https://github.com/storytold/craft-libs", rev = "" | | Read | Write | |---|---|---| -| Uncompressed | 8, 16, 24 or 32 bits per pixel described by channel masks, RGB or luminance, with or without alpha; padded rows (`DDSD_PITCH`) | BGRA, BGRX, luminance, luminance with alpha | +| Uncompressed | 8, 16, 24 or 32 bits per pixel described by channel masks, RGB or luminance, with or without alpha; padded rows (`DDSD_PITCH`); 16 bits per channel: `R16_UNORM`, `R16G16_UNORM`, `R16G16B16A16_UNORM` (DXGI 56, 35, 11 and their typeless forms; legacy FourCC 36) | BGRA, BGRX, luminance, luminance with alpha; from 16-bit pixels `R16_UNORM`, `R16G16_UNORM`, `R16G16B16A16_UNORM` | | Block-compressed | BC1, BC2, BC3 (DXT1, DXT3, DXT5); unsigned BC4 (ATI1, BC4U, DXGI 79 and 80) and BC5 (ATI2, BC5U, DXGI 82 and 83) | BC1, BC2, BC3, BC4, BC5 | -| Headers | legacy and DX10 | legacy; DX10 for BC4 and BC5 | +| Headers | legacy and DX10 | legacy; DX10 for BC4, BC5 and the 16-bit formats | | Mip levels | the top level only | optional full chain, box-filtered | Cubemaps, texture arrays, volume textures, premultiplied-alpha textures (DXT2/DXT4, or DX10 alpha mode 2), signed (SNORM) BC4/BC5 and -the other DXGI formats (BC6H, BC7, float) are refused with `DdsError::Unsupported` rather than read +the other DXGI formats (BC6H, BC7, float, signed or integer 16-bit) are refused with `DdsError::Unsupported` rather than read in part. -Pixels are 8 bits per channel, interleaved and top row first, in one of four `Layout`s (`Gray`, +Pixels are 8 bits per channel (`encode`, `Texture::decode`) or 16 bits per channel (`encode16`, +`Texture::decode16`), interleaved and top row first, in one of four `Layout`s (`Gray`, `GrayA`, `Rgb`, `Rgba`). BC1 to BC3 are written from RGBA, BC4 from gray and BC5 from RGB (`DdsCompression::layout` names it). BC4 reads back as gray. BC5 stores red and green only, so it reads back as RGB with blue 0: for a tangent-space normal map, X and Y, with Z left to the renderer. ATI2 files are read as Microsoft's tools read them (red in the first block), not with the two blocks swapped as FFmpeg does. +From 16-bit pixels, uncompressed textures keep 16 bits per channel: gray as `R16_UNORM`, gray +and alpha (or any two channels, such as a normal map's X and Y) as `R16G16_UNORM`, RGB (alpha +65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10 header; they read back as gray, gray +and alpha, and RGBA. BC4 and BC5 keep 8-bit endpoints, as the formats store them, but pick them +and the indices against the 16-bit values, so a smooth height or normal map has less error than +when its 8-bit rounding is compressed. BC1 to BC3 round the pixels to 8 bits first. +`Texture::decode` rounds 16-bit formats to 8 bits; `Texture::decode16` returns them exactly, +interpolates BC4 and BC5 at 16 bits, and scales every 8-bit format by 257. + ```rust use craft_dds::{DdsCompression, DdsOptions, Layout, Texture}; diff --git a/crates/dds/src/bc.rs b/crates/dds/src/bc.rs index 863aa3077..c19a393f3 100644 --- a/crates/dds/src/bc.rs +++ b/crates/dds/src/bc.rs @@ -4,7 +4,9 @@ //! knows about DDS headers, so other containers of the same blocks can use it. //! //! BC4 is one block of 16 single-channel values, the same block BC3 stores its alpha in; BC5 is -//! two of them, red then green. [`value_block`] and [`decode_values`] serve all three. +//! two of them, red then green. [`value_block`] and [`decode_values`] serve all three; +//! [`value_block16`] and [`decode_values16`] are the same blocks for 16-bit values (BC4 and BC5 +//! from 16-bit sources). //! //! The encoder fits a block's colours with two RGB565 endpoints along their principal axis, //! refines them by least squares, and scores every candidate against the exact palette the @@ -69,6 +71,26 @@ pub(crate) fn decode_colour(block: &[u8], one_bit_alpha: bool) -> Block { std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) } +/// [`alpha_palette`] at 16 bits: the same eight values before they are rounded to 8 bits, as a +/// decoder that interpolates at a higher precision shows them (endpoints scaled by 257, so 255 +/// is 65535). +pub(crate) fn alpha_palette16(a0: u8, a1: u8) -> [u16; 8] { + let (a0, a1) = (u32::from(a0) * 257, u32::from(a1) * 257); + let mut palette = [a0, a1, 0, 0, 0, 0, 0, 65535]; + if a0 > a1 { + for (k, v) in palette.iter_mut().enumerate().skip(2) { + let k = k as u32; + *v = ((8 - k) * a0 + (k - 1) * a1 + 3) / 7; + } + } else { + for (k, v) in palette.iter_mut().enumerate().take(6).skip(2) { + let k = k as u32; + *v = ((6 - k) * a0 + (k - 1) * a1 + 2) / 5; + } + } + palette.map(|v| v as u16) +} + /// BC2's alpha: four explicit bits per texel. pub(crate) fn decode_bc2_alpha(block: &[u8]) -> [u8; 16] { let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0))); @@ -84,6 +106,14 @@ pub(crate) fn decode_values(block: &[u8]) -> [u8; 16] { std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize]) } +/// [`decode_values`] at 16 bits, from [`alpha_palette16`]. +pub(crate) fn decode_values16(block: &[u8]) -> [u16; 16] { + let byte = |i: usize| block.get(i).copied().unwrap_or(0); + let palette = alpha_palette16(byte(0), byte(1)); + let bits = u64::from_le_bytes([byte(2), byte(3), byte(4), byte(5), byte(6), byte(7), 0, 0]); + std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize]) +} + /// A BC1 block. Texels under half opacity become BC1's transparent black, which needs the /// three-colour mode; an opaque block takes whichever mode fits its colours better. pub(crate) fn encode_bc1(block: &Block, inside: Mask) -> [u8; 8] { @@ -369,6 +399,64 @@ fn value_fit(values: &[u8; 16], inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32) ([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error) } +/// [`value_block`] for 16-bit values: the endpoints are still 8 bits, but each end of each mode +/// tries the 8-bit levels just below and above the 16-bit extreme, and every candidate is scored +/// against the 16-bit palette ([`alpha_palette16`]). A block that spans less than one 8-bit level +/// is flat from 8-bit values but gets the interpolated steps between two adjacent levels here. +pub(crate) fn value_block16(values: &[u16; 16], inside: Mask) -> [u8; 8] { + let mut all: Option<(u16, u16)> = None; + let mut mid = all; + let widen = |r: Option<(u16, u16)>, v: u16| Some(r.map_or((v, v), |(lo, hi)| (lo.min(v), hi.max(v)))); + for (t, &v) in values.iter().enumerate() { + if inside >> t & 1 != 0 { + all = widen(all, v); + if !matches!(v, 0 | 65535) { + mid = widen(mid, v); + } + } + } + let (lo, hi) = all.unwrap_or((0, 0)); + let (mid_lo, mid_hi) = mid.unwrap_or((0, 0)); + let (down, up) = (|v: u16| (v / 257) as u8, |v: u16| v.div_ceil(257) as u8); + // The eight-value mode's pairs first (a0 the high end), then the six-value mode's: on a tie + // the eight-value mode wins, as in `value_block`. Each pair's palette follows its order, so a + // pair that lands in the other mode is still scored as the decoder reads it. + let candidates = [ + (up(hi), down(lo)), + (up(hi), up(lo)), + (down(hi), down(lo)), + (down(hi), up(lo)), + (down(mid_lo), down(mid_hi)), + (down(mid_lo), up(mid_hi)), + (up(mid_lo), down(mid_hi)), + (up(mid_lo), up(mid_hi)), + ]; + let mut best: Option<([u8; 8], u64)> = None; + for (a0, a1) in candidates { + let fit = value_fit16(values, inside, a0, a1); + if best.is_none_or(|(_, e)| fit.1 < e) { + best = Some(fit); + } + } + best.map_or([0; 8], |(bytes, _)| bytes) +} + +fn value_fit16(values: &[u16; 16], inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u64) { + let palette = alpha_palette16(a0, a1); + let mut bits = 0u64; + let mut error = 0u64; + for (t, &v) in values.iter().enumerate() { + if inside >> t & 1 == 0 { + continue; + } + let (i, d) = nearest(&palette, |&p| u32::from(p.abs_diff(v)).pow(2)); + error += u64::from(d); + bits |= (i as u64) << (3 * t); + } + let b = bits.to_le_bytes(); + ([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error) +} + #[cfg(test)] mod tests { use super::*; @@ -484,6 +572,36 @@ mod tests { assert_eq!(nearest::(&[], |_| 7), (0, 0)); } + #[test] + fn value_blocks_decode_the_frozen_vectors_at_16_bits() { + // Eight-value mode (200 > 50): index 2 is (6 * 200 + 50) / 7 of 255 at 16 bits, which + // the 8-bit palette rounds to 179. + let bits: u64 = (0..16).fold(0, |b, t| b | 2 << (3 * t)); + let b = bits.to_le_bytes(); + let block = [200, 50, b[0], b[1], b[2], b[3], b[4], b[5]]; + assert_eq!(decode_values16(&block), [((6 * 200 + 50) * 257 + 3) / 7; 16]); + assert_eq!(decode_values(&block), [179; 16]); + // Six-value mode's indices 6 and 7 are 0 and 65535. + let bits: u64 = (0..16).fold(0, |b, t| b | (6 + (t % 2)) << (3 * t)); + let b = bits.to_le_bytes(); + let got = decode_values16(&[10, 250, b[0], b[1], b[2], b[3], b[4], b[5]]); + assert_eq!(got, std::array::from_fn(|t| if t % 2 == 0 { 0 } else { 65535 })); + } + + #[test] + fn value_blocks_from_16_bits_step_between_two_8_bit_levels() { + // A ramp across 0.8 of one 8-bit level: from 8 bits it is one or two flat values; from 16 + // bits the endpoints straddle it and the steps between them follow the ramp. + let values: [u16; 16] = std::array::from_fn(|t| 25_700 + (t as u16) * 13); + let error = |got: [u16; 16]| values.iter().zip(got).map(|(a, b)| u64::from(a.abs_diff(b)).pow(2)).sum::(); + let ours = error(decode_values16(&value_block16(&values, u16::MAX))); + let from_8_bits = error(decode_values16(&value_block(&values.map(|v| ((u32::from(v) + 128) / 257) as u8), u16::MAX))); + assert!(ours * 4 < from_8_bits, "{ours} vs {from_8_bits} from 8 bits"); + // Values outside the image don't count. + let solid = value_block16(&std::array::from_fn(|t| if t == 0 { 1000 } else { 60_000 }), 1); + assert!(decode_values16(&solid)[0].abs_diff(1000) < 129); + } + #[test] fn texels_outside_the_image_are_ignored() { // Only the top-left texel is inside: its colour alone decides the endpoints. diff --git a/crates/dds/src/lib.rs b/crates/dds/src/lib.rs index f7b44fc7a..336994285 100644 --- a/crates/dds/src/lib.rs +++ b/crates/dds/src/lib.rs @@ -2,7 +2,8 @@ //! "DDS_HEADER", "DDS_PIXELFORMAT", "DDS_HEADER_DXT10"). //! //! Reading: the top mip level of a 2D texture, either uncompressed (8 to 32 bits per pixel -//! described by channel masks, RGB or luminance) or block-compressed BC1/BC2/BC3 +//! described by channel masks, RGB or luminance, or 16 bits per channel: `R16_UNORM`, +//! `R16G16_UNORM`, `R16G16B16A16_UNORM`) or block-compressed BC1/BC2/BC3 //! (DXT1/DXT3/DXT5) or unsigned BC4/BC5 (ATI1/ATI2), with or without the DX10 header. BC4 opens //! as gray and BC5 as RGB with blue 0: BC5 stores only red and green, and a normal map's Z is //! left for the caller to reconstruct. Cubemaps, texture arrays and volume textures are refused @@ -14,7 +15,12 @@ //! legacy FourCCs are vendor names that programs read differently (ATI2's two blocks are swapped //! by some). BC6H, BC7 and sRGB formats aren't written. //! -//! Pixels are 8 bits per channel, interleaved, top row first, in one of the [`Layout`]s. The +//! Pixels are 8 bits per channel ([`encode`], [`Texture::decode`]) or 16 bits per channel +//! ([`encode16`], [`Texture::decode16`]), interleaved, top row first, in one of the [`Layout`]s. +//! Uncompressed 16-bit pixels are written with the DX10 header as `R16_UNORM` (gray), +//! `R16G16_UNORM` (gray and alpha, or any two channels such as a normal map's X and Y) or +//! `R16G16B16A16_UNORM` (RGB, with alpha 65535, and RGBA). BC4 and BC5 fit their 8-bit endpoints +//! and their indices to the 16-bit values; BC1 to BC3 round the pixels to 8 bits first. The //! crate has no dependencies; the `parallel` feature encodes and decodes rows of blocks (and mip //! levels) on several threads with rayon. //! @@ -38,7 +44,7 @@ use std::fmt; use bc::{Block, Mask}; -/// The channels of a pixel, 8 bits each, in this order. +/// The channels of a pixel, 8 or 16 bits each, in this order. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Layout { Gray, @@ -187,6 +193,8 @@ const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00; const DDSCAPS2_VOLUME: u32 = 0x20_0000; const DIMENSION_TEXTURE2D: u32 = 3; const MISC_TEXTURECUBE: u32 = 0x4; +/// The legacy FourCC (a number, not text) of 16-bit RGBA. +const D3DFMT_A16B16G16R16: u32 = 36; fn bad(reason: impl fmt::Display) -> DdsError { DdsError::Malformed(reason.to_string()) @@ -242,6 +250,12 @@ impl Field { let max = u64::from(self.max); ((v * 255 + max / 2) / max) as u8 } + + fn get16(self, pixel: u32) -> u16 { + let v = u64::from((pixel >> self.shift) & self.max); + let max = u64::from(self.max); + ((v * 65535 + max / 2) / max) as u16 + } } #[derive(Clone, Copy)] @@ -255,6 +269,8 @@ enum Encoding { Bc1, Bc2, Bc3, + /// 16 bits per channel, little-endian: gray, gray and alpha (or red and green), or RGBA. + Unorm16 { channels: usize }, /// One value block: gray. Bc4, /// Two value blocks: red, then green. @@ -282,6 +298,8 @@ impl Encoding { Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => Layout::GrayA, Encoding::Masks { gray: true, .. } => Layout::Gray, Encoding::Masks { fields, .. } if fields[3].is_none() => Layout::Rgb, + Encoding::Unorm16 { channels: 1 } => Layout::Gray, + Encoding::Unorm16 { channels: 2 } => Layout::GrayA, Encoding::Bc4 => Layout::Gray, Encoding::Bc5 => Layout::Rgb, _ => Layout::Rgba, @@ -291,7 +309,7 @@ impl Encoding { /// Bytes per 4×4 block; 0 for uncompressed pixels. fn block_size(&self) -> usize { match self { - Encoding::Masks { .. } => 0, + Encoding::Masks { .. } | Encoding::Unorm16 { .. } => 0, Encoding::Bc1 | Encoding::Bc4 => 8, Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc5 => 16, } @@ -301,9 +319,14 @@ impl Encoding { fn level_layout(&self, width: u32, height: u32, pitch: Option) -> Result<(usize, usize), DdsError> { let overflow = || bad("pixel data size overflow"); let (w, h) = (usize::try_from(width).map_err(|_| overflow())?, usize::try_from(height).map_err(|_| overflow())?); - let (row_bytes, rows) = match self { - Encoding::Masks { bytes, .. } => { - let packed = w.checked_mul(*bytes).ok_or_else(overflow)?; + let pixel_bytes = match self { + Encoding::Masks { bytes, .. } => Some(*bytes), + Encoding::Unorm16 { channels } => Some(2 * channels), + _ => None, + }; + let (row_bytes, rows) = match pixel_bytes { + Some(bytes) => { + let packed = w.checked_mul(bytes).ok_or_else(overflow)?; let row_bytes = match pitch { Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?, None => packed, @@ -313,9 +336,7 @@ impl Encoding { } (row_bytes, h) } - Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => { - (w.div_ceil(4).checked_mul(self.block_size()).ok_or_else(overflow)?, h.div_ceil(4)) - } + None => (w.div_ceil(4).checked_mul(self.block_size()).ok_or_else(overflow)?, h.div_ceil(4)), }; let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?; Ok((row_bytes, size)) @@ -327,6 +348,10 @@ fn legacy_encoding(b: &[u8]) -> Result { let (flags, fourcc, bits) = (le32(b, 80)?, le32(b, 84)?, le32(b, 88)?); let alpha = if flags & DDPF_ALPHAPIXELS != 0 { le32(b, 104)? } else { 0 }; if flags & DDPF_FOURCC != 0 { + // D3DFMT_A16B16G16R16, the legacy name of R16G16B16A16_UNORM (DirectXTex writes it so). + if fourcc == D3DFMT_A16B16G16R16 { + return Ok(Encoding::Unorm16 { channels: 4 }); + } return match &fourcc.to_le_bytes() { b"DXT1" => Ok(Encoding::Bc1), b"DXT3" => Ok(Encoding::Bc2), @@ -364,7 +389,7 @@ fn dx10_encoding(b: &[u8]) -> Result { } // miscFlags2 bits 0-2 are the alpha mode; 2 is premultiplied. Only the formats with a real // alpha channel are affected, and the legacy path refuses their DXT2/DXT4 equivalents too. - if le32(b, HEADER + 16)? & 0x7 == 2 && matches!(dxgi, 27..=29 | 73..=78 | 87 | 90 | 91) { + if le32(b, HEADER + 16)? & 0x7 == 2 && matches!(dxgi, 9 | 11 | 27..=29 | 73..=78 | 87 | 90 | 91) { return Err(unsupported(format!("premultiplied-alpha DX10 textures are not supported (DXGI format {dxgi})"))); } const RGBA: [u32; 4] = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; @@ -372,6 +397,10 @@ fn dx10_encoding(b: &[u8]) -> Result { const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0]; match dxgi { 27..=29 => Encoding::masks(32, RGBA, false), + // Typeless, then UNORM, as for BC4 and BC5 below. + 9 | 11 => Ok(Encoding::Unorm16 { channels: 4 }), + 33 | 35 => Ok(Encoding::Unorm16 { channels: 2 }), + 53 | 56 => Ok(Encoding::Unorm16 { channels: 1 }), 87 | 90 | 91 => Encoding::masks(32, BGRA, false), 88 | 92 | 93 => Encoding::masks(32, BGRX, false), 70..=72 => Ok(Encoding::Bc1), @@ -382,7 +411,7 @@ fn dx10_encoding(b: &[u8]) -> Result { 79 | 80 => Ok(Encoding::Bc4), 82 | 83 => Ok(Encoding::Bc5), 81 | 84 => Err(unsupported(format!("signed (SNORM) BC4 and BC5 textures are not supported (DXGI format {dxgi})"))), - _ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1 to BC5 and 8-bit RGBA or BGRA are)"))), + _ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1 to BC5, 8-bit RGBA or BGRA and 16-bit R, RG or RGBA are)"))), } } @@ -448,6 +477,65 @@ impl<'a> Texture<'a> { /// Decode with an explicit per-buffer output byte budget; allocation remains fallible. pub fn decode_with_limit(&self, max_bytes: u64) -> Result, DdsError> { + let encoding = self.encoding; + let (data, row_pitch, total) = self.top_level(1, max_bytes)?; + let (w, channels) = (self.width as usize, encoding.layout().channels()); + let row_size = w * channels; + let mut pixels = alloc_zeroed(total)?; + match encoding { + Encoding::Masks { bytes, gray, fields } => decode_masks(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size), + // Each 16-bit sample rounded to the nearest 8-bit level. + Encoding::Unorm16 { .. } => rows(&mut pixels, row_size, |y, dst| { + let src = data.get(y * row_pitch..).unwrap_or_default(); + for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) { + *d = to8(u16::from_le_bytes(*s)); + } + }), + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => { + decode_blocks(encoding, data, (w, channels), &mut pixels) + } + } + Ok(pixels) + } + + /// The top mip level's pixels in [`Texture::layout`] at 16 bits per channel, bounded by + /// [`DEFAULT_MAX_DECODE_BYTES`]. 16-bit formats come back exactly, as do channel masks wider + /// than 8 bits; BC4 and BC5 values are interpolated at 16 bits; every other format is its + /// 8-bit value times 257 (so 255 is 65535). + pub fn decode16(&self) -> Result, DdsError> { + self.decode16_with_limit(DEFAULT_MAX_DECODE_BYTES) + } + + /// [`Texture::decode16`] with an explicit per-buffer output byte budget (two bytes a sample). + pub fn decode16_with_limit(&self, max_bytes: u64) -> Result, DdsError> { + let encoding = self.encoding; + let (data, row_pitch, total) = self.top_level(2, max_bytes)?; + let (w, channels) = (self.width as usize, encoding.layout().channels()); + let row_size = w * channels; + let mut pixels = alloc_zeroed(total / 2)?; + match encoding { + Encoding::Masks { bytes, gray, fields } => decode_masks16(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size), + Encoding::Unorm16 { .. } => rows(&mut pixels, row_size, |y, dst| { + let src = data.get(y * row_pitch..).unwrap_or_default(); + for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) { + *d = u16::from_le_bytes(*s); + } + }), + Encoding::Bc4 | Encoding::Bc5 => decode_value_blocks16(encoding, data, (w, channels), &mut pixels), + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { + // The limit was checked above for the 16-bit buffer, which is twice this one. + let narrow = self.decode_with_limit(u64::MAX)?; + for (d, v) in pixels.iter_mut().zip(narrow) { + *d = u16::from(v) * 257; + } + } + } + Ok(pixels) + } + + /// The top level's stored bytes and row stride, and the decoded size in bytes at + /// `sample_bytes` a sample, checked against `max_bytes` before the stored data is. + fn top_level(&self, sample_bytes: usize, max_bytes: u64) -> Result<(&'a [u8], usize, usize), DdsError> { let (b, flags, encoding) = (self.bytes, self.flags, self.encoding); let (width, height) = (self.width, self.height); // The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set. @@ -455,23 +543,21 @@ impl<'a> Texture<'a> { let (row_pitch, size) = encoding.level_layout(width, height, pitch)?; let (w, h) = (width as usize, height as usize); let channels = encoding.layout().channels(); - let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?; + let row_size = w.checked_mul(channels).and_then(|n| n.checked_mul(sample_bytes)).ok_or_else(|| bad("pixel size overflow"))?; let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?; if total as u64 > max_bytes { return Err(DdsError::LimitExceeded { required: total, limit: max_bytes }); } let data = b.get(self.start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?; - let mut pixels = alloc_zeroed(total)?; - match encoding { - Encoding::Masks { bytes, gray, fields } => decode_masks(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size), - Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => { - decode_blocks(encoding, data, (w, channels), &mut pixels) - } - } - Ok(pixels) + Ok((data, row_pitch, total)) } } +/// A 16-bit sample rounded to the nearest 8-bit level. +fn to8(v: u16) -> u8 { + ((u32::from(v) + 128) / 257) as u8 +} + /// An uncompressed pixel format: bytes per pixel, luminance or RGB, and the red (or /// luminance), green, blue and alpha fields. struct Masks { @@ -531,6 +617,51 @@ fn decode_masks(m: Masks, data: &[u8], row_pitch: usize, pixels: &mut [u8], row_ }); } +/// [`decode_masks`] at 16 bits: every field scaled to 0..=65535, so fields wider than 8 bits +/// (luminance or red and green of 16 bits) keep their precision. +fn decode_masks16(m: Masks, data: &[u8], row_pitch: usize, pixels: &mut [u16], row_size: usize) { + let Masks { bytes, gray, fields } = m; + let channels = if gray { 1 + usize::from(fields[3].is_some()) } else { 3 + usize::from(fields[3].is_some()) }; + rows(pixels, row_size, |y, dst_row| { + let src_row = data.get(y * row_pitch..).unwrap_or_default(); + for (src, dst) in src_row.chunks_exact(bytes).zip(dst_row.chunks_exact_mut(channels)) { + let pixel = src.iter().rev().fold(0u32, |v, &byte| v << 8 | u32::from(byte)); + let channel = |i: usize| fields[i].map_or(0, |f| f.get16(pixel)); + let values = if gray { [channel(0), channel(3), 0, 0] } else { [channel(0), channel(1), channel(2), channel(3)] }; + dst.copy_from_slice(&values[..channels]); + } + }); +} + +/// BC4 or BC5 blocks into 16-bit `pixels` of `width` × `channels` samples a row: gray, or red and +/// green with blue 0, interpolated at 16 bits. +fn decode_value_blocks16(encoding: Encoding, data: &[u8], (width, channels): (usize, usize), pixels: &mut [u16]) { + let block_size = encoding.block_size().max(1); + let blocks_x = width.div_ceil(4); + let row_size = width * channels; + rows(pixels, row_size.saturating_mul(4), |by, band| { + let src = data.get(by * blocks_x * block_size..).unwrap_or_default(); + for (bx, block) in src.chunks_exact(block_size).take(blocks_x).enumerate() { + let red = bc::decode_values16(block); + let green = if matches!(encoding, Encoding::Bc5) { bc::decode_values16(block.get(8..).unwrap_or_default()) } else { [0; 16] }; + let x0 = bx * 4; + for (ty, line) in band.chunks_exact_mut(row_size).enumerate() { + let dst = line.get_mut(x0 * channels..).unwrap_or_default(); + for (tx, d) in dst.chunks_exact_mut(channels).take(4).enumerate() { + let t = ty * 4 + tx; + for (c, v) in d.iter_mut().enumerate() { + *v = match c { + 0 => red[t], + 1 => green[t], + _ => 0, + }; + } + } + } + } + }); +} + /// Block-compressed data into `pixels` of `width` × `channels` bytes a row, one row of blocks /// per task. Blocks wholly inside the image copy whole rows of texels; only those on the right /// edge check each texel. @@ -593,19 +724,47 @@ const DDSCAPS_TEXTURE: u32 = 0x1000; const DDSCAPS_MIPMAP: u32 = 0x40_0000; const DXGI_BC4_UNORM: u32 = 80; const DXGI_BC5_UNORM: u32 = 83; +const DXGI_R16G16B16A16_UNORM: u32 = 11; +const DXGI_R16G16_UNORM: u32 = 35; +const DXGI_R16_UNORM: u32 = 56; fn encode_err(reason: impl fmt::Display) -> DdsError { DdsError::Encode(reason.to_string()) } -/// A zeroed buffer of `len` bytes, or [`DdsError::Alloc`] when it can't be reserved. -fn alloc_zeroed(len: usize) -> Result, DdsError> { +/// A zeroed buffer of `len` samples, or [`DdsError::Alloc`] (in bytes) when it can't be reserved. +fn alloc_zeroed(len: usize) -> Result, DdsError> { let mut v = Vec::new(); - v.try_reserve_exact(len).map_err(|_| DdsError::Alloc(len))?; - v.resize(len, 0); + v.try_reserve_exact(len).map_err(|_| DdsError::Alloc(len.saturating_mul(size_of::())))?; + v.resize(len, T::default()); Ok(v) } +/// A sample of 8 or 16 bits: what the mip chain averages and the buffers hold. +trait Sample: Copy + Default + Send + Sync { + fn wide(self) -> u32; + /// An average already within the sample's range. + fn from_average(v: u32) -> Self; +} + +impl Sample for u8 { + fn wide(self) -> u32 { + u32::from(self) + } + fn from_average(v: u32) -> u8 { + v as u8 + } +} + +impl Sample for u16 { + fn wide(self) -> u32 { + u32::from(self) + } + fn from_average(v: u32) -> u16 { + v as u16 + } +} + /// How a written texture stores its pixels. #[derive(Clone, Copy, PartialEq, Eq)] enum Store { @@ -622,6 +781,14 @@ enum Store { Bc3, Bc4, Bc5, + /// 16-bit gray (`R16_UNORM`). + R16, + /// 16-bit gray and alpha, or any two channels (`R16G16_UNORM`). + Rg16, + /// 16-bit RGB with alpha 65535 (`R16G16B16A16_UNORM`). + Rgb16, + /// 16-bit RGBA (`R16G16B16A16_UNORM`). + Rgba16, } impl Store { @@ -642,12 +809,24 @@ impl Store { }) } + /// [`Store::new`] for 16-bit pixels: uncompressed ones keep 16 bits per channel. + fn new16(compression: DdsCompression, layout: Layout) -> Result { + Ok(match (compression, layout) { + (DdsCompression::None, Layout::Gray) => Store::R16, + (DdsCompression::None, Layout::GrayA) => Store::Rg16, + (DdsCompression::None, Layout::Rgb) => Store::Rgb16, + (DdsCompression::None, Layout::Rgba) => Store::Rgba16, + _ => Store::new(compression, layout)?, + }) + } + /// Bytes per pixel, or per 4×4 block for a block compression. fn unit(self) -> usize { match self { Store::Luminance => 1, - Store::LuminanceAlpha => 2, - Store::Bgrx | Store::Bgra => 4, + Store::LuminanceAlpha | Store::R16 => 2, + Store::Bgrx | Store::Bgra | Store::Rg16 => 4, + Store::Rgb16 | Store::Rgba16 => 8, Store::Bc1 | Store::Bc4 => 8, Store::Bc2 | Store::Bc3 | Store::Bc5 => 16, } @@ -662,6 +841,9 @@ impl Store { match self { Store::Bc4 => Some(DXGI_BC4_UNORM), Store::Bc5 => Some(DXGI_BC5_UNORM), + Store::R16 => Some(DXGI_R16_UNORM), + Store::Rg16 => Some(DXGI_R16G16_UNORM), + Store::Rgb16 | Store::Rgba16 => Some(DXGI_R16G16B16A16_UNORM), _ => None, } } @@ -686,7 +868,7 @@ impl Store { Store::Bc1 => (DDPF_FOURCC, *b"DXT1", 0, [0; 4]), Store::Bc2 => (DDPF_FOURCC, *b"DXT3", 0, [0; 4]), Store::Bc3 => (DDPF_FOURCC, *b"DXT5", 0, [0; 4]), - Store::Bc4 | Store::Bc5 => (DDPF_FOURCC, *b"DX10", 0, [0; 4]), + Store::Bc4 | Store::Bc5 | Store::R16 | Store::Rg16 | Store::Rgb16 | Store::Rgba16 => (DDPF_FOURCC, *b"DX10", 0, [0; 4]), } } } @@ -708,11 +890,40 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D return Err(encode_err("texture dimensions must be nonzero")); } let store = Store::new(options.compression, layout)?; + encode_levels((width, height), layout, pixels, store, options.mipmaps, write_level) +} + +/// [`encode`] for 16-bit pixels. Uncompressed, they keep 16 bits per channel: gray as +/// `R16_UNORM`, gray and alpha (or any two channels, such as a normal map's X and Y) as +/// `R16G16_UNORM`, RGB (with alpha 65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10 +/// header. BC4 and BC5 pick their endpoints and indices against the 16-bit values; BC1 to BC3 +/// round the pixels to 8 bits and write what [`encode`] writes from them. +pub fn encode16(width: u32, height: u32, layout: Layout, pixels: &[u16], options: DdsOptions) -> Result, DdsError> { + if width == 0 || height == 0 { + return Err(encode_err("texture dimensions must be nonzero")); + } + let store = Store::new16(options.compression, layout)?; + if matches!(store, Store::Bc1 | Store::Bc2 | Store::Bc3) { + let mut narrow = alloc_zeroed(pixels.len())?; + for (d, &v) in narrow.iter_mut().zip(pixels) { + *d = to8(v); + } + return encode(width, height, layout, &narrow, options); + } + encode_levels((width, height), layout, pixels, store, options.mipmaps, write_level16) +} + +/// A level writer: one level of `width` × `height` pixels (in the image's layout) into `dst`. +type WriteLevel = fn(Store, &[T], (usize, usize), &mut [u8]) -> Result<(), DdsError>; + +/// The header and every level of `pixels`, each written by `write`. +fn encode_levels((width, height): (u32, u32), layout: Layout, pixels: &[T], store: Store, mipmaps: bool, write: WriteLevel) -> Result, DdsError> { let expected = (width as usize).checked_mul(height as usize).and_then(|n| n.checked_mul(layout.channels())); if expected != Some(pixels.len()) { - return Err(encode_err(format!("{} bytes of pixels for a {width}x{height} {layout:?} texture", pixels.len()))); + let unit = if size_of::() == 1 { "bytes" } else { "16-bit samples" }; + return Err(encode_err(format!("{} {unit} of pixels for a {width}x{height} {layout:?} texture", pixels.len()))); } - let levels = if options.mipmaps { mip_levels(width, height) } else { 1 }; + let levels = if mipmaps { mip_levels(width, height) } else { 1 }; let top = (width as usize, height as usize); // Every level's size, checked before anything is allocated. let mut sizes = Vec::new(); @@ -724,7 +935,7 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D let head = if store.dxgi().is_some() { HEADER + DX10_HEADER } else { HEADER }; let total = sizes.iter().try_fold(head, |total, &s| total.checked_add(s)).ok_or_else(|| encode_err("texture size overflow"))?; let header = header(width, height, store, levels, sizes.first().copied().unwrap_or(0))?; - let mut out = alloc_zeroed(total)?; + let mut out = alloc_zeroed::(total)?; let (Some(dst), Some(src)) = (out.get_mut(..head), header.get(..head)) else { return Err(encode_err("output buffer too small")); }; @@ -735,7 +946,7 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D let (mut size, mut at) = (top, head); for (i, &bytes) in sizes.iter().enumerate() { let dst = out.get_mut(at..at + bytes).ok_or_else(|| encode_err("output buffer too small"))?; - write_level(store, &level, size, dst)?; + write(store, &level, size, dst)?; at += bytes; if i + 1 < sizes.len() { let next = half(size); @@ -777,7 +988,8 @@ fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Res } put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 }); if let Some(dxgi) = store.dxgi() { - // One 2D texture, no misc flags, alpha mode unknown (the formats have no alpha). + // One 2D texture, no misc flags, alpha mode unknown (straight for the one format with + // alpha, RGBA16, as readers take it). put(HEADER, dxgi); put(HEADER + 4, DIMENSION_TEXTURE2D); put(HEADER + 12, 1); @@ -788,7 +1000,7 @@ fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Res /// Runs `f` on each `len`-byte row of `out` with its index, in parallel with the `parallel` /// feature. -fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) { +fn rows(out: &mut [T], len: usize, f: impl Fn(usize, &mut [T]) + Send + Sync) { #[cfg(feature = "parallel")] { use rayon::prelude::*; @@ -804,6 +1016,8 @@ fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), DdsError> { let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?; match store { + // 16-bit stores are written by `write_level16`. + Store::R16 | Store::Rg16 | Store::Rgb16 | Store::Rgba16 => return Err(encode_err("16-bit storage needs 16-bit pixels")), Store::Luminance | Store::LuminanceAlpha => { // The image's gray (and alpha) samples, in the same order. let src = pixels.get(..dst.len()).ok_or_else(|| encode_err("the level is smaller than its size"))?; @@ -851,10 +1065,44 @@ fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst Ok(()) } +/// [`write_level`] for 16-bit pixels: little-endian samples, or BC4/BC5 blocks fitted to them. +fn write_level16(store: Store, pixels: &[u16], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), DdsError> { + let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?; + match store { + Store::R16 | Store::Rg16 | Store::Rgba16 => { + // The image's samples, in the same order. + for (d, s) in dst.as_chunks_mut::<2>().0.iter_mut().zip(pixels) { + *d = s.to_le_bytes(); + } + } + Store::Rgb16 => rows(dst, row_bytes, |y, row| { + let src = pixels.get(y * width * 3..).unwrap_or_default(); + for (d, s) in row.as_chunks_mut::<8>().0.iter_mut().zip(src.as_chunks::<3>().0) { + let [r, g, b, a] = [s[0], s[1], s[2], u16::MAX].map(u16::to_le_bytes); + *d = [r[0], r[1], g[0], g[1], b[0], b[1], a[0], a[1]]; + } + }), + Store::Bc4 | Store::Bc5 => { + let channels = if store == Store::Bc4 { 1 } else { 3 }; + rows(dst, row_bytes, |by, row| { + for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() { + for (c, d) in d.as_chunks_mut::<8>().0.iter_mut().enumerate() { + let (values, inside) = gather_channel(pixels, (width, height), channels, c, (bx, by)); + *d = bc::value_block16(&values, inside); + } + } + }); + } + // `encode16` rounds BC1 to BC3 to 8 bits; the 8-bit stores take 8-bit pixels. + _ => return Err(encode_err("8-bit storage needs 8-bit pixels")), + } + Ok(()) +} + /// Channel `c` of block (`bx`, `by`) of pixels with `channels` channels, and which of its texels /// lie inside the image. -fn gather_channel(pixels: &[u8], (width, height): (usize, usize), channels: usize, c: usize, (bx, by): (usize, usize)) -> ([u8; 16], Mask) { - let mut values = [0u8; 16]; +fn gather_channel(pixels: &[T], (width, height): (usize, usize), channels: usize, c: usize, (bx, by): (usize, usize)) -> ([T; 16], Mask) { + let mut values = [T::default(); 16]; let mut inside: Mask = 0; for (t, v) in values.iter_mut().enumerate() { let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4); @@ -902,7 +1150,7 @@ fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: us /// The next mip level: each pixel the average of the 2×2 pixels it covers, or of 2×3, 3×2 or /// 3×3 along the last column and row of an odd-sized level, so no pixel is dropped. -fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result, DdsError> { +fn downsample(src: &[T], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result, DdsError> { let len = nw.checked_mul(nh).and_then(|n| n.checked_mul(channels)).ok_or_else(|| encode_err("mip level size overflow"))?; let mut dst = alloc_zeroed(len)?; let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 }; @@ -919,8 +1167,8 @@ fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), chan let interior = nw.saturating_sub(1); for ((px, a), b) in row.chunks_exact_mut(ch).zip(r0.chunks_exact(2 * ch)).zip(r1.chunks_exact(2 * ch)).take(interior) { for (c, v) in px.iter_mut().enumerate() { - let sum = u32::from(a[c]) + u32::from(a[ch + c]) + u32::from(b[c]) + u32::from(b[ch + c]); - *v = ((sum + 2) / 4) as u8; + let sum = a[c].wide() + a[ch + c].wide() + b[c].wide() + b[ch + c].wide(); + *v = T::from_average((sum + 2) / 4); } } edge = interior; @@ -932,12 +1180,12 @@ fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), chan for sy in ys.clone() { for sx in xs.clone() { if let Some(&s) = src.get((sy * w + sx) * channels + c) { - sum += u32::from(s); + sum += s.wide(); n += 1; } } } - *v = ((sum + n / 2) / n.max(1)) as u8; + *v = T::from_average((sum + n / 2) / n.max(1)); } } }); @@ -980,4 +1228,127 @@ mod tests { assert!(matches!(texture.decode(), Err(DdsError::Malformed(_)))); assert_eq!(Texture::parse(&file).and_then(|t| t.decode()), Ok(vec![7; 64])); } + + /// Samples spread over the whole 16-bit range, `channels` a pixel. + fn spread16(width: usize, height: usize, channels: usize) -> Vec { + (0..width * height * channels).map(|i| (i as u32 * 7919 % 65536) as u16).collect() + } + + #[test] + fn uncompressed_16_bit_pixels_round_trip_exactly() { + let (w, h) = (7, 5); + for (layout, dxgi, read_as, pixel_bytes) in [(Layout::Gray, 56, Layout::Gray, 2), (Layout::GrayA, 35, Layout::GrayA, 4), (Layout::Rgb, 11, Layout::Rgba, 8), (Layout::Rgba, 11, Layout::Rgba, 8)] { + let pixels = spread16(w, h, layout.channels()); + let want: Vec = if layout == Layout::Rgb { pixels.chunks(3).flat_map(|p| [p[0], p[1], p[2], 65535]).collect() } else { pixels.clone() }; + for mipmaps in [false, true] { + let file = encode16(w as u32, h as u32, layout, &pixels, DdsOptions { compression: DdsCompression::None, mipmaps }).unwrap(); + assert_eq!(le32(&file, HEADER).unwrap(), dxgi, "{layout:?}"); + // 7×5, then 3×2 and 1×1. + let samples = if mipmaps { 7 * 5 + 3 * 2 + 1 } else { 7 * 5 }; + assert_eq!(file.len(), HEADER + DX10_HEADER + samples * pixel_bytes); + let texture = Texture::parse(&file).unwrap(); + assert_eq!(texture.layout(), read_as); + assert_eq!(texture.decode16().unwrap(), want); + assert_eq!(texture.decode().unwrap(), want.iter().map(|&v| to8(v)).collect::>()); + if layout == Layout::Rgba { + let mut premultiplied = file.clone(); + premultiplied[HEADER + 16] = 2; + assert!(matches!(Texture::parse(&premultiplied), Err(DdsError::Unsupported(_)))); + } + } + } + } + + #[test] + fn mip_levels_of_16_bit_pixels_average_at_16_bits() { + // 1000 to 1003 average to 1002 (rounded), which 8 bits can't hold. + let file = encode16(2, 2, Layout::Gray, &[1000, 1001, 1002, 1003], DdsOptions { compression: DdsCompression::None, mipmaps: true }).unwrap(); + assert_eq!(le32(&file, 28).unwrap(), 2); + assert_eq!(file[file.len() - 2..], 1002u16.to_le_bytes()); + } + + #[test] + fn legacy_16_bit_textures_decode_at_16_bits() { + // D3DFMT_A16B16G16R16: the legacy header's numeric FourCC 36, the same pixels as the + // DX10 file. + let pixels = spread16(3, 2, 4); + let file = encode16(3, 2, Layout::Rgba, &pixels, DdsOptions::default()).unwrap(); + let mut legacy = file[..HEADER].to_vec(); + legacy[84..88].copy_from_slice(&36u32.to_le_bytes()); + legacy.extend_from_slice(&file[HEADER + DX10_HEADER..]); + let texture = Texture::parse(&legacy).unwrap(); + assert_eq!((texture.layout(), texture.decode16().unwrap()), (Layout::Rgba, pixels)); + // L16: a 16-bit luminance mask keeps its 16 bits through `decode16`. + let mut l16 = encode(2, 1, Layout::Gray, &[0, 0], DdsOptions::default()).unwrap(); + l16[20..24].copy_from_slice(&4u32.to_le_bytes()); + l16[88..92].copy_from_slice(&16u32.to_le_bytes()); + l16[92..96].copy_from_slice(&0xffffu32.to_le_bytes()); + l16.truncate(HEADER); + l16.extend([300u16, 65000].iter().flat_map(|v| v.to_le_bytes())); + let texture = Texture::parse(&l16).unwrap(); + assert_eq!(texture.decode16().unwrap(), [300, 65000]); + assert_eq!(texture.decode().unwrap(), [1, 253]); + } + + #[test] + fn bc4_and_bc5_from_16_bits_beat_the_8_bit_rounded_source() { + // Smooth ramps, about half an 8-bit level across a block, as in height and normal maps. + // 62×61 leaves partial blocks on the right and bottom edges. + let (w, h) = (62usize, 61usize); + let at = |i: usize| (i % w, i / w); + let height: Vec = (0..w * h).map(|i| (20_000 + at(i).0 * 29 + at(i).1 * 11) as u16).collect(); + let normal: Vec = (0..w * h).flat_map(|i| [(10_000 + at(i).0 * 23 + at(i).1 * 7) as u16, (40_000 + at(i).1 * 27 + at(i).0 * 5) as u16, 65535]).collect(); + for (compression, layout, pixels) in [(DdsCompression::Bc4, Layout::Gray, &height), (DdsCompression::Bc5, Layout::Rgb, &normal)] { + let options = DdsOptions { compression, mipmaps: true }; + let rounded: Vec = pixels.iter().map(|&v| to8(v)).collect(); + let from16 = Texture::parse(&encode16(w as u32, h as u32, layout, pixels, options).unwrap()).unwrap(); + let from8 = Texture::parse(&encode(w as u32, h as u32, layout, &rounded, options).unwrap()).unwrap(); + let channels = layout.channels(); + // Root-mean-square error of the stored channels (BC5: red and green), in 16-bit units. + let rms = |got: &[u16]| { + let stored = pixels.iter().zip(got).enumerate().filter(|(i, _)| i % channels < 2); + let (sum, n) = stored.fold((0f64, 0f64), |(sum, n), (_, (&a, &b))| (sum + (f64::from(a) - f64::from(b)).powi(2), n + 1.0)); + (sum / n).sqrt() + }; + let (ours, theirs) = (rms(&from16.decode16().unwrap()), rms(&from8.decode16().unwrap())); + eprintln!("{compression:?}: RMS error {ours:.1} from 16 bits, {theirs:.1} from the 8-bit rounded source (16-bit units)"); + assert!(ours * 2.0 < theirs, "{compression:?}: {ours} vs {theirs}"); + // `decode` and `decode16` read the same blocks: 8 bits are the 16-bit values rounded, + // give or take the 8-bit palette's own rounding. + for texture in [&from16, &from8] { + let (narrow, wide) = (texture.decode().unwrap(), texture.decode16().unwrap()); + assert!(narrow.iter().zip(&wide).all(|(&a, &b)| a.abs_diff(to8(b)) <= 1)); + } + } + } + + #[test] + fn bc1_to_bc3_from_16_bits_write_what_the_rounded_pixels_write() { + let pixels = spread16(6, 5, 4); + let rounded: Vec = pixels.iter().map(|&v| to8(v)).collect(); + for compression in [DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3] { + for mipmaps in [false, true] { + let options = DdsOptions { compression, mipmaps }; + assert_eq!(encode16(6, 5, Layout::Rgba, &pixels, options), encode(6, 5, Layout::Rgba, &rounded, options)); + } + } + // `encode16` takes the layouts `encode` takes, and checks the buffer size the same way. + for compression in DdsCompression::ALL { + for layout in [Layout::Gray, Layout::GrayA, Layout::Rgb, Layout::Rgba] { + let options = DdsOptions { compression, mipmaps: false }; + let n = layout.channels(); + assert_eq!(encode16(1, 1, layout, &[9; 4][..n], options).is_ok(), encode(1, 1, layout, &[9; 4][..n], options).is_ok(), "{compression:?} from {layout:?}"); + } + } + assert!(matches!(encode16(2, 2, Layout::Rgb, &[0; 11], DdsOptions::default()), Err(DdsError::Encode(_)))); + assert!(matches!(encode16(0, 2, Layout::Rgb, &[], DdsOptions::default()), Err(DdsError::Encode(_)))); + } + + #[test] + fn decode16_checks_its_budget_in_bytes() { + let file = encode16(4, 4, Layout::Gray, &[7; 16], DdsOptions::default()).unwrap(); + let texture = Texture::parse(&file).unwrap(); + assert!(matches!(texture.decode16_with_limit(31), Err(DdsError::LimitExceeded { required: 32, limit: 31 }))); + assert_eq!(texture.decode16_with_limit(32).unwrap(), [7; 16]); + } } From 5ef5134ccc28db3a3aaba1e1a0c189340c5d277c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 15:55:15 -0500 Subject: [PATCH 360/471] image-codecs: keep stored WebP dimensions independent of EXIF compatibility (#1921) --- crates/image-codecs/src/webp_probe.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs index 07d6e0269..4a31def00 100644 --- a/crates/image-codecs/src/webp_probe.rs +++ b/crates/image-codecs/src/webp_probe.rs @@ -25,9 +25,10 @@ pub fn header(bytes: &[u8]) -> Result<(u32, u32, u16), CodecError> { if !has_image { return Err(err("no image data")); } - let (w, h) = crate::probe_size(bytes)?; + // Read stored dimensions directly. Undoing probe_size with the compatibility EXIF + // orientation is incorrect when its lenient parser disagrees with the strict one. + let (w, h, _) = crate::codecs::webp::probe(bytes, &crate::Limits::default())?; let orientation = exif.map_or(1, craft_metadata::compat_orientation::orientation); - let (w, h) = if (5..=8).contains(&orientation) { (h, w) } else { (w, h) }; Ok((w, h, orientation)) } @@ -47,6 +48,20 @@ mod tests { assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); } + #[test] + fn malformed_exif_does_not_swap_stored_dimensions() { + let mut image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + image.meta.exif = Some(b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00".to_vec()); + let mut bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + let exif = bytes.windows(4).position(|v| v == b"EXIF").unwrap() + 8; + bytes[exif + 18] = 6; + // Compatibility metadata preserves orientation despite a malformed count; + // the strict orientation used by probe_size rejects it. Stored size is neither. + bytes[exif + 14..exif + 18].copy_from_slice(&0u32.to_le_bytes()); + assert_eq!(crate::probe_size(&bytes).unwrap(), (2, 3)); + assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); + } + #[test] fn rejects_missing_and_truncated_image_chunks() { let mut bytes = b"RIFF\x00\x00\x00\x00WEBP".to_vec(); From e4d27fc1cd030be8c1d163220d07f9a00ee0fb8d Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 21:58:20 +0100 Subject: [PATCH 361/471] fcproj-format: schema v18 for camera raw Source Settings A no-op v17 -> v18 step so FilmCraft can save per-clip camera raw Source Settings (Interpretation::raw) and older builds refuse such projects instead of dropping the settings on save. Library part of storytold/filmcraft#1078 (Part of storytold/filmcraft#1065); the MXF CinemaDNG part already landed in #1145. --- crates/fcproj-format/src/lib.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/fcproj-format/src/lib.rs b/crates/fcproj-format/src/lib.rs index 98e50cf59..4ac1b7243 100644 --- a/crates/fcproj-format/src/lib.rs +++ b/crates/fcproj-format/src/lib.rs @@ -13,7 +13,7 @@ pub type Migration = fn(Value) -> Result; /// edit a shipped one. pub const MIGRATIONS: &[Migration] = &[ v1_to_v2, v2_to_v3, v3_to_v4, v4_to_v5, v5_to_v6, v6_to_v7, v7_to_v8, v8_to_v9, v9_to_v10, v10_to_v11, v11_to_v12, v12_to_v13, v13_to_v14, - v14_to_v15, v15_to_v16, v16_to_v17, + v14_to_v15, v15_to_v16, v16_to_v17, v17_to_v18, ]; /// The schema version this build writes (and the newest it reads). @@ -252,6 +252,14 @@ fn v16_to_v17(doc: Value) -> Result { Ok(doc) } +/// v17 → v18: camera raw Source Settings on media items (`Interpretation::raw`: white balance, +/// exposure, highlights, demosaic quality and output colour space of DNG / CinemaDNG footage; +/// absent = as the file says). Existing data needs no change; older builds must refuse such +/// projects instead of silently dropping the settings when saving. +fn v17_to_v18(doc: Value) -> Result { + Ok(doc) +} + #[cfg(test)] mod tests { use super::*; @@ -262,10 +270,10 @@ mod tests { let bare = json!({"version": 2, "items": {}, "root": {}, "extra": [1, 2]}); assert_eq!(detect_version(&bare).unwrap(), 1); let migrated = migrate_with(MIGRATIONS, bare, 1).unwrap(); - assert_eq!(migrated["schema_version"], 17); + assert_eq!(migrated["schema_version"], 18); assert_eq!(migrated["project"]["extra"], json!([1, 2])); assert_eq!(migrated["generator"], "FilmCraft (schema v1)"); - assert_eq!(detect_bytes(serde_json::to_string(&migrated).unwrap().as_bytes()).unwrap(), 17); + assert_eq!(detect_bytes(serde_json::to_string(&migrated).unwrap().as_bytes()).unwrap(), 18); let parsed: EnvelopeIn = serde_json::from_value(migrated).unwrap(); assert!(parsed.view.is_none()); assert_eq!(parsed.project["extra"], json!([1, 2])); @@ -286,10 +294,27 @@ mod tests { assert_eq!(version, 1); assert_eq!(file.project["name"], "Cut"); } + /// Schema 18 (camera raw Source Settings): a v17 project upgrades with its data untouched, and a + /// v18 project with `interpret.raw` settings is read rather than refused as too new. + #[test] + fn v17_upgrades_and_v18_raw_settings_are_read() { + let item = json!({"Media": {"interpret": {"par": [1, 1]}}}); + let v17 = json!({"format": FORMAT_ID, "schema_version": 17, "project": {"items": {"1": {"kind": item}}, "root": {}}}); + let up = migrate_with(MIGRATIONS, v17.clone(), 17).unwrap(); + assert_eq!(up["schema_version"], 18); + assert_eq!(up["project"], v17["project"]); + let raw = json!({"white_balance": {"Custom": {"temperature": 3200.0, "tint": 12.5}}, "exposure": -0.75}); + let mut v18 = up; + v18["project"]["items"]["1"]["kind"]["Media"]["interpret"]["raw"] = raw.clone(); + let bytes = serde_json::to_vec(&v18).unwrap(); + let (file, version): (EnvelopeIn, _) = decode(&bytes).unwrap(); + assert_eq!(version, 18); + assert_eq!(file.project["items"]["1"]["kind"]["Media"]["interpret"]["raw"], raw); + } #[test] fn rejects_wrong_future_and_damaged_envelopes() { assert!(matches!(detect_bytes(br#"{"format":"other","schema_version":1}"#), Err(FormatError::NotAProject(_)))); - assert!(matches!(detect_bytes(br#"{"schema_version":18}"#), Err(FormatError::TooNew { found: 18, supported: 17 }))); + assert!(matches!(detect_bytes(br#"{"schema_version":19}"#), Err(FormatError::TooNew { found: 19, supported: 18 }))); assert!(matches!(detect_bytes(br#"{"schema_version":0}"#), Err(FormatError::TooOld { found: 0, oldest: 1 }))); assert!(matches!(detect_bytes(b"{"), Err(FormatError::Corrupt(_)))); assert!(matches!(migrate_with(MIGRATIONS, json!([]), 1), Err(FormatError::Migration { from: 1, .. }))); From 853f630d6d54eccc90a6a018fc03fabe698880fc Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 21:59:19 +0100 Subject: [PATCH 362/471] dds: review fixes for 16-bit support Reword decode16 and DdsCompression::None docs, check the 16-bit buffer length before narrowing for BC1-BC3, and test typeless DXGI codes, padded 16-bit rows, G16R16 masks and decode16 of BC1. --- crates/dds/README.md | 4 +- crates/dds/src/bc.rs | 2 +- crates/dds/src/lib.rs | 105 +++++++++++++++++++++++++++++++++++------- 3 files changed, 92 insertions(+), 19 deletions(-) diff --git a/crates/dds/README.md b/crates/dds/README.md index 7a25eb36e..2dae26bc1 100644 --- a/crates/dds/README.md +++ b/crates/dds/README.md @@ -35,7 +35,9 @@ and alpha, and RGBA. BC4 and BC5 keep 8-bit endpoints, as the formats store them and the indices against the 16-bit values, so a smooth height or normal map has less error than when its 8-bit rounding is compressed. BC1 to BC3 round the pixels to 8 bits first. `Texture::decode` rounds 16-bit formats to 8 bits; `Texture::decode16` returns them exactly, -interpolates BC4 and BC5 at 16 bits, and scales every 8-bit format by 257. +rescales channel-mask fields from their own width (a 16-bit luminance field exactly), +interpolates BC4 and BC5 at 16 bits, and scales BC1 to BC3 by 257. `craft-image-codecs` still +reads DDS at 8 bits. ```rust use craft_dds::{DdsCompression, DdsOptions, Layout, Texture}; diff --git a/crates/dds/src/bc.rs b/crates/dds/src/bc.rs index c19a393f3..7000a09e4 100644 --- a/crates/dds/src/bc.rs +++ b/crates/dds/src/bc.rs @@ -579,7 +579,7 @@ mod tests { let bits: u64 = (0..16).fold(0, |b, t| b | 2 << (3 * t)); let b = bits.to_le_bytes(); let block = [200, 50, b[0], b[1], b[2], b[3], b[4], b[5]]; - assert_eq!(decode_values16(&block), [((6 * 200 + 50) * 257 + 3) / 7; 16]); + assert_eq!(decode_values16(&block), [(((6 * 200 + 50) * 257 + 3) / 7_u32) as u16; 16]); assert_eq!(decode_values(&block), [179; 16]); // Six-value mode's indices 6 and 7 are 0 and 65535. let bits: u64 = (0..16).fold(0, |b, t| b | (6 + (t % 2)) << (3 * t)); diff --git a/crates/dds/src/lib.rs b/crates/dds/src/lib.rs index 336994285..183ec4fa1 100644 --- a/crates/dds/src/lib.rs +++ b/crates/dds/src/lib.rs @@ -11,7 +11,7 @@ //! //! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha), //! BC1/BC2/BC3 or BC4/BC5, optionally with its full mip chain. The legacy header describes all -//! but BC4 and BC5, which are written with the DX10 header (`BC4_UNORM`, `BC5_UNORM`): their +//! but BC4, BC5 and the 16-bit formats, which are written with the DX10 header (`BC4_UNORM`, `BC5_UNORM`): their //! legacy FourCCs are vendor names that programs read differently (ATI2's two blocks are swapped //! by some). BC6H, BC7 and sRGB formats aren't written. //! @@ -105,7 +105,7 @@ impl std::error::Error for DdsError {} #[non_exhaustive] pub enum DdsCompression { /// Uncompressed, 8 bits per channel: BGRA (BGRX without alpha), or luminance with or - /// without alpha for grayscale. Lossless. + /// without alpha for grayscale. From [`encode16`], 16 bits per channel instead. Lossless. #[default] None, /// BC1 (DXT1): 4 bits per pixel. Alpha is on or off: texels under half opacity become @@ -270,7 +270,9 @@ enum Encoding { Bc2, Bc3, /// 16 bits per channel, little-endian: gray, gray and alpha (or red and green), or RGBA. - Unorm16 { channels: usize }, + Unorm16 { + channels: usize, + }, /// One value block: gray. Bc4, /// Two value blocks: red, then green. @@ -499,9 +501,10 @@ impl<'a> Texture<'a> { } /// The top mip level's pixels in [`Texture::layout`] at 16 bits per channel, bounded by - /// [`DEFAULT_MAX_DECODE_BYTES`]. 16-bit formats come back exactly, as do channel masks wider - /// than 8 bits; BC4 and BC5 values are interpolated at 16 bits; every other format is its - /// 8-bit value times 257 (so 255 is 65535). + /// [`DEFAULT_MAX_DECODE_BYTES`]. 16-bit formats come back exactly; channel-mask fields are + /// rescaled from their own width to 0..=65535 (a 16-bit luminance field exactly, an 8-bit one + /// times 257); BC4 and BC5 values are interpolated at 16 bits; BC1 to BC3 are their 8-bit + /// values times 257 (so 255 is 65535). pub fn decode16(&self) -> Result, DdsError> { self.decode16_with_limit(DEFAULT_MAX_DECODE_BYTES) } @@ -903,6 +906,7 @@ pub fn encode16(width: u32, height: u32, layout: Layout, pixels: &[u16], options return Err(encode_err("texture dimensions must be nonzero")); } let store = Store::new16(options.compression, layout)?; + check_len((width, height), layout, pixels)?; if matches!(store, Store::Bc1 | Store::Bc2 | Store::Bc3) { let mut narrow = alloc_zeroed(pixels.len())?; for (d, &v) in narrow.iter_mut().zip(pixels) { @@ -913,16 +917,29 @@ pub fn encode16(width: u32, height: u32, layout: Layout, pixels: &[u16], options encode_levels((width, height), layout, pixels, store, options.mipmaps, write_level16) } -/// A level writer: one level of `width` × `height` pixels (in the image's layout) into `dst`. -type WriteLevel = fn(Store, &[T], (usize, usize), &mut [u8]) -> Result<(), DdsError>; - -/// The header and every level of `pixels`, each written by `write`. -fn encode_levels((width, height): (u32, u32), layout: Layout, pixels: &[T], store: Store, mipmaps: bool, write: WriteLevel) -> Result, DdsError> { +/// Refuses a pixel buffer that isn't `width` × `height` pixels of `layout`. +fn check_len((width, height): (u32, u32), layout: Layout, pixels: &[T]) -> Result<(), DdsError> { let expected = (width as usize).checked_mul(height as usize).and_then(|n| n.checked_mul(layout.channels())); if expected != Some(pixels.len()) { let unit = if size_of::() == 1 { "bytes" } else { "16-bit samples" }; return Err(encode_err(format!("{} {unit} of pixels for a {width}x{height} {layout:?} texture", pixels.len()))); } + Ok(()) +} + +/// A level writer: one level of `width` × `height` pixels (in the image's layout) into `dst`. +type WriteLevel = fn(Store, &[T], (usize, usize), &mut [u8]) -> Result<(), DdsError>; + +/// The header and every level of `pixels`, each written by `write`. +fn encode_levels( + (width, height): (u32, u32), + layout: Layout, + pixels: &[T], + store: Store, + mipmaps: bool, + write: WriteLevel, +) -> Result, DdsError> { + check_len((width, height), layout, pixels)?; let levels = if mipmaps { mip_levels(width, height) } else { 1 }; let top = (width as usize, height as usize); // Every level's size, checked before anything is allocated. @@ -1237,9 +1254,15 @@ mod tests { #[test] fn uncompressed_16_bit_pixels_round_trip_exactly() { let (w, h) = (7, 5); - for (layout, dxgi, read_as, pixel_bytes) in [(Layout::Gray, 56, Layout::Gray, 2), (Layout::GrayA, 35, Layout::GrayA, 4), (Layout::Rgb, 11, Layout::Rgba, 8), (Layout::Rgba, 11, Layout::Rgba, 8)] { + for (layout, dxgi, read_as, pixel_bytes) in [ + (Layout::Gray, 56, Layout::Gray, 2), + (Layout::GrayA, 35, Layout::GrayA, 4), + (Layout::Rgb, 11, Layout::Rgba, 8), + (Layout::Rgba, 11, Layout::Rgba, 8), + ] { let pixels = spread16(w, h, layout.channels()); - let want: Vec = if layout == Layout::Rgb { pixels.chunks(3).flat_map(|p| [p[0], p[1], p[2], 65535]).collect() } else { pixels.clone() }; + let want: Vec = + if layout == Layout::Rgb { pixels.chunks(3).flat_map(|p| [p[0], p[1], p[2], 65535]).collect() } else { pixels.clone() }; for mipmaps in [false, true] { let file = encode16(w as u32, h as u32, layout, &pixels, DdsOptions { compression: DdsCompression::None, mipmaps }).unwrap(); assert_eq!(le32(&file, HEADER).unwrap(), dxgi, "{layout:?}"); @@ -1288,6 +1311,40 @@ mod tests { let texture = Texture::parse(&l16).unwrap(); assert_eq!(texture.decode16().unwrap(), [300, 65000]); assert_eq!(texture.decode().unwrap(), [1, 253]); + // G16R16 as masks (red low, green high) reads as RGB at 16 bits, blue 0. + let mut g16r16 = l16[..HEADER].to_vec(); + g16r16[80..84].copy_from_slice(&DDPF_RGB.to_le_bytes()); + g16r16[20..24].copy_from_slice(&8u32.to_le_bytes()); + g16r16[88..92].copy_from_slice(&32u32.to_le_bytes()); + g16r16[96..100].copy_from_slice(&0xffff_0000u32.to_le_bytes()); + g16r16.extend([300u16, 40000, 65535, 7].iter().flat_map(|v| v.to_le_bytes())); + let texture = Texture::parse(&g16r16).unwrap(); + assert_eq!((texture.layout(), texture.decode16().unwrap()), (Layout::Rgb, vec![300, 40000, 0, 65535, 7, 0])); + } + + #[test] + fn typeless_and_padded_16_bit_textures_are_read() { + // Rows padded to 8 bytes, as other writers may store them: the padding is skipped. + let file = encode16(3, 2, Layout::Gray, &[1, 2, 3, 4, 5, 6], DdsOptions::default()).unwrap(); + let mut padded = file[..HEADER + DX10_HEADER].to_vec(); + padded[20..24].copy_from_slice(&8u32.to_le_bytes()); + for row in file[HEADER + DX10_HEADER..].chunks(6) { + padded.extend_from_slice(row); + padded.extend_from_slice(&[0xee, 0xee]); + } + assert_eq!(Texture::parse(&padded).unwrap().decode16().unwrap(), [1, 2, 3, 4, 5, 6]); + // The typeless forms read as the UNORM ones; typeless RGBA refuses premultiplied alpha too. + for (unorm, typeless, layout) in [(56u32, 53u32, Layout::Gray), (35, 33, Layout::GrayA), (11, 9, Layout::Rgba)] { + let pixels = spread16(3, 2, layout.channels()); + let file = encode16(3, 2, layout, &pixels, DdsOptions::default()).unwrap(); + assert_eq!(le32(&file, HEADER).unwrap(), unorm); + let mut retyped = file.clone(); + retyped[HEADER..HEADER + 4].copy_from_slice(&typeless.to_le_bytes()); + let texture = Texture::parse(&retyped).unwrap(); + assert_eq!((texture.layout(), texture.decode16().unwrap()), (layout, pixels)); + retyped[HEADER + 16] = 2; + assert_eq!(Texture::parse(&retyped).is_err(), layout == Layout::Rgba); + } } #[test] @@ -1297,12 +1354,15 @@ mod tests { let (w, h) = (62usize, 61usize); let at = |i: usize| (i % w, i / w); let height: Vec = (0..w * h).map(|i| (20_000 + at(i).0 * 29 + at(i).1 * 11) as u16).collect(); - let normal: Vec = (0..w * h).flat_map(|i| [(10_000 + at(i).0 * 23 + at(i).1 * 7) as u16, (40_000 + at(i).1 * 27 + at(i).0 * 5) as u16, 65535]).collect(); + let normal: Vec = + (0..w * h).flat_map(|i| [(10_000 + at(i).0 * 23 + at(i).1 * 7) as u16, (40_000 + at(i).1 * 27 + at(i).0 * 5) as u16, 65535]).collect(); for (compression, layout, pixels) in [(DdsCompression::Bc4, Layout::Gray, &height), (DdsCompression::Bc5, Layout::Rgb, &normal)] { let options = DdsOptions { compression, mipmaps: true }; let rounded: Vec = pixels.iter().map(|&v| to8(v)).collect(); - let from16 = Texture::parse(&encode16(w as u32, h as u32, layout, pixels, options).unwrap()).unwrap(); - let from8 = Texture::parse(&encode(w as u32, h as u32, layout, &rounded, options).unwrap()).unwrap(); + let (file16, file8) = + (encode16(w as u32, h as u32, layout, pixels, options).unwrap(), encode(w as u32, h as u32, layout, &rounded, options).unwrap()); + let from16 = Texture::parse(&file16).unwrap(); + let from8 = Texture::parse(&file8).unwrap(); let channels = layout.channels(); // Root-mean-square error of the stored channels (BC5: red and green), in 16-bit units. let rms = |got: &[u16]| { @@ -1337,10 +1397,21 @@ mod tests { for layout in [Layout::Gray, Layout::GrayA, Layout::Rgb, Layout::Rgba] { let options = DdsOptions { compression, mipmaps: false }; let n = layout.channels(); - assert_eq!(encode16(1, 1, layout, &[9; 4][..n], options).is_ok(), encode(1, 1, layout, &[9; 4][..n], options).is_ok(), "{compression:?} from {layout:?}"); + assert_eq!( + encode16(1, 1, layout, &[9; 4][..n], options).is_ok(), + encode(1, 1, layout, &[9; 4][..n], options).is_ok(), + "{compression:?} from {layout:?}" + ); } } assert!(matches!(encode16(2, 2, Layout::Rgb, &[0; 11], DdsOptions::default()), Err(DdsError::Encode(_)))); + let bc1 = DdsOptions { compression: DdsCompression::Bc1, mipmaps: false }; + assert_eq!(encode16(1, 1, Layout::Rgba, &[0; 3], bc1), Err(DdsError::Encode("3 16-bit samples of pixels for a 1x1 Rgba texture".into()))); + // Reading BC1 to BC3 at 16 bits: their 8-bit values times 257. + let file = encode16(6, 5, Layout::Rgba, &pixels, bc1).unwrap(); + let texture = Texture::parse(&file).unwrap(); + let narrow = texture.decode().unwrap(); + assert_eq!(texture.decode16().unwrap(), narrow.iter().map(|&v| u16::from(v) * 257).collect::>()); assert!(matches!(encode16(0, 2, Layout::Rgb, &[], DdsOptions::default()), Err(DdsError::Encode(_)))); } From 892ef9116c432d7afb5c08aba93e93a4e0b1548f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 16:21:25 -0500 Subject: [PATCH 363/471] ci: accept moved stable rust toolchain pin (#1472) From deaee4e8e10ad3e4a2406b8fe7213562d5e538c2 Mon Sep 17 00:00:00 2001 From: KenM76 Date: Mon, 16 Nov 2026 16:47:28 -0500 Subject: [PATCH 364/471] craft-mesh: decode a glTF data URI's percent escapes to bytes A base64 body with `%2B`, `%2F` or `%3D` escapes was refused as bad base64, and a plain body lost every escaped byte that was not UTF-8 to U+FFFD. Escapes are now decoded to bytes first, then base64 when the URI says so. `uri_decode` is unchanged for file names. Closes #1936 Co-Authored-By: Claude Opus 5.5 --- crates/mesh/src/gltf.rs | 13 ++++++++++--- crates/mesh/src/tests/gltf.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/mesh/src/gltf.rs b/crates/mesh/src/gltf.rs index 9d45a4453..e92b1fcba 100644 --- a/crates/mesh/src/gltf.rs +++ b/crates/mesh/src/gltf.rs @@ -115,6 +115,11 @@ pub use crate::base64_decode; /// Percent-decode a URI. A `%` that two hex digits do not follow stays as it is. pub fn uri_decode(s: &str) -> String { + String::from_utf8_lossy(&percent_decode(s)).into_owned() +} + +/// [`uri_decode`] as bytes, so escaped bytes that are not UTF-8 survive. +fn percent_decode(s: &str) -> Vec { let b = s.as_bytes(); let mut out = Vec::with_capacity(b.len()); let mut i = 0; @@ -128,7 +133,7 @@ pub fn uri_decode(s: &str) -> String { i += 1; } } - String::from_utf8_lossy(&out).into_owned() + out } /// The media type and bytes of a `data:` URI. @@ -137,10 +142,12 @@ fn data_uri(uri: &str) -> Option)>> { Some((|| { let (meta, data) = rest.split_once(',').ok_or_else(|| malformed(KIND, "a data URI without a comma"))?; let mime = meta.split(';').next().unwrap_or("").to_string(); + // Any character may be escaped (RFC 3986 2.1), base64's `+`, `/` and `=` included. + let bytes = percent_decode(data); let bytes = if meta.ends_with(";base64") { - base64_decode(data).ok_or_else(|| malformed(KIND, "bad base64 in a data URI"))? + std::str::from_utf8(&bytes).ok().and_then(base64_decode).ok_or_else(|| malformed(KIND, "bad base64 in a data URI"))? } else { - uri_decode(data).into_bytes() + bytes }; Ok((mime, bytes)) })()) diff --git a/crates/mesh/src/tests/gltf.rs b/crates/mesh/src/tests/gltf.rs index d35b63d57..d1386dce2 100644 --- a/crates/mesh/src/tests/gltf.rs +++ b/crates/mesh/src/tests/gltf.rs @@ -152,6 +152,34 @@ fn gltf_text(buffer: &str, image: &str) -> (Vec, Vec) { (serde_json::to_vec(&doc).unwrap(), b.bin) } +/// What a `.gltf` file holding `buffer` and `image` as URIs reads as: the image's bytes, or +/// the error. +fn embedded(buffer: &str, image: &str) -> std::result::Result>, Error> { + let (doc, _) = gltf_text(buffer, image); + read(&doc, &Limits::default(), &mut |_| None).map(|s| s.images[0].data.clone()) +} + +/// A data URI may escape any character (RFC 3986 §2.1), base64's `+`, `/` and `=` included. +#[test] +fn percent_escapes_in_a_data_uri_are_decoded_to_bytes() { + let bin = gltf_text("", "").1; + let buffer = format!("data:application/octet-stream;base64,{}", base64(&bin)); + // 0xFB 0xFF 0xBF: "+/+/" in base64. + let image = [0xFB, 0xFF, 0xBF, 0xFB, 0xFF, 0xBF]; + assert_eq!(base64(&image), "+/+/+/+/"); + let escaped = |s: &str| s.replace('+', "%2B").replace('/', "%2f").replace('=', "%3D"); + let want = Some(image.to_vec()); + let plain_image = format!("data:image/png;base64,{}", base64(&image)); + let (head, body) = buffer.split_once(',').unwrap(); + let every = format!("{head},{}", body.bytes().map(|c| format!("%{c:02X}")).collect::()); + assert_eq!(embedded(&every, &plain_image).unwrap(), want, "a buffer escaped throughout"); + assert_eq!(embedded(&buffer, &escaped(&plain_image)).unwrap(), want, "an escaped image"); + // Not base64: the escaped bytes as they are, high bytes too. + assert_eq!(embedded(&buffer, "data:image/png,%FB%FF%BF%FB%ff%bf").unwrap(), want); + assert_eq!(embedded(&buffer, "data:,a%20b%").unwrap(), Some(b"a b%".to_vec())); + assert_eq!(embedded(&buffer, "data:image/png;base64,%25").unwrap(), None, "an escaped % is not base64"); +} + #[test] fn embedded_and_external_resources() { // Embedded: data URIs for the buffer and the image. From 8c1498d1f82308ea4bc5ac744e83d1f62b11dbd8 Mon Sep 17 00:00:00 2001 From: KenM76 Date: Mon, 16 Nov 2026 16:53:23 -0500 Subject: [PATCH 365/471] craft-mesh: warn when glTF morph targets or other scenes are left out A primitive's morph targets are not read, and only one scene becomes the roots. Both now add a warning: a count of the primitives drawn in their base shape, and which scene of how many was read. Closes #1938. Co-Authored-By: Claude Opus 5.5 --- crates/mesh/src/gltf.rs | 15 ++++++++++++--- crates/mesh/src/tests/gltf.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/crates/mesh/src/gltf.rs b/crates/mesh/src/gltf.rs index 9d45a4453..2581aeafd 100644 --- a/crates/mesh/src/gltf.rs +++ b/crates/mesh/src/gltf.rs @@ -15,8 +15,9 @@ //! same accessors), and each primitive a [`Part`]. //! //! Not read: required extensions that change how geometry is stored (Draco, meshopt) are -//! [`Error::Unsupported`]; points, lines, morph targets and -//! other extensions are passed over (skipped primitives are counted in a warning). +//! [`Error::Unsupported`]; points, lines, morph targets, scenes other than the default and +//! other extensions are passed over (skipped primitives, primitives with morph targets and the +//! other scenes are named in warnings). //! //! Buffers are needed to read anything, so an external buffer is asked of the resolver (by its //! percent-decoded URI) and is [`Error::Missing`] when declined. External images are only named @@ -345,6 +346,7 @@ impl Doc<'_> { struct Skipped { triangles: usize, primitives: usize, + morphs: usize, } fn rows_of(v: &[f64]) -> impl Iterator + '_ { @@ -439,6 +441,7 @@ fn primitive(d: &Doc, p: &Value, mesh: &mut Mesh, shared: &mut Shared, materials return Ok(()); }; charge(&d.parts_left, 1, "primitives")?; + skipped.morphs += usize::from(!arr(p, "targets").is_empty()); let key = attrs.to_string(); let ([base, normal_base, uv_base, tangent_base], nv) = match shared.get(&key) { Some(known) => *known, @@ -648,7 +651,12 @@ fn nodes(j: &Value, limits: &Limits, scene: &mut Scene) -> Result<()> { children: indices(n, "children", list.len()), }); } - scene.roots = match arr(j, "scenes").get(idx(j, "scene").unwrap_or(0)).or_else(|| arr(j, "scenes").first()) { + let scenes = arr(j, "scenes"); + let chosen = idx(j, "scene").filter(|s| *s < scenes.len()).unwrap_or(0); + if scenes.len() > 1 { + scene.warnings.push(format!("scene {chosen} of {} was read; the others were left out", scenes.len())); + } + scene.roots = match scenes.get(chosen) { Some(s) => indices(s, "nodes", list.len()), None => { // No scene: every node that is nobody's child. @@ -832,6 +840,7 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { animations(&d, limits, &mut scene)?; count_warning(&mut scene.warnings, skipped.primitives, "primitive(s) are points, lines or have no positions and were left out"); count_warning(&mut scene.warnings, skipped.triangles, "triangle(s) could not be read and were left out"); + count_warning(&mut scene.warnings, skipped.morphs, "primitive(s) have morph targets, which were left out (drawn in the base shape)"); // A hierarchy with a cycle, or one too deep to walk, is refused here rather than by every caller. scene.instances(limits)?; Ok(scene) diff --git a/crates/mesh/src/tests/gltf.rs b/crates/mesh/src/tests/gltf.rs index d35b63d57..0e6733b65 100644 --- a/crates/mesh/src/tests/gltf.rs +++ b/crates/mesh/src/tests/gltf.rs @@ -829,3 +829,33 @@ fn specular_glossiness_and_webp_textures() { let plain = &s.materials[1]; assert_eq!((plain.base_color, plain.metallic, plain.roughness, plain.base_color_texture), ([1.0; 4], 0.0, 0.0, None)); } + +#[test] +fn morph_targets_and_other_scenes_are_named_in_warnings() { + let mut b = B::default(); + let p = b.f32s(&QUAD[..9], "VEC3", 3); + let d = b.f32s(&[0.0; 9], "VEC3", 3); + let plain = json!({ "attributes": { "POSITION": p } }); + let morphed = json!({ "attributes": { "POSITION": p }, "targets": [{ "POSITION": d }] }); + let doc = |scene: Value, scenes: Value| { + b.glb(json!({ + "scene": scene, + "scenes": scenes, + "nodes": [{ "mesh": 0 }, { "mesh": 0 }], + "meshes": [{ "primitives": [plain, morphed, morphed] }], + })) + }; + let s = parse(&doc(json!(1), json!([{ "nodes": [0] }, { "nodes": [1] }, { "nodes": [] }]))).unwrap(); + assert_eq!(s.roots, [1]); + assert_eq!( + s.warnings, + ["scene 1 of 3 was read; the others were left out", "2 primitive(s) have morph targets, which were left out (drawn in the base shape)",] + ); + // The morphed primitives are still drawn, in their base shape. + assert_eq!(s.meshes[0].parts.len(), 3); + // One scene, or an out-of-range choice, is not a warning about scenes. + let one = parse(&doc(json!(5), json!([{ "nodes": [0] }]))).unwrap(); + assert_eq!((one.roots.as_slice(), one.warnings.len()), ([0].as_slice(), 1)); + let s = parse(&one_mesh(&b, plain.clone())).unwrap(); + assert!(s.warnings.is_empty(), "{:?}", s.warnings); +} From 511e260d23fecfd5839cad2220999f998638097a Mon Sep 17 00:00:00 2001 From: KenM76 Date: Mon, 16 Nov 2026 16:55:57 -0500 Subject: [PATCH 366/471] craft-mesh: test every scene choice; README names the new warnings The README's glTF "Not read" line was doubled and listed sparse accessors and tangents, which are read. Co-Authored-By: Claude Opus 5.5 --- crates/mesh/README.md | 6 +++--- crates/mesh/src/tests/gltf.rs | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/mesh/README.md b/crates/mesh/README.md index 61074a2cb..3b57208fa 100644 --- a/crates/mesh/README.md +++ b/crates/mesh/README.md @@ -110,9 +110,9 @@ FBX 6.x files (a `Model` holds its mesh, `Properties60`, objects connected by na geometry, transforms, the hierarchy and materials. Not read: FBX 5 and older, 6.x textures, animation, skin deformers, blend shapes, cameras, lights, NURBS. -Not read (glTF): required geometry extensions (Draco, meshopt) are -Not read (glTF): sparse accessors and required geometry extensions (Draco, meshopt) are -`Error::Unsupported`; points, lines, morph targets, tangents and other extensions are passed over. +Not read (glTF): required geometry extensions (Draco, meshopt) are `Error::Unsupported`. Points, +lines and morph targets are left out and counted in a warning; scenes other than the default are +named in one; other extensions are passed over. Not written: skins, animations, cameras, lights, and `.gltf` with separate files. ## Hostile input diff --git a/crates/mesh/src/tests/gltf.rs b/crates/mesh/src/tests/gltf.rs index 0e6733b65..09ae6eee3 100644 --- a/crates/mesh/src/tests/gltf.rs +++ b/crates/mesh/src/tests/gltf.rs @@ -853,9 +853,18 @@ fn morph_targets_and_other_scenes_are_named_in_warnings() { ); // The morphed primitives are still drawn, in their base shape. assert_eq!(s.meshes[0].parts.len(), 3); - // One scene, or an out-of-range choice, is not a warning about scenes. - let one = parse(&doc(json!(5), json!([{ "nodes": [0] }]))).unwrap(); - assert_eq!((one.roots.as_slice(), one.warnings.len()), ([0].as_slice(), 1)); + let morphs = "2 primitive(s) have morph targets, which were left out (drawn in the base shape)"; + // An out-of-range choice falls back to the first scene. + let s = parse(&doc(json!(5), json!([{ "nodes": [1] }, { "nodes": [0] }]))).unwrap(); + assert_eq!(s.roots, [1]); + assert_eq!(s.warnings, ["scene 0 of 2 was read; the others were left out", morphs]); + // One scene, or none, is not a warning about scenes. + let s = parse(&doc(json!(5), json!([{ "nodes": [0] }]))).unwrap(); + assert_eq!(s.roots, [0]); + assert_eq!(s.warnings, [morphs]); + let s = parse(&doc(json!(null), json!(null))).unwrap(); + assert_eq!(s.roots, [0, 1]); + assert_eq!(s.warnings, [morphs]); let s = parse(&one_mesh(&b, plain.clone())).unwrap(); assert!(s.warnings.is_empty(), "{:?}", s.warnings); } From a6b7bdce3ee3401a6148c6f3e8003b6dba109ae9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 14:01:25 -0800 Subject: [PATCH 367/471] ps: write numbers from their rounded integers instead of the float formatter push_num_places now writes the digits of round(v * 10^places) directly when that integer is below 2^52 in magnitude, placing the decimal point and trimming fractional zeros itself. Below 2^52 the integer is exact and dividing it by 10^places lands within half a decimal step of the exact quotient, so the text is the same as the formatter's. NaN, infinities, scaling overflow and larger rounded values take the existing formatter path unchanged. Requests for more than nine places are still clamped to nine first. Tests compare the output with the formatter path for places 0 to 12 over fixed boundary values (negative zero, ties, 1e15, 2^52, f64::MAX, non-finite values) and about 144,000 values in all (about 48,000 generated, plus the neighboring bit patterns of each). Widening the limit to 2^54 makes the comparison fail, for example -1709448349065955.5 at one place. On 1,200,000 path coordinates at four places (release build, wall clock on a Linux host running other builds at the same time, seven alternating runs), the formatter path took 362 to 373 ms and the integer path 38 to 44 ms. --- crates/ps/src/write.rs | 290 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 288 insertions(+), 2 deletions(-) diff --git a/crates/ps/src/write.rs b/crates/ps/src/write.rs index 97201771b..89c2e39df 100644 --- a/crates/ps/src/write.rs +++ b/crates/ps/src/write.rs @@ -47,6 +47,9 @@ pub fn written(v: f64) -> f64 { /// finite). pub fn push_num_places(s: &mut String, v: f64, places: u32) { let places = places.min(9); + if push_num_scaled(s, v, places) { + return; + } let scale = 10f64.powi(places as i32); // Past where the scaling overflows, values have no fractional digits to round. let v = if !v.is_finite() { @@ -70,6 +73,75 @@ pub fn push_num_places(s: &mut String, v: f64, places: u32) { } } +/// `10^places` for `places` up to 9, each exact in an `f64`. +const POW10: [f64; 10] = [1.0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9]; + +/// 2^52: below it every integer is an `f64`, and dividing one by `10^places` lands within half a +/// decimal step of the exact quotient, so the decimal digits are those of the integer. +const SCALED_LIMIT: f64 = 4_503_599_627_370_496.0; + +/// Append `v` with at most `places` decimals by writing the digits of the rounded integer +/// `v * 10^places`, the same text [`push_num_places`] writes. Returns false and appends nothing +/// when `places` is above 9, `v` is not finite, the scaling overflows or the rounded integer is +/// 2^52 or more in magnitude. +fn push_num_scaled(s: &mut String, v: f64, places: u32) -> bool { + let Some(&scale) = POW10.get(places as usize) else { + return false; + }; + let k = (v * scale).round(); + let magnitude = k.abs(); + if magnitude.is_nan() || magnitude >= SCALED_LIMIT { + return false; + } + // `magnitude` is a whole number below 2^52, so the cast is exact. + let mut m = magnitude as u64; + let mut decimals = places as usize; + while decimals > 0 && m.is_multiple_of(10) { + m /= 10; + decimals -= 1; + } + let unit = POW10_INT.get(decimals).copied().unwrap_or(1); + let (whole, fraction) = (m / unit, m % unit); + // A rounded negative zero has no sign: it is written `0`. + if k < 0.0 { + s.push('-'); + } + push_digits(s, whole, POW10_INT.iter().take_while(|&&p| p <= whole).count().max(1)); + if decimals > 0 { + s.push('.'); + push_digits(s, fraction, decimals); + } + true +} + +/// `10^n` for `n` up to 16, enough for every digit of an integer below 2^52. +const POW10_INT: [u64; 17] = [ + 1, + 10, + 100, + 1_000, + 10_000, + 100_000, + 1_000_000, + 10_000_000, + 100_000_000, + 1_000_000_000, + 10_000_000_000, + 100_000_000_000, + 1_000_000_000_000, + 10_000_000_000_000, + 100_000_000_000_000, + 1_000_000_000_000_000, + 10_000_000_000_000_000, +]; + +/// Append the last `width` decimal digits of `n`, leading zeros included. +fn push_digits(s: &mut String, n: u64, width: usize) { + for &p in POW10_INT.iter().take(width).rev() { + s.push(char::from(b'0' + ((n / p) % 10) as u8)); + } +} + /// `vs` as numbers separated by spaces, then a space. pub fn push_nums(s: &mut String, vs: &[f64]) { for v in vs { @@ -243,8 +315,8 @@ pub fn inflate(data: &[u8], max: u64) -> Option> { #[cfg(test)] mod tests { use super::{ - FillRule, PaintOps, PathOps, bytes, clip_op, deflate, fill_op, inflate, matrix, num, push_num, push_num_places, push_nums, push_path, string, - written, + FillRule, PaintOps, PathOps, bytes, clip_op, deflate, fill_op, inflate, matrix, num, push_num, push_num_places, push_num_scaled, push_nums, + push_path, string, written, }; use kurbo::{Affine, BezPath}; @@ -285,6 +357,220 @@ mod tests { assert_eq!(places(1e15 - 1.0, 0), "999999999999999"); } + /// `push_num_places` without the integer path: every value goes through the float formatter. + fn base_push_num_places(s: &mut String, v: f64, places: u32) { + use std::fmt::Write; + let places = places.min(9); + let scale = 10f64.powi(places as i32); + let v = if !v.is_finite() { + 0.0 + } else if (v * scale).is_finite() { + (v * scale).round() / scale + } else { + v + }; + if v == v.trunc() && v.abs() < 1e15 { + let _ = write!(s, "{}", v as i64); + return; + } + let start = s.len(); + let _ = write!(s, "{v:.*}", places as usize); + if s[start..].contains('.') { + let keep = s[start..].trim_end_matches('0').trim_end_matches('.').len(); + s.truncate(start + keep); + } + } + + /// A deterministic xorshift sequence. + fn next(state: &mut u64) -> u64 { + *state ^= *state << 13; + *state ^= *state >> 7; + *state ^= *state << 17; + *state + } + + /// Values that hit decimal ties, the 2^52 and 1e15 limits and their neighboring bit patterns, + /// coordinates of every magnitude, and arbitrary bit patterns. + fn number_samples() -> Vec { + let two52 = 4_503_599_627_370_496.0f64; + let mut values = vec![ + 0.0, + -0.0, + 1.0, + -1.0, + 0.5, + -0.5, + 1.5, + 2.5, + -2.5, + 0.1 + 0.2, + f64::from(0.4f32), + 0.123_456_78, + 123_456.789_012_345, + -0.000_04, + -0.000_05, + 0.000_05, + 1e-10, + -1e-10, + f64::MIN_POSITIVE, + -f64::MIN_POSITIVE, + 5e-324, + 1e15, + 1e15 - 1.0, + -1e15, + 1e15 + 0.5, + two52, + two52 - 1.0, + two52 - 0.5, + -two52, + 1e16, + 1e20, + 1.5e305, + f64::MAX, + f64::MIN, + f64::NAN, + f64::INFINITY, + f64::NEG_INFINITY, + ]; + let mut scale = 1.0; + for _ in 0..=9 { + for v in [two52 / scale, (two52 - 1.0) / scale, 1e15 / scale, 0.5 / scale, 1.5 / scale, 2.5 / scale] { + values.extend([v, -v]); + } + scale *= 10.0; + } + let mut state = 0x9e37_79b9_7f4a_7c15u64; + for _ in 0..12_000 { + let r = next(&mut state); + values.push(f64::from_bits(r)); + let exponent = (r % 24) as i32 - 8; + let mantissa = (next(&mut state) % 2_000_000_001) as f64 / 1e9 - 1.0; + values.push(mantissa * 10f64.powi(exponent)); + let places = (r % 10) as i32; + let tie = ((next(&mut state) % 20_000_000_001) as f64 - 1e10 + 0.5) / 10f64.powi(places); + values.push(tie); + values.push((next(&mut state) % 100_000_000) as f64 / 1e4 - 5_000.0); + } + let neighbors: Vec = values + .iter() + .filter(|v| v.is_finite() && **v != 0.0) + .flat_map(|v| [f64::from_bits(v.to_bits() - 1), f64::from_bits(v.to_bits() + 1)]) + .collect(); + values.extend(neighbors); + values + } + + #[test] + fn numbers_match_base_formatter() { + let mut integer_path = 0usize; + for places in 0..=12 { + for v in number_samples() { + let mut expected = String::from("x "); + base_push_num_places(&mut expected, v, places); + let mut got = String::from("x "); + push_num_places(&mut got, v, places); + assert_eq!(got, expected, "{v:e} ({:#x}) at {places} places", v.to_bits()); + if push_num_scaled(&mut String::new(), v, places.min(9)) { + integer_path += 1; + } + } + } + // The comparison covers the integer path, not only the fallback. + assert!(integer_path > 200_000, "{integer_path} values took the integer path"); + } + + #[test] + fn coordinates_are_written_through_integers() { + for (v, places) in [(0.0, 4), (-0.0, 4), (1.5, 4), (-2.25, 4), (4.3333, 4), (612.0, 0), (0.123_456_78, 7), (-0.000_05, 4), (1.23456789, 9)] { + let mut s = String::from("x "); + assert!(push_num_scaled(&mut s, v, places), "{v} at {places} places"); + let mut expected = String::from("x "); + base_push_num_places(&mut expected, v, places); + assert_eq!(s, expected); + } + let two52 = 4_503_599_627_370_496.0f64; + for (v, places) in [ + (f64::NAN, 4), + (f64::INFINITY, 4), + (f64::NEG_INFINITY, 0), + (f64::MAX, 4), + (f64::MIN, 0), + (1e305, 9), + (two52, 0), + (-two52, 0), + (two52 / 1e9, 9), + (1e20, 0), + (1.5, 10), + ] { + let mut s = String::from("x "); + assert!(!push_num_scaled(&mut s, v, places), "{v} at {places} places"); + assert_eq!(s, "x "); + } + } + + #[test] + fn number_boundaries_keep_base_text() { + let places = |v: f64, n: u32| { + let mut s = String::new(); + push_num_places(&mut s, v, n); + s + }; + let two52 = 4_503_599_627_370_496.0f64; + assert_eq!(places(1e15, 0), "1000000000000000"); + assert_eq!(places(-1e15, 0), "-1000000000000000"); + assert_eq!(places(two52 - 1.0, 0), "4503599627370495"); + assert_eq!(places(two52, 0), "4503599627370496"); + assert_eq!(places(1e20, 0), "100000000000000000000"); + assert_eq!(places(450_359_962_737_049.5, 1), "450359962737049.5"); + assert_eq!(places(-0.0, 4), "0"); + assert_eq!(places(-0.000_04, 4), "0"); + assert_eq!(places(-0.000_05, 4), "-0.0001"); + assert_eq!(places(0.5, 0), "1"); + assert_eq!(places(-1.5, 0), "-2"); + assert_eq!(places(0.000_000_001, 9), "0.000000001"); + assert_eq!(places(1.25, 20), "1.25"); + assert_eq!(places(10.5, 4), "10.5"); + assert_eq!(places(100.0, 4), "100"); + let mut s = String::from("x"); + push_nums(&mut s, &[]); + assert_eq!(s, "x"); + let mut scale = 1.0; + for (n, value) in super::POW10.iter().enumerate() { + assert_eq!(*value, scale, "10^{n}"); + assert_eq!(*value, 10f64.powi(n as i32), "10^{n}"); + scale *= 10.0; + } + } + + /// Times the base formatter against `push_num_places` on path coordinates. Run with + /// `cargo test --release -p craft-ps --features write number_writing_timing -- --ignored --nocapture`. + #[test] + #[ignore = "timing probe"] + fn number_writing_timing() { + use std::time::Instant; + let mut state = 0x2545_f491_4f6c_dd1du64; + let values: Vec = (0..1_200_000).map(|_| (next(&mut state) % 12_000_000_000) as f64 / 1e7 - 200.0).collect(); + let mut base_ms = vec![]; + let mut new_ms = vec![]; + for _ in 0..7 { + let start = Instant::now(); + let mut base = String::with_capacity(values.len() * 10); + for v in &values { + base_push_num_places(&mut base, *v, 4); + base.push(' '); + } + base_ms.push(start.elapsed().as_secs_f64() * 1e3); + let start = Instant::now(); + let mut new = String::with_capacity(values.len() * 10); + push_nums(&mut new, &values); + new_ms.push(start.elapsed().as_secs_f64() * 1e3); + assert_eq!(new, base); + } + println!("{} numbers at 4 places", values.len()); + println!("base formatter ms: {base_ms:.2?}"); + println!("integer path ms: {new_ms:.2?}"); + } + #[test] fn strings_and_bytes_are_escaped() { assert_eq!(string("ok"), "(ok)"); From 5541e248e86c923168866de7e183dee4ef4eeeda Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Mon, 16 Nov 2026 17:12:46 -0500 Subject: [PATCH 368/471] wordmodel: append to a paragraph in O(1) with push_text and push_object Building a paragraph through insert_text/insert_object at its end walked every run (and insert_object counted every object before the offset) on each call, so n appends cost O(n^2): one paragraph of 64k runs took 8.5 minutes. push_text and push_object extend the last run or start a new one, giving the same paragraph as inserting at len(). --- crates/wordmodel/src/para.rs | 31 +++++++++++++++++++++ crates/wordmodel/src/tests.rs | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/crates/wordmodel/src/para.rs b/crates/wordmodel/src/para.rs index bbfeb9cbc..901e300b6 100644 --- a/crates/wordmodel/src/para.rs +++ b/crates/wordmodel/src/para.rs @@ -296,6 +296,37 @@ impl Paragraph { Ok(()) } + /// Append plain text (U+FFFC is dropped) with `props`. Returns the appended length. The same as + /// [`Self::insert_text`] at [`Self::len`], but it doesn't walk the runs or the text, so building + /// a paragraph of `n` pieces this way is linear rather than quadratic in `n`. + pub fn push_text(&mut self, s: &str, props: &CharProps) -> usize { + let clean: std::borrow::Cow = if s.contains(OBJ) { s.chars().filter(|c| *c != OBJ).collect::().into() } else { s.into() }; + if clean.is_empty() { + return 0; + } + self.text.push_str(&clean); + self.push_run(clean.len(), props); + self.touch(); + clean.len() + } + + /// Append an object with `props`: [`Self::insert_object`] at [`Self::len`], without counting + /// the objects already there. + pub fn push_object(&mut self, obj: InlineObject, props: &CharProps) { + self.text.push(OBJ); + self.objects.push(obj); + self.push_run(OBJ.len_utf8(), props); + self.touch(); + } + + /// Extend the last run by `len` when it has `props`, or start a new one. + fn push_run(&mut self, len: usize, props: &CharProps) { + match self.runs.last_mut() { + Some(last) if last.props == *props => last.len += len, + _ => self.runs.push(Run { len, props: props.clone() }), + } + } + fn insert_run(&mut self, off: usize, len: usize, props: CharProps) { // Split the run at `off`, insert, merge neighbours. let mut start = 0; diff --git a/crates/wordmodel/src/tests.rs b/crates/wordmodel/src/tests.rs index 088bd3bd2..b4d1b17dd 100644 --- a/crates/wordmodel/src/tests.rs +++ b/crates/wordmodel/src/tests.rs @@ -644,6 +644,57 @@ fn para_of_objects(objs: Vec) -> Paragraph { p } +/// push_text and push_object build the same paragraph as inserting at the end. +#[test] +fn pushing_matches_inserting_at_the_end() { + let bold = CharProps { bold: Some(true), ..Default::default() }; + let plain = CharProps::default(); + let (mut pushed, mut inserted) = (Paragraph::new(), Paragraph::new()); + let steps: [(&str, &CharProps, bool); 7] = [ + ("Hello ", &plain, false), + ("world", &bold, false), + ("", &bold, true), + (", again", &bold, false), + ("", &bold, false), + ("x\u{FFFC}y", &plain, false), + ("", &plain, true), + ]; + for (i, (text, props, object)) in steps.into_iter().enumerate() { + if object { + let obj = InlineObject::BookmarkStart { name: format!("b{i}") }; + pushed.push_object(obj.clone(), props); + inserted.insert_object(inserted.len(), obj, props).unwrap(); + } else { + let n = pushed.push_text(text, props); + assert_eq!(n, inserted.insert_text(inserted.len(), text, props).unwrap()); + } + assert_eq!(pushed, inserted, "after step {i}"); + } + assert_eq!(pushed.text, "Hello world\u{FFFC}, againxy\u{FFFC}"); + assert_eq!(pushed.runs.len(), 3, "equal neighbours merge: {:?}", pushed.runs); + assert_eq!(pushed.objects.len(), 2); +} + +#[test] +fn pushing_a_long_paragraph_is_linear() { + // Appending through insert_text/insert_object walked every run (and counted every object) on + // each call: 64k alternating runs took minutes. Pushing takes milliseconds. + let n = 64_000; + let bold = CharProps { bold: Some(true), ..Default::default() }; + let plain = CharProps::default(); + let t = std::time::Instant::now(); + let mut p = Paragraph::new(); + for i in 0..n { + p.push_text("word ", if i % 2 == 0 { &bold } else { &plain }); + if i % 8 == 0 { + p.push_object(InlineObject::BookmarkStart { name: format!("b{i}") }, &plain); + } + } + assert_eq!(p.runs.iter().map(|r| r.len).sum::(), p.text.len()); + assert_eq!(p.objects.len(), n / 8); + assert!(t.elapsed().as_secs_f32() < 10.0, "{:?}", t.elapsed()); +} + #[test] fn object_walks_are_linear_in_the_paragraph() { // #3614: loops of object_offsets + object_at recounted the objects before each one, so a From d5eddf0cc2dd43e4edc47669de9f90491448f579 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Mon, 16 Nov 2026 17:16:24 -0500 Subject: [PATCH 369/471] flow: a table with no style has no header row from the default look flow_table made a table's first row a header row whenever its look had header_row set, and the look defaults to Word's (header row on). The look is an option of the table's style (w:tblLook/@w:firstRow), so a table with no style, or a style the document doesn't have, got a header row Word never shows: RTF bolded it and marked it \trhdr (repeat on each page), HTML made it th, and ODT repeated it. Only Repeat as header row (RowProps::header) makes such a row a header now; styled tables are unchanged. --- crates/flow/src/from_doc.rs | 5 ++++- crates/flow/src/lib.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/flow/src/from_doc.rs b/crates/flow/src/from_doc.rs index b7204f1bf..d85953a0a 100644 --- a/crates/flow/src/from_doc.rs +++ b/crates/flow/src/from_doc.rs @@ -1101,7 +1101,10 @@ fn flow_table(doc: &Document, t: &Table, depth: usize, tracked: FlowOpts) -> FTa let ncols = t.rows.iter().map(|r| r.first_grid() + r.cells.iter().map(|c| c.span()).sum::() + r.grid_after()).max().unwrap_or(0).max(t.grid.len()); for (ri, r) in t.rows.iter().enumerate() { - let header = r.props.header || (ri == 0 && t.props.look.header_row && t.rows.len() > 1); + // The look's header row is an option of the table's style (`w:tblLook/@w:firstRow`): with + // no table style there is nothing to apply, so the first row stays an ordinary row, as in + // Word. Only Repeat as header row makes it one then. + let header = r.props.header || (ri == 0 && style.is_some() && t.props.look.header_row && t.rows.len() > 1); let mut cells = Vec::new(); for (ci, c) in r.cells.iter().enumerate() { let styled = if bake { styled_cell(t, style.as_ref(), (ri, ci), ncols) } else { None }; diff --git a/crates/flow/src/lib.rs b/crates/flow/src/lib.rs index 774b5ffd1..7dc83c99d 100644 --- a/crates/flow/src/lib.rs +++ b/crates/flow/src/lib.rs @@ -80,6 +80,32 @@ mod tests { assert!(kept); } + /// The default look's header row is an option of the table style: a table with no style (or a + /// style the document doesn't have) has an ordinary first row, so RTF doesn't bold it or + /// repeat it on each page, and HTML doesn't make it `th`. Repeat as header row still does. + #[test] + fn look_header_row_needs_a_table_style() { + use craft_wordmodel::{Block, Document, table::Table}; + let first_row_header = |style: Option<&str>, repeat: bool| { + let mut t = Table::new(2, 2, 200.0); + t.props.style = style.map(str::to_string); + assert!(t.props.look.header_row, "the default look has a header row"); + if let Some(r) = t.rows.first_mut() { + r.props.header = repeat; + } + let mut doc = Document::new(); + doc.body = vec![std::sync::Arc::new(Block::Table(t))]; + let flow = from_doc(&doc); + let Some(FBlock::Table(ft)) = flow.blocks.iter().find(|b| matches!(b, FBlock::Table(_))) else { panic!("no table in {:?}", flow.blocks) }; + assert!(ft.rows.get(1).is_some_and(|r| r.iter().all(|c| !c.header)), "the second row is never a header"); + ft.rows.first().is_some_and(|r| r.iter().all(|c| c.header)) + }; + assert!(first_row_header(Some("TableGrid"), false), "a table style applies the look's header row"); + assert!(!first_row_header(None, false), "no style, no header row"); + assert!(!first_row_header(Some("NoSuchStyle"), false), "a missing style applies nothing"); + assert!(first_row_header(None, true), "Repeat as header row"); + } + #[test] fn percent_decode_requires_hex_digits() { assert_eq!(percent_decode("%+A"), b"%+A".to_vec()); From 778493598fcca586745a502133fca1368b911a7c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 17:53:46 -0500 Subject: [PATCH 370/471] Migrate Flatpak updates to the OCI image and appimage pipeline (#1318) --- Cargo.lock | 8 +- crates/update/Cargo.toml | 2 + crates/update/src/detect.rs | 210 ++++++++++++++++- crates/update/src/install.rs | 381 +++++++++++++++++++++++++++++-- crates/update/src/packages.rs | 181 ++++++++++++++- crates/update/src/process.rs | 78 +++++++ crates/update/src/self_update.rs | 20 +- crates/update/src/status.rs | 17 ++ 8 files changed, 857 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8679e4eac..ef82f1afa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2887,6 +2887,10 @@ dependencies = [ name = "craft-sniff" version = "0.1.0" +[[package]] +name = "craft-ssip" +version = "0.1.0" + [[package]] name = "craft-stencilcraft-document" version = "0.1.0" @@ -2900,10 +2904,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-ssip" -version = "0.1.0" - [[package]] name = "craft-stencilcraft-format" version = "0.1.0" diff --git a/crates/update/Cargo.toml b/crates/update/Cargo.toml index 93a0719a5..e130656e3 100644 --- a/crates/update/Cargo.toml +++ b/crates/update/Cargo.toml @@ -11,6 +11,8 @@ repository.workspace = true workspace = true [features] +# Opt-in migration of first party Flatpak bundles to the ArtCraft OCI remote. +oci_migration = [] default = [] egui = ["dep:egui", "dep:image"] local-mirror = [] diff --git a/crates/update/src/detect.rs b/crates/update/src/detect.rs index 437e315d7..53c000ebc 100644 --- a/crates/update/src/detect.rs +++ b/crates/update/src/detect.rs @@ -67,6 +67,9 @@ fn from_manifest(app: &App, manifest: &Manifest) -> Option { source: InstallSource::Launcher, location: rec.path.display().to_string(), package: None, + externally_managed: false, + origin: None, + installation: None, }) } @@ -169,7 +172,15 @@ pub mod macos { let full_key = format!("{}Version", app.name); let text = [full_key.as_str(), "CFBundleShortVersionString", "CFBundleVersion"].iter().find_map(|k| dict.get(k).and_then(plist::Value::as_string)); - Some(Installed { version: text.and_then(version::parse), source: InstallSource::MacApp, location: path.display().to_string(), package: None }) + Some(Installed { + version: text.and_then(version::parse), + source: InstallSource::MacApp, + location: path.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }) } } @@ -277,6 +288,9 @@ pub mod windows { source: InstallSource::Msi, location: m.exe.map_or_else(|| app.name.to_string(), |p| p.display().to_string()), package: Some(m.product_code), + externally_managed: false, + origin: None, + installation: None, }) } } @@ -303,7 +317,14 @@ pub mod linux { let t = Duration::from_secs(10); let mut out = Vec::new(); let mut add = |manager, list: Vec<(String, String)>| { - out.extend(list.into_iter().filter(|(n, _)| ours(n)).map(|(name, version)| Package { manager, name, version })); + out.extend(list.into_iter().filter(|(n, _)| ours(n)).map(|(name, version)| Package { + manager, + name, + version, + user: false, + origin: None, + installation: None, + })); }; if let Some(list) = capture("dpkg-query", &["-W", "-f", "${Package}|${db:Status-Status}|${Version}\n"], t) { add(InstallSource::Deb, packages::parse_dpkg(&list)); @@ -319,15 +340,28 @@ pub mod linux { add(InstallSource::Pacman, packages::parse_pacman(&list)); } let arch = format!("--arch={}", std::env::consts::ARCH); - if let Some(list) = crate::process::capture_flatpak(&["list", "--app", &arch, "--columns=application,version"], t) { - let mut flatpaks = packages::parse_flatpak_list(&list); + if let Some(list) = crate::process::capture_flatpak(&["list", "--app", &arch, "--columns=application,version,origin,installation"], t) { + let mut flatpaks = packages::parse_flatpak_list_with_origin(&list); // Bundles built from a release tarball often list no version: ask Flatpak directly. - for (id, version) in flatpaks.iter_mut().filter(|(id, v)| v.is_empty() && id.starts_with("ai.storyteller.")) { - if let Some(v) = crate::process::capture_flatpak(&["info", &arch, id], t).as_deref().and_then(packages::parse_flatpak_info) { + for (id, version, _, installation) in flatpaks.iter_mut().filter(|(id, v, _, _)| v.is_empty() && id.starts_with("ai.storyteller.")) { + let custom_scope; + let scope_arg = match installation.as_deref() { + Some("user") => vec!["--user"], + Some("system") | None => vec!["--system"], + Some(named) => { + custom_scope = format!("--installation={named}"); + vec![custom_scope.as_str()] + } + }; + let info_args = [&["info"][..], &scope_arg, &[&arch, id]].concat(); + if let Some(v) = crate::process::capture_flatpak(&info_args, t).as_deref().and_then(packages::parse_flatpak_info) { *version = v; } } - add(InstallSource::Flatpak, flatpaks); + out.extend(flatpaks.into_iter().filter(|(n, _, _, _)| ours(n)).map(|(name, version, origin, installation)| { + let is_user = installation.as_deref() == Some("user"); + Package { manager: InstallSource::Flatpak, name, version, user: is_user, origin, installation } + })); } out } @@ -402,12 +436,41 @@ pub mod linux { pub fn detect(app: &App, snapshot: &Snapshot) -> Option { if let Some((path, version)) = appimages(app).into_iter().next() { - return Some(Installed { version, source: InstallSource::AppImage, location: path.display().to_string(), package: None }); + return Some(Installed { + version, + source: InstallSource::AppImage, + location: path.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }); } let order = [InstallSource::Deb, InstallSource::Rpm, InstallSource::Pacman, InstallSource::Flatpak]; if let Some(p) = packages::find(app, &snapshot.packages, &order) { - let location = if p.manager == InstallSource::Flatpak { app.bundle_id() } else { binary_on_path(app.id) }; - return Some(Installed { version: packages::version_of(p), source: p.manager, location, package: Some(p.name.clone()) }); + let location = if p.manager == InstallSource::Flatpak { p.name.clone() } else { binary_on_path(app.id) }; + let externally_managed = p.manager == InstallSource::Flatpak + && p.origin.as_deref().is_none_or(|origin| { + if packages::is_flatpark_origin(origin) { + return true; + } + let scope = match p.installation.as_deref() { + Some("user") => "--user".to_string(), + Some("system") | None => "--system".to_string(), + Some(name) => format!("--installation={name}"), + }; + crate::process::capture_flatpak(&["remotes", &scope, "--show-disabled", "--columns=name,url"], Duration::from_secs(10)) + .is_none_or(|remotes| packages::remote_manages_flatpak(origin, &remotes)) + }); + return Some(Installed { + version: packages::version_of(p), + source: p.manager, + location, + package: Some(p.name.clone()), + externally_managed, + origin: p.origin.clone(), + installation: p.installation.clone(), + }); } detect_system_install(app) } @@ -418,7 +481,15 @@ pub mod linux { /// to whatever installed it, since it can't tell which package manager that was. fn detect_system_install(app: &App) -> Option { let bin = find_binary(app.id)?; - Some(Installed { version: binary_version(&bin, app), source: InstallSource::Binary, location: bin.display().to_string(), package: None }) + Some(Installed { + version: binary_version(&bin, app), + source: InstallSource::Binary, + location: bin.display().to_string(), + package: None, + externally_managed: true, + origin: None, + installation: None, + }) } fn find_binary(id: &str) -> Option { @@ -445,7 +516,15 @@ pub mod freebsd { if !bin.exists() { return None; } - Some(Installed { version: binary_version(&bin, app), source: InstallSource::Binary, location: bin.display().to_string(), package: None }) + Some(Installed { + version: binary_version(&bin, app), + source: InstallSource::Binary, + location: bin.display().to_string(), + package: None, + externally_managed: true, + origin: None, + installation: None, + }) } /// ` --version` prints ` 0.5.0 (sha, date)`. @@ -690,6 +769,113 @@ mod tests { assert!(strip_cli_prefix("photocraft-cli 1.0.0", "photocraft").is_none()); } + /// The launcher's own record is the first thing detection trusts, and its source is the + /// launcher rather than whatever installed the file underneath. + #[test] + fn a_manifest_record_for_a_file_that_is_there_is_a_launcher_install() { + let app = App::by_id("photocraft").unwrap(); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("photocraft.AppImage"); + std::fs::write(&path, b"").unwrap(); + let mut manifest = crate::manifest::Manifest::default(); + manifest.apps.insert( + app.id.to_string(), + crate::manifest::Record { + version: "0.6.1".into(), + kind: crate::PackageKind::AppImage, + path: path.clone(), + installed_at: 0, + files: Vec::new(), + needs_root: false, + }, + ); + let found = from_manifest(app, &manifest).expect("the record names a file that is there"); + assert_eq!(found.source, InstallSource::Launcher); + assert_eq!(found.version.map(|v| v.to_string()).as_deref(), Some("0.6.1")); + assert_eq!(found.location, path.display().to_string()); + assert!(!found.externally_managed); + assert_eq!(found.origin, None); + assert_eq!(found.installation, None); + } + + /// A record whose file the user removed is not an install, so detection keeps looking. + #[test] + fn a_manifest_record_for_a_file_that_is_gone_is_not_an_install() { + let app = App::by_id("photocraft").unwrap(); + let dir = tempfile::tempdir().unwrap(); + let mut manifest = crate::manifest::Manifest::default(); + manifest.apps.insert( + app.id.to_string(), + crate::manifest::Record { + version: "0.6.0".into(), + kind: crate::PackageKind::AppImage, + path: dir.path().join("gone.AppImage"), + installed_at: 0, + files: Vec::new(), + needs_root: false, + }, + ); + assert!(from_manifest(app, &manifest).is_none()); + } + + /// A package the snapshot already found, as the launcher sees it after a package-manager + /// query: only the fields detection reads are varied. + #[cfg(target_os = "linux")] + fn injected(manager: InstallSource, installation: Option<&str>, origin: Option<&str>) -> crate::packages::Package { + crate::packages::Package { + manager, + name: match manager { + InstallSource::Flatpak => "ai.storyteller.photocraft".to_string(), + _ => "photocraft".to_string(), + }, + version: "0.6.2".into(), + user: installation == Some("user"), + origin: origin.map(str::to_string), + installation: installation.map(str::to_string), + } + } + + #[cfg(target_os = "linux")] + #[test] + fn a_deb_package_is_reported_with_its_manager_and_version() { + let app = App::by_id("photocraft").unwrap(); + let snapshot = Snapshot { packages: vec![injected(InstallSource::Deb, None, None)] }; + let found = linux::detect(app, &snapshot).expect("the injected package is installed"); + assert_eq!(found.source, InstallSource::Deb); + assert_eq!(found.version.map(|v| v.to_string()).as_deref(), Some("0.6.2")); + assert_eq!(found.package.as_deref(), Some("photocraft")); + assert!(!found.externally_managed); + assert_eq!(found.installation, None); + } + + /// A Flatpak from a remote the launcher doesn't drive stays externally managed, in every + /// scope the `flatpak remotes` query can need. + #[cfg(target_os = "linux")] + #[test] + fn a_flatpak_from_a_repository_remote_is_externally_managed() { + let app = App::by_id("photocraft").unwrap(); + for installation in [Some("user"), Some("system"), Some("custom"), None] { + let snapshot = Snapshot { packages: vec![injected(InstallSource::Flatpak, installation, Some("flathub"))] }; + let found = linux::detect(app, &snapshot).expect("the injected Flatpak is installed"); + assert_eq!(found.source, InstallSource::Flatpak); + assert!(found.externally_managed, "{installation:?}"); + assert_eq!(found.location, "ai.storyteller.photocraft"); + assert_eq!(found.origin.as_deref(), Some("flathub")); + assert_eq!(found.installation.as_deref(), installation); + } + } + + /// FlatPark's own remote is known to manage the copy without asking flatpak again. + #[cfg(target_os = "linux")] + #[test] + fn a_flatpark_flatpak_is_externally_managed_without_asking_its_remotes() { + let app = App::by_id("photocraft").unwrap(); + let snapshot = Snapshot { packages: vec![injected(InstallSource::Flatpak, Some("user"), Some("flatpark"))] }; + let found = linux::detect(app, &snapshot).expect("the injected Flatpak is installed"); + assert!(found.externally_managed); + assert_eq!(found.origin.as_deref(), Some("flatpark")); + } + #[cfg(target_os = "linux")] #[test] fn finds_appimages_in_every_folder_newest_first() { diff --git a/crates/update/src/install.rs b/crates/update/src/install.rs index 4b65e3af0..071434595 100644 --- a/crates/update/src/install.rs +++ b/crates/update/src/install.rs @@ -94,6 +94,11 @@ pub fn install_at( { not_running(app, process::running(Path::new(&inst.location)))?; } + if kind == PackageKind::Flatpak + && let Some(installed) = existing.filter(|installed| installed.source == InstallSource::Flatpak && installed.externally_managed) + { + return Err(managed_elsewhere(app, installed, false).unwrap_or_else(|| "Update this app through its Flatpak repository".into())); + } bind_artifact_version(app, release, asset)?; if !job.replace && let Some(inst) = existing @@ -104,6 +109,16 @@ pub fn install_at( } check_download_link(app, release, asset)?; log::info!("{}: starting {} to {} ({})", app.name, if existing.is_some() { "update" } else { "install" }, release.version_label(), kind.label()); + #[cfg(all(target_os = "linux", feature = "oci_migration"))] + if kind == PackageKind::Flatpak + && !job.replace + && !release.prerelease + && let Some(installed) = existing + && let Some(expected) = release.version.as_ref() + && linux::migrate_bundle_to_oci(app, installed, expected, job.stop)? + { + return Ok(None); + } let file = download(release, asset, job)?; log::info!("{}: download ready ({}), running installer...", app.name, file.display()); // Past this point the install can't be interrupted halfway. @@ -363,6 +378,9 @@ pub(crate) fn choose_kinds_with( // opens and removes them but never replaces them. InstallSource::Pacman if is_release_arch_package(app, inst) => Some(PackageKind::ArchPkg), InstallSource::Pacman => return Vec::new(), + // Repository-managed Flatpaks are externally managed: the launcher will not + // replace them with a downloaded local .flatpak bundle. + InstallSource::Flatpak if inst.externally_managed => return Vec::new(), InstallSource::Flatpak => Some(PackageKind::Flatpak), InstallSource::AppImage => Some(PackageKind::AppImage), InstallSource::Binary => Some(PackageKind::TarGz), @@ -463,6 +481,16 @@ fn managed_elsewhere_with(app: &App, installed: &Installed, uninstall: bool, rep let pkg = installed.package.clone().unwrap_or_else(|| app.id.to_string()); Some(pacman_update_hint(app, &pkg, repo_fn(&pkg).as_deref())) } + InstallSource::Flatpak if !uninstall && installed.externally_managed => { + let default_pkg = app.bundle_id(); + let pkg = installed.package.as_deref().unwrap_or(&default_pkg); + let scope = match installed.installation.as_deref() { + Some("user") => "--user".to_string(), + Some("system") | None => "--system".to_string(), + Some(name) => format!("--installation={name}"), + }; + Some(format!("{} was installed from a Flatpak repository. Update it with flatpak update {scope} {pkg}", app.name)) + } InstallSource::Binary => Some(format!("{} was installed outside the launcher. Update or remove it the way it was installed.", app.name)), _ => None, } @@ -805,8 +833,15 @@ fn cancelled(stop: &Stop) -> impl Fn() -> bool + '_ { || stop.check().is_err_and(|e| e == crate::protocol::CANCELLED) } +/// For a running helper command: only CANCELLED ends it. PAUSED lets the current command finish +/// (callers check `stop` before starting the next one). #[cfg(target_os = "linux")] -pub(crate) mod linux { +fn kill_on_cancel(stop: &Stop) -> impl Fn() -> Result<(), String> + '_ { + || if cancelled(stop)() { Err(crate::protocol::CANCELLED.into()) } else { Ok(()) } +} + +#[cfg(target_os = "linux")] +pub mod linux { use std::ffi::{OsStr, OsString}; use std::os::unix::fs::PermissionsExt; use std::time::Duration; @@ -817,7 +852,7 @@ pub(crate) mod linux { use super::{ App, INSTALLER, InstallSource, Installed, JobPhase, PackageKind, Path, PathBuf, Paths, Record, SHARED_CALL, SYSTEM_BIN_DIRS, Stop, cancelled, - find_in, pkexec, process, run_as_root, swap_into_place, tree, turn, + find_in, kill_on_cancel, pkexec, process, run_as_root, swap_into_place, tree, turn, }; /// apt waits this long for another package tool (a software centre, automatic updates) to @@ -1076,6 +1111,125 @@ pub(crate) mod linux { root.queue.alone(&cancelled(stop), &mut turn(phase, JobPhase::Installing), install).unwrap_or(Err(CANCELLED.into())) } + /// Opt-in OCI migration; only an absent ref permits bundle fallback. + #[cfg(feature = "oci_migration")] + pub fn migrate_bundle_to_oci(app: &App, installed: &Installed, expected: &super::version::Version, stop: &Stop) -> Result { + if installed.source != InstallSource::Flatpak || installed.externally_managed || !app.repo.starts_with("storytold/") { + return Ok(false); + } + // Only bundle-created origins qualify; never redirect another repository's install. + if !installed.origin.as_deref().is_some_and(|origin| origin.ends_with("-origin")) { + return Ok(false); + } + let id = installed.package.as_deref().ok_or("Flatpak installation has no package ID")?; + let scope = match installed.installation.as_deref() { + Some("user") => "--user".to_string(), + Some("system") => "--system".to_string(), + Some(name) => format!("--installation={name}"), + None => return Err("Flatpak installation scope is unknown".into()), + }; + // `cancellable` is false for steps that must not stop part-way: killing flatpak-spawn + // doesn't wait for the host transaction, and `--reinstall` removes the old ref first. + let flatpak = |args: &[&str], cancellable: bool| -> Result { + let mut cmd = if process::in_sandbox() { + let mut cmd = process::command("flatpak-spawn"); + cmd.args(["--host", "--watch-bus", "flatpak"]); + cmd + } else { + process::command("flatpak") + }; + cmd.arg(&scope).args(args); + if cancellable { + stop.check()?; // paused or cancelled: start no further command + } + let end = kill_on_cancel(stop); + let output = process::cancellable_output(cmd, || if cancellable { end() } else { Ok(()) })?; + if !output.status.success() { + return Err(format!("Flatpak OCI migration failed: {}", String::from_utf8_lossy(&output.stderr).trim())); + } + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) + }; + let run = |args: &[&str]| flatpak(args, true); + let reference = run(&["info", "--show-ref", id])?; + let reference = reference.trim(); + if !reference.starts_with(&format!("app/{id}/")) || !reference.ends_with("/stable") { + return Ok(false); + } + let remotes = run(&["remotes", "--show-disabled", "--columns=name,url"])?; + let remote = remotes.lines().find_map(|line| { + let (name, _) = line.split_once('\t')?; + crate::packages::is_artcraft_remote(name, line).then(|| name.to_string()) + }); + let remote = match remote { + Some(remote) => remote, + None => { + if remotes.lines().any(|line| line.split('\t').next() == Some("artcraft-oci")) { + return Err("The artcraft-oci remote points to a different repository".into()); + } + // Another app's migration may add the remote first; the URL is checked below. + run(&["remote-add", "--if-not-exists", "artcraft-oci", "https://getartcraft.com/artcraft-oci.flatpakrepo"])?; + let remotes = run(&["remotes", "--show-disabled", "--columns=name,url"])?; + if !crate::packages::is_artcraft_remote("artcraft-oci", &remotes) { + return Err("The ArtCraft OCI descriptor configured an unexpected repository".into()); + } + "artcraft-oci".to_string() + } + }; + // The Version column comes from the remote's AppStream catalog, which Flatpak only fetches + // on demand; without a refresh a new remote lists refs with empty versions. + run(&["update", "--appstream", &remote])?; + let refs = run(&["remote-ls", "--app", "--columns=ref,version", &remote])?; + if !crate::packages::oci_ref_available(reference, expected, &refs)? { + return Ok(false); + } + stop.check()?; + if let Err(e) = flatpak(&["install", "--reinstall", "-y", "--noninteractive", &remote, reference], false) { + // The bundle origin has no repository to restore from, so point at the bundle itself. + if flatpak(&["info", id], false).is_err() { + return Err(format!("{e}. {} is no longer installed; reinstall it from the .flatpak bundle at {}", app.name, app.releases_url())); + } + return Err(e); + } + let origin = run(&["info", "--show-origin", id])?; + if origin.trim() != remote { + return Err("Flatpak did not switch the installed app to the ArtCraft OCI remote".into()); + } + Ok(true) + } + + /// Updates an installed Flatpak from its remote repository (such as FlatPark). + pub fn update_flatpak_app(app: &App, id: &str, installation: Option<&str>, stop: &Stop, phase: &mut dyn FnMut(JobPhase)) -> Result<(), String> { + phase(JobPhase::Installing); + stop.check()?; + let scope_arg = match installation { + Some("user") => "--user".to_string(), + Some("system") | None => "--system".to_string(), + Some(named) => format!("--installation={named}"), + }; + // Let Flatpak's system helper authorize system updates through polkit. + // watch-bus also terminates the host command if the sandbox proxy exits. + let mut cmd = if process::in_sandbox() { + let mut command = process::command("flatpak-spawn"); + command.args(["--host", "--watch-bus", "flatpak"]); + command + } else { + process::command("flatpak") + }; + cmd.args(["update", &scope_arg, "-y", "--noninteractive", id]); + let output = process::cancellable_output(cmd, kill_on_cancel(stop))?; + if output.status.success() { + return Ok(()); + } + let stderr = String::from_utf8_lossy(&output.stderr); + let err = stderr.trim(); + if err.is_empty() { + Err(format!("Flatpak couldn't update {}", app.name)) + } else { + let clean = err.strip_prefix("error: ").or_else(|| err.strip_prefix("Error: ")).unwrap_or(err); + Err(format!("Flatpak couldn't update {}: {clean}", app.name)) + } + } + /// Whether a bundle install of `id` failed because the installation already has the bundle's /// commit: Flatpak's "already installed", in the C locale. Any other failure leaves the app alone. pub(crate) fn already_installed(err: &str, id: &str) -> bool { @@ -2481,6 +2635,9 @@ mod tests { source: InstallSource::Msi, location: dir.path().join("MSI/PhotoCraft/photocraft.exe").display().to_string(), package: None, + externally_managed: false, + origin: None, + installation: None, }; assert_eq!(destination(app, PackageKind::Msi, Some(&msi), Some(&old), &unavailable).unwrap(), Some(dir.path().join("MSI/PhotoCraft"))); assert!(destination(app, PackageKind::TarGz, None, None, &locations).unwrap_err().contains("aren't supported")); @@ -2492,8 +2649,24 @@ mod tests { let app = App::by_id("photocraft").unwrap(); let path = dir.path().join("old.AppImage"); let rec = record("0.5.0", PackageKind::AppImage, path.clone(), vec![], false); - let launcher = Installed { version: None, source: InstallSource::Launcher, location: path.display().to_string(), package: None }; - let users = Installed { version: None, source: InstallSource::AppImage, location: "/home/me/AppImages/p.AppImage".into(), package: None }; + let launcher = Installed { + version: None, + source: InstallSource::Launcher, + location: path.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; + let users = Installed { + version: None, + source: InstallSource::AppImage, + location: "/home/me/AppImages/p.AppImage".into(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; let chosen = dir.path().join("New apps"); std::fs::create_dir(&chosen).unwrap(); let mut locations = InstallLocations { root: Some(chosen.display().to_string()), ..Default::default() }; @@ -3333,7 +3506,15 @@ esac #[test] fn updates_keep_the_installed_format() { - let inst = |source| Installed { version: None, source, location: String::new(), package: None }; + let inst = |source| Installed { + version: None, + source, + location: String::new(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; let app = crate::App::by_id("photocraft").unwrap(); let available = available_kinds(); if let Some(first) = available.first() { @@ -3350,7 +3531,15 @@ esac #[test] fn only_the_release_arch_package_is_updated_by_the_launcher() { - let pacman = |name: &str| Installed { version: None, source: InstallSource::Pacman, location: String::new(), package: Some(name.into()) }; + let pacman = |name: &str| Installed { + version: None, + source: InstallSource::Pacman, + location: String::new(), + package: Some(name.into()), + externally_managed: false, + origin: None, + installation: None, + }; let app = crate::App::by_id("wordcraft").unwrap(); assert!(is_release_arch_package(app, &pacman("ai.storyteller.wordcraft"))); for aur in ["wordcraft", "wordcraft-bin", "wordcraft-git"] { @@ -3378,7 +3567,15 @@ esac #[test] fn aur_packages_are_not_updated_as_arch_pkg() { - let pacman = |name: &str| Installed { version: None, source: InstallSource::Pacman, location: String::new(), package: Some(name.into()) }; + let pacman = |name: &str| Installed { + version: None, + source: InstallSource::Pacman, + location: String::new(), + package: Some(name.into()), + externally_managed: false, + origin: None, + installation: None, + }; let app = crate::App::by_id("filmcraft").unwrap(); let installed = pacman("filmcraft-bin"); assert!(managed_elsewhere_with(app, &installed, false, |_| None).is_some()); @@ -3388,7 +3585,15 @@ esac #[test] fn release_arch_packages_are_updated_as_arch_pkg() { - let pacman = |name: &str| Installed { version: None, source: InstallSource::Pacman, location: String::new(), package: Some(name.into()) }; + let pacman = |name: &str| Installed { + version: None, + source: InstallSource::Pacman, + location: String::new(), + package: Some(name.into()), + externally_managed: false, + origin: None, + installation: None, + }; let app = crate::App::by_id("filmcraft").unwrap(); let installed = pacman("ai.storyteller.filmcraft"); assert_eq!(managed_elsewhere_with(app, &installed, false, |_| None), None); @@ -3396,6 +3601,102 @@ esac assert_eq!(choose_kinds_with(app, Some(&installed), None, &[], &[PackageKind::ArchPkg], false), vec![PackageKind::ArchPkg]); } + #[test] + fn flatpark_flatpaks_are_externally_managed_and_not_updated_via_bundle() { + let app = crate::App::by_id("photocraft").unwrap(); + let flatpark_installed = Installed { + version: version::parse("0.19.0"), + source: InstallSource::Flatpak, + location: "ai.storyteller.photocraft".into(), + package: Some("ai.storyteller.photocraft".into()), + externally_managed: true, + origin: Some("flatpark".into()), + installation: Some("user".into()), + }; + assert!(flatpark_installed.is_externally_managed()); + assert_eq!(choose_kinds_with(app, Some(&flatpark_installed), None, &[], &[PackageKind::Flatpak], false), Vec::::new()); + let hint = managed_elsewhere_with(app, &flatpark_installed, false, |_| None); + assert_eq!( + hint.as_deref(), + Some("PhotoCraft was installed from a Flatpak repository. Update it with flatpak update --user ai.storyteller.photocraft") + ); + // During uninstall, managed_elsewhere returns None so it can be removed + assert_eq!(managed_elsewhere_with(app, &flatpark_installed, true, |_| None), None); + + // When installed under a former bundle ID, the hint targets that detected ID + let former_installed = Installed { + version: version::parse("0.19.0"), + source: InstallSource::Flatpak, + location: "ai.storyteller.photocraft-old".into(), + package: Some("ai.storyteller.photocraft-old".into()), + externally_managed: true, + origin: Some("flatpark".into()), + installation: Some("system".into()), + }; + let hint_former = managed_elsewhere_with(app, &former_installed, false, |_| None); + assert_eq!( + hint_former.as_deref(), + Some("PhotoCraft was installed from a Flatpak repository. Update it with flatpak update --system ai.storyteller.photocraft-old") + ); + + let bundle_installed = Installed { + version: version::parse("0.19.0"), + source: InstallSource::Flatpak, + location: "ai.storyteller.photocraft".into(), + package: Some("ai.storyteller.photocraft".into()), + externally_managed: false, + origin: Some("photocraft-origin".into()), + installation: Some("user".into()), + }; + assert!(!bundle_installed.is_externally_managed()); + assert_eq!(choose_kinds_with(app, Some(&bundle_installed), None, &[], &[PackageKind::Flatpak], false), vec![PackageKind::Flatpak]); + assert_eq!(managed_elsewhere_with(app, &bundle_installed, false, |_| None), None); + } + + /// A copy in a named Flatpak installation is addressed through that installation, not the + /// default system scope. + #[test] + fn a_named_flatpak_installation_is_addressed_by_name() { + let app = crate::App::by_id("photocraft").unwrap(); + let installed = Installed { + version: version::parse("0.19.0"), + source: InstallSource::Flatpak, + location: "ai.storyteller.photocraft".into(), + package: Some("ai.storyteller.photocraft".into()), + externally_managed: true, + origin: Some("flatpark".into()), + installation: Some("work".into()), + }; + assert_eq!( + managed_elsewhere_with(app, &installed, false, |_| None).as_deref(), + Some("PhotoCraft was installed from a Flatpak repository. Update it with flatpak update --installation=work ai.storyteller.photocraft") + ); + } + + #[test] + #[cfg(target_os = "linux")] + fn update_flatpak_app_respects_cancellation() { + let app = crate::App::by_id("photocraft").unwrap(); + let stop = Stop::default(); + stop.cancel(); + let mut phase = |_| (); + assert_eq!(linux::update_flatpak_app(app, "ai.storyteller.photocraft", Some("user"), &stop, &mut phase), Err(CANCELLED.into())); + } + + #[test] + #[cfg(target_os = "linux")] + fn pause_lets_a_running_flatpak_command_finish_and_only_cancel_ends_it() { + let stop = Stop::default(); + stop.pause(); + assert_eq!(kill_on_cancel(&stop)(), Ok(())); + let app = crate::App::by_id("photocraft").unwrap(); + let mut phase = |_| (); + // A paused job starts no further command. + assert_eq!(linux::update_flatpak_app(app, "ai.storyteller.photocraft", Some("user"), &stop, &mut phase), Err(crate::protocol::PAUSED.into())); + stop.cancel(); + assert_eq!(kill_on_cancel(&stop)(), Err(CANCELLED.into())); + } + #[test] fn unavailable_arch_package_not_selected_for_new_install() { let app = crate::App::by_id("filmcraft").unwrap(); @@ -3419,6 +3720,9 @@ esac source: InstallSource::Launcher, location: dir.path().join("PhotoCraft.AppImage").display().to_string(), package: None, + externally_managed: false, + origin: None, + installation: None, }; let stop = Stop::default(); let mut job = Job { http: &http, paths: &paths, stop: &stop, phase, install_locations: &locations, replace: false }; @@ -3595,8 +3899,15 @@ esac let old = dir.path().join("Apps").join("PhotoCraft.AppImage"); std::fs::create_dir_all(old.parent().unwrap()).unwrap(); std::fs::write(&old, b"the installed 0.4.0").unwrap(); - let installed = - Installed { version: version::parse("0.4.0"), source: InstallSource::Launcher, location: old.display().to_string(), package: None }; + let installed = Installed { + externally_managed: false, + origin: None, + installation: None, + version: version::parse("0.4.0"), + source: InstallSource::Launcher, + location: old.display().to_string(), + package: None, + }; let previous = record("0.4.0", PackageKind::AppImage, old.clone(), vec![], false); let mut phases = Vec::new(); let mut phase = |p: JobPhase| phases.push(p); @@ -3637,7 +3948,15 @@ esac #[cfg(unix)] let _probe = crate::process::tests::RunningProbe::start(&location); assert!(process::is_running(Path::new(&location)), "{location} should be seen as running"); - let installed = Installed { version: version::parse("0.4.0"), source: InstallSource::Launcher, location, package: None }; + let installed = Installed { + externally_managed: false, + origin: None, + installation: None, + version: version::parse("0.4.0"), + source: InstallSource::Launcher, + location, + package: None, + }; let stop = Stop::default(); stop.cancel(); let mut job = Job { http: &http, paths: &paths, stop: &stop, phase, install_locations: &locations, replace: false }; @@ -3881,7 +4200,15 @@ esac } fn found_at(bundle: &Path) -> Installed { - Installed { version: None, source: InstallSource::MacApp, location: bundle.display().to_string(), package: None } + Installed { + version: None, + source: InstallSource::MacApp, + location: bundle.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + } } /// What [`check`] says when `codesign` fails with `failure`. @@ -4062,7 +4389,15 @@ esac let dir = tempfile::tempdir().unwrap(); let apps = dir.path().join("Applications"); let current = bundle(&apps, "0.5.0"); - let existing = Installed { version: None, source: InstallSource::MacApp, location: current.display().to_string(), package: None }; + let existing = Installed { + version: None, + source: InstallSource::MacApp, + location: current.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; let paths = Paths::under(dir.path()); let image = dmg(dir.path(), "0.6.0"); @@ -4095,7 +4430,15 @@ esac let dir = tempfile::tempdir().unwrap(); let apps = dir.path().join("Applications"); let current = bundle(&apps, "0.5.0"); - let existing = Installed { version: None, source: InstallSource::MacApp, location: current.display().to_string(), package: None }; + let existing = Installed { + version: None, + source: InstallSource::MacApp, + location: current.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; let image = dmg(dir.path(), "0.6.0"); let release = Release { tag: "v0.6.0".into(), version: version::parse("0.6.0"), ..Release::default() }; @@ -4125,7 +4468,15 @@ esac let dir = tempfile::tempdir().unwrap(); let apps = dir.path().join("Applications"); let current = bundle(&apps, "0.5.0"); - let existing = Installed { version: None, source: InstallSource::MacApp, location: current.display().to_string(), package: None }; + let existing = Installed { + version: None, + source: InstallSource::MacApp, + location: current.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; let paths = Paths::under(dir.path()); let image = dmg(dir.path(), "0.6.0"); diff --git a/crates/update/src/packages.rs b/crates/update/src/packages.rs index 1820c2f15..72c93a4b0 100644 --- a/crates/update/src/packages.rs +++ b/crates/update/src/packages.rs @@ -13,6 +13,12 @@ pub struct Package { pub name: String, /// As the package manager reports it (`0.5.0-1`); empty when it doesn't say. pub version: String, + /// Whether this is a user installation (`true`) or system installation (`false`). + pub user: bool, + /// The origin remote (`flatpark`, `-origin`, `flathub`), when known. + pub origin: Option, + /// The installation scope or name (`user`, `system`, or a named installation), for Flatpak installs. + pub installation: Option, } /// `dpkg-query -W -f '${Package}|${db:Status-Status}|${Version}\n'`: installed packages only. @@ -46,18 +52,35 @@ pub fn parse_pacman(text: &str) -> Vec<(String, String)> { .collect() } -/// `flatpak list --app --columns=application,version`: the version column is often empty for -/// bundles built from a release tarball. -pub fn parse_flatpak_list(text: &str) -> Vec<(String, String)> { +/// `flatpak list --app --columns=application,version,origin,installation`: parses id, version, origin, and installation. +pub fn parse_flatpak_list_with_origin(text: &str) -> Vec<(String, String, Option, Option)> { text.lines() .filter_map(|line| { let mut f = line.split('\t'); let id = f.next()?.trim(); - (!id.is_empty()).then(|| (id.to_string(), f.next().unwrap_or("").trim().to_string())) + if id.is_empty() { + return None; + } + let version = f.next().unwrap_or("").trim().to_string(); + let origin = f.next().map(str::trim).filter(|o| !o.is_empty()).map(ToString::to_string); + let installation = f.next().map(str::trim).filter(|i| !i.is_empty()).map(ToString::to_string); + Some((id.to_string(), version, origin, installation)) }) .collect() } +/// `flatpak list --app --columns=application,version`: the version column is often empty for +/// bundles built from a release tarball. +pub fn parse_flatpak_list(text: &str) -> Vec<(String, String)> { + parse_flatpak_list_with_origin(text).into_iter().map(|(id, v, _, _)| (id, v)).collect() +} + +/// Whether an origin string indicates an install from FlatPark. +pub fn is_flatpark_origin(origin: &str) -> bool { + let o = origin.trim(); + o.eq_ignore_ascii_case("flatpark") || o.to_ascii_lowercase().contains("flatpark") +} + /// The `Version:` line of `flatpak info `. pub fn parse_flatpak_info(text: &str) -> Option { text.lines().find_map(|l| l.trim().strip_prefix("Version:").map(|v| v.trim().to_string())).filter(|v| !v.is_empty()) @@ -78,11 +101,14 @@ pub fn candidate_names(app: &App, manager: InstallSource) -> Vec { } } -/// The package of `app` among `installed`, preferring the manager listed first in `order`. +/// The package of `app` among `installed`, preferring the manager listed first in `order`. A user +/// install beats a system one under any of the app's ids; within a scope the current id wins. pub fn find<'a>(app: &App, installed: &'a [Package], order: &[InstallSource]) -> Option<&'a Package> { order.iter().find_map(|manager| { let names = candidate_names(app, *manager); - names.iter().find_map(|n| installed.iter().find(|p| p.manager == *manager && p.name == *n)) + [true, false] + .into_iter() + .find_map(|user| names.iter().find_map(|n| installed.iter().find(|p| p.manager == *manager && p.user == user && p.name == *n))) }) } @@ -96,7 +122,7 @@ mod tests { use super::*; fn pkgs(manager: InstallSource, list: Vec<(String, String)>) -> Vec { - list.into_iter().map(|(name, version)| Package { manager, name, version }).collect() + list.into_iter().map(|(name, version)| Package { manager, name, version, user: false, origin: None, installation: None }).collect() } #[test] @@ -111,6 +137,8 @@ mod tests { parse_flatpak_list("ai.storyteller.wordcraft\t\nai.storyteller.cadcraft\t0.3.0\n"), [("ai.storyteller.wordcraft".into(), String::new()), ("ai.storyteller.cadcraft".into(), "0.3.0".into())] ); + // A row with no id is not a package listing. + assert!(parse_flatpak_list_with_origin("\t0.2.0\tflatpark\tuser\n").is_empty()); let info = "\nWordCraft - Word processor\n\n ID: ai.storyteller.wordcraft\n Version: 0.3.0\n Branch: master\n"; assert_eq!(parse_flatpak_info(info).as_deref(), Some("0.3.0")); assert_eq!(parse_flatpak_info("Version: \n"), None); @@ -158,4 +186,143 @@ mod tests { assert_eq!(version_of(hit).unwrap().to_string(), "0.5.0"); assert_eq!(find(photo, &installed, &[InstallSource::Flatpak]).unwrap().version, "0.4.0"); } + + #[test] + fn parses_flatpak_list_with_origin_and_spots_flatpark() { + let text = "ai.storyteller.photocraft\t0.19.0\tflatpark\tuser\nai.storyteller.cadcraft\t0.16.0\tcadcraft-origin\tsystem\nai.storyteller.wordcraft\t0.14.0\t\t\n"; + let list = parse_flatpak_list_with_origin(text); + assert_eq!( + list, + [ + ("ai.storyteller.photocraft".into(), "0.19.0".into(), Some("flatpark".into()), Some("user".into())), + ("ai.storyteller.cadcraft".into(), "0.16.0".into(), Some("cadcraft-origin".into()), Some("system".into())), + ("ai.storyteller.wordcraft".into(), "0.14.0".into(), None, None), + ] + ); + assert!(is_flatpark_origin("flatpark")); + assert!(is_flatpark_origin("FlatPark")); + assert!(is_flatpark_origin("flatpark-origin")); + assert!(!is_flatpark_origin("cadcraft-origin")); + assert!(!is_flatpark_origin("flathub")); + assert!(!is_flatpark_origin("")); + } + + #[test] + fn flatpak_find_prioritizes_user_over_system() { + let photo = App::by_id("photocraft").unwrap(); + let installed = vec![ + Package { + manager: InstallSource::Flatpak, + name: "ai.storyteller.photocraft".into(), + version: "0.18.0".into(), + user: false, + origin: Some("flathub".into()), + installation: Some("system".into()), + }, + Package { + manager: InstallSource::Flatpak, + name: "ai.storyteller.photocraft".into(), + version: "0.19.0".into(), + user: true, + origin: Some("flatpark".into()), + installation: Some("user".into()), + }, + ]; + let found = find(photo, &installed, &[InstallSource::Flatpak]).unwrap(); + assert!(found.user); + assert_eq!(found.version, "0.19.0"); + assert_eq!(found.origin.as_deref(), Some("flatpark")); + assert_eq!(found.installation.as_deref(), Some("user")); + } +} + +/// Matches the OCI URL from https://getartcraft.com/artcraft-oci.flatpakrepo, +/// independently of its local remote name (normally `artcraft-oci`). +pub fn is_artcraft_remote(origin: &str, remotes: &str) -> bool { + remotes.lines().any(|line| { + let Some((name, url)) = line.split_once('\t') else { return false }; + let url = url.trim().trim_end_matches('/'); + name.trim() == origin && url == "oci+https://getartcraft.com" + }) +} + +#[cfg(test)] +mod remote_url_tests { + use super::*; + #[test] + fn recognizes_custom_names_and_rejects_unrelated_urls() { + assert!(is_artcraft_remote("artcraft-oci", "artcraft-oci\toci+https://getartcraft.com\n")); + assert!(is_artcraft_remote("custom", "custom\toci+https://getartcraft.com/\n")); + for url in [ + "oci+https://getartcraft.com.evil", + "oci+https://getartcraft.com/other", + "oci+http://getartcraft.com", + "https://getartcraft.com/linux", + "https://getartcraft.com/artcraft-oci.flatpakrepo", + "https://getartcraft.com", + ] { + assert!(!is_artcraft_remote("custom", &format!("custom\t{url}"))); + } + assert!(!is_artcraft_remote("other", "custom\toci+https://getartcraft.com")); + } +} + +/// A missing ref permits bundle fallback; an unknown or different version must not replace it. +#[cfg(all(target_os = "linux", feature = "oci_migration"))] +pub(crate) fn oci_ref_available(reference: &str, expected: &version::Version, listing: &str) -> Result { + for line in listing.lines() { + let mut fields = line.split('\t'); + if fields.next().map(str::trim) != Some(reference) { + continue; + } + let candidate = fields.next().and_then(|value| version::parse(value.trim())); + if candidate.as_ref() != Some(expected) { + return Err("The ArtCraft OCI repository does not yet provide the selected version".into()); + } + return Ok(true); + } + Ok(false) +} + +#[cfg(all(test, target_os = "linux", feature = "oci_migration"))] +mod migration_tests { + use super::*; + #[test] + fn migration_requires_the_exact_ref_and_selected_version() { + let reference = "app/ai.storyteller.photocraft/x86_64/stable"; + let expected = version::parse("1.2.3").unwrap(); + assert_eq!(oci_ref_available(reference, &expected, &format!("{reference}\t1.2.3")), Ok(true)); + assert_eq!(oci_ref_available(reference, &expected, "app/ai.storyteller.photocraft/aarch64/stable\t1.2.3"), Ok(false)); + for value in ["", "1.2.2", "1.2.4", "unknown"] { + assert!(oci_ref_available(reference, &expected, &format!("{reference}\t{value}")).is_err()); + } + } +} + +/// Only a confirmed bundle origin with no repository URL permits bundle replacement. +/// Missing rows are unknown, so preserve remote ownership conservatively. +pub fn remote_manages_flatpak(origin: &str, remotes: &str) -> bool { + remotes + .lines() + .find_map(|line| { + let mut fields = line.split('\t'); + if fields.next().map(str::trim) != Some(origin) { + return None; + } + Some(fields.next().is_some_and(|url| !url.trim().is_empty())) + }) + .unwrap_or(true) +} + +#[cfg(test)] +mod remote_ownership_tests { + use super::*; + #[test] + fn only_confirmed_empty_urls_allow_bundle_replacement() { + assert!(!remote_manages_flatpak("app-origin", "app-origin\t\n")); + assert!(remote_manages_flatpak("renamed", "renamed\thttps://example.org/repo\n")); + assert!(remote_manages_flatpak("renamed", "renamed\toci+https://getartcraft.com\n")); + assert!(remote_manages_flatpak("missing", "other\t\n")); + assert!(!remote_manages_flatpak("app-origin", "app-origin")); + } } diff --git a/crates/update/src/process.rs b/crates/update/src/process.rs index 13ed8dda4..c20c3bc45 100644 --- a/crates/update/src/process.rs +++ b/crates/update/src/process.rs @@ -839,3 +839,81 @@ mod inherited_pipe_tests { } } } + +/// Runs a helper while polling cancellation and draining its bounded output. +#[cfg(target_os = "linux")] +pub(crate) fn cancellable_output(mut cmd: Command, check: impl Fn() -> Result<(), String>) -> Result { + check()?; + let mut child = + cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().map_err(|e| format!("couldn't run flatpak ({e})"))?; + let stdout = read_all(child.stdout.take()).map_err(|e| format!("couldn't read flatpak output ({e})"))?; + let stderr = read_all(child.stderr.take()).map_err(|e| format!("couldn't read flatpak output ({e})"))?; + let started = Instant::now(); + let status = loop { + if started.elapsed() >= Duration::from_secs(60 * 60) { + let _ = child.kill(); + let _ = child.wait(); + return Err("Flatpak did not finish within one hour".into()); + } + if let Err(e) = check() { + let _ = child.kill(); + let _ = child.wait(); + return Err(e); + } + match child.try_wait() { + Ok(Some(status)) => break status, + Ok(None) => std::thread::sleep(Duration::from_millis(25)), + Err(e) => { + let _ = child.kill(); + let _ = child.wait(); + return Err(format!("couldn't wait for flatpak ({e})")); + } + } + }; + let deadline = Instant::now(); + while !stdout.is_finished() || !stderr.is_finished() { + check()?; + if deadline.elapsed() > Duration::from_secs(5) { + return Err("Flatpak output did not close after completion".into()); + } + std::thread::sleep(Duration::from_millis(25)); + } + Ok(std::process::Output { status, stdout: stdout.join().unwrap_or_default(), stderr: stderr.join().unwrap_or_default() }) +} + +#[cfg(all(test, target_os = "linux"))] +mod update_cancel_tests { + use super::*; + + #[test] + fn cancels_a_running_helper_promptly() { + let mut cmd = command("sleep"); + cmd.arg("30"); + let start = Instant::now(); + let result = cancellable_output(cmd, || if start.elapsed() >= Duration::from_millis(100) { Err("cancelled".into()) } else { Ok(()) }); + assert_eq!(result.unwrap_err(), "cancelled"); + assert!(start.elapsed() < Duration::from_secs(5)); + } + + /// A descendant that outlives the helper keeps the pipes open; the caller gets an error at + /// the deadline instead of waiting for it. + #[test] + fn output_held_open_by_a_descendant_fails_at_the_deadline() { + let mut cmd = command("sh"); + cmd.args(["-c", "sleep 8 &"]); + let start = Instant::now(); + let err = cancellable_output(cmd, || Ok(())).expect_err("a descendant holding the pipes should time out"); + assert!(err.contains("did not close after completion"), "{err}"); + assert!(start.elapsed() < Duration::from_secs(8), "{:?}", start.elapsed()); + } + + #[test] + fn retains_both_output_streams_and_exit_status() { + let mut cmd = command("sh"); + cmd.args(["-c", "printf output; printf error >&2; exit 7"]); + let output = cancellable_output(cmd, || Ok(())).unwrap(); + assert_eq!(output.status.code(), Some(7)); + assert_eq!(output.stdout, b"output"); + assert_eq!(output.stderr, b"error"); + } +} diff --git a/crates/update/src/self_update.rs b/crates/update/src/self_update.rs index a20f466ce..f96c3af63 100644 --- a/crates/update/src/self_update.rs +++ b/crates/update/src/self_update.rs @@ -878,7 +878,15 @@ pub fn prepare_update_with_options( match install { #[cfg(target_os = "macos")] Install::MacApp(bundle) => { - let existing = Installed { version: None, source: InstallSource::MacApp, location: bundle.display().to_string(), package: None }; + let existing = Installed { + version: None, + source: InstallSource::MacApp, + location: bundle.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; let wait = install::macos::CheckTurn { stop, phase: &mut *phase }; let placed = install::macos::install_dmg(app, &file, Some(&existing), None, false, paths, &install::macos::Expect::self_update(release), wait); @@ -3535,7 +3543,15 @@ mod tests { assert!(ok, "hdiutil create failed"); drop(one_at_a_time); let current = apps.join("ArtCraft Launcher.app"); - let existing = Installed { version: None, source: InstallSource::MacApp, location: current.display().to_string(), package: None }; + let existing = Installed { + version: None, + source: InstallSource::MacApp, + location: current.display().to_string(), + package: None, + externally_managed: false, + origin: None, + installation: None, + }; // An unsigned test bundle: the signature check has its own tests in `install`. let expect = install::macos::Expect { team: None, version: None, quit_first: false }; let stop = Stop::default(); diff --git a/crates/update/src/status.rs b/crates/update/src/status.rs index 1cd0cf76c..cc985df0a 100644 --- a/crates/update/src/status.rs +++ b/crates/update/src/status.rs @@ -61,4 +61,21 @@ pub struct Installed { /// The package that installed it, for package-manager installs (`photocraft-bin`). #[serde(default)] pub package: Option, + /// Whether this copy is managed externally (such as a Flatpak from FlatPark) and + /// will not be replaced with a downloaded bundle. + #[serde(default)] + pub externally_managed: bool, + /// The remote repository or origin (`flatpark`, `-origin`), when known. + #[serde(default)] + pub origin: Option, + /// The installation scope or name (`user`, `system`, or a named installation), for Flatpak installs. + #[serde(default)] + pub installation: Option, +} + +impl Installed { + /// Whether this copy is managed externally and should not be replaced by a downloaded bundle. + pub fn is_externally_managed(&self) -> bool { + self.externally_managed + } } From a9efd722b49924588448fba5c59f36d3aee3f4c6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 17:56:38 -0500 Subject: [PATCH 371/471] Expose bounded BMP header recognition for consumer cutovers (#1929) * Expose supported BMP header recognition Port the BMP routing check from LightCraft commit 304180a500d1b3f87c8c841f269d305161d6c8b9, preserving minimum length and DIB allowlist. * Test BMP routing boundaries and unsupported DIB sizes --------- Co-authored-by: Brandon Thomas --- crates/image-codecs/src/bmp_probe.rs | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/image-codecs/src/bmp_probe.rs b/crates/image-codecs/src/bmp_probe.rs index 59c78ca1e..5b47721bc 100644 --- a/crates/image-codecs/src/bmp_probe.rs +++ b/crates/image-codecs/src/bmp_probe.rs @@ -1,5 +1,18 @@ //! Header-only BMP dimensions and pixel-data coverage, without allocating a canvas. +/// Recognize a BMP file header with a supported DIB header kind. +/// +/// This is a format-routing check, not full validation: dimensions and pixel data may still +/// be invalid. Unlike the broad signature detector, it requires the minimum 26-byte core +/// header and a known DIB size, preserving callers that reject arbitrary `BM` prefixes. +pub fn is_supported_header(b: &[u8]) -> bool { + if b.len() >= 26 && b.starts_with(b"BM") { + let dib = u32::from_le_bytes([b[14], b[15], b[16], b[17]]); + return matches!(dib, 12 | 16 | 40 | 52 | 56 | 64 | 108 | 124); + } + false +} + /// Coverage of the declared pixel array. Row byte counts include row padding; RLE coverage /// counts the pixels drawn by commands and may be smaller than the canvas. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -142,4 +155,23 @@ mod tests { let nibble = bmp(2, &[0, 3, 0x12, 0x30, 0, 1]); assert_eq!(coverage(&nibble, 4, 2).pixels, 3); } + #[test] + fn supported_header_recognition_preserves_length_signature_and_dib_rules() { + let mut b = [0_u8; 26]; + b[..2].copy_from_slice(b"BM"); + for dib in [12_u32, 16, 40, 52, 56, 64, 108, 124] { + b[14..18].copy_from_slice(&dib.to_le_bytes()); + assert!(is_supported_header(&b), "DIB {dib}"); + for n in 0..26 { + assert!(!is_supported_header(&b[..n]), "prefix {n}"); + } + } + for dib in [0_u32, 13, 39, 41, 125, u32::MAX] { + b[14..18].copy_from_slice(&dib.to_le_bytes()); + assert!(!is_supported_header(&b), "DIB {dib}"); + } + b[14..18].copy_from_slice(&40_u32.to_le_bytes()); + b[0] = b'X'; + assert!(!is_supported_header(&b)); + } } From be0b1e655229e34f1ad263ef3d2170e46449c39b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 18:22:56 -0500 Subject: [PATCH 372/471] cover the Flatpak repository guard and its update command (#1956) The Flatpak/OCI migration left update_flatpak_app and the refusal to replace a repository-managed Flatpak without tests. A stand-in flatpak on PATH, in child processes so the environment stays private, covers every scope flag, both command shapes, a successful update and Flatpak's error text; the guard test asserts the refusal happens before anything is downloaded. --- crates/update/src/install.rs | 111 +++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/crates/update/src/install.rs b/crates/update/src/install.rs index 071434595..3b3513294 100644 --- a/crates/update/src/install.rs +++ b/crates/update/src/install.rs @@ -3673,6 +3673,117 @@ esac ); } + /// A Flatpak the user installed from a repository is never replaced by the launcher's bundle: + /// the refusal names the repository's own update command, and nothing is downloaded. + #[test] + #[cfg(target_os = "linux")] + fn a_repository_flatpak_is_not_replaced_by_a_bundle_install() { + let app = crate::App::by_id("photocraft").unwrap(); + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let mut phases = Vec::new(); + let mut phase = |p: JobPhase| phases.push(p); + let mut job = Job { + http: &http, + paths: &paths, + stop: &Stop::default(), + phase: &mut phase, + install_locations: &InstallLocations::default(), + replace: false, + }; + let release = Release { version: version::parse("0.20.0"), tag: "v0.20.0".into(), ..Release::default() }; + let asset = Asset { name: "photocraft-0.20.0-linux-x86_64.flatpak".into(), ..Asset::default() }; + let installed = Installed { + version: version::parse("0.19.0"), + source: InstallSource::Flatpak, + location: app.bundle_id(), + package: Some(app.bundle_id()), + externally_managed: true, + origin: Some("flatpark".into()), + installation: Some("work".into()), + }; + let error = install(app, &release, &asset, PackageKind::Flatpak, Some(&installed), None, &mut job).unwrap_err(); + assert_eq!( + error, + "PhotoCraft was installed from a Flatpak repository. Update it with flatpak update --installation=work ai.storyteller.photocraft" + ); + assert!(phases.is_empty(), "the bundle installer started for a Flatpak the user manages"); + assert!(!paths.cache.exists(), "the refusal must come before anything is downloaded"); + } + + /// `update_flatpak_app` drives the user's own copy: the scope flag for the installation it sits + /// in, a successful update, and Flatpak's error text with its `error: ` prefix removed. A + /// stand-in `flatpak` on PATH reports what it was asked to run; the test runs in child + /// processes so that PATH stays private to it (as `updater`'s pretend-version test does). + #[test] + #[cfg(target_os = "linux")] + fn a_flatpak_from_a_repository_is_updated_through_its_own_installation() { + const NAME: &str = "install::tests::a_flatpak_from_a_repository_is_updated_through_its_own_installation"; + const DIR: &str = "CRAFT_UPDATE_FAKE_FLATPAK_DIR"; + if std::env::var_os(DIR).is_none() { + use std::os::unix::fs::PermissionsExt; + let dir = tempfile::tempdir().unwrap(); + let script = r#"#!/bin/sh +echo "$(basename "$0") $*" >> "$CRAFT_UPDATE_FAKE_FLATPAK_CALLS" +case "$*" in +*denied.app*) echo "error: Permission denied" >&2; exit 1 ;; +*diskfull.app*) echo "Error: Disk full" >&2; exit 1 ;; +*silent.app*) exit 1 ;; +esac +exit 0 +"#; + for program in ["flatpak", "flatpak-spawn"] { + let path = dir.path().join(program); + std::fs::write(&path, script).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + let path = format!("{}:{}", dir.path().display(), std::env::var("PATH").unwrap_or_default()); + for sandboxed in [false, true] { + let calls = dir.path().join(format!("calls-{sandboxed}")); + let mut cmd = std::process::Command::new(std::env::current_exe().unwrap()); + cmd.args(["--exact", NAME, "--nocapture"]) + .env(DIR, dir.path()) + .env("CRAFT_UPDATE_FAKE_FLATPAK_CALLS", &calls) + .env("PATH", &path) + .env_remove("FLATPAK_ID"); + if sandboxed { + cmd.env("FLATPAK_ID", "ai.storyteller.photocraft"); + } + let status = cmd.status().unwrap(); + assert!(status.success(), "the sandboxed={sandboxed} run failed"); + } + return; + } + let calls = std::env::var_os("CRAFT_UPDATE_FAKE_FLATPAK_CALLS").map(PathBuf::from).unwrap(); + let app = crate::App::by_id("photocraft").unwrap(); + let stop = Stop::default(); + let mut phase = |_| (); + let id = app.bundle_id(); + assert_eq!(linux::update_flatpak_app(app, &id, None, &stop, &mut phase), Ok(())); + assert_eq!( + linux::update_flatpak_app(app, "denied.app", Some("user"), &stop, &mut phase), + Err("Flatpak couldn't update PhotoCraft: Permission denied".to_string()) + ); + assert_eq!( + linux::update_flatpak_app(app, "diskfull.app", Some("work"), &stop, &mut phase), + Err("Flatpak couldn't update PhotoCraft: Disk full".to_string()) + ); + assert_eq!( + linux::update_flatpak_app(app, "silent.app", Some("system"), &stop, &mut phase), + Err("Flatpak couldn't update PhotoCraft".to_string()) + ); + let prefix = if process::in_sandbox() { "flatpak-spawn --host --watch-bus flatpak" } else { "flatpak" }; + let expected: Vec = vec![ + format!("{prefix} update --system -y --noninteractive {id}"), + format!("{prefix} update --user -y --noninteractive denied.app"), + format!("{prefix} update --installation=work -y --noninteractive diskfull.app"), + format!("{prefix} update --system -y --noninteractive silent.app"), + ]; + let calls = std::fs::read_to_string(&calls).unwrap(); + assert_eq!(calls.lines().map(str::to_string).collect::>(), expected); + } + #[test] #[cfg(target_os = "linux")] fn update_flatpak_app_respects_cancellation() { From 12db39ca963d2a333bd1891b0dbba65218800fd8 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 373/471] Extract score writer changes from SoundCraft 0751dd31 --- crates/score/src/lib.rs | 344 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 344 insertions(+) create mode 100644 crates/score/src/lib.rs diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs new file mode 100644 index 000000000..30375b652 --- /dev/null +++ b/crates/score/src/lib.rs @@ -0,0 +1,344 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! MusicXML and SVG score writing over application-independent notation data. + +use std::fmt::Write as _; + +#[derive(Debug, Clone, PartialEq)] +pub struct ScoreSetup { + pub title: String, + pub composer: String, + pub bars_per_system: u32, + pub show_track_names: bool, +} + +/// One measure: its meter and its items (notes, chords or rests), in sixteenths. +#[derive(Debug, Clone, PartialEq)] +pub struct Measure { + pub numerator: u32, + pub denominator: u32, + pub items: Vec, +} + +/// A notehead group (chord) or a rest (no pitches). +#[derive(Debug, Clone, PartialEq)] +pub struct Item { + pub pitches: Vec, + /// Duration in sixteenths; always one of the supported duration values. + pub units: u32, + pub tie_start: bool, + pub tie_stop: bool, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Part { + pub name: String, + /// True when the part reads best in bass clef. + pub bass: bool, + pub measures: Vec, +} + +fn spell(pitch: u8) -> (char, i32, i32) { + const STEPS: [(char, i32); 12] = + [('C', 0), ('C', 1), ('D', 0), ('D', 1), ('E', 0), ('F', 0), ('F', 1), ('G', 0), ('G', 1), ('A', 0), ('A', 1), ('B', 0)]; + let (step, alter) = STEPS[usize::from(pitch % 12)]; + (step, alter, i32::from(pitch / 12) - 1) +} + +fn xml_escape(s: &str) -> String { + s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) +} + +fn note_type(units: u32) -> (&'static str, bool) { + match units { + 16 => ("whole", false), + 12 => ("half", true), + 8 => ("half", false), + 6 => ("quarter", true), + 4 => ("quarter", false), + 3 => ("eighth", true), + 2 => ("eighth", false), + _ => ("16th", false), + } +} + +/// MusicXML 4.0 (partwise) for `parts`. +pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> String { + let mut x = String::new(); + x.push_str("\n"); + x.push_str("\n"); + x.push_str("\n"); + let _ = writeln!(x, " {}", xml_escape(&setup.title)); + x.push_str(" \n"); + if !setup.composer.is_empty() { + let _ = writeln!(x, " {}", xml_escape(&setup.composer)); + } + x.push_str(" SoundCraft\n \n \n"); + for (i, p) in parts.iter().enumerate() { + let _ = writeln!(x, " {}", i + 1, xml_escape(&p.name)); + } + x.push_str(" \n"); + for (i, p) in parts.iter().enumerate() { + let _ = writeln!(x, " ", i + 1); + let mut meter = (0, 0); + for (mi, m) in p.measures.iter().enumerate() { + let _ = writeln!(x, " ", mi + 1); + if mi == 0 || meter != (m.numerator, m.denominator) { + x.push_str(" "); + if mi == 0 { + x.push_str("40"); + } + let _ = write!(x, "", m.numerator, m.denominator); + if mi == 0 { + x.push_str(if p.bass { "F4" } else { "G2" }); + } + x.push_str("\n"); + meter = (m.numerator, m.denominator); + } + for it in &m.items { + let (ty, dot) = note_type(it.units); + if it.pitches.is_empty() { + let _ = writeln!( + x, + " {}1{ty}{}", + it.units, + if dot { "" } else { "" } + ); + continue; + } + for (k, pitch) in it.pitches.iter().enumerate() { + let (step, alter, octave) = spell(*pitch); + x.push_str(" "); + if k > 0 { + x.push_str(""); + } + let _ = write!(x, "{step}"); + if alter != 0 { + let _ = write!(x, "{alter}"); + } + let _ = write!(x, "{octave}{}", it.units); + if it.tie_stop { + x.push_str(""); + } + if it.tie_start { + x.push_str(""); + } + let _ = write!(x, "1{ty}"); + if dot { + x.push_str(""); + } + if alter != 0 { + x.push_str("sharp"); + } + if it.tie_stop || it.tie_start { + x.push_str(""); + if it.tie_stop { + x.push_str(""); + } + if it.tie_start { + x.push_str(""); + } + x.push_str(""); + } + x.push_str("\n"); + } + } + x.push_str(" \n"); + } + x.push_str(" \n"); + } + x.push_str("\n"); + x +} + +/// Staff position of `pitch` in half-spaces above the bottom line (treble E4 / bass G2). +fn staff_step(pitch: u8, bass: bool) -> i32 { + let (step, _, octave) = spell(pitch); + let idx = "CDEFGAB".find(step).map_or(0, |i| i as i32); + let diatonic = octave * 7 + idx; + diatonic - if bass { 2 * 7 + 4 } else { 4 * 7 + 2 } +} + +/// An engraved score as a printable SVG page (A4 portrait width, as tall as needed). +pub fn svg(parts: &[Part], setup: &ScoreSetup) -> String { + const W: f64 = 794.0; + const MARGIN: f64 = 48.0; + const SP: f64 = 7.0; // staff space + const STAFF_GAP: f64 = 70.0; + const SYSTEM_GAP: f64 = 40.0; + let n_measures = parts.iter().map(|p| p.measures.len()).max().unwrap_or(0); + let per = setup.bars_per_system.max(1) as usize; + let systems = n_measures.div_ceil(per).max(1); + let label_w = if setup.show_track_names { 70.0 } else { 0.0 }; + let staff_x0 = MARGIN + label_w + 34.0; + let staff_x1 = W - MARGIN; + let sys_h = parts.len().max(1) as f64 * STAFF_GAP + SYSTEM_GAP; + let top = 110.0; + let h = top + systems as f64 * sys_h + MARGIN; + let mut o = String::new(); + let _ = + writeln!(o, ""); + let _ = writeln!(o, ""); + let _ = writeln!(o, "{}", W / 2.0, xml_escape(&setup.title)); + if !setup.composer.is_empty() { + let _ = writeln!(o, "{}", W - MARGIN, xml_escape(&setup.composer)); + } + let bar_w = (staff_x1 - staff_x0) / per as f64; + for sys in 0..systems { + let y_sys = top + sys as f64 * sys_h; + for (pi, p) in parts.iter().enumerate() { + let y0 = y_sys + pi as f64 * STAFF_GAP; // top line + let bottom = y0 + 4.0 * SP; + if setup.show_track_names && sys == 0 { + let _ = writeln!(o, "{}", y0 + 2.0 * SP + 4.0, xml_escape(&p.name)); + } + for l in 0..5 { + let y = y0 + f64::from(l) * SP; + let _ = writeln!( + o, + "", + staff_x0 - 30.0 + ); + } + let clef = if p.bass { "𝄢" } else { "𝄞" }; + let _ = writeln!( + o, + "{clef}", + staff_x0 - 29.0, + bottom + if p.bass { -SP } else { SP * 0.6 }, + if p.bass { 26 } else { 36 } + ); + for k in 0..per { + let mi = sys * per + k; + let x0 = staff_x0 + k as f64 * bar_w; + let _ = writeln!( + o, + "", + x0 + bar_w, + x0 + bar_w + ); + let Some(m) = p.measures.get(mi) else { continue }; + if pi == 0 { + let _ = writeln!(o, "{}", x0 + 2.0, y0 - 8.0, mi + 1); + } + let total: u32 = m.items.iter().map(|i| i.units).sum::().max(1); + let mut acc = 0u32; + for it in &m.items { + let x = x0 + 10.0 + (bar_w - 16.0) * f64::from(acc) / f64::from(total); + acc += it.units; + let (_, dot) = note_type(it.units); + if it.pitches.is_empty() { + rest(&mut o, x, y0, SP, it.units); + if dot { + let _ = writeln!(o, "", x + 7.0, y0 + 1.5 * SP); + } + continue; + } + let steps: Vec = it.pitches.iter().map(|p2| staff_step(*p2, p.bass)).collect(); + let filled = it.units < 8; + for (pitch, st) in it.pitches.iter().zip(&steps) { + let y = bottom - f64::from(*st) * SP / 2.0; + // Ledger lines. + let mut l = if *st < 0 { -2 } else { 10 }; + while (*st < 0 && l >= *st) || (*st > 8 && l <= *st) { + let ly = bottom - f64::from(l) * SP / 2.0; + let _ = writeln!( + o, + "", + x - 7.0, + x + 7.0 + ); + l += if *st < 0 { -2 } else { 2 }; + } + let fill = if filled { "black" } else { "white" }; + let _ = writeln!( + o, + "" + ); + if spell(*pitch).1 != 0 { + let _ = writeln!(o, "♯", x - 14.0, y + 4.0); + } + if dot { + let _ = writeln!(o, "", x + 8.0, y - 1.5); + } + } + if it.units < 16 { + let (lo, hi) = (steps.iter().copied().min().unwrap_or(4), steps.iter().copied().max().unwrap_or(4)); + let up = (lo + hi) < 8; + let (sx, y_from, y_to) = if up { + (x + 4.3, bottom - f64::from(lo) * SP / 2.0, bottom - f64::from(hi) * SP / 2.0 - 3.5 * SP) + } else { + (x - 4.3, bottom - f64::from(hi) * SP / 2.0, bottom - f64::from(lo) * SP / 2.0 + 3.5 * SP) + }; + let _ = writeln!( + o, + "" + ); + let flags = match it.units { + 1 => 2, + 2 | 3 => 1, + _ => 0, + }; + for f in 0..flags { + let fy = y_to + f64::from(f) * if up { 5.0 } else { -5.0 }; + let dy = if up { 9.0 } else { -9.0 }; + let _ = writeln!( + o, + "", + dy * 0.5 + ); + } + } + if it.tie_start { + let y = bottom - f64::from(steps[0]) * SP / 2.0 + 6.0; + let _ = writeln!(o, "", x + 3.0); + } + } + } + } + // System bracket. + let y_last = y_sys + (parts.len().max(1) - 1) as f64 * STAFF_GAP + 4.0 * SP; + let _ = writeln!( + o, + "", + staff_x0 - 30.0, + staff_x0 - 30.0 + ); + } + o.push_str("\n"); + o +} + +fn rest(o: &mut String, x: f64, y0: f64, sp: f64, units: u32) { + let mid = y0 + 2.0 * sp; + match units { + 16 => { + let _ = writeln!(o, "", x - 4.5, y0 + sp, sp / 2.0); + } + 8 | 12 => { + let _ = writeln!(o, "", x - 4.5, mid - sp / 2.0, sp / 2.0); + } + 4 | 6 => { + let _ = writeln!( + o, + "", + x - 2.0, + mid - 9.0 + ); + } + _ => { + let _ = writeln!(o, "", x - 2.0, mid - 3.0); + let _ = writeln!( + o, + "", + x + 2.5, + mid - 4.0, + x - 1.0, + mid + 7.0 + ); + if units == 1 { + let _ = writeln!(o, "", x - 3.5, mid + 2.0); + } + } + } +} + From 68abf77e2ddaea226a742efcb363630fa07faa3c Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 374/471] Extract score writer changes from SoundCraft 7509b64c --- crates/score/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index 30375b652..3aee96472 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -45,7 +45,13 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) + s.chars() + .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) + .collect::() + .replace('&', "&") + .replace('<', "<") + .replace('>', ">") + .replace('"', """) } fn note_type(units: u32) -> (&'static str, bool) { From 9dfaaf27af936868710d44042ede6705cb9e616f Mon Sep 17 00:00:00 2001 From: Samuel Bacaro Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 375/471] Extract score writer changes from SoundCraft ccd3fe7c --- crates/score/src/lib.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index 3aee96472..d51ab8333 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -3,6 +3,14 @@ use std::fmt::Write as _; +const MAX_OUTPUT_BYTES: usize = 256 * 1024 * 1024; // 256 MiB +fn over_budget(buf: &str) -> Result<(), String> { + if buf.len() > MAX_OUTPUT_BYTES { + return Err(format!("score too large: over {} MiB", MAX_OUTPUT_BYTES / (1024 * 1024))); + } + Ok(()) +} + #[derive(Debug, Clone, PartialEq)] pub struct ScoreSetup { pub title: String, @@ -45,13 +53,7 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - s.chars() - .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) - .collect::() - .replace('&', "&") - .replace('<', "<") - .replace('>', ">") - .replace('"', """) + s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) } fn note_type(units: u32) -> (&'static str, bool) { @@ -68,7 +70,7 @@ fn note_type(units: u32) -> (&'static str, bool) { } /// MusicXML 4.0 (partwise) for `parts`. -pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> String { +pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result { let mut x = String::new(); x.push_str("\n"); x.push_str("\n"); @@ -149,11 +151,12 @@ pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> String { } } x.push_str(" \n"); + over_budget(&x)?; } x.push_str(" \n"); } x.push_str("\n"); - x + Ok(x) } /// Staff position of `pitch` in half-spaces above the bottom line (treble E4 / bass G2). @@ -165,7 +168,7 @@ fn staff_step(pitch: u8, bass: bool) -> i32 { } /// An engraved score as a printable SVG page (A4 portrait width, as tall as needed). -pub fn svg(parts: &[Part], setup: &ScoreSetup) -> String { +pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result { const W: f64 = 794.0; const MARGIN: f64 = 48.0; const SP: f64 = 7.0; // staff space @@ -299,6 +302,7 @@ pub fn svg(parts: &[Part], setup: &ScoreSetup) -> String { let _ = writeln!(o, "", x + 3.0); } } + over_budget(&o)?; } } // System bracket. @@ -311,7 +315,7 @@ pub fn svg(parts: &[Part], setup: &ScoreSetup) -> String { ); } o.push_str("\n"); - o + Ok(o) } fn rest(o: &mut String, x: f64, y0: f64, sp: f64, units: u32) { From 8c6e050f324f8e50bddf556d12fcd48e3476d505 Mon Sep 17 00:00:00 2001 From: Samuel Bacaro Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 376/471] Extract score writer changes from SoundCraft fa77e03e --- crates/score/src/lib.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index d51ab8333..59a28de68 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -4,9 +4,9 @@ use std::fmt::Write as _; const MAX_OUTPUT_BYTES: usize = 256 * 1024 * 1024; // 256 MiB -fn over_budget(buf: &str) -> Result<(), String> { - if buf.len() > MAX_OUTPUT_BYTES { - return Err(format!("score too large: over {} MiB", MAX_OUTPUT_BYTES / (1024 * 1024))); +fn over_budget(buf: &str, budget: usize) -> Result<(), String> { + if buf.len() > budget { + return Err(format!("score too large: over {} bytes", budget)); } Ok(()) } @@ -71,10 +71,19 @@ fn note_type(units: u32) -> (&'static str, bool) { /// MusicXML 4.0 (partwise) for `parts`. pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result { + musicxml_budgeted(parts, setup, MAX_OUTPUT_BYTES) +} + +/// [`musicxml`] with an explicit byte budget (kept separate so tests can pass a tiny budget +/// instead of building a 256 MiB score). +fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result { let mut x = String::new(); x.push_str("\n"); x.push_str("\n"); x.push_str("\n"); + // The header (and an oversized title/composer, escaped) can itself exceed the budget even with + // no measures, so check before the per-measure loop too. + over_budget(&x, budget)?; let _ = writeln!(x, " {}", xml_escape(&setup.title)); x.push_str(" \n"); if !setup.composer.is_empty() { @@ -85,6 +94,7 @@ pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result { let _ = writeln!(x, " {}", i + 1, xml_escape(&p.name)); } x.push_str(" \n"); + over_budget(&x, budget)?; for (i, p) in parts.iter().enumerate() { let _ = writeln!(x, " ", i + 1); let mut meter = (0, 0); @@ -151,11 +161,12 @@ pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result { } } x.push_str(" \n"); - over_budget(&x)?; + over_budget(&x, budget)?; } x.push_str(" \n"); } x.push_str("\n"); + over_budget(&x, budget)?; Ok(x) } @@ -169,6 +180,12 @@ fn staff_step(pitch: u8, bass: bool) -> i32 { /// An engraved score as a printable SVG page (A4 portrait width, as tall as needed). pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result { + svg_budgeted(parts, setup, MAX_OUTPUT_BYTES) +} + +/// [`svg`] with an explicit byte budget (kept separate so tests can pass a tiny budget instead of +/// building a 256 MiB score). +fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result { const W: f64 = 794.0; const MARGIN: f64 = 48.0; const SP: f64 = 7.0; // staff space @@ -190,7 +207,9 @@ pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result { let _ = writeln!(o, "{}", W / 2.0, xml_escape(&setup.title)); if !setup.composer.is_empty() { let _ = writeln!(o, "{}", W - MARGIN, xml_escape(&setup.composer)); + over_budget(&o, budget)?; } + over_budget(&o, budget)?; let bar_w = (staff_x1 - staff_x0) / per as f64; for sys in 0..systems { let y_sys = top + sys as f64 * sys_h; @@ -302,8 +321,13 @@ pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result { let _ = writeln!(o, "", x + 3.0); } } - over_budget(&o)?; + // Check each measure, so a run of rest-only or missing measures that only draws + // bar lines cannot grow the output past the budget untouched. + over_budget(&o, budget)?; } + // The staff markup (lines, clef, track names) itself grows with the part count even + // without any items, so check once per staff too. + over_budget(&o, budget)?; } // System bracket. let y_last = y_sys + (parts.len().max(1) - 1) as f64 * STAFF_GAP + 4.0 * SP; @@ -313,8 +337,10 @@ pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result { staff_x0 - 30.0, staff_x0 - 30.0 ); + over_budget(&o, budget)?; } o.push_str("\n"); + over_budget(&o, budget)?; Ok(o) } From 1002aca27092b6fe41e3af5b3212080a354555cb Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 377/471] Extract score writer changes from SoundCraft 5075da2f --- crates/score/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index 59a28de68..023f35554 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -53,7 +53,13 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) + s.chars() + .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) + .collect::() + .replace('&', "&") + .replace('<', "<") + .replace('>', ">") + .replace('"', """) } fn note_type(units: u32) -> (&'static str, bool) { From bfef2e5ce28dbc1e57e897058851024d161bd09b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 378/471] Extract score writer changes from SoundCraft fa34e0c1 --- crates/score/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index 023f35554..c64ac0054 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -198,7 +198,9 @@ fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 379/471] Extract score writer changes from SoundCraft 4bdda5ff --- crates/score/src/lib.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index c64ac0054..d8e757b21 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -53,13 +53,10 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - s.chars() - .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) - .collect::() - .replace('&', "&") - .replace('<', "<") - .replace('>', ">") - .replace('"', """) + // Keep the score export's existing literal whitespace and quote spelling. The + // shared attribute writer owns XML legality/escaping; only its generated + // whitespace references are restored (input ampersands were escaped first). + craft_xml::esc_attr(s).replace(" ", "\t").replace(" ", "\n").replace(" ", "\r") } fn note_type(units: u32) -> (&'static str, bool) { From 3d160af4d90797f39196c22e81288d9c403030d2 Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Mon, 16 Nov 2026 19:20:52 -0500 Subject: [PATCH 380/471] Extract score writer changes from SoundCraft 21cb618d --- crates/score/src/lib.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index d8e757b21..cab0ad9bd 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -33,8 +33,10 @@ pub struct Item { pub pitches: Vec, /// Duration in sixteenths; always one of the supported duration values. pub units: u32, - pub tie_start: bool, - pub tie_stop: bool, + /// Pitches continuing into the next item. + pub tie_start: Vec, + /// Pitches continuing from the previous item. + pub tie_stop: Vec, } #[derive(Debug, Clone, PartialEq)] @@ -53,10 +55,13 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - // Keep the score export's existing literal whitespace and quote spelling. The - // shared attribute writer owns XML legality/escaping; only its generated - // whitespace references are restored (input ampersands were escaped first). - craft_xml::esc_attr(s).replace(" ", "\t").replace(" ", "\n").replace(" ", "\r") + s.chars() + .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) + .collect::() + .replace('&', "&") + .replace('<', "<") + .replace('>', ">") + .replace('"', """) } fn note_type(units: u32) -> (&'static str, bool) { @@ -137,10 +142,10 @@ fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Resul let _ = write!(x, "{alter}"); } let _ = write!(x, "{octave}{}", it.units); - if it.tie_stop { + if it.tie_stop.contains(pitch) { x.push_str(""); } - if it.tie_start { + if it.tie_start.contains(pitch) { x.push_str(""); } let _ = write!(x, "1{ty}"); @@ -150,12 +155,12 @@ fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Resul if alter != 0 { x.push_str("sharp"); } - if it.tie_stop || it.tie_start { + if it.tie_stop.contains(pitch) || it.tie_start.contains(pitch) { x.push_str(""); - if it.tie_stop { + if it.tie_stop.contains(pitch) { x.push_str(""); } - if it.tie_start { + if it.tie_start.contains(pitch) { x.push_str(""); } x.push_str(""); @@ -321,9 +326,16 @@ fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result", x + 3.0); + for pitch in &it.tie_start { + let above = it.pitches.first().is_some_and(|low| pitch > low); + let direction = if above { -1.0 } else { 1.0 }; + let y = bottom - f64::from(staff_step(*pitch, p.bass)) * SP / 2.0 + direction * 6.0; + let _ = writeln!( + o, + "", + x + 3.0, + direction * 5.0 + ); } } // Check each measure, so a run of rest-only or missing measures that only draws From 9b570237ef8682c3848bacf374ff1db06dc95241 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 19:23:04 -0500 Subject: [PATCH 381/471] feat(score): register reusable writers with bounded public notation input --- ATTRIBUTION.md | 4 +++ Cargo.lock | 7 +++++ Cargo.toml | 1 + crates/score/Cargo.toml | 14 +++++++++ crates/score/src/lib.rs | 65 ++++++++++++++++++++++++++++++++--------- 5 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 crates/score/Cargo.toml diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index ab167207c..70a929de8 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -103,3 +103,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso `crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained. The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate. + +## Score serializers + +`crates/score` extracts SoundCraft MusicXML and SVG score writing from `crates/engine/src/score.rs` at main `648ed9db9a0e98cc55643babffac381521a556c8`, with per-pitch ties adapted from [SoundCraft #182](https://github.com/storytold/soundcraft/pull/182) at `21cb618d93ec0e8d4771ac534994de8d5996ff10`. Source is MIT OR Apache-2.0. Import commits preserve Brandon Thomas, NetterSilver, Samuel Bacaro and Vikram Bhaskaran as primary authors of their respective changes. The consumer retains session mapping and layout. Shared API wiring, validation and focused tests are subsequent Dylan M. Taylor changes. diff --git a/Cargo.lock b/Cargo.lock index ef82f1afa..41290ac89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2791,6 +2791,13 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-score" +version = "0.1.0" +dependencies = [ + "craft-xml", +] + [[package]] name = "craft-scraft-format" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c8ae36287..8c3c97517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-score = { path = "crates/score" } craft-shx = { path = "crates/shx" } craft-http-url = { path = "crates/http-url" } craft-usd = { path = "crates/usd" } diff --git a/crates/score/Cargo.toml b/crates/score/Cargo.toml new file mode 100644 index 000000000..f283282c4 --- /dev/null +++ b/crates/score/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-score" +description = "Application-independent MusicXML and printable SVG score writers" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[dependencies] +craft-xml.workspace = true + +[lints] +workspace = true diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index cab0ad9bd..130f9e857 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -13,6 +13,8 @@ fn over_budget(buf: &str, budget: usize) -> Result<(), String> { #[derive(Debug, Clone, PartialEq)] pub struct ScoreSetup { + /// Software name recorded in the MusicXML encoding metadata. + pub software: String, pub title: String, pub composer: String, pub bars_per_system: u32, @@ -55,13 +57,10 @@ fn spell(pitch: u8) -> (char, i32, i32) { } fn xml_escape(s: &str) -> String { - s.chars() - .filter(|c| matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}')) - .collect::() - .replace('&', "&") - .replace('<', "<") - .replace('>', ">") - .replace('"', """) + // Keep the score export's existing literal whitespace and quote spelling. The + // shared attribute writer owns XML legality/escaping; only its generated + // whitespace references are restored (input ampersands were escaped first). + craft_xml::esc_attr(s).replace(" ", "\t").replace(" ", "\n").replace(" ", "\r") } fn note_type(units: u32) -> (&'static str, bool) { @@ -82,9 +81,10 @@ pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result { musicxml_budgeted(parts, setup, MAX_OUTPUT_BYTES) } -/// [`musicxml`] with an explicit byte budget (kept separate so tests can pass a tiny budget -/// instead of building a 256 MiB score). -fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result { +/// MusicXML with an explicit byte budget, capped at 256 MiB. +pub fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result { + validate(parts, setup)?; + let budget = budget.min(MAX_OUTPUT_BYTES); let mut x = String::new(); x.push_str("\n"); x.push_str("\n"); @@ -97,7 +97,8 @@ fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Resul if !setup.composer.is_empty() { let _ = writeln!(x, " {}", xml_escape(&setup.composer)); } - x.push_str(" SoundCraft\n \n \n"); + let _ = writeln!(x, " {}", xml_escape(&setup.software)); + x.push_str(" \n \n"); for (i, p) in parts.iter().enumerate() { let _ = writeln!(x, " {}", i + 1, xml_escape(&p.name)); } @@ -191,9 +192,10 @@ pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result { svg_budgeted(parts, setup, MAX_OUTPUT_BYTES) } -/// [`svg`] with an explicit byte budget (kept separate so tests can pass a tiny budget instead of -/// building a 256 MiB score). -fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result { +/// SVG with an explicit byte budget, capped at 256 MiB. +pub fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result { + validate(parts, setup)?; + let budget = budget.min(MAX_OUTPUT_BYTES); const W: f64 = 794.0; const MARGIN: f64 = 48.0; const SP: f64 = 7.0; // staff space @@ -395,3 +397,38 @@ fn rest(o: &mut String, x: f64, y0: f64, sp: f64, units: u32) { } } +// Public notation data can come from callers other than SoundCraft's bounded layout. +// Validate before writing/escaping so malicious dimensions cannot overflow sums or create +// an unbounded temporary string before the output-budget checks run. +fn validate(parts: &[Part], setup: &ScoreSetup) -> Result<(), String> { + let invalid = || "score exceeds supported notation limits".to_string(); + if parts.len() > 4096 { + return Err(invalid()); + } + let mut text = setup.title.len().checked_add(setup.composer.len()).and_then(|n| n.checked_add(setup.software.len())).ok_or_else(invalid)?; + for part in parts { + text = text.checked_add(part.name.len()).ok_or_else(invalid)?; + if text > 16 * 1024 * 1024 || part.measures.len() > 2000 { + return Err(invalid()); + } + for measure in &part.measures { + if measure.items.len() > 128 { + return Err(invalid()); + } + for item in &measure.items { + if ![1, 2, 3, 4, 6, 8, 12, 16].contains(&item.units) + || item.pitches.len() > 16 + || item.tie_start.len() > 16 + || item.tie_stop.len() > 16 + || item.pitches.iter().chain(&item.tie_start).chain(&item.tie_stop).any(|p| *p > 127) + { + return Err(invalid()); + } + } + } + } + if text > 16 * 1024 * 1024 { + return Err(invalid()); + } + Ok(()) +} From 0006665d60e6a4ee6aeb231de33cd41d38931ece Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Mon, 16 Nov 2026 19:23:23 -0500 Subject: [PATCH 382/471] test(score): retain per-pitch MusicXML and SVG tie regression from SoundCraft 182 --- crates/score/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index 130f9e857..fccb5c32f 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -432,3 +432,37 @@ fn validate(parts: &[Part], setup: &ScoreSetup) -> Result<(), String> { } Ok(()) } +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::indexing_slicing)] +mod tests { + use super::*; + fn setup() -> ScoreSetup { + ScoreSetup { software: "SoundCraft".into(), title: "A<&\u{1}".into(), composer: "B".into(), bars_per_system: 4, show_track_names: true } + } + fn parts() -> Vec { + vec![Part { + name: "Keys".into(), + bass: false, + measures: vec![Measure { + numerator: 4, + denominator: 4, + items: vec![ + Item { pitches: vec![60, 64], units: 4, tie_start: vec![64], tie_stop: vec![] }, + Item { pitches: vec![64], units: 12, tie_start: vec![], tie_stop: vec![64] }, + ], + }], + }] + } + #[test] + fn per_pitch_ties_and_escaping_survive_serialization() { + let parts = parts(); + let xml = musicxml(&parts, &setup()).unwrap(); + assert!(xml.contains("A<&")); + let notes: Vec<_> = xml.split("").skip(1).map(|n| n.split("").next().unwrap()).collect(); + assert!(notes[0].contains("4")); + assert!(!notes[0].contains("") && notes[1].contains("")); + assert!(notes[2].contains("12") && notes[2].contains("")); + assert_eq!(svg(&parts, &setup()).unwrap().matches("stroke-width=\"0.9\"").count(), 1); + } +} From d5d65b128dfc74f77ffd23ea08fd3ea428a7d0cd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 19:23:23 -0500 Subject: [PATCH 383/471] test(score): exercise byte budgets and hostile notation input --- crates/score/src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index fccb5c32f..7ec2141c5 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -465,4 +465,13 @@ mod tests { assert!(notes[2].contains("12") && notes[2].contains("")); assert_eq!(svg(&parts, &setup()).unwrap().matches("stroke-width=\"0.9\"").count(), 1); } + #[test] + fn byte_budgets_and_hostile_durations_fail() { + assert!(musicxml_budgeted(&parts(), &setup(), 32).is_err()); + assert!(svg_budgeted(&parts(), &setup(), 32).is_err()); + let mut parts = parts(); + parts[0].measures[0].items[0].units = u32::MAX; + assert!(musicxml(&parts, &setup()).is_err()); + assert!(svg(&parts, &setup()).is_err()); + } } From 9595e0ca9bff15b04c0096458c63d96d8b090b63 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 16 Nov 2026 19:25:08 -0500 Subject: [PATCH 384/471] test(score): retain original pitch spelling and staff placement regression --- crates/score/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/score/src/lib.rs b/crates/score/src/lib.rs index 7ec2141c5..7ca06261e 100644 --- a/crates/score/src/lib.rs +++ b/crates/score/src/lib.rs @@ -474,4 +474,12 @@ mod tests { assert!(musicxml(&parts, &setup()).is_err()); assert!(svg(&parts, &setup()).is_err()); } + #[test] + fn spelling_and_staff_steps() { + assert_eq!(spell(60), ('C', 0, 4)); + assert_eq!(spell(61), ('C', 1, 4)); + assert_eq!(staff_step(64, false), 0); // E4: bottom treble line + assert_eq!(staff_step(77, false), 8); // F5: top treble line + assert_eq!(staff_step(43, true), 0); // G2: bottom bass line + } } From a0cd0e2afbe5c3c1a232c21b0ba36e39e03f07a5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 20:10:44 -0500 Subject: [PATCH 385/471] fix(flow): use CSS percentage scale for numeric HSL channels --- crates/flow/src/color.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/flow/src/color.rs b/crates/flow/src/color.rs index a80fdf60a..941b25902 100644 --- a/crates/flow/src/color.rs +++ b/crates/flow/src/color.rs @@ -131,7 +131,8 @@ pub fn parse_color(s: &str) -> Option { if let Some(p) = x.strip_suffix('%') { Some((p.trim().parse::().ok()? / 100.0).clamp(0.0, 1.0)) } else { - Some(x.parse::().ok()?.clamp(0.0, 1.0)) + // CSS Color 4: numeric saturation and lightness use the same 0–100 scale as percentages. + Some((x.parse::().ok()? / 100.0).clamp(0.0, 1.0)) } }; let s = parse_pct(parts[1])?; @@ -193,4 +194,20 @@ mod tests { assert_eq!(parse_color("rgba(0, 0, 0, inf)"), Some(Rgb(0, 0, 0))); assert_eq!(parse_color("rgba(0, 0, 0, nan%)"), Some(Rgb(0, 0, 0))); } + + #[test] + fn numeric_hsl_channels_use_the_percentage_scale() { + for (numeric, percent, expected) in [ + ("hsl(0 0 50)", "hsl(0 0% 50%)", Rgb(128, 128, 128)), + ("hsl(0 100 50)", "hsl(0 100% 50%)", Rgb(255, 0, 0)), + ("hsl(120 50 50)", "hsl(120 50% 50%)", Rgb(64, 191, 64)), + ("hsl(0 0 1)", "hsl(0 0% 1%)", Rgb(3, 3, 3)), + ("hsl(0 -10 150)", "hsl(0 -10% 150%)", Rgb(255, 255, 255)), + ("hsl(0 100 -10)", "hsl(0 100% -10%)", Rgb(0, 0, 0)), + ("hsla(0.5turn 100 50 / 50%)", "hsla(0.5turn 100% 50% / 50%)", Rgb(128, 255, 255)), + ] { + assert_eq!(parse_color(numeric), Some(expected), "{numeric}"); + assert_eq!(parse_color(numeric), parse_color(percent), "{numeric} == {percent}"); + } + } } From e2bf53f2bdba4828e21097774a568ed9c74b0004 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 20:25:09 -0500 Subject: [PATCH 386/471] craft-audio: share non-seeking canonical WAV streaming primitives (#1962) --- ATTRIBUTION.md | 4 ++ crates/audio/src/lib.rs | 4 ++ crates/audio/src/wav_stream.rs | 119 +++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 crates/audio/src/wav_stream.rs diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index ab167207c..8840199b2 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -103,3 +103,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso `crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained. The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate. + +## Non-seeking WAV primitives + +`crates/audio/src/wav_stream.rs` is extracted from AniCraft `crates/audio/src/wav.rs` at `d875a7feb250e6ab06aa28a37fb78a0699e88330` (MIT OR Apache-2.0). Tanish Manku is retained as the primary import author, from original commit `30c121d269a49d9abe54a3834ed4aa0201542535`. Odd-data padding accounting, shared API documentation and regression tests are subsequent Dylan M. Taylor changes. diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index c7382a973..e4a1c1695 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -775,3 +775,7 @@ mod tests { /// DVD and Blu-ray LPCM packet decoding. pub mod lpcm; + +/// Non-seeking canonical WAV streaming primitives. +#[cfg(feature = "pcm-encode")] +pub mod wav_stream; diff --git a/crates/audio/src/wav_stream.rs b/crates/audio/src/wav_stream.rs new file mode 100644 index 000000000..9da2bdcc9 --- /dev/null +++ b/crates/audio/src/wav_stream.rs @@ -0,0 +1,119 @@ +//! Canonical RIFF/WAVE header and interleaved sample blocks for non-seekable sinks. +//! +//! Extracted from AniCraft's WAV writer, preserving its byte-level compatibility. +//! Unlike `FileEncoder`, these primitives do not require `Seek` and leave final lengths, +//! chunk padding and sink error handling to the caller. The caller must supply a complete +//! frame count and append exactly that many frames. For odd-length data, append a pad byte +//! (the header includes that padding in the RIFF size); current AniCraft output is always stereo (even). +//! Non-finite samples become silence, including float output. This deliberately differs +//! from the general encoder's infinity handling. Prefer `FileEncoder` for new seekable sinks. + +/// Sample formats written. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Bits { + /// Signed 16-bit PCM. + Int16, + /// Signed 24-bit PCM. + Int24, + /// IEEE 754 32-bit float. + Float32, +} + +impl Bits { + fn bytes(self) -> u16 { + match self { + Bits::Int16 => 2, + Bits::Int24 => 3, + Bits::Float32 => 4, + } + } +} + +const PCM: u16 = 1; +const FLOAT: u16 = 3; + +/// A WAV header for `frames` frames of `channels` channels. +/// Returns `None` for zero rate/channels or any size/byte-rate field overflow. +pub fn header(rate: u32, channels: u16, bits: Bits, frames: u64) -> Option> { + if rate == 0 || channels == 0 { + return None; + } + let b = bits.bytes(); + let data = u32::try_from(frames.checked_mul(u64::from(b) * u64::from(channels))?).ok()?; + let riff = data.checked_add(36)?.checked_add(data & 1)?; + let block = channels.checked_mul(b)?; + let byte_rate = rate.checked_mul(u32::from(block))?; + let tag = if bits == Bits::Float32 { FLOAT } else { PCM }; + let mut h = Vec::with_capacity(44); + h.extend_from_slice(b"RIFF"); + h.extend_from_slice(&riff.to_le_bytes()); + h.extend_from_slice(b"WAVEfmt "); + h.extend_from_slice(&16u32.to_le_bytes()); + h.extend_from_slice(&tag.to_le_bytes()); + h.extend_from_slice(&channels.to_le_bytes()); + h.extend_from_slice(&rate.to_le_bytes()); + h.extend_from_slice(&byte_rate.to_le_bytes()); + h.extend_from_slice(&block.to_le_bytes()); + h.extend_from_slice(&(b * 8).to_le_bytes()); + h.extend_from_slice(b"data"); + h.extend_from_slice(&data.to_le_bytes()); + Some(h) +} + +/// Appends samples in `bits` format. Integers are rounded and clipped to range; `x · 32768` +/// for 16-bit, so 16-bit sources read by [`crate::decode`] come back bit-identical. +pub fn append(out: &mut Vec, samples: &[f32], bits: Bits) { + out.reserve(samples.len() * usize::from(bits.bytes())); + for &s in samples { + let s = if s.is_finite() { s } else { 0.0 }; + match bits { + Bits::Int16 => out.extend_from_slice(&((s * 32_768.0).round().clamp(-32_768.0, 32_767.0) as i16).to_le_bytes()), + Bits::Int24 => { + let v = (f64::from(s) * 8_388_608.0).round().clamp(-8_388_608.0, 8_388_607.0) as i32; + out.extend_from_slice(v.to_le_bytes().get(..3).unwrap_or(&[0, 0, 0])); + } + Bits::Float32 => out.extend_from_slice(&s.to_le_bytes()), + } + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::indexing_slicing)] +mod tests { + use super::*; + + #[test] + fn canonical_header_and_odd_padding() { + let h = header(48_000, 2, Bits::Int16, 1).unwrap(); + assert_eq!(h.len(), 44); + assert_eq!(&h[..12], b"RIFF\x28\0\0\0WAVE"); + assert_eq!(&h[28..36], &[0, 238, 2, 0, 4, 0, 16, 0]); + let h = header(8_000, 1, Bits::Int24, 1).unwrap(); + assert_eq!(&h[4..8], &40u32.to_le_bytes()); + assert_eq!(&h[40..44], &3u32.to_le_bytes()); + assert!(header(0, 2, Bits::Int16, 1).is_none()); + assert!(header(48_000, 0, Bits::Int16, 1).is_none()); + assert!(header(u32::MAX, 2, Bits::Int16, 1).is_none()); + assert!(header(48_000, 2, Bits::Float32, u64::MAX).is_none()); + assert!(header(48_000, u16::MAX, Bits::Float32, 0).is_none()); + } + + #[test] + fn samples_preserve_rounding_clipping_silence_and_chunk_boundaries() { + let samples = [-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY]; + let mut pcm = Vec::new(); + append(&mut pcm, &samples, Bits::Int16); + let expected = [-32768i16, -32768, -16384, 0, 16384, 32767, 32767, 0, 0, 0]; + assert_eq!(pcm, expected.iter().flat_map(|s| s.to_le_bytes()).collect::>()); + for bits in [Bits::Int16, Bits::Int24, Bits::Float32] { + let mut whole = Vec::new(); + append(&mut whole, &samples, bits); + let mut blocks = Vec::new(); + for block in samples.chunks(3) { + append(&mut blocks, block, bits); + } + assert_eq!(whole, blocks); + assert_eq!(&whole[whole.len() - 3 * usize::from(bits.bytes())..], vec![0; 3 * usize::from(bits.bytes())]); + } + } +} From 8479a1f3bbabb0e0b25194c508ab2eb14aa5490f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 20:30:05 -0500 Subject: [PATCH 387/471] pkcs7: CMS signing with caller-owned key providers (#1963) --- crates/pkcs7/src/cms.rs | 116 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 10 deletions(-) diff --git a/crates/pkcs7/src/cms.rs b/crates/pkcs7/src/cms.rs index 083aa4302..2bccf5099 100644 --- a/crates/pkcs7/src/cms.rs +++ b/crates/pkcs7/src/cms.rs @@ -449,25 +449,55 @@ struct SignedDataInput<'a> { content_digest: &'a [u8], } -fn build(input: SignedDataInput<'_>) -> Result, Error> { - let SignedDataInput { key, cert, chain, alg, content_type, content, content_digest } = input; - let cert_hash = DigestAlg::Sha256.digest(&[&cert.raw]); - let general_names = der::seq(&[&der::explicit(4, &cert.issuer.raw)]); +/// Public certificate material used by the CMS writer. `serial` is the original INTEGER +/// contents, including its sign padding; `issuer_der` and `der` are complete DER encodings. +/// Callers obtain these fields from an already parsed certificate. No private key is held. +#[derive(Clone, Copy, Debug)] +pub struct SigningCertificate<'a> { + pub der: &'a [u8], + pub issuer_der: &'a [u8], + pub serial: &'a [u8], +} + +/// Inputs for a CAdES SignedData envelope. The caller computes `content_digest` using `alg`. +/// `content` is absent for detached signatures, or the bytes to encapsulate for a token. +/// Certificate-chain entries are complete DER certificates; the signer is deduplicated. +#[derive(Clone, Copy, Debug)] +pub struct SigningInput<'a> { + pub cert: SigningCertificate<'a>, + pub chain: &'a [&'a [u8]], + pub alg: DigestAlg, + pub content_type: &'a str, + pub content: Option<&'a [u8]>, + pub content_digest: &'a [u8], + /// Complete DER AlgorithmIdentifier matching the callback's signing operation. + pub signature_algorithm: &'a [u8], +} + +/// Build the same signed attributes and envelope as [`sign_detached`] / [`sign_encapsulated`], +/// signing through a caller-owned key provider. The callback receives the exact DER SET +/// of signed attributes once (not its digest). It must sign those bytes using `alg` and the +/// supplied signature AlgorithmIdentifier. Its error is returned unchanged, without retry. +/// This lets apps retain platform/keychain/token signing without exporting a private key. +pub fn sign_with(input: SigningInput<'_>, sign: impl FnOnce(&[u8]) -> Result, E>) -> Result, E> { + let SigningInput { cert, chain, alg, content_type, content, content_digest, signature_algorithm } = input; + let cert_hash = DigestAlg::Sha256.digest(&[cert.der]); + let general_names = der::seq(&[&der::explicit(4, cert.issuer_der)]); // The serial as the certificate has it: `cert.serial` is the INTEGER's own bytes, so one with // its high bit set gets no leading zero here. - let serial = der::tlv(tag::INTEGER, if cert.serial.is_empty() { &[0] } else { &cert.serial }); + let serial = der::tlv(tag::INTEGER, if cert.serial.is_empty() { &[0] } else { cert.serial }); let ess = der::seq(&[&der::seq(&[&der::seq(&[&der::octets(&cert_hash), &der::seq(&[&general_names, &serial])])])]); let attrs = [attribute(CONTENT_TYPE, &der::oid(content_type)), attribute(MESSAGE_DIGEST, &der::octets(content_digest)), attribute(SIGNING_CERT_V2, &ess)]; let refs: Vec<&[u8]> = attrs.iter().map(Vec::as_slice).collect(); let set = der::set_of(&refs); - let signature = key.sign(alg, &set)?; + let signature = sign(&set)?; let mut signed_attrs = set.clone(); signed_attrs[0] = tag::ctx(0); - let sid = der::seq(&[&cert.issuer.raw, &serial]); - let signer = der::seq(&[&der::int(1), &sid, &alg.algorithm(), &signed_attrs, &key.signature_algorithm(alg), &der::octets(&signature)]); - let mut certs: Vec<&[u8]> = vec![&cert.raw]; - certs.extend(chain.iter().filter(|c| c.raw != cert.raw).map(|c| c.raw.as_slice())); + let sid = der::seq(&[cert.issuer_der, &serial]); + let signer = der::seq(&[&der::int(1), &sid, &alg.algorithm(), &signed_attrs, signature_algorithm, &der::octets(&signature)]); + let mut certs: Vec<&[u8]> = vec![cert.der]; + certs.extend(chain.iter().copied().filter(|c| *c != cert.der)); let encap = match content { Some(c) => der::seq(&[&der::oid(content_type), &der::explicit(0, &der::octets(c))]), None => der::seq(&[&der::oid(content_type)]), @@ -477,10 +507,76 @@ fn build(input: SignedDataInput<'_>) -> Result, Error> { Ok(der::seq(&[&der::oid(SIGNED_DATA), &der::explicit(0, &signed_data)])) } +fn build(input: SignedDataInput<'_>) -> Result, Error> { + let SignedDataInput { key, cert, chain, alg, content_type, content, content_digest } = input; + let chain: Vec<&[u8]> = chain.iter().map(|c| c.raw.as_slice()).collect(); + sign_with( + SigningInput { + cert: SigningCertificate { der: &cert.raw, issuer_der: &cert.issuer.raw, serial: &cert.serial }, + chain: &chain, + alg, + content_type, + content, + content_digest, + signature_algorithm: &key.signature_algorithm(alg), + }, + |attrs| key.sign(alg, attrs), + ) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn callback_signer_matches_builtin_and_returns_provider_error_once() -> Result<(), Box> { + use crate::x509::Name; + let key = PrivateKey::generate_p256()?; + let name = Name::build("Callback signer", "", "", "", ""); + let from = Time { year: 2026, month: 1, day: 1, hour: 0, minute: 0, second: 0 }; + let cert = Certificate::self_signed(&name, &key, from, 1, &[1])?; + let alg = DigestAlg::Sha256; + let bytes = b"content"; + let digest = alg.digest(&[bytes]); + let algorithm = key.signature_algorithm(alg); + let chain = [cert.raw.as_slice()]; + for content in [None, Some(bytes.as_slice())] { + let input = SigningInput { + cert: SigningCertificate { der: &cert.raw, issuer_der: &cert.issuer.raw, serial: &cert.serial }, + chain: &chain, + alg, + content_type: DATA, + content, + content_digest: &digest, + signature_algorithm: &algorithm, + }; + let mut calls = 0; + let via_callback = sign_with(input, |attrs| { + calls += 1; + assert_eq!(Tlv::parse_all(attrs)?.tag, tag::SET); + key.sign(alg, attrs) + })?; + assert_eq!(calls, 1); + let builtin = match content { + Some(c) => sign_encapsulated(&key, &cert, std::slice::from_ref(&cert), alg, DATA, c)?, + None => sign_detached(&key, &cert, std::slice::from_ref(&cert), alg, &digest)?, + }; + assert_eq!(via_callback, builtin); + let parsed = SignedData::parse(&via_callback)?; + assert_eq!(parsed.certificates.len(), 1); + assert!(parsed.verify_signature(&cert, &digest)?); + assert_eq!(parsed.content.as_deref(), content); + let mut calls = 0; + let failed = sign_with(input, |_| { + calls += 1; + Err(42u32) + }); + assert_eq!(failed, Err(42)); + assert_eq!(calls, 1); + } + Ok(()) + } + #[test] fn strip_removes_multiple_leading_zero_bytes() { assert_eq!(strip(&[0, 0, 0, 0x42]), &[0x42]); From 472462df9f1cb7316a92d1be21cdbf90e0980300 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 20:36:13 -0500 Subject: [PATCH 388/471] craft-hdr: direct RGBA decoding and preallocation axis limits (#1961) --- crates/hdr/src/lib.rs | 40 ++++++++++++++++++++++++++++++++++------ crates/hdr/src/tests.rs | 21 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/crates/hdr/src/lib.rs b/crates/hdr/src/lib.rs index d46232306..731737ce1 100644 --- a/crates/hdr/src/lib.rs +++ b/crates/hdr/src/lib.rs @@ -43,16 +43,19 @@ pub type Result = std::result::Result; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[non_exhaustive] pub struct Limits { - /// Most pixels (width × height) a picture may have; each takes 12 bytes decoded. Default 2^26. + /// Most pixels (width × height) a picture may have; each takes 12 bytes as RGB or 16 as RGBA. Default 2^26. /// A picture over the target's largest allocation is refused at any setting. pub max_pixels: u64, /// Longest header read, in bytes (real ones are a few hundred). Default 64 KiB. pub max_header: usize, + /// Largest width or height, checked before allocating the validation scan line. + /// Defaults to `u32::MAX`, preserving the existing RGB decoder limit. + pub max_axis: u32, } impl Default for Limits { fn default() -> Self { - Self { max_pixels: 1 << 26, max_header: 64 << 10 } + Self { max_pixels: 1 << 26, max_header: 64 << 10, max_axis: u32::MAX } } } @@ -65,6 +68,15 @@ pub struct Picture { pub rgb: Vec, } +/// A decoded opaque RGBA picture, without an intermediate RGB allocation. +#[derive(Debug, Clone, PartialEq)] +pub struct RgbaPicture { + pub width: u32, + pub height: u32, + /// Scene-linear RGBA, top row first. Every alpha sample is 1.0. + pub rgba: Vec, +} + /// The largest allocation in bytes that a `Vec` or `String` can make on the target. const MAX_ALLOC_BYTES: u64 = isize::MAX as u64; @@ -187,6 +199,19 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result { /// [`decode`] with `max_alloc_bytes` as the largest allocation the target allows. fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result { + let (width, height, rgb) = decode_channels::<3>(b, limits, max_alloc_bytes)?; + Ok(Picture { width, height, rgb }) +} + +/// Decodes directly to opaque linear float RGBA, with no intermediate RGB image. +/// The allocation limit includes all four channels; scan lines are validated first. +pub fn decode_rgba(b: &[u8], limits: &Limits) -> Result { + let (width, height, rgba) = decode_channels::<4>(b, limits, MAX_ALLOC_BYTES)?; + Ok(RgbaPicture { width, height, rgba }) +} + +// Only the RGB and RGBA entry points call this helper. +fn decode_channels(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result<(u32, u32, Vec)> { if !sniff(b) { return Err(Error::NotHdr); } @@ -235,13 +260,16 @@ fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result limits.max_axis || height > limits.max_axis { + return Err(Error::LimitExceeded("axis length")); + } let pixels = u64::from(width) * u64::from(height); if pixels > limits.max_pixels { return Err(Error::LimitExceeded("pixel count")); } // One scan line of RGBE pixels and the float picture, each within the target's largest allocation. let scan_len = alloc_len(u64::from(if x1 { height } else { width }), size_of::<[u8; 4]>(), max_alloc_bytes); - let rgb_len = pixels.checked_mul(3).and_then(|floats| alloc_len(floats, size_of::(), max_alloc_bytes)); + let rgb_len = pixels.checked_mul(CHANNELS as u64).and_then(|floats| alloc_len(floats, size_of::(), max_alloc_bytes)); let (Some(scan_len), Some(total)) = (scan_len, rgb_len) else { return Err(Error::LimitExceeded("pixel count")) }; // A scan line takes at least a few bits of file, so a header cannot ask for more than the data has. if (b.len().saturating_sub(pos) as u64).saturating_mul(4096) < pixels { @@ -255,7 +283,7 @@ fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result Result(bytes, &Limits::default(), 28).is_ok()); + assert_eq!(decode_channels::<4>(bytes, &Limits::default(), 28), Err(Error::LimitExceeded("pixel count"))); +} + +#[test] +fn axis_limit_precedes_pixel_data_and_scratch_allocation() { + let limits = Limits { max_axis: 32768, ..Limits::default() }; + for resolution in ["-Y 1 +X 32769", "-Y 32769 +X 1", "+X 1 -Y 32769"] { + let bytes = format!("#?RADIANCE\n\n{resolution}\n"); + assert_eq!(decode_rgba(bytes.as_bytes(), &limits), Err(Error::LimitExceeded("axis length"))); + assert_eq!(decode(bytes.as_bytes(), &limits), Err(Error::LimitExceeded("axis length"))); + } +} From 006ed3d965d5f3b77829b87a43eacd5ca0b4f997 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 20:46:05 -0500 Subject: [PATCH 389/471] svg: share bounded simple stylesheet parsing and matching (#1968) --- crates/svg/README.md | 15 ++ crates/svg/src/lib.rs | 3 + crates/svg/src/simple_css.rs | 346 +++++++++++++++++++++++++++++++++++ 3 files changed, 364 insertions(+) create mode 100644 crates/svg/README.md create mode 100644 crates/svg/src/simple_css.rs diff --git a/crates/svg/README.md b/crates/svg/README.md new file mode 100644 index 000000000..a387e58c2 --- /dev/null +++ b/crates/svg/README.md @@ -0,0 +1,15 @@ + +### Simple stylesheet compatibility subset + +`simple_css` preserves EffectCraft's compound tag/class/id stylesheet grammar and indexed +matching budget, rather than substituting the richer selector semantics of `paths`. +Property values remain text; color precision, path-prefix behavior and geometry policy +stay with the importer. Call `parse` for each sheet, then `StyleSheet::index`; discard the +sheet after a parse error and reject the import when its matching-work callback refuses. + +The parser/matcher and structural tests were ported from EffectCraft +`60e2cbbbe3d73c0a9074c25a1a615ccfec18721c`, originally by FredApps. Nested at-rule skipping +and its regression test preserve NetterSilver's +`10e173c0e49696ad335e3da8a069889c089b09a6`; both contributors retain primary Git authorship +for their respective port commits. Source and destination use MIT OR Apache-2.0. +The shared adapter adds an aggregate 64 MiB stylesheet-text limit and idempotent indexing. diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index 7cd36692e..286bd36d5 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -30,3 +30,6 @@ pub mod svgz; #[cfg(feature = "tree")] pub mod tree; pub mod write; + +/// Compatibility CSS subset for simple SVG importers. +pub mod simple_css; diff --git a/crates/svg/src/simple_css.rs b/crates/svg/src/simple_css.rs new file mode 100644 index 000000000..767b25257 --- /dev/null +++ b/crates/svg/src/simple_css.rs @@ -0,0 +1,346 @@ +//! Compatibility stylesheet subset for simple SVG importers, extracted from EffectCraft. +//! +//! Supports compound tag/class/id selectors and comma lists. Unsupported selectors/at-rules +//! are skipped; malformed trailing rules keep the accepted prefix. This deliberately is not +//! the richer `paths` cascade or a full CSS parser. Property values are retained as text, so +//! caller colour precision and geometry policy are unaffected. `!important` is stripped as in +//! the source importer; it does not change precedence here. +//! +//! Call `parse` for each style element, then `StyleSheet::index` before matching. Discard a +//! sheet after a parse error. Matching charges the caller's aggregate budget before applying +//! any declarations. The caller must stop using results when its budget callback refuses. +use std::collections::HashMap; + +/// Aggregate stylesheet byte bound before copying or allocating declaration strings. +pub const MAX_CSS_BYTES: usize = 64 * 1024 * 1024; + +/// A stylesheet exceeds its byte, selector or declaration limit. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Error(pub String); +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} +impl std::error::Error for Error {} + +/// The most selectors (each entry of a comma list counts) the `

x

"), &|_| None, true); } } + +/// Ensure container elements (such as `div` and `p`) with background colors do not leak +/// character shading onto their child text runs (#1786). +#[test] +fn container_background_color_does_not_leak_into_character_shading() { + let html = r#"
code
"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert!(!p.runs.is_empty(), "expected nonempty runs"); + assert_eq!(&p.text, "code"); + assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x1e, 0x1e, 0x1e)), "container div background is preserved as paragraph shading"); + for r in &p.runs { + assert_eq!(r.props.shading, None, "character should have no shading from container div"); + } +} + +/// Verify that table background colors cascade down to cell shading through implicit or explicit +/// row groups (`tbody`, `thead`, `tfoot`), but do not leak into cell character shading. +#[test] +fn table_background_inherits_through_row_groups() { + let html = r#"
y
cell1
+
cell2
+
head
"#; + let doc = import(html); + let tables: Vec<_> = doc + .body + .iter() + .filter_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .collect(); + assert_eq!(tables.len(), 3); + for (t, want_hex) in tables.iter().zip(["e8f0fa", "d0e0f0", "c0d0e0"]) { + let cell = &t.rows[0].cells[0]; + assert_eq!( + cell.props.shading.map(|c| c.hex().to_ascii_lowercase()), + Some(want_hex.to_string()), + "cell should inherit shading from table or row group" + ); + let p = &cell.blocks[0]; + let craft_wordmodel::Block::Para(p) = &**p else { + panic!("expected table cell block to be a paragraph"); + }; + assert!(!p.runs.is_empty(), "expected nonempty runs"); + for r in &p.runs { + assert_eq!(r.props.shading, None, "cell text should have no character shading"); + } + } +} + +/// Verify that block-level container backgrounds (`p`, `pre`, `div`) are preserved as paragraph +/// shading on paragraphs rather than character shading on individual runs (#1786). +#[test] +fn block_element_backgrounds_preserve_paragraph_shading() { + let html = r#"

text

code

inner

"#; + let doc = import(html); + assert_eq!(doc.body.len(), 3); + let craft_wordmodel::Block::Para(p1) = &*doc.body[0] else { panic!("expected Para") }; + let craft_wordmodel::Block::Para(p2) = &*doc.body[1] else { panic!("expected Para") }; + let craft_wordmodel::Block::Para(p3) = &*doc.body[2] else { panic!("expected Para") }; + assert_eq!(p1.props.shading, Some(craft_wordmodel::Rgb(0x1e, 0x1e, 0x1e))); + assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x2a, 0x2a, 0x2a))); + assert_eq!(p3.props.shading, Some(craft_wordmodel::Rgb(0x3a, 0x3a, 0x3a))); + assert_eq!(&p1.text, "text"); + assert_eq!(&p2.text, "code"); + assert_eq!(&p3.text, "inner"); + assert!(!p1.runs.is_empty()); + assert!(!p2.runs.is_empty()); + assert!(!p3.runs.is_empty()); + for r in &p1.runs { + assert_eq!(r.props.shading, None, "character should have no shading from paragraph background"); + } + for r in &p2.runs { + assert_eq!(r.props.shading, None, "character should have no shading from pre background"); + } + for r in &p3.runs { + assert_eq!(r.props.shading, None, "character should have no shading from container div background"); + } +} + +/// Verify that paragraph shading created by a container background survives an HTML round trip (#1786). +#[test] +fn paragraph_shading_survives_an_html_round_trip() { + let html = r#"

code

pre
"#; + let doc = import(html); + let out = export(&doc); + assert!(out.contains("background-color:#1E1E1E"), "paragraph shading is exported: {out}"); + assert!(out.contains("background-color:#2A2A2A"), "code block shading is exported: {out}"); + + let back = import(&out); + let craft_wordmodel::Block::Para(p1) = &*back.body[0] else { panic!("expected Para") }; + let craft_wordmodel::Block::Para(p2) = &*back.body[1] else { panic!("expected Para") }; + assert_eq!(p1.props.shading, Some(craft_wordmodel::Rgb(0x1e, 0x1e, 0x1e))); + assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x2a, 0x2a, 0x2a))); +} + +/// Verify that paragraph shading inside a single-paragraph table cell survives an HTML round trip (#1786). +#[test] +fn table_cell_paragraph_shading_survives_an_html_round_trip() { + let html = r#"

text

"#; + let doc = import(html); + let out = export(&doc); + assert!(out.contains("background-color:#123456"), "paragraph shading is exported: {out}"); + + let back = import(&out); + let table = back + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .expect("expected table"); + let cell = &table.rows[0].cells[0]; + assert_eq!(cell.props.shading, None, "paragraph shading must not become cell shading"); + let craft_wordmodel::Block::Para(p) = &*cell.blocks[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); +} + +/// Verify that consecutive code paragraphs with different backgrounds export their own `
`.
+#[test]
+fn consecutive_code_blocks_keep_their_own_shading() {
+    let html = r#"
a
b
"#; + let out = export(&import(html)); + assert!(out.contains("
a
"), "{out}"); + assert!(out.contains("
b
"), "{out}"); +} + +/// Verify that paragraph shading inherited from a paragraph style is exported (#1786). +#[test] +fn paragraph_shading_from_a_style_survives_html_export() { + let mut doc = Document::new(); + std::sync::Arc::make_mut(&mut doc.styles).styles.push(craft_wordmodel::Style { + id: "Shaded".into(), + name: "Shaded".into(), + para: craft_wordmodel::props::ParaProps { shading: Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)), ..Default::default() }, + ..Default::default() + }); + let mut p = craft_wordmodel::Paragraph::with_text("text", craft_wordmodel::CharProps::default()); + p.props.style = Some("Shaded".into()); + doc.body = vec![craft_wordmodel::para_block(p)]; + + let out = export(&doc); + assert!(out.contains("background-color:#123456"), "style shading is exported: {out}"); +} + +/// A closing parenthesis without an opening one invalidates the declaration (#1786). +#[test] +fn background_shorthand_unbalanced_closing_paren_is_ignored() { + let html = r#"text"#; + let doc = import(html); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0xff, 0x00, 0x00)), "invalid shorthand must not clear inherited shading"); +} + +/// A shaded paragraph keeps its shading on both sides of an internal page break (#1786). +#[test] +fn paragraph_shading_survives_an_internal_page_break() { + let mut p = craft_wordmodel::Paragraph::with_text("before\u{000C}after", craft_wordmodel::CharProps::default()); + p.props.shading = Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)); + let mut doc = Document::new(); + doc.body = vec![craft_wordmodel::para_block(p)]; + let out = export(&doc); + assert_eq!(out.matches("background-color:#123456").count(), 2, "{out}"); +} + +/// Verify that CSS-escaped quote characters in background shorthand URLs remain part of the URL +/// content and do not cause the shorthand declaration to be rejected. +#[test] +fn background_shorthand_escaped_quotes_in_url() { + let doc = import(r#"

text

"#); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(&p.text, "text"); + assert!(!p.runs.is_empty()); + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); +} + +/// Verify that valid multi-token CSS background shorthands without a color (such as `none repeat`) +/// reset character shading, while invalid declarations are ignored. +#[test] +fn background_shorthand_without_color_resets_background() { + let html = r#"plain"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, None); + + let html_invalid = r#"shaded"#; + let doc_invalid = import(html_invalid); + let p_invalid = match &*doc_invalid.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_invalid.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); +} + +/// Verify that CSS `background: inherit` keeps inherited character shading instead of removing it (#1786). +#[test] +fn background_inherit_retains_inline_shading() { + let html = r#"shaded"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)), "background: inherit should retain inherited character shading"); + + // Also verify background-color: inherit retains shading. + let html_color = r#"shaded"#; + let doc_color = import(html_color); + let p_color = match &*doc_color.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!( + p_color.runs[0].props.shading, + Some(craft_wordmodel::Rgb(255, 0, 0)), + "background-color: inherit should retain inherited character shading" + ); + + // CSS-wide keyword inherit mixed with other values is invalid and ignored. + let html_mixed = r#"green"#; + let doc_mixed = import(html_mixed); + let p_mixed = match &*doc_mixed.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!( + p_mixed.runs[0].props.shading, + Some(craft_wordmodel::Rgb(0, 255, 0)), + "invalid shorthand mixing inherit with values should be ignored" + ); +} + +/// Verify that hostile background declarations with deeply nested slashes do not cause stack overflow (#1786). +#[test] +fn hostile_background_slashes_never_stack_overflow() { + let slashes = "/".repeat(20_000); + let html = format!(r#"hostile"#); + let doc = import(&html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, None); +} + +/// Verify that non-breaking space (U+00A0) is not treated as CSS whitespace when splitting tokens (#1786). +#[test] +fn background_whitespace_preserves_nbsp() { + let html = "shaded"; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!( + p.runs[0].props.shading, + Some(craft_wordmodel::Rgb(255, 0, 0)), + "non-breaking space is not CSS whitespace and should not clear shading" + ); +} + +/// Verify that background shorthands exceeding the token limit are rejected and unapplied (#1786). +#[test] +fn background_shorthand_truncated_rejects_unapplied() { + let mut layers = String::new(); + for i in 0..64 { + layers.push_str(&format!("url(img{i}.png), ")); + } + layers.push_str("#00ff00"); + let html = format!(r#"retained"#); + let doc = import(&html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!( + p.runs[0].props.shading, + Some(craft_wordmodel::Rgb(255, 0, 0)), + "truncated shorthand should be rejected and leave inherited background untouched" + ); +} + +/// Verify that shorthands with image-set or calc functions parse and preserve colors (#1786). +#[test] +fn background_shorthand_valid_functions_preserve_color() { + let html_image_set = r#"image-set"#; + let doc = import(html_image_set); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); + + let html_calc = r#"calc"#; + let doc_calc = import(html_calc); + let p_calc = match &*doc_calc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_calc.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); +} + +/// Verify that multi-layer background shorthands validate slashes per layer and preserve final-layer color (#1786). +#[test] +fn background_shorthand_multi_layer_slashes_apply_color() { + let html = r#"multi-layer"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); + + // Color in a non-final layer is invalid CSS and must be ignored. + let html_invalid_color = r#"invalid"#; + let doc_invalid_color = import(html_invalid_color); + let p_invalid_color = match &*doc_invalid_color.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_invalid_color.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + // Multiple slashes in a single layer makes the declaration invalid. + let html_multi_slash = + r#"invalid"#; + let doc_multi_slash = import(html_multi_slash); + let p_multi_slash = match &*doc_multi_slash.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_multi_slash.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); +} + +/// Verify that background shorthands with empty layers (leading, trailing, or double commas) are rejected (#1786). +#[test] +fn background_shorthand_rejects_empty_layers() { + // Leading comma + let html_leading = r#"leading"#; + let doc_leading = import(html_leading); + let p_leading = match &*doc_leading.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_leading.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + // Trailing comma + let html_trailing = r#"trailing"#; + let doc_trailing = import(html_trailing); + let p_trailing = match &*doc_trailing.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_trailing.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + // Double comma + let html_double = r#"double"#; + let doc_double = import(html_double); + let p_double = match &*doc_double.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_double.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); +} + +/// Verify that zero-alpha colors in background shorthands or declarations remain transparent (#1786). +#[test] +fn background_zero_alpha_colors_are_transparent() { + for decl in ["rgb(0 0 0 / 0)", "rgba(0, 0, 0, 0)", "#0000", "#00000000", "#ffffff00", "hsla(120, 100%, 50%, 0)", "hsl(120 100% 50% / 0)"] { + let html = format!(r#"transparent"#); + let doc = import(&html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, None, "zero-alpha color `{decl}` must clear background rather than composite over white"); + } +} + +/// Verify that ancestor table shading is preserved beneath transparent table layers (#1786). +#[test] +fn transparent_table_layers_preserve_ancestor_table_shading() { + let html = r#" + + + + + + +
transparent_layersexplicit_cell
"#; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!( + table.rows[0].cells[0].props.shading, + Some(craft_wordmodel::Rgb(0xe8, 0xf0, 0xfa)), + "table background must show through transparent tbody, tr, and td" + ); + assert_eq!( + table.rows[0].cells[1].props.shading, + Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)), + "cell with explicit background must override table background" + ); +} + +/// Verify that block-level container background is available to immediate inline children using explicit `inherit` (#1786). +#[test] +fn block_background_explicit_inherit() { + // When the immediate parent

has no background, an inline span inheriting background gets transparent (None): + let html_indirect = r#"

text

"#; + let doc_indirect = import(html_indirect); + let p_indirect = match &*doc_indirect.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_indirect.runs[0].props.shading, None); + + // When the span is an immediate child of the styled div, it inherits the div's computed background: + let html_direct = r#"
text
"#; + let doc_direct = import(html_direct); + let p_direct = match &*doc_direct.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_direct.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + let html_shorthand = r#"
text
"#; + let doc_shorthand = import(html_shorthand); + let p_shorthand = match &*doc_shorthand.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_shorthand.runs[0].props.shading, Some(craft_wordmodel::Rgb(0, 255, 0))); +} + +/// Verify that CSS-escaped quotes inside background URLs do not close the quote or drop declaration colors (#1786). +#[test] +fn background_escaped_quote_url() { + let html = r#"text"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); + + let html_single = r#"text"#; + let doc_single = import(html_single); + let p_single = match &*doc_single.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_single.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x65, 0x43, 0x21))); +} + +/// Verify that invalid hex color declarations like `#ggg0` do not clear inherited shading (#1786). +#[test] +fn background_invalid_hex_retains_shading() { + let html = r#"text"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); +} + +/// Verify that background size keywords (`cover`, `contain`) without preceding position and slash are invalid and retain shading (#1786). +#[test] +fn background_cover_without_slash_invalid() { + let html_cover = r#"text"#; + let doc_cover = import(html_cover); + let p_cover = match &*doc_cover.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_cover.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + let html_contain = r#"text"#; + let doc_contain = import(html_contain); + let p_contain = match &*doc_contain.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_contain.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + let html_valid = r#"text"#; + let doc_valid = import(html_valid); + let p_valid = match &*doc_valid.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p_valid.runs[0].props.shading, None); +} + +/// Verify that table backgrounds declared on thead or tbody are applied to cells (#1786). +#[test] +fn table_row_group_background_inheritance() { + let html = r#" + + + + + + +
head
body
"#; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0, 255, 0))); + assert_eq!(table.rows[1].cells[0].props.shading, Some(craft_wordmodel::Rgb(0, 0, 255))); +} + +/// Verify that background shorthand with slashes inside URL functions correctly identifies the position/size slash (#1786). +#[test] +fn background_url_with_slashes_and_position_size() { + let html = r#"text"#; + let doc = import(html); + let p = match &*doc.body[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); +} + +/// Verify that a table inside an inline element with background shading does not turn character shading into table/cell background (#1786). +#[test] +fn table_inside_inline_element_does_not_inherit_character_shading() { + let html = r#"
cell
"#; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(table.rows[0].cells[0].props.shading, None); +} + +/// Verify that a child with `background: inherit` inside an unstyled cell in a shaded table does not inherit the table fallback (#1786). +#[test] +fn cell_inherit_uses_cell_background_not_table_fallback() { + let html = r#"
text
"#; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + let p = match &*table.rows[0].cells[0].blocks[0] { + craft_wordmodel::Block::Para(p) => p, + _ => panic!("expected paragraph"), + }; + assert_eq!(p.runs[0].props.shading, None); +} + +/// Verify that CSS `background: transparent` overrides HTML `bgcolor` presentational attribute (#1786). +#[test] +fn bgcolor_overridden_by_css_transparent() { + let html = r##"
cell
"##; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(table.rows[0].cells[0].props.shading, None); +} + +/// Verify that nested container shading is preserved when merging list continuation paragraphs (#1786). +#[test] +fn list_continuation_preserves_nested_container_shading() { + let html = r#"
  • before
    inside
    after
"#; + let flow = parse(html); + assert_eq!(flow.blocks.len(), 1); + let FBlock::Para(p) = &flow.blocks[0] else { panic!("expected Para") }; + assert!(p.list.is_some(), "expected list item"); + let dark = Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)); + let inside = p + .inlines + .iter() + .find_map(|i| match i { + Inline::Text(t, fmt) if t.contains("inside") => Some(fmt.background), + _ => None, + }) + .expect("inside text inline"); + assert_eq!(inside, dark, "nested container shading should be preserved on merged run"); + + let doc = import(html); + let craft_wordmodel::Block::Para(p_doc) = &*doc.body[0] else { panic!("expected Para") }; + let mut offset = 0usize; + let mut inside_shading = None; + for r in &p_doc.runs { + let text_slice = &p_doc.text[offset..offset + r.len]; + if text_slice.contains("inside") { + inside_shading = Some(r.props.shading); + } else if text_slice.contains("before") || text_slice.contains("after") { + assert_eq!(r.props.shading, None); + } + offset += r.len; + } + assert_eq!(inside_shading, Some(dark)); +} + +/// Verify that starting a new row group implicitly closes any preceding sibling row group so +/// cell shading is not inherited from an omitted-end-tag preceding group (#1786). +#[test] +fn consecutive_row_groups_with_omitted_end_tag_do_not_leak_shading() { + let html = r#"
a
b
"#; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(table.rows.len(), 2); + assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + assert_eq!(table.rows[1].cells[0].props.shading, None, "second cell should not inherit shading from closed tbody"); +} + +/// Verify that a list item with a shaded container followed by unshaded text preserves separate +/// background regions so the trailing text does not inherit the container's background (#1786). +#[test] +fn shaded_container_first_in_list_item_preserves_unshaded_trailing_text() { + let html = r#"
  • inside
    after
"#; + let doc = import(html); + assert_eq!(doc.body.len(), 1); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, None, "list paragraph as a whole should have no background when regions differ"); + let dark = Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)); + let mut offset = 0usize; + let mut inside_shading = None; + let mut after_shading = None; + for r in &p.runs { + let text_slice = &p.text[offset..offset + r.len]; + if text_slice.contains("inside") { + inside_shading = Some(r.props.shading); + } else if text_slice.contains("after") { + after_shading = Some(r.props.shading); + } + offset += r.len; + } + assert_eq!(inside_shading, Some(dark)); + assert_eq!(after_shading, Some(None), "trailing text should have no shading"); +} + +/// Verify that container backgrounds behind tables are preserved on table cells (#1786). +#[test] +fn container_background_behind_table_preserved_on_cells() { + let html = r#"
text
"#; + let doc = import(html); + let table = doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11))); + + // Nested table inside an unshaded cell stops at the cell boundary: + let nested_html = r#"
nested
"#; + let nested_doc = import(nested_html); + let outer_table = nested_doc + .body + .iter() + .find_map(|b| match &**b { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(outer_table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11))); + let inner_table = match &*outer_table.rows[0].cells[0].blocks[0] { + craft_wordmodel::Block::Table(t) => t, + _ => panic!("expected nested Table"), + }; + assert_eq!(inner_table.rows[0].cells[0].props.shading, None, "nested table in unshaded cell must stop at cell boundary"); +} + +/// Verify that page imports preserve root (body/html) backgrounds while pasted fragments do not (#1786). +#[test] +fn root_background_preserved_on_page_import_only() { + let html = r#"

text

"#; + let page_doc = import_page(html, &|_| None); + let craft_wordmodel::Block::Para(p_page) = &*page_doc.body[0] else { panic!("expected Para") }; + assert_eq!(p_page.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)), "page import must preserve body background on paragraphs"); + assert_eq!( + p_page.runs[0].props.color, + Some(craft_wordmodel::TextColor::Rgb(craft_wordmodel::Rgb(0xff, 0xff, 0xff))), + "page import keeps the root foreground with its background" + ); + + let frag_doc = import(html); + let craft_wordmodel::Block::Para(p_frag) = &*frag_doc.body[0] else { panic!("expected Para") }; + assert_eq!(p_frag.props.shading, None, "fragment paste must not turn body background into paragraph shading"); + assert_eq!(p_frag.runs[0].props.color, None, "fragment paste drops the root foreground with its background"); +} + +/// Verify that an inline child with `background: inherit` does not receive character shading from a pasted body background (#1786). +#[test] +fn pasted_body_background_not_inherited_by_inline_child() { + let html = r#"text"#; + let frag_doc = import(html); + let craft_wordmodel::Block::Para(p_frag) = &*frag_doc.body[0] else { panic!("expected Para") }; + assert_eq!(p_frag.runs[0].props.shading, None, "inline child in pasted fragment must not inherit character shading from body background"); + + let page_doc = import_page(html, &|_| None); + let craft_wordmodel::Block::Para(p_page) = &*page_doc.body[0] else { panic!("expected Para") }; + assert_eq!( + p_page.runs[0].props.shading, + Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)), + "inline child in page import inherits body background via inherit" + ); +} + +#[test] +fn root_background_on_html_and_foreground_on_body() { + let html = r#"

text

"#; + let frag_doc = import(html); + let craft_wordmodel::Block::Para(p_frag) = &*frag_doc.body[0] else { panic!("expected Para") }; + assert_eq!(p_frag.props.shading, None, "fragment paste drops html background"); + assert_eq!(p_frag.runs[0].props.color, None, "fragment paste drops body foreground when html supplied suppressed background"); + + let page_doc = import_page(html, &|_| None); + let craft_wordmodel::Block::Para(p_page) = &*page_doc.body[0] else { panic!("expected Para") }; + assert_eq!(p_page.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11))); + assert_eq!(p_page.runs[0].props.color, Some(craft_wordmodel::TextColor::Rgb(craft_wordmodel::Rgb(0xff, 0xff, 0xff)))); +} + +#[test] +fn background_unitless_zero_lengths() { + let html = r#"

text

"#; + let doc = import(html); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56))); + + let html2 = r#"

text

"#; + let doc2 = import(html2); + let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") }; + assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x65, 0x43, 0x21))); +} + +#[test] +fn background_comments_skipped_outside_strings() { + let html = r#"

text

"#; + let doc = import(html); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11))); + + let html2 = r#"

text

"#; + let doc2 = import(html2); + let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") }; + assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x22, 0x22, 0x22))); +} + +#[test] +fn invalid_color_does_not_clear_preceding_background() { + let html = r#"

text

"#; + let doc = import(html); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); + + let html2 = r#"

text

"#; + let doc2 = import(html2); + let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") }; + assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(255, 0, 0))); +} + +#[test] +fn code_block_export_preserves_foreground_color() { + let html = r#"
const x = 1;
"#; + let doc = import_page(html, &|_| None); + let exported = export(&doc); + assert!(exported.contains("style=\"background-color:#000000\""), "exported pre must keep background: {}", exported); + assert!(exported.contains("color:#FFFFFF"), "exported code inlines must keep foreground: {}", exported); + assert!(exported.contains("const x = 1;"), "exported code text preserved: {}", exported); +} + +#[test] +fn background_comments_inside_functions() { + let html = r#"

text

"#; + let doc = import(html); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11))); + assert_eq!(p.runs[0].props.color, Some(craft_wordmodel::TextColor::Rgb(craft_wordmodel::Rgb(0xff, 0xff, 0xff)))); +} + +#[test] +fn zero_alpha_colors_with_none_channels() { + let html = r#"

text

"#; + let doc = import(html); + let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") }; + assert_eq!(p.props.shading, None); + + let html2 = r#"

text

"#; + let doc2 = import(html2); + let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") }; + assert_eq!(p2.props.shading, None); +} From cfad8f0bcca6900ddd88361220fb5ed432ce817e Mon Sep 17 00:00:00 2001 From: el-j Date: Tue, 17 Nov 2026 03:30:57 +0100 Subject: [PATCH 417/471] craft-captions: PJS, RealText and JACOsub subtitle formats (#1557) (#1788) --- crates/captions/src/jacosub.rs | 242 +++++++++++++++++ crates/captions/src/lib.rs | 50 +++- crates/captions/src/pjs.rs | 102 ++++++++ crates/captions/src/realtext.rs | 247 ++++++++++++++++++ crates/captions/src/text.rs | 46 ++++ crates/captions/tests/it/main.rs | 1 + .../captions/tests/it/pjs_realtext_jacosub.rs | 174 ++++++++++++ 7 files changed, 860 insertions(+), 2 deletions(-) create mode 100644 crates/captions/src/jacosub.rs create mode 100644 crates/captions/src/pjs.rs create mode 100644 crates/captions/src/realtext.rs create mode 100644 crates/captions/tests/it/pjs_realtext_jacosub.rs diff --git a/crates/captions/src/jacosub.rs b/crates/captions/src/jacosub.rs new file mode 100644 index 000000000..30233b6f1 --- /dev/null +++ b/crates/captions/src/jacosub.rs @@ -0,0 +1,242 @@ +//! JACOsub (`.jss`, `.js`): multi-feature scriptable subtitle format. +//! +//! ```text +//! # JACOsub file +//! #TimeRes 100 +//! 0:00:01.00 0:00:03.00 D0 First line\nsecond line +//! ``` +//! +//! Lines beginning with `#` are comments or header directives. Timed caption lines begin with +//! `START STOP [DIRECTIVE] Text`, where timestamps are `H:MM:SS.FF` (or `@units`) relative to +//! the declared `#TimeRes` (default 30). Inside the text, `\n` represents a newline. + +use crate::text::{escape_markup, ms_to_tick, plain_text, tick_to_ms}; +use crate::{Cue, Document, Error, Result}; + +/// Parse a JACOsub timestamp `H:MM:SS.FF` or `@units` into milliseconds. +pub(crate) fn parse_time(s: &str, time_res: i64) -> Option { + let s = s.trim(); + if s.is_empty() { + return None; + } + + if let Some(units_str) = s.strip_prefix('@') { + let units: i64 = units_str.trim().parse().ok()?; + let time_res = if time_res > 0 { time_res } else { 30 }; + return Some(units.saturating_mul(1000) / time_res); + } + + let (h_str, rest) = s.split_once(':')?; + let (m_str, rest) = rest.split_once(':')?; + let (s_str, f_str) = rest.split_once('.')?; + + let h: i64 = h_str.trim().parse().ok()?; + let m: i64 = m_str.trim().parse().ok()?; + let sec: i64 = s_str.trim().parse().ok()?; + let frac: i64 = f_str.trim().parse().ok()?; + + let time_res = if time_res > 0 { time_res } else { 30 }; + let frac_ms = frac.saturating_mul(1000) / time_res; + + let total_secs = h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec); + Some(total_secs.saturating_mul(1000).saturating_add(frac_ms)) +} + +/// Format milliseconds into JACOsub timestamp string `H:MM:SS.cc` (at 100 centiseconds/sec). +pub(crate) fn format_time(ms: i64) -> String { + let ms = ms.max(0); + let total_secs = ms / 1000; + let centis = (ms % 1000) / 10; + let hours = total_secs / 3600; + let mins = (total_secs % 3600) / 60; + let secs = total_secs % 60; + + format!("{hours}:{mins:02}:{secs:02}.{centis:02}") +} + +/// Check if a line is a JACOsub timed caption line: `H:MM:SS.FF H:MM:SS.FF ...` or `@units @units ...`. +pub(crate) fn is_timed_line(line: &str) -> bool { + let line = line.trim(); + if line.starts_with('#') { + return false; + } + let mut parts = line.split_whitespace(); + let Some(t0) = parts.next() else { return false }; + let Some(t1) = parts.next() else { return false }; + (t0.contains(':') && t0.contains('.') && t1.contains(':') && t1.contains('.')) || (t0.starts_with('@') && t1.starts_with('@')) +} + +pub fn parse(text: &str) -> Result { + let mut doc = Document::default(); + let mut time_res: i64 = 30; // standard default + let mut shift_ms: i64 = 0; + let mut found = false; + + for (n, line) in text.lines().enumerate() { + let line = line.trim(); + if line.is_empty() { + continue; + } + + if let Some(cmd) = line.strip_prefix('#') { + let cmd_lower = cmd.to_ascii_lowercase(); + if cmd_lower.starts_with("timeres") || cmd_lower.starts_with('t') { + let parts: Vec<&str> = cmd.split_whitespace().collect(); + if parts.len() >= 2 + && let Ok(res) = parts[1].parse::() + && res > 0 + { + time_res = res; + } + } else if cmd_lower.starts_with("shift") || cmd_lower.starts_with('s') { + let parts: Vec<&str> = cmd.split_whitespace().collect(); + if parts.len() >= 2 { + let s_val = parts[1].trim(); + let (neg, s_raw) = if let Some(stripped) = s_val.strip_prefix('-') { + (true, stripped) + } else if let Some(stripped) = s_val.strip_prefix('+') { + (false, stripped) + } else { + (false, s_val) + }; + if let Some(ms) = parse_time(s_raw, time_res) { + shift_ms = if neg { -ms } else { ms }; + } + } + } + continue; + } + + if !is_timed_line(line) { + continue; + } + + let mut parts = line.split_whitespace(); + let Some(start_str) = parts.next() else { continue }; + let Some(end_str) = parts.next() else { continue }; + + let start_ms = match parse_time(start_str, time_res) { + Some(ms) => ms.saturating_add(shift_ms).max(0), + None => { + doc.warnings.push(format!("line {}: invalid start timestamp, skipped", n + 1)); + continue; + } + }; + let end_ms = match parse_time(end_str, time_res) { + Some(ms) => ms.saturating_add(shift_ms).max(0), + None => { + doc.warnings.push(format!("line {}: invalid end timestamp, skipped", n + 1)); + continue; + } + }; + + found = true; + + // Skip to after the end timestamp in the original line to preserve exact spaces + let after_end_pos = match line.find(end_str) { + Some(idx) => idx + end_str.len(), + None => continue, + }; + let remainder = &line[after_end_pos..]; + let trimmed_rem = remainder.trim_start(); + + // Check if there is a directive prefix: either [Directive] or a single non-space word starting with D + let text_part = if let Some(stripped) = trimmed_rem.strip_prefix('[') { + match stripped.find(']') { + Some(end_bracket) => stripped[end_bracket + 1..].trim_start(), + None => trimmed_rem, + } + } else if let Some(space_idx) = trimmed_rem.find(' ') { + let first_token = &trimmed_rem[..space_idx]; + if first_token.starts_with('D') && first_token[1..].chars().all(|c| c.is_ascii_digit()) { + trimmed_rem[space_idx + 1..].trim_start() + } else { + trimmed_rem + } + } else { + trimmed_rem + }; + + // Text parsing: `\n` represents newline + let raw_lines: Vec<&str> = text_part.split(r"\n").map(str::trim).filter(|l| !l.is_empty()).collect(); + let lines: Vec = raw_lines.into_iter().map(escape_markup).collect(); + + let start = ms_to_tick(start_ms); + let end = ms_to_tick(end_ms); + if end <= start || lines.is_empty() { + doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1)); + continue; + } + + doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() }); + } + + if !found && !text.trim().is_empty() { + return Err(Error::NotFormat("JACOsub")); + } + + Ok(doc) +} + +pub fn write(doc: &Document) -> String { + let mut out = String::new(); + out.push_str("# JACOsub file\n"); + out.push_str("#TimeRes 100\n\n"); + + for c in &doc.cues { + let start_ms = tick_to_ms(c.start); + let end_ms = tick_to_ms(c.end).max(start_ms + 10); + let raw_lines = plain_text(&c.text); + let lines: Vec<&str> = raw_lines.lines().map(str::trim).filter(|l| !l.is_empty()).collect(); + + if !lines.is_empty() { + let text = lines.join(r"\n"); + out.push_str(&format!("{} {} D0 {}\n", format_time(start_ms), format_time(end_ms), text)); + } + } + + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_and_write_roundtrip() { + let jss = "# JACOsub file\n#TimeRes 100\n\n0:00:01.00 0:00:03.00 D0 First line\\nsecond line\n0:00:04.50 0:00:06.00 D0 Second cue\n"; + let doc = parse(jss).expect("parse"); + assert_eq!(doc.cues.len(), 2); + assert_eq!(doc.cues[0].start, ms_to_tick(1000)); + assert_eq!(doc.cues[0].end, ms_to_tick(3000)); + assert_eq!(doc.cues[0].text, "First line\nsecond line"); + assert_eq!(doc.cues[1].start, ms_to_tick(4500)); + assert_eq!(doc.cues[1].end, ms_to_tick(6000)); + assert_eq!(doc.cues[1].text, "Second cue"); + + let written = write(&doc); + assert_eq!(written, jss); + } + + #[test] + fn parse_bracketed_directive() { + let jss = "#TimeRes 100\n0:00:01.00 0:00:03.00 [Style1] Bracketed style text\n"; + let doc = parse(jss).expect("parse"); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].text, "Bracketed style text"); + } + + #[test] + fn negative_shift_under_one_hour() { + let jss = "#TimeRes 100\n#Shift -0:00:01.00\n0:00:02.00 0:00:04.00 D0 Shifted line\n"; + let doc = parse(jss).expect("parse"); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].start, ms_to_tick(1000)); + assert_eq!(doc.cues[0].end, ms_to_tick(3000)); + } + + #[test] + fn rejects_non_jacosub() { + assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err()); + } +} diff --git a/crates/captions/src/lib.rs b/crates/captions/src/lib.rs index e8a492722..ca99dba43 100644 --- a/crates/captions/src/lib.rs +++ b/crates/captions/src/lib.rs @@ -34,7 +34,10 @@ struct ReadmeExamples; pub mod ass; pub mod cea608; +pub mod jacosub; pub mod mcc; +pub mod pjs; +pub mod realtext; pub mod scc; pub mod srt; pub mod stl; @@ -72,10 +75,28 @@ pub enum Format { Dfxp, /// Advanced SubStation Alpha (also reads SubStation Alpha). Ass, + /// Phoenix Japanimation Society (PJS) subtitles. + Pjs, + /// RealText streaming timed text markup. + RealText, + /// JACOsub scriptable subtitles. + JacoSub, } impl Format { - pub const ALL: [Format; 8] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp, Format::Ass]; + pub const ALL: [Format; 11] = [ + Format::Srt, + Format::WebVtt, + Format::Scc, + Format::Mcc, + Format::Stl, + Format::Ttml, + Format::Dfxp, + Format::Ass, + Format::Pjs, + Format::RealText, + Format::JacoSub, + ]; pub fn name(self) -> &'static str { match self { Format::Srt => "SubRip (SRT)", @@ -86,6 +107,9 @@ impl Format { Format::Ttml => "TTML (IMSC1)", Format::Dfxp => "DFXP (TTML1)", Format::Ass => "Advanced SubStation Alpha (ASS)", + Format::Pjs => "PJS", + Format::RealText => "RealText", + Format::JacoSub => "JACOsub", } } pub fn extension(self) -> &'static str { @@ -98,6 +122,9 @@ impl Format { Format::Ttml => "ttml", Format::Dfxp => "dfxp", Format::Ass => "ass", + Format::Pjs => "pjs", + Format::RealText => "rt", + Format::JacoSub => "jss", } } pub fn from_name(s: &str) -> Option { @@ -110,6 +137,9 @@ impl Format { "ttml" | "imsc" | "imsc1" | "xml" => Some(Format::Ttml), "dfxp" => Some(Format::Dfxp), "ass" | "ssa" => Some(Format::Ass), + "pjs" => Some(Format::Pjs), + "rt" | "realtext" => Some(Format::RealText), + "jss" | "js" | "jacosub" => Some(Format::JacoSub), _ => None, } } @@ -260,6 +290,16 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option { if head.starts_with("[Script Info]") { return Some(Format::Ass); } + if realtext::is_realtext(head) { + return Some(Format::RealText); + } + let first_lines: Vec<&str> = head.lines().map(str::trim).filter(|l| !l.is_empty()).take(3).collect(); + if first_lines.first().is_some_and(|l| pjs::split_cue(l).is_some()) { + return Some(Format::Pjs); + } + if first_lines.iter().any(|l| jacosub::is_timed_line(l)) || (head.starts_with('#') && (head.contains("TimeRes") || head.contains("#T"))) { + return Some(Format::JacoSub); + } let ext = ext.map(|e| e.trim_start_matches('.').to_ascii_lowercase()); if (head.starts_with('<') && (head.contains("") || head.contains(":tt "))) && (head.contains("ns/ttml") || head.contains("ttaf1")) @@ -271,7 +311,7 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option { match ext.as_deref() { Some("srt") if has_arrow_timing || head.is_empty() => Some(Format::Srt), Some("vtt") if has_arrow_timing => Some(Format::WebVtt), - Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa") => None, + Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa" | "pjs" | "rt" | "jss" | "js") => None, _ if has_arrow_timing => Some(Format::Srt), _ => None, } @@ -306,6 +346,9 @@ fn parse_unbounded(bytes: &[u8], format: Format) -> Result { Format::Mcc => mcc::parse(&text), Format::Ttml | Format::Dfxp => ttml::parse(&text), Format::Ass => ass::parse(&text), + Format::Pjs => pjs::parse(&text), + Format::RealText => realtext::parse(&text), + Format::JacoSub => jacosub::parse(&text), Format::Stl => stl::parse(bytes), } } @@ -344,6 +387,9 @@ pub fn write(doc: &Document, format: Format, opts: WriteOptions) -> Vec { Format::Ttml => ttml::write(doc, ttml::Flavor::Imsc1, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(), Format::Dfxp => ttml::write(doc, ttml::Flavor::Dfxp, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(), Format::Ass => ass::write(doc).into_bytes(), + Format::Pjs => pjs::write(doc).into_bytes(), + Format::RealText => realtext::write(doc).into_bytes(), + Format::JacoSub => jacosub::write(doc).into_bytes(), } } diff --git a/crates/captions/src/pjs.rs b/crates/captions/src/pjs.rs new file mode 100644 index 000000000..91874941b --- /dev/null +++ b/crates/captions/src/pjs.rs @@ -0,0 +1,102 @@ +//! Phoenix Japanimation Society (PJS) subtitles (`.pjs`): times in tenths of a second. +//! +//! ```text +//! 10,50,"First line" +//! 60,120,"Second line|with break" +//! ``` +//! +//! Lines are comma-separated `start,end,"text"` with times in deciseconds (tenths of a second). +//! Inside the quotes, `|` breaks a line. + +use crate::text::{escape_markup, ms_to_tick, number, plain_lines, tick_to_ms}; +use crate::{Cue, Document, Error, Result}; + +/// `start,end,"text"` as its parts. +pub(crate) fn split_cue(line: &str) -> Option<(i64, i64, &str)> { + let (start_str, rest) = line.split_once(',')?; + let (end_str, text_str) = rest.split_once(',')?; + let start = number(start_str.trim())?; + let end = number(end_str.trim())?; + let text = text_str.trim(); + let text = if let Some(stripped) = text.strip_prefix('"').and_then(|t| t.strip_suffix('"')) { stripped } else { text }; + Some((start, end, text)) +} + +/// Parse a PJS subtitle document. +pub fn parse(text: &str) -> Result { + let mut doc = Document::default(); + let mut found = false; + for (n, line) in text.lines().enumerate() { + let line = line.trim(); + if line.is_empty() { + continue; + } + let Some((start_ds, end_ds, raw)) = split_cue(line) else { + doc.warnings.push(format!("line {}: not a PJS cue, skipped", n + 1)); + continue; + }; + found = true; + let mut lines = Vec::new(); + for l in raw.split('|') { + let trimmed = l.trim(); + if !trimmed.is_empty() { + let unquoted = trimmed.replace("\"\"", "\""); + lines.push(escape_markup(&unquoted)); + } + } + let start = ms_to_tick(start_ds.saturating_mul(100)); + let end = ms_to_tick(end_ds.saturating_mul(100)); + if end <= start || lines.is_empty() { + doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1)); + continue; + } + doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() }); + } + if !found && !text.trim().is_empty() { + return Err(Error::NotFormat("PJS")); + } + Ok(doc) +} + +/// Serialize a document into PJS format. +pub fn write(doc: &Document) -> String { + let mut out = String::new(); + let ds = |t| (tick_to_ms(t) + 50) / 100; + for c in &doc.cues { + let start = ds(c.start); + let end = ds(c.end).max(start + 1); + let lines: Vec = + plain_lines(&c.text).into_iter().map(|l| l.replace('"', "\"\"").replace('|', "|")).filter(|l| !l.is_empty()).collect(); + if !lines.is_empty() { + out.push_str(&format!("{start},{end},\"{}\"\n", lines.join("|"))); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_and_write_roundtrip() { + let pjs = "10,50,\"First line\"\n60,120,\"Second line|with break\"\n130,150,\"Say \"\"Hi\"\"\"\n"; + let doc = parse(pjs).expect("parse"); + assert_eq!(doc.cues.len(), 3); + assert_eq!(doc.cues[0].start, ms_to_tick(1000)); + assert_eq!(doc.cues[0].end, ms_to_tick(5000)); + assert_eq!(doc.cues[0].text, "First line"); + assert_eq!(doc.cues[1].start, ms_to_tick(6000)); + assert_eq!(doc.cues[1].end, ms_to_tick(12000)); + assert_eq!(doc.cues[1].text, "Second line\nwith break"); + assert_eq!(doc.cues[2].text, "Say \"Hi\""); + + let written = write(&doc); + assert_eq!(written, pjs); + } + + #[test] + fn rejects_non_pjs() { + assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err()); + } +} diff --git a/crates/captions/src/realtext.rs b/crates/captions/src/realtext.rs new file mode 100644 index 000000000..1c4328a3b --- /dev/null +++ b/crates/captions/src/realtext.rs @@ -0,0 +1,247 @@ +//! RealText (`.rt`): streaming HTML-like timed text markup. +//! +//! ```text +//! +//! +//! ``` +//! +//! RealText documents encapsulate timed text inside a `` container with `