diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 77d3578cb..c087ecda1 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -95,3 +95,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso | `crates/raw/docs/raf-corpus.sha256` (hashes only; the files are not bundled) | The photographers who contributed each file to raw.pixls.us | [raw.pixls.us](https://raw.pixls.us/) | CC0-1.0 | | `crates/drawingml/src/preset_text.txt` | DeckCraft contributors (Nikita Rakitin); generated by `crates/drawingml/tests/fixtures/make_preset_guides.py` from the guide formulas of ECMA-376 Part 1 `presetShapeDefinitions.xml` | [storytold/deckcraft#650](https://github.com/storytold/deckcraft/pull/650) | MIT OR Apache-2.0 (the formulas are the format's definition in ECMA-376) | | `crates/drawingml/tests/fixtures/preset_table.txt` | DeckCraft contributors (Nikita Rakitin); generated by `crates/drawingml/tests/fixtures/make_preset_table.py` from ECMA-376 Part 1 `presetShapeDefinitions.xml` | [storytold/deckcraft#650](https://github.com/storytold/deckcraft/pull/650) | MIT OR Apache-2.0 (the formulas are the format's definition in ECMA-376) | + +## 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. diff --git a/Cargo.lock b/Cargo.lock index 761575903..f2e172fd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1920,6 +1920,7 @@ dependencies = [ "craft-heif", "craft-jpeg", "craft-metadata", + "craft-video", "crc32fast", "exr", "flate2", @@ -1969,6 +1970,13 @@ dependencies = [ "zune-jpeg", ] +[[package]] +name = "craft-jsonrpc" +version = "0.1.0" +dependencies = [ + "serde_json", +] + [[package]] name = "craft-jxl" version = "0.1.0" @@ -2057,6 +2065,16 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-mcmat" +version = "0.1.0" +dependencies = [ + "craft-zip", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-media" version = "0.1.0" @@ -2244,6 +2262,7 @@ dependencies = [ name = "craft-pcraft-format" version = "0.1.0" dependencies = [ + "flate2", "serde", "serde_json", "thiserror 2.0.21", @@ -2356,6 +2375,13 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-plist" +version = "0.1.0" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-platform" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index d60c3469b..2d8687195 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ craft-proto = { path = "crates/proto" } acadrust = "=0.6.6" log = "0.4" # internal: add each crate here as `craft- = { path = "crates/" }` +craft-plist = { path = "crates/plist" } craft-platform = { path = "crates/platform" } craft-aep = { path = "crates/aep" } craft-affinity = { path = "crates/affinity" } @@ -51,6 +52,7 @@ craft-dialogs = { path = "crates/dialogs" } craft-doc-binary = { path = "crates/doc-binary" } craft-docx = { path = "crates/docx" } craft-document-bundle = { path = "crates/document-bundle" } +craft-mcmat = { path = "crates/mcmat" } craft-designcraft-format = { path = "crates/designcraft-format" } craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } @@ -88,6 +90,7 @@ craft-indd = { path = "crates/indd" } craft-jmap = { path = "crates/jmap" } craft-ical = { path = "crates/ical" } craft-jpeg = { path = "crates/jpeg" } +craft-jsonrpc = { path = "crates/jsonrpc" } craft-jxl = { path = "crates/jxl" } craft-kiwi = { path = "crates/kiwi" } craft-keymap = { path = "crates/keymap" } diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index 404b5a315..c52c145f5 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -531,10 +531,32 @@ pub fn write_cube(l: &LutFile) -> String { s.push_str(&format!("DOMAIN_MAX {} {} {}\n", l.domain_max[0], l.domain_max[1], l.domain_max[2])); } s.push_str(&format!("LUT_3D_SIZE {}\n", l.size)); + write_cube_rows(&mut s, l); + s +} + +/// Writes a `.cube` file with a creator comment, section comments and an explicit input domain. +/// +/// This layout is useful for exported LUTs; [`write_cube`] keeps its compact layout for embedded +/// tables. Values retain six decimal places, and their order and quantisation belong to the +/// caller. A default domain is written as `0.0` and `1.0` for compatibility with export consumers. +pub fn write_cube_commented(l: &LutFile, creator: &str) -> String { + let mut s = format!("#Created by: {creator}\nTITLE \"{}\"\n\n#LUT size\nLUT_3D_SIZE {}\n\n#data domain\n", l.title.replace('"', "'"), l.size); + if l.domain_is_default() { + s.push_str("DOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n"); + } else { + s.push_str(&format!("DOMAIN_MIN {} {} {}\n", l.domain_min[0], l.domain_min[1], l.domain_min[2])); + s.push_str(&format!("DOMAIN_MAX {} {} {}\n", l.domain_max[0], l.domain_max[1], l.domain_max[2])); + } + s.push_str("\n#LUT data points\n"); + write_cube_rows(&mut s, l); + s +} + +fn write_cube_rows(s: &mut String, l: &LutFile) { for c in l.data.as_chunks::<3>().0 { s.push_str(&format!("{:.6} {:.6} {:.6}\n", c[0], c[1], c[2])); } - s } fn luma(c: [f32; 3]) -> f32 { @@ -602,6 +624,30 @@ pub fn builtin(id: &str) -> Option { mod tests { use super::*; + #[test] + fn commented_cube_preserves_export_header_and_rows() { + let mut lut = super::LutFile::identity(2).unwrap(); + lut.title = "A \"look\"".into(); + let text = super::write_cube_commented(&lut, "ExampleCraft"); + assert!(text.starts_with("#Created by: ExampleCraft\nTITLE \"A 'look'\"\n\n#LUT size\nLUT_3D_SIZE 2\n\n#data domain\nDOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n\n#LUT data points\n")); + let parsed = super::parse_cube(&text).unwrap(); + assert_eq!(parsed.data, lut.data); + assert_eq!(parsed.title, "A 'look'"); + assert_eq!(text.lines().count(), 11 + 8); + let rows = |s: &str| s.lines().filter(|line| line.starts_with(|c: char| c.is_ascii_digit())).map(str::to_owned).collect::>(); + assert_eq!(rows(&text), rows(&super::write_cube(&lut))); + } + + #[test] + fn commented_cube_keeps_nondefault_domain_and_sample_precision() { + let mut lut = super::LutFile::identity(2).unwrap(); + lut.domain_min = [0.125, 0.25, 0.375]; + lut.domain_max = [0.625, 0.75, 0.875]; + lut.data[0] = 0.833344; + let parsed = super::parse_cube(&super::write_cube_commented(&lut, "ExampleCraft")).unwrap(); + assert_eq!(parsed, lut); + } + #[test] fn cube_title_hashes_and_comment_quotes_are_distinct() { let rows = "LUT_1D_SIZE 2\n0 0 0 # low\n1 1 1 # high\n"; diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index fde9b27e9..df71d9860 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -49,4 +49,5 @@ image = { version = "0.25", default-features = false, features = ["png", "jpeg", sha2 = { version = "0.10", default-features = false, features = ["std"] } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +craft-video = { workspace = true, features = ["av1-decode"] } jxl-oxide = { version = "=0.12.6", default-features = false, features = ["rayon"] } diff --git a/crates/image-codecs/src/codecs/avif.rs b/crates/image-codecs/src/codecs/avif.rs index d27112fa1..042b5684b 100644 --- a/crates/image-codecs/src/codecs/avif.rs +++ b/crates/image-codecs/src/codecs/avif.rs @@ -25,12 +25,17 @@ fn pq_encode(nits: f32) -> f32 { #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] fn hlg_signal(rgb: [f32; 3]) -> [f32; 3] { + const K: [f32; 3] = [0.2627, 0.6780, 0.0593]; + const GAMMA: f32 = 1.2; const A: f32 = 0.17883277; const B: f32 = 0.28466892; const C: f32 = 0.559_910_7; - rgb.map(|v| { - let s = if v.is_finite() { v.clamp(0.0, 12.0) / 12.0 } else { 0.0 }; - if s <= 1.0 / 12.0 { (3.0 * s).sqrt() } else { A * (12.0 * s - B).ln() + C } + let display = rgb.map(|v| (if v.is_finite() { v.max(0.0) } else { 0.0 }) * (HDR_REFERENCE_WHITE_NITS / crate::cicp::HLG_PEAK_NITS)); + let y = K[0] * display[0] + K[1] * display[1] + K[2] * display[2]; + let inverse_ootf = if y > 0.0 { y.powf((1.0 - GAMMA) / GAMMA) } else { 0.0 }; + display.map(|v| { + let scene = (v * inverse_ootf).clamp(0.0, 1.0); + if scene <= 1.0 / 12.0 { (3.0 * scene).sqrt() } else { A * (12.0 * scene - B).ln() + C } }) } @@ -39,8 +44,14 @@ fn hlg_signal(rgb: [f32; 3]) -> [f32; 3] { pub fn encode_avif_with_speed(img: &Image, quality: u8, speed: u8) -> Result, CodecError> { #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] { - let mut enc = - ravif::Encoder::new().with_quality(f32::from(quality.clamp(1, 100))).with_speed(speed.clamp(1, 10)).with_bit_depth(ravif::BitDepth::Ten); + let mut enc = ravif::Encoder::new() + .with_quality(f32::from(quality.clamp(1, 100))) + .with_alpha_quality(f32::from(quality.clamp(1, 100))) + .with_speed(speed.clamp(1, 10)) + .with_bit_depth(ravif::BitDepth::Ten); + if let Some(exif) = img.meta.exif.as_deref() { + enc = enc.with_exif(exif); + } let alpha_mode = if img.layout().has_alpha() { ravif::AlphaColorMode::UnassociatedClean } else { ravif::AlphaColorMode::UnassociatedDirty }; enc = enc.with_alpha_color_mode(alpha_mode); let w = img.width() as usize; @@ -209,3 +220,94 @@ pub fn encode_avif_hdr( Err(CodecError::unsupported(Format::Avif, "AVIF encoding is not available in this build")) } } + +#[cfg(test)] +mod tests { + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + #[test] + fn sdr_avif_uses_requested_alpha_quality() { + use crate::{ChannelLayout, Image}; + + let pixels: Vec = (0..1024).map(|i| ravif::RGBA8::new(100, 120, 140, ((i * 37) % 256) as u8)).collect(); + let samples: Vec = pixels.iter().flat_map(|p| [p.r, p.g, p.b, p.a]).collect(); + let image = Image::from_u8(32, 32, ChannelLayout::Rgba, samples).unwrap(); + let actual = super::encode_avif_with_speed(&image, 20, 10).unwrap(); + let expected = ravif::Encoder::new() + .with_quality(20.0) + .with_alpha_quality(20.0) + .with_speed(10) + .with_bit_depth(ravif::BitDepth::Ten) + .with_alpha_color_mode(ravif::AlphaColorMode::UnassociatedClean) + .encode_rgba(ravif::Img::new(&pixels, 32, 32)) + .unwrap() + .avif_file; + assert!(actual == expected, "encoded alpha must use the requested quality"); + } + + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + #[test] + fn sdr_avif_keeps_exif_metadata() { + use crate::{ChannelLayout, Image}; + + let exif = b"II*\0shared-avif-exif".to_vec(); + let mut image = Image::from_u8(2, 1, ChannelLayout::Rgb, vec![20, 40, 60, 80, 100, 120]).unwrap(); + image.meta.exif = Some(exif.clone()); + + let avif = super::encode_avif_with_speed(&image, 80, 8).unwrap(); + assert!(avif.windows(exif.len()).any(|window| window == exif.as_slice())); + } + + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + #[test] + fn hlg_mapping_matches_reference_white_and_peak() { + let white = super::hlg_signal([1.0; 3]); + assert!(white.iter().all(|&value| (value - 0.75).abs() < 0.005), "{white:?}"); + + let peak = super::hlg_signal([crate::cicp::HLG_PEAK_NITS / super::HDR_REFERENCE_WHITE_NITS; 3]); + assert!(peak.iter().all(|&value| (value - 1.0).abs() < 0.005), "{peak:?}"); + + let invalid = super::hlg_signal([f32::NAN, -1.0, f32::INFINITY]); + assert_eq!(invalid, [0.0; 3]); + } + + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + #[test] + fn hlg_encoded_output_signals_bt2020_hlg() { + let avif = super::encode_avif_hdr(1, 1, &[1.0; 3], 100, 10, None, super::HdrTransfer::Hlg).unwrap(); + let nclx = avif.windows(4).position(|window| window == b"nclx").expect("the AVIF has nclx color metadata"); + let color = avif.get(nclx + 4..nclx + 11).expect("complete nclx color metadata"); + assert_eq!(&color[..2], &[0, 9], "BT.2020 primaries"); + assert_eq!(&color[2..4], &[0, 18], "BT.2100 HLG transfer"); + assert_eq!(&color[4..6], &[0, 9], "BT.2020 non-constant luminance matrix"); + assert!(!avif.windows(4).any(|window| window == b"clli"), "HLG does not signal PQ content-light metadata"); + } + + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + #[test] + fn hlg_encoded_pixels_preserve_reference_white_and_peak() { + let mut rgb = Vec::new(); + for _ in 0..32 { + for x in 0..64 { + let level = if x < 32 { 1.0 } else { crate::cicp::HLG_PEAK_NITS / super::HDR_REFERENCE_WHITE_NITS }; + rgb.extend_from_slice(&[level; 3]); + } + } + let avif = super::encode_avif_hdr(64, 32, &rgb, 100, 10, None, super::HdrTransfer::Hlg).unwrap(); + let mut offset = 0; + let payload = loop { + let header = avif.get(offset..offset + 8).expect("complete AVIF box header"); + let size = u32::from_be_bytes(header[..4].try_into().unwrap()) as usize; + assert!(size >= 8 && offset + size <= avif.len(), "valid AVIF box size"); + if &header[4..] == b"mdat" { + break &avif[offset + 8..offset + size]; + } + offset += size; + }; + let mut decoder = craft_video::av1::decode::Decoder::new(); + let mut frames = decoder.decode(payload).unwrap(); + frames.extend(decoder.flush_result().unwrap()); + let luma = &frames.first().expect("encoded AV1 frame").planes[0]; + assert!((i32::from(luma[16 * 64 + 16]) - 767).abs() <= 5, "diffuse white"); + assert!((i32::from(luma[16 * 64 + 48]) - 1023).abs() <= 5, "peak white"); + } +} diff --git a/crates/jsonrpc/Cargo.toml b/crates/jsonrpc/Cargo.toml new file mode 100644 index 000000000..7456ececf --- /dev/null +++ b/crates/jsonrpc/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-jsonrpc" +description = "Bounded JSON-RPC 2.0 server envelopes without transport or application routing" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde_json = { workspace = true } diff --git a/crates/jsonrpc/README.md b/crates/jsonrpc/README.md new file mode 100644 index 000000000..8711c25ff --- /dev/null +++ b/crates/jsonrpc/README.md @@ -0,0 +1,31 @@ +# craft-jsonrpc + +`craft-jsonrpc` provides bounded JSON-RPC 2.0 server envelopes independently of a transport or an +application's method router. It can read a bounded newline-delimited message, parse it from bytes, +check request IDs and batch sizes, dispatch requests and notifications separately, and build +response envelopes. It does not start a server, choose an MCP protocol version, or interpret tool +names and arguments. + +`Limits` defaults to 64 MiB per message and 4096 batch entries. `parse_json` checks the byte limit +before deserialization, and `process` rejects an oversized batch before calling any handler. +For newline-delimited transports, `read_line` bounds memory while reading; after an over-limit +line, the caller must close the stream or discard the rest of that line. +`serde_json`'s nesting limit remains in effect. Response sizes are the caller's responsibility. + +A valid request receives the handler's result or error with its original string, numeric, or null +ID. The default accepts numeric IDs as required by [JSON-RPC 2.0](https://www.jsonrpc.org/specification#request_object), +which discourages fractional values. Set `integer_ids_only` and clear `allow_null_ids` for +protocols such as MCP that require integer or string IDs. A legacy server can retain its previous +null-ID behavior independently. An omitted ID is a notification: it calls the separate notification +handler and gets no reply. +Malformed batch entries receive an invalid-request error; an empty batch gets one error response; +a batch containing only notifications has no response. Invalid parameter shapes receive an +invalid-params error for requests and no reply for notifications. The handler decides whether +object or array parameters are valid for each method. + +The caller may inspect the bounded parsed value before `process` to support an existing legacy +protocol. Parse failures and limit failures are returned explicitly so each transport can choose +its error and connection policy; `parse_error_response` builds the JSON-RPC parse error. + +The envelope behavior is extracted from StencilCraft's JSON-lines MCP/control server. App-specific +method routing, legacy control requests, and MCP lifecycle policy stay with the app. diff --git a/crates/jsonrpc/src/lib.rs b/crates/jsonrpc/src/lib.rs new file mode 100644 index 000000000..9f8128d1d --- /dev/null +++ b/crates/jsonrpc/src/lib.rs @@ -0,0 +1,197 @@ +//! Bounded JSON-RPC 2.0 server envelopes without a transport or application router. +//! +//! Parse a bounded line with [`parse_json`], then pass the value to [`process`]. A host may +//! inspect the parsed value first for a legacy protocol. `process` validates envelopes, dispatches +//! requests and notifications separately, and constructs single or batch replies. The host owns +//! the transport, method semantics and response-size policy. + +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use serde_json::{Value, json}; +use std::io::BufRead; + +/// Bounds on a single JSON document and on the number of requests in a batch. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + pub max_message_bytes: usize, + pub max_batch_items: usize, + /// Reject fractional JSON numbers as IDs for protocols that require integer IDs. + pub integer_ids_only: bool, + /// Permit an explicit null request ID, as JSON-RPC does; some protocols prohibit it. + pub allow_null_ids: bool, +} + +impl Default for Limits { + fn default() -> Self { + Self { max_message_bytes: 64 * 1024 * 1024, max_batch_items: 4096, integer_ids_only: false, allow_null_ids: true } + } +} + +/// Failure before dispatch. An over-limit batch is rejected before any callback is invoked. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ParseError { + MessageTooLarge, + InvalidJson, + BatchTooLarge, +} + +impl std::fmt::Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::MessageTooLarge => "JSON-RPC message exceeds the byte limit", + Self::InvalidJson => "invalid JSON", + Self::BatchTooLarge => "JSON-RPC batch exceeds the item limit", + }) + } +} +impl std::error::Error for ParseError {} + +/// Error reading a newline-delimited message. After `TooLong`, close or otherwise discard the +/// remaining input line before reading another message. +#[derive(Debug)] +pub enum LineError { + Io(std::io::Error), + TooLong, +} + +impl std::fmt::Display for LineError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(error) => write!(f, "message read failed: {error}"), + Self::TooLong => f.write_str("message exceeds the byte limit"), + } + } +} +impl std::error::Error for LineError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Io(error) => Some(error), + Self::TooLong => None, + } + } +} + +/// Read at most `max_bytes` before a newline, without allocating for an oversized line. The +/// newline is consumed but omitted from the returned bytes. EOF with no bytes returns `None`; +/// an empty line returns `Some(Vec::new())`. +pub fn read_line(reader: &mut R, max_bytes: usize) -> Result>, LineError> { + let mut line = Vec::new(); + loop { + let bytes = reader.fill_buf().map_err(LineError::Io)?; + if bytes.is_empty() { + return Ok((!line.is_empty()).then_some(line)); + } + let newline = bytes.iter().position(|&byte| byte == b'\n'); + let len = newline.unwrap_or(bytes.len()); + if line.len().saturating_add(len) > max_bytes { + return Err(LineError::TooLong); + } + line.extend_from_slice(&bytes[..len]); + reader.consume(len + usize::from(newline.is_some())); + if newline.is_some() { + return Ok(Some(line)); + } + } +} + +/// Parse JSON after checking its byte length. `serde_json` also bounds nesting depth. +pub fn parse_json(bytes: &[u8], limits: Limits) -> Result { + if bytes.len() > limits.max_message_bytes { + return Err(ParseError::MessageTooLarge); + } + serde_json::from_slice(bytes).map_err(|_| ParseError::InvalidJson) +} + +/// A validated request or notification. A present null `id` is distinct from an omitted `id`. +#[derive(Clone, Copy, Debug)] +pub struct Request<'a> { + pub id: Option<&'a Value>, + pub method: &'a str, + pub params: Option<&'a Value>, +} + +/// Inspect a single request envelope. This validates its JSON-RPC version, method and ID; +/// [`process`] additionally validates parameter shape before dispatching it. +pub fn request(value: &Value, limits: Limits) -> Option> { + let object = value.as_object()?; + let id = object.get("id"); + let valid_id = id.is_none_or(|v| { + (v.is_null() && limits.allow_null_ids) || v.is_string() || if limits.integer_ids_only { v.is_i64() || v.is_u64() } else { v.is_number() } + }); + if object.get("jsonrpc").and_then(Value::as_str) != Some("2.0") || !valid_id { + return None; + } + Some(Request { id, method: object.get("method")?.as_str()?, params: object.get("params") }) +} + +/// A JSON-RPC error returned by a method handler. +#[derive(Clone, Debug, PartialEq)] +pub struct RpcError { + pub code: i64, + pub message: String, + pub data: Option, +} + +impl RpcError { + pub fn new(code: i64, message: impl Into) -> Self { + Self { code, message: message.into(), data: None } + } +} + +/// Build an error response, including optional error data. +pub fn error_response(id: Value, error: RpcError) -> Value { + let mut detail = json!({"code": error.code, "message": error.message}); + if let (Some(data), Some(object)) = (error.data, detail.as_object_mut()) { + object.insert("data".into(), data); + } + json!({"jsonrpc": "2.0", "id": id, "error": detail}) +} + +/// Build the standard parse-error response for input that is not JSON. +pub fn parse_error_response() -> Value { + error_response(Value::Null, RpcError::new(-32700, "Parse error")) +} + +fn invalid_request() -> Value { + error_response(Value::Null, RpcError::new(-32600, "Invalid Request")) +} + +fn one(value: &Value, limits: Limits, on_request: &mut F, on_notification: &mut N) -> Option +where + F: FnMut(Request<'_>) -> Result, + N: FnMut(Request<'_>), +{ + let Some(request) = request(value, limits) else { return Some(invalid_request()) }; + let id = request.id; + if request.params.is_some_and(|v| !v.is_object() && !v.is_array()) { + return id.map(|id| error_response(id.clone(), RpcError::new(-32602, "Invalid params"))); + } + let Some(id) = id else { + on_notification(request); + return None; + }; + Some(match on_request(request) { + Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}), + Err(error) => error_response(id.clone(), error), + }) +} + +/// Process a parsed JSON-RPC message. Notifications never receive a response. Invalid batch +/// entries produce individual errors; an empty batch produces one invalid-request response. +/// A batch over the limit returns [`ParseError::BatchTooLarge`] without invoking either callback. +pub fn process(value: &Value, limits: Limits, mut on_request: F, mut on_notification: N) -> Result, ParseError> +where + F: FnMut(Request<'_>) -> Result, + N: FnMut(Request<'_>), +{ + let Some(batch) = value.as_array() else { return Ok(one(value, limits, &mut on_request, &mut on_notification)) }; + if batch.len() > limits.max_batch_items { + return Err(ParseError::BatchTooLarge); + } + if batch.is_empty() { + return Ok(Some(invalid_request())); + } + let responses: Vec<_> = batch.iter().filter_map(|v| one(v, limits, &mut on_request, &mut on_notification)).collect(); + Ok((!responses.is_empty()).then_some(Value::Array(responses))) +} diff --git a/crates/jsonrpc/tests/server.rs b/crates/jsonrpc/tests/server.rs new file mode 100644 index 000000000..a9186f4ae --- /dev/null +++ b/crates/jsonrpc/tests/server.rs @@ -0,0 +1,172 @@ +use craft_jsonrpc::{Limits, ParseError, RpcError, parse_error_response, parse_json, process}; +use serde_json::{Value, json}; + +#[test] +fn batches_preserve_ids_order_and_suppress_notification_replies() { + let input = json!([ + {"jsonrpc": "2.0", "method": "notifications/initialized"}, + {"jsonrpc": "2.0", "id": "a", "method": "ping"}, + {"jsonrpc": "2.0", "id": 2, "method": "unknown"}, + 17, + {"jsonrpc": "2.0", "id": null, "method": "ping"} + ]); + let mut notifications = Vec::new(); + let result = process( + &input, + Limits::default(), + |request| match request.method { + "ping" => Ok(json!({})), + _ => Err(RpcError::new(-32601, "Method not found")), + }, + |request| notifications.push(request.method.to_owned()), + ) + .unwrap() + .unwrap(); + assert_eq!(notifications, ["notifications/initialized"]); + assert_eq!( + result, + json!([ + {"jsonrpc":"2.0", "id":"a", "result":{}}, + {"jsonrpc":"2.0", "id":2, "error":{"code":-32601,"message":"Method not found"}}, + {"jsonrpc":"2.0", "id":null, "error":{"code":-32600,"message":"Invalid Request"}}, + {"jsonrpc":"2.0", "id":null, "result":{}} + ]) + ); +} + +#[test] +fn notifications_never_receive_responses_including_all_notification_batches() { + let input = json!([ + {"jsonrpc":"2.0", "method":"tools/call", "params":{"name":"mutating"}}, + {"jsonrpc":"2.0", "method":"notifications/initialized"} + ]); + let mut requests = 0; + let mut notices = 0; + let result = process( + &input, + Limits::default(), + |_| { + requests += 1; + Ok(Value::Null) + }, + |_| notices += 1, + ) + .unwrap(); + assert!(result.is_none()); + assert_eq!(requests, 0); + assert_eq!(notices, 2); +} + +#[test] +fn invalid_envelopes_and_params_return_standard_errors() { + for id in [json!(true), json!([]), json!({})] { + let input = json!({"jsonrpc":"2.0", "id":id, "method":"ping"}); + let reply = process(&input, Limits::default(), |_| panic!("invalid id dispatched"), |_| panic!("invalid id notified")).unwrap().unwrap(); + assert_eq!(reply["id"], Value::Null); + assert_eq!(reply["error"]["code"], -32600); + } + for input in [json!({}), json!({"jsonrpc":"1.0", "id":1, "method":"ping"}), json!({"jsonrpc":"2.0", "id":1}), json!([])] { + let reply = + process(&input, Limits::default(), |_| panic!("invalid request dispatched"), |_| panic!("invalid notification")).unwrap().unwrap(); + assert_eq!(reply["error"]["code"], -32600); + } + let input = json!({"jsonrpc":"2.0", "id":4, "method":"ping", "params":17}); + let reply = process(&input, Limits::default(), |_| panic!("invalid params dispatched"), |_| {}).unwrap().unwrap(); + assert_eq!(reply["id"], 4); + assert_eq!(reply["error"]["code"], -32602); + let notification = json!({"jsonrpc":"2.0", "method":"notice", "params":17}); + assert!(process(¬ification, Limits::default(), |_| panic!("invalid request"), |_| panic!("invalid notification")).unwrap().is_none()); +} + +#[test] +fn arrays_are_valid_params_and_errors_can_include_data() { + let input = json!({"jsonrpc":"2.0", "id":-7, "method":"test", "params":[1, 2]}); + let reply = process( + &input, + Limits::default(), + |request| { + assert_eq!(request.params, Some(&json!([1, 2]))); + let mut error = RpcError::new(-32000, "failure"); + error.data = Some(json!({"why":"example"})); + Err(error) + }, + |_| {}, + ) + .unwrap() + .unwrap(); + assert_eq!(reply, json!({"jsonrpc":"2.0", "id":-7, "error":{"code":-32000,"message":"failure","data":{"why":"example"}}})); +} + +#[test] +fn parse_and_batch_limits_apply_before_dispatch() { + let limits = Limits { max_message_bytes: 3, max_batch_items: 1, ..Limits::default() }; + assert_eq!(parse_json(b"null", limits), Err(ParseError::MessageTooLarge)); + assert_eq!(parse_json(b"123", limits).unwrap(), json!(123)); + assert_eq!(parse_json(b"{", limits), Err(ParseError::InvalidJson)); + assert_eq!(parse_error_response()["error"]["code"], -32700); + let input = json!([{"jsonrpc":"2.0", "id":1, "method":"ping"}, {"jsonrpc":"2.0", "method":"notice"}]); + assert_eq!( + process(&input, limits, |_| panic!("over-limit batch dispatched"), |_| panic!("over-limit batch notified")), + Err(ParseError::BatchTooLarge) + ); + let one = json!([{"jsonrpc":"2.0", "id":1, "method":"ping"}]); + assert_eq!(process(&one, limits, |_| Ok(json!({})), |_| {}).unwrap().unwrap().as_array().unwrap().len(), 1); +} + +#[test] +fn invalid_utf8_and_excessive_nesting_are_rejected() { + assert_eq!(parse_json(&[0xff], Limits::default()), Err(ParseError::InvalidJson)); + let nested = format!("{}0{}", "[".repeat(200), "]".repeat(200)); + assert_eq!(parse_json(nested.as_bytes(), Limits::default()), Err(ParseError::InvalidJson)); +} + +#[test] +fn bounded_line_read_handles_chunks_empty_lines_and_eof() { + use std::io::{BufReader, Cursor}; + let mut reader = BufReader::with_capacity(2, Cursor::new(b"abc\n\nxy")); + assert_eq!(craft_jsonrpc::read_line(&mut reader, 3).unwrap(), Some(b"abc".to_vec())); + assert_eq!(craft_jsonrpc::read_line(&mut reader, 3).unwrap(), Some(Vec::new())); + assert_eq!(craft_jsonrpc::read_line(&mut reader, 3).unwrap(), Some(b"xy".to_vec())); + assert_eq!(craft_jsonrpc::read_line(&mut reader, 3).unwrap(), None); + let mut too_long = BufReader::with_capacity(2, Cursor::new(b"abcd\n")); + assert!(matches!(craft_jsonrpc::read_line(&mut too_long, 3), Err(craft_jsonrpc::LineError::TooLong))); +} + +#[test] +fn bounded_line_read_propagates_io_errors() { + struct Broken; + impl std::io::Read for Broken { + fn read(&mut self, _: &mut [u8]) -> std::io::Result { + Err(std::io::Error::other("broken")) + } + } + let mut reader = std::io::BufReader::new(Broken); + assert!(matches!(craft_jsonrpc::read_line(&mut reader, 1), Err(craft_jsonrpc::LineError::Io(_)))); +} + +#[test] +fn inspect_single_request_distinguishes_null_ids_and_invalid_envelopes() { + let request = json!({"jsonrpc":"2.0", "id":null, "method":"ping"}); + let parsed = craft_jsonrpc::request(&request, Limits::default()).unwrap(); + assert_eq!(parsed.id, Some(&Value::Null)); + assert_eq!(parsed.method, "ping"); + let notification = json!({"jsonrpc":"2.0", "method":"ping"}); + assert!(craft_jsonrpc::request(¬ification, Limits::default()).unwrap().id.is_none()); + assert!(craft_jsonrpc::request(&json!({"jsonrpc":"2.0", "id":false, "method":"ping"}), Limits::default()).is_none()); + assert!(craft_jsonrpc::request(&json!({"method":"ping"}), Limits::default()).is_none()); +} + +#[test] +fn numeric_ids_follow_the_selected_protocol_policy() { + let input = json!({"jsonrpc":"2.0", "id":1.5, "method":"ping"}); + let reply = process(&input, Limits::default(), |_| Ok(json!({})), |_| {}).unwrap().unwrap(); + assert_eq!(reply["id"], json!(1.5)); + let strict = Limits { integer_ids_only: true, ..Limits::default() }; + let reply = process(&input, strict, |_| panic!("fractional ID dispatched"), |_| {}).unwrap().unwrap(); + assert_eq!(reply["id"], Value::Null); + assert_eq!(reply["error"]["code"], -32600); + let no_null = Limits { allow_null_ids: false, ..strict }; + let null_id = json!({"jsonrpc":"2.0", "id":null, "method":"ping"}); + let reply = process(&null_id, no_null, |_| panic!("null ID dispatched"), |_| {}).unwrap().unwrap(); + assert_eq!(reply["error"]["code"], -32600); +} diff --git a/crates/mcmat/Cargo.toml b/crates/mcmat/Cargo.toml new file mode 100644 index 000000000..ee3a18626 --- /dev/null +++ b/crates/mcmat/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "craft-mcmat" +description = "Bounded portable material archive interchange for Craft applications" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-zip = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/mcmat/README.md b/crates/mcmat/README.md new file mode 100644 index 000000000..074a0ac44 --- /dev/null +++ b/crates/mcmat/README.md @@ -0,0 +1,17 @@ +# craft-mcmat + +A bounded reader and writer for portable MaterialCraft `.mcmat` ZIP packages. A package +contains `material.json`, named texture maps and a source package. Applications interpret +parameter values and cook or render maps themselves; this crate does not depend on an app's +model. It supports callers that read either version 1 or version 2 of the manifest, or both. + +`is_material` checks for a manifest with a bounded ZIP index; `Material::open` takes caller-provided version, entry-count, map-count, per-entry and total +expanded-byte limits. Each subsequent `read` and `read_source` shares that budget, including +repeated references to the same ZIP member. Duplicate member names are refused. The manifest +preserves unknown fields at the top level and on maps; parameter, input and preset objects are +kept as JSON values so their unknown fields survive too. `write` preserves the caller's source +entry name and map names, and rejects duplicate archive member names through `craft-zip`. + +The source package is opaque. Version 1 commonly names `source.mcraft`; MaterialCraft's version 2 +writer names `source.sbs`. Callers that invoke another application must preserve the source's +format and extension rather than infer it from `.mcmat` alone. diff --git a/crates/mcmat/src/lib.rs b/crates/mcmat/src/lib.rs new file mode 100644 index 000000000..75b8709ef --- /dev/null +++ b/crates/mcmat/src/lib.rs @@ -0,0 +1,216 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] +//! Bounded interchange for MaterialCraft portable materials (`.mcmat`). +//! +//! The manifest and its version belong to the format, while interpreting parameter values, +//! cooking maps and opening the source package belong to the caller. Unknown fields survive +//! deserialization and serialization, including fields added by later versions. + +use std::collections::BTreeMap; + +use craft_zip::{Archive, Compression, Writer}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +pub const FORMAT: &str = "craft-material"; +pub const MANIFEST_ENTRY: &str = "material.json"; + +/// A portable manifest; typed parameter values remain JSON so applications can adapt their own model. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Manifest { + pub format: String, + pub version: u32, + pub name: String, + pub graph: String, + pub size: [u32; 2], + pub maps: Vec, + #[serde(default)] + pub parameters: Vec, + #[serde(default)] + pub inputs: Vec, + #[serde(default)] + pub presets: Vec, + pub source: String, + #[serde(flatten)] + pub extra: BTreeMap, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct MapEntry { + pub usage: String, + pub file: String, + #[serde(flatten)] + pub extra: BTreeMap, +} + +/// All limits are supplied by the consumer, including the versions it understands. +#[derive(Clone, Copy, Debug)] +pub struct Limits { + pub min_version: u32, + pub max_version: u32, + pub max_entries: u64, + pub max_maps: usize, + pub max_manifest_bytes: u64, + pub max_entry_bytes: u64, + pub max_total_bytes: u64, +} + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("invalid portable material: {0}")] + Invalid(String), + #[error("unsupported portable material version {0}")] + Version(u32), + #[error("portable material limit: {0}")] + Limit(String), + #[error("portable material archive: {0}")] + Archive(#[from] craft_zip::Error), +} + +/// Whether a bounded ZIP index contains a material manifest. This does not validate the manifest. +pub fn is_material(bytes: &[u8], max_entries: u64) -> bool { + Archive::open(bytes, craft_zip::Limits { max_entries, max_total_bytes: 0 }) + .is_ok_and(|archive| !archive.has_duplicate_names() && archive.contains(MANIFEST_ENTRY)) +} + +/// Manifest and original archive, retaining one shared expanded-byte budget across reads. +pub struct Material<'a> { + pub manifest: Manifest, + archive: Archive<'a>, + max_entry_bytes: u64, +} + +impl<'a> Material<'a> { + pub fn open(bytes: &'a [u8], limits: Limits) -> Result { + let mut archive = Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: limits.max_total_bytes })?; + if archive.has_duplicate_names() { + return Err(Error::Invalid("duplicate archive entries".into())); + } + let json = archive.read(MANIFEST_ENTRY, limits.max_manifest_bytes)?.ok_or_else(|| Error::Invalid("material.json is missing".into()))?; + let manifest: Manifest = serde_json::from_slice(&json).map_err(|e| Error::Invalid(format!("material.json: {e}")))?; + if manifest.format != FORMAT { + return Err(Error::Invalid(format!("format `{}`", manifest.format))); + } + if !(limits.min_version..=limits.max_version).contains(&manifest.version) { + return Err(Error::Version(manifest.version)); + } + if manifest.maps.len() > limits.max_maps { + return Err(Error::Limit(format!("{} maps exceeds {}", manifest.maps.len(), limits.max_maps))); + } + Ok(Self { manifest, archive, max_entry_bytes: limits.max_entry_bytes }) + } + + /// Read one named member, charging it to the same budget as previous reads. + pub fn read(&mut self, name: &str) -> Result, Error> { + self.archive.read(name, self.max_entry_bytes)?.ok_or_else(|| Error::Invalid(format!("{name} is missing"))) + } + + pub fn read_source(&mut self) -> Result, Error> { + self.read(&self.manifest.source.clone()) + } +} + +/// Write a manifest, its named maps, and the source. Duplicate member names are rejected. +pub fn write(manifest: &Manifest, maps: &[(String, Vec)], source: &[u8]) -> Result, Error> { + // Flattened fields must not serialize a second copy of a known key. + const KNOWN: &[&str] = &["format", "version", "name", "graph", "size", "maps", "parameters", "inputs", "presets", "source"]; + if manifest.extra.keys().any(|key| KNOWN.contains(&key.as_str())) + || manifest.maps.iter().any(|map| map.extra.contains_key("usage") || map.extra.contains_key("file")) + { + return Err(Error::Invalid("an extra field duplicates a known field".into())); + } + let json = serde_json::to_vec_pretty(manifest).map_err(|e| Error::Invalid(format!("material.json: {e}")))?; + let mut writer = Writer::new(); + writer.add(MANIFEST_ENTRY, &json, Compression::Deflated)?; + for (name, bytes) in maps { + writer.add(name, bytes, Compression::Stored)?; + } + writer.add(&manifest.source, source, Compression::Stored)?; + Ok(writer.finish()?) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn limits() -> Limits { + Limits { + min_version: 1, + max_version: 2, + max_entries: 100, + max_maps: 4, + max_manifest_bytes: 4096, + max_entry_bytes: 100, + max_total_bytes: 8192, + } + } + + fn sample(version: u32) -> Manifest { + serde_json::from_value(serde_json::json!({ + "format": "craft-material", "version": version, "name": "Stone", "graph": "Stone", "size": [64, 64], + "maps": [{"usage": "baseColor", "file": "maps/base.png", "colorSpace": "srgb", "futureMap": true}], + "parameters": [{"id": "rough", "value": {"t": "float", "v": 0.25}, "futureParam": 42}], + "inputs": [{"identifier": "curvature", "kind": "gray"}], "presets": [], + "source": if version == 1 { "source.mcraft" } else { "source.sbs" }, "futureField": {"keep": true} + })) + .unwrap() + } + + #[test] + fn both_protocol_versions_keep_unknown_fields_and_payloads() { + for version in [1, 2] { + let manifest = sample(version); + let bytes = write(&manifest, &[("maps/base.png".into(), vec![1, 2, 3])], b"source").unwrap(); + assert!(is_material(&bytes, 100)); + assert!(!is_material(&bytes, 1)); + let mut material = Material::open(&bytes, limits()).unwrap(); + assert_eq!(material.manifest, manifest); + assert_eq!(material.read("maps/base.png").unwrap(), [1, 2, 3]); + assert_eq!(material.read_source().unwrap(), b"source"); + let json = serde_json::to_value(&material.manifest).unwrap(); + assert_eq!(json["futureField"]["keep"], true); + assert_eq!(json["maps"][0]["futureMap"], true); + assert_eq!(json["parameters"][0]["futureParam"], 42); + } + } + + #[test] + fn limits_apply_to_the_manifest_maps_and_aggregate_reads() { + let manifest = sample(2); + let bytes = write(&manifest, &[("maps/base.png".into(), vec![1; 8])], &[2; 8]).unwrap(); + let mut unsupported = limits(); + unsupported.max_version = 1; + assert!(matches!(Material::open(&bytes, unsupported), Err(Error::Version(2)))); + let mut no_maps = limits(); + no_maps.max_maps = 0; + assert!(matches!(Material::open(&bytes, no_maps), Err(Error::Limit(_)))); + let mut tiny_entry = limits(); + tiny_entry.max_entry_bytes = 7; + assert!(Material::open(&bytes, tiny_entry).unwrap().read("maps/base.png").is_err()); + let json_len = serde_json::to_vec_pretty(&manifest).unwrap().len() as u64; + let mut aggregate = limits(); + aggregate.max_total_bytes = json_len + 15; + let mut material = Material::open(&bytes, aggregate).unwrap(); + assert_eq!(material.read("maps/base.png").unwrap().len(), 8); + assert!(material.read_source().is_err()); + } + + #[test] + fn refuses_extra_fields_that_duplicate_known_keys() { + let mut manifest = sample(2); + manifest.extra.insert("version".into(), 3.into()); + assert!(write(&manifest, &[], b"source").is_err()); + manifest.extra.remove("version"); + manifest.maps[0].extra.insert("file".into(), "other".into()); + assert!(write(&manifest, &[], b"source").is_err()); + } + + #[test] + fn refuses_duplicate_names_on_write() { + let manifest = sample(2); + let duplicate = [("maps/base.png".to_string(), vec![1]), ("maps/base.png".to_string(), vec![2])]; + assert!(write(&manifest, &duplicate, b"source").is_err()); + } +} diff --git a/crates/metadata/src/camera_fields.rs b/crates/metadata/src/camera_fields.rs new file mode 100644 index 000000000..ade835736 --- /dev/null +++ b/crates/metadata/src/camera_fields.rs @@ -0,0 +1,445 @@ +//! Unnormalized camera-field EXIF view, reused from PhotoCraft (exposure, aperture, +//! ISO, focal length, camera and lens names). Input is the raw TIFF-structured EXIF payload +//! (`II*\0` / `MM\0*`, as stored in `Document.metadata.exif`); malformed data yields `None` +//! fields, never a panic. Tag numbers are from the EXIF 2.32 specification (CIPA DC-008). + +// Semantics originate in PhotoCraft, including Brandon Thomas and Fabio Lussi. +// Unlike read_exif this view does not normalize dates or derive missing fields. +#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used, clippy::panic))] + +/// Camera settings read from EXIF. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct CameraInfo { + pub make: Option, + pub model: Option, + pub lens: Option, + /// Seconds. + pub exposure_time: Option, + pub f_number: Option, + pub iso: Option, + /// Millimetres. + pub focal_length: Option, + /// 35 mm-equivalent focal length in millimetres. + pub focal_length_35mm: Option, + /// DateTimeOriginal as stored (`YYYY:MM:DD HH:MM:SS`). + pub date_taken: Option, +} + +impl CameraInfo { + /// Linear exposure factor `t · ISO / N²` (relative brightness of the image for one scene). + pub fn exposure_factor(&self) -> Option { + let t = self.exposure_time?; + let n = self.f_number.unwrap_or(1.0).max(0.1); + Some(t * self.iso.unwrap_or(100.0) / 100.0 / (n * n)) + } +} + +/// Text bytes decoded per EXIF read (real Make/Model/Lens/Date strings are a few dozen bytes). +const MAX_TEXT_BYTES: usize = 1 << 20; + +struct Tiff<'a> { + b: &'a [u8], + order: craft_tiff::ByteOrder, + /// Text bytes this read may still decode: duplicate tags can share one large blank value. + text_left: std::cell::Cell, +} + +impl Tiff<'_> { + fn u16(&self, o: usize) -> Option { + self.order.read_u16(self.b, o) + } + fn u32(&self, o: usize) -> Option { + self.order.read_u32(self.b, o) + } + /// Entries of the IFD at `off`: (tag, type, count, value offset field position). + fn ifd(&self, off: usize) -> Vec<(u16, u16, u32, usize)> { + let Some(n) = self.u16(off) else { return Vec::new() }; + (0..n as usize) + .filter_map(|k| { + // Checked: offsets come from the file, and usize is 32 bits on wasm. + let e = off.checked_add(2)?.checked_add(k * 12)?; + Some((self.u16(e)?, self.u16(e.checked_add(2)?)?, self.u32(e.checked_add(4)?)?, e.checked_add(8)?)) + }) + .collect() + } + fn data_at(&self, typ: u16, _count: u32, field: usize) -> Option { + // Keep this caller's historical classic-TIFF type policy; shared spans also + // support IFD/BigTIFF types which CameraInfo never accepted. + if !(1..=12).contains(&typ) { + return None; + } + let span = craft_tiff::entry_span(self.b, field.checked_sub(8)?, self.order, false, 0)?; + usize::try_from(span.start).ok() + } + fn number(&self, typ: u16, count: u32, field: usize) -> Option { + let at = self.data_at(typ, count, field)?; + match typ { + 3 => self.u16(at).map(f64::from), + 4 => self.u32(at).map(f64::from), + 5 => { + let (n, d) = (self.u32(at)?, self.u32(at.checked_add(4)?)?); + (d != 0).then(|| n as f64 / d as f64) + } + 10 => { + let (n, d) = (self.u32(at)? as i32, self.u32(at.checked_add(4)?)? as i32); + (d != 0).then(|| n as f64 / d as f64) + } + _ => None, + } + } + fn text(&self, typ: u16, count: u32, field: usize) -> Option { + #[cfg(test)] + TEXT_DECODES.with(|c| c.set(c.get() + 1)); + if typ != 2 { + return None; + } + let at = self.data_at(typ, count, field)?; + let n = usize::try_from(count).ok()?; + self.text_left.set(self.text_left.get().checked_sub(n)?); + let s = self.b.get(at..at.checked_add(n)?)?; + let s = String::from_utf8_lossy(s).trim_end_matches('\0').trim().to_string(); + (!s.is_empty()).then_some(s) + } +} + +#[cfg(test)] +thread_local!(static TEXT_DECODES: std::cell::Cell = const { std::cell::Cell::new(0) }); + +/// Reads [`CameraInfo`] from a TIFF-structured EXIF payload (an `Exif\0\0` prefix is skipped). +pub fn read(exif: &[u8]) -> CameraInfo { + let b = exif.strip_prefix(b"Exif\0\0").unwrap_or(exif); + let mut info = CameraInfo::default(); + let le = match b.get(..4) { + Some([b'I', b'I', 42, 0]) => true, + Some([b'M', b'M', 0, 42]) => false, + _ => return info, + }; + let t = Tiff { + b, + order: if le { craft_tiff::ByteOrder::Little } else { craft_tiff::ByteOrder::Big }, + text_left: std::cell::Cell::new(MAX_TEXT_BYTES), + }; + let Some(ifd0) = t.u32(4) else { return info }; + let mut queue = vec![ifd0 as usize]; + let mut seen = Vec::new(); + while let Some(off) = queue.pop() { + if seen.contains(&off) || seen.len() > 8 { + continue; + } + seen.push(off); + for (tag, typ, count, field) in t.ifd(off) { + match tag { + // Duplicate TIFF text tags can point at the same large payload. Keep the first + // recognized value and do not repeatedly decode/copy later duplicates. + 0x010F if info.make.is_none() => info.make = t.text(typ, count, field), + 0x0110 if info.model.is_none() => info.model = t.text(typ, count, field), + 0x829A => info.exposure_time = t.number(typ, count, field), + 0x829D => info.f_number = t.number(typ, count, field), + 0x8827 => info.iso = t.number(typ, count, field), + 0x920A => info.focal_length = t.number(typ, count, field), + 0xA405 => info.focal_length_35mm = t.number(typ, count, field).filter(|v| *v > 0.0), + 0xA434 if info.lens.is_none() => info.lens = t.text(typ, count, field), + 0x9003 if info.date_taken.is_none() => info.date_taken = t.text(typ, count, field), + 0x8769 => { + if let Some(p) = t.u32(field) { + queue.push(p as usize); + } + } + _ => {} + } + } + } + info +} + +/// Build a classic TIFF EXIF block using the shared TIFF writer. +/// Raw date strings and numeric rounding match the camera-field view. Text is +/// bounded to the reader's 1 MiB aggregate budget; oversized inputs return an error. +/// Physical directory placement is not part of this API's contract. +pub fn try_build(info: &CameraInfo) -> craft_tiff::Result> { + use craft_tiff::{ByteOrder, IfdBuilder, TiffError, TiffWriter, Value}; + let total = + [&info.make, &info.model, &info.lens, &info.date_taken].into_iter().flatten().try_fold(0usize, |n, s| n.checked_add(s.len())?.checked_add(1)); + if total.is_none_or(|n| n > MAX_TEXT_BYTES) { + return Err(TiffError::Limit("camera EXIF text")); + } + let mut root = IfdBuilder::new(); + let mut exif = IfdBuilder::new(); + for (tag, value) in [(0x010f, &info.make), (0x0110, &info.model)] { + if let Some(value) = value { + root.set(tag, Value::Ascii(value.clone())); + } + } + for (tag, value) in [(0x9003, &info.date_taken), (0xa434, &info.lens)] { + if let Some(value) = value { + exif.set(tag, Value::Ascii(value.clone())); + } + } + for (tag, value) in [(0x829a, info.exposure_time), (0x829d, info.f_number), (0x920a, info.focal_length)] { + if let Some(value) = value { + exif.set(tag, Value::Rational(vec![((value * 10000.0).round() as u32, 10000)])); + } + } + for (tag, value) in [(0x8827, info.iso), (0xa405, info.focal_length_35mm)] { + if let Some(value) = value { + exif.set(tag, Value::Short(vec![value as u16])); + } + } + // Empty child IFDs are valid here; the reader yields absent fields. + root.set_child(0x8769, exif); + TiffWriter::new(ByteOrder::Little, false).write(&[root]) +} + +#[cfg(test)] +fn build(info: &CameraInfo) -> Vec { + try_build(info).unwrap_or_default() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trips_and_survives_garbage() { + let info = CameraInfo { + make: Some("Generic".into()), + model: Some("Camera 1".into()), + lens: Some("24-70mm".into()), + exposure_time: Some(0.004), + f_number: Some(8.0), + iso: Some(200.0), + focal_length: Some(35.0), + focal_length_35mm: Some(52.0), + date_taken: Some("2024:02:17 12:40:19".into()), + }; + let b = build(&info); + let r = read(&b); + assert_eq!(r, info); + assert!((r.exposure_factor().unwrap() - 0.004 * 2.0 / 64.0).abs() < 1e-9); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend_from_slice(&b); + assert_eq!(read(&prefixed), info); + assert_eq!(read(b"nonsense"), CameraInfo::default()); + for cut in [5, 12, 30, b.len() - 3] { + let _ = read(&b[..cut]); + } + } + + /// Hostile payloads: huge counts, offsets near `u32::MAX`, self-referencing IFDs and random + /// bytes read as `None` fields instead of panicking or overflowing (usize is 32 bits on wasm). + #[test] + fn blank_duplicate_text_tags_share_one_decode_budget() { + // Blank values never fill `make`, so every duplicate would decode the same 1 MiB again; + // the per-read budget stops after the first, so the later real value is not reached. + let blank = MAX_TEXT_BYTES; + let entry = |count: u32, off: u32| { + let mut e = Vec::new(); + e.extend_from_slice(&0x010Fu16.to_le_bytes()); + e.extend_from_slice(&2u16.to_le_bytes()); + e.extend_from_slice(&count.to_le_bytes()); + e.extend_from_slice(&off.to_le_bytes()); + e + }; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + b.extend_from_slice(&3u16.to_le_bytes()); + b.extend(entry(blank as u32, 50)); + b.extend(entry(blank as u32, 50)); + b.extend(entry(2, u32::from_le_bytes(*b"M\0\0\0"))); + b.extend_from_slice(&0u32.to_le_bytes()); + assert_eq!(b.len(), 50); + b.extend(std::iter::repeat_n(b' ', blank)); + assert_eq!(read(&b).make, None); + } + + #[test] + fn duplicate_text_tags_keep_the_first_value_without_copying_large_duplicates() { + let value = vec![b'X'; 2 * 1024 * 1024]; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + // IFD0: Make="M", a duplicate Make pointing at a 2 MiB payload, and the Exif IFD link. + b.extend_from_slice(&3u16.to_le_bytes()); + b.extend_from_slice(&0x010Fu16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&2u32.to_le_bytes()); + b.extend_from_slice(b"M\0\0\0"); + b.extend_from_slice(&0x010Fu16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&(value.len() as u32).to_le_bytes()); + b.extend_from_slice(&80u32.to_le_bytes()); + b.extend_from_slice(&0x8769u16.to_le_bytes()); + b.extend_from_slice(&4u16.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.extend_from_slice(&50u32.to_le_bytes()); + b.extend_from_slice(&0u32.to_le_bytes()); + // Exif IFD at 50: DateTimeOriginal="D" and the same large duplicate payload at offset 80. + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&0x9003u16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&2u32.to_le_bytes()); + b.extend_from_slice(b"D\0\0\0"); + b.extend_from_slice(&0x9003u16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&(value.len() as u32).to_le_bytes()); + b.extend_from_slice(&80u32.to_le_bytes()); + b.extend_from_slice(&0u32.to_le_bytes()); + b.extend_from_slice(&value); + + TEXT_DECODES.with(|c| c.set(0)); + let info = read(&b); + assert_eq!(info.make.as_deref(), Some("M")); + assert_eq!(info.date_taken.as_deref(), Some("D")); + // One decode per tag: duplicates must be skipped before `text` copies their payload. + assert_eq!(TEXT_DECODES.with(|c| c.get()), 2); + } + + #[test] + fn hostile_payloads_never_panic() { + let entry = |tag: u16, typ: u16, count: u32, value: u32| { + let mut e = tag.to_le_bytes().to_vec(); + e.extend_from_slice(&typ.to_le_bytes()); + e.extend_from_slice(&count.to_le_bytes()); + e.extend_from_slice(&value.to_le_bytes()); + e + }; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + let entries = [ + entry(0x010F, 2, u32::MAX, 8), // Make: count far past the end + entry(0x0110, 2, 16, u32::MAX - 1), // Model: offset at the top of the range + entry(0x829A, 5, u32::MAX, 20), // ExposureTime: count overflows the size + entry(0x829D, 5, 1, u32::MAX - 3), // FNumber: rational straddles u32::MAX + entry(0x9003, 2, 0x4000_0000, 8), // DateTimeOriginal: size overflow on 32 bits + entry(0x8769, 4, 1, 8), // Exif IFD pointing back at IFD0 + entry(0x8769, 4, 1, u32::MAX), // ... and past the end + ]; + b.extend_from_slice(&(entries.len() as u16 + 3).to_le_bytes()); // claims more entries than exist + for e in entries { + b.extend_from_slice(&e); + } + assert_eq!(read(&b), CameraInfo::default()); + let mut be = b"MM\0*".to_vec(); + be.extend_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(read(&be), CameraInfo::default()); + // Deterministic noise behind a valid header, and every truncation of a real payload. + let mut x = 0x2545_F491u32; + for _ in 0..200 { + let mut n = b"II*\0\x08\0\0\0".to_vec(); + for _ in 0..96 { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + n.push(x as u8); + } + let _ = read(&n); + } + let good = build(&CameraInfo { + make: Some("Make".into()), + date_taken: Some("2024:02:17 12:40:19".into()), + exposure_time: Some(0.004), + ..Default::default() + }); + for cut in 0..good.len() { + let _ = read(&good[..cut]); + } + } +} + +// Keep the previous byte reader only as a test oracle during the shared-span cutover. +#[cfg(test)] +#[path = "camera_fields_legacy_tests.rs"] +mod legacy; + +#[cfg(test)] +mod shared_span_parity { + use super::*; + + fn compare(bytes: &[u8]) { + assert_eq!(format!("{:?}", read(bytes)), format!("{:?}", legacy::read(bytes))); + } + + #[test] + fn shared_writer_matches_legacy_fields_and_rejects_oversized_text() { + for value in [0.0, -1.0, 0.004, 2.8, 65535.0, f64::INFINITY, f64::NAN] { + let camera = CameraInfo { + exposure_time: Some(value), + f_number: Some(value), + iso: Some(value), + focal_length: Some(value), + focal_length_35mm: Some(value), + date_taken: Some("raw date".into()), + make: Some("M".into()), + model: Some("Model".into()), + lens: Some("Lens".into()), + }; + let old = legacy::CameraInfo { + exposure_time: camera.exposure_time, + f_number: camera.f_number, + iso: camera.iso, + focal_length: camera.focal_length, + focal_length_35mm: camera.focal_length_35mm, + date_taken: camera.date_taken.clone(), + make: camera.make.clone(), + model: camera.model.clone(), + lens: camera.lens.clone(), + }; + let bytes = try_build(&camera).unwrap(); + assert_eq!(format!("{:?}", read(&bytes)), format!("{:?}", legacy::read(&legacy::build(&old)))); + } + assert_eq!(read(&try_build(&CameraInfo::default()).unwrap()), CameraInfo::default()); + assert!(try_build(&CameraInfo { make: Some("x".repeat(MAX_TEXT_BYTES)), ..Default::default() }).is_err()); + assert!(try_build(&CameraInfo { make: Some("x".repeat(MAX_TEXT_BYTES - 1)), ..Default::default() }).is_ok()); + } + + #[test] + fn preserves_camera_selection_for_valid_truncated_and_mutated_payloads() { + let bytes = build(&CameraInfo { + make: Some("Maker".into()), + model: Some("Camera".into()), + lens: Some("Lens".into()), + exposure_time: Some(0.125), + f_number: Some(2.8), + iso: Some(200.0), + focal_length: Some(50.0), + focal_length_35mm: Some(75.0), + date_taken: Some("not normalized: original date".into()), + }); + compare(&bytes); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend_from_slice(&bytes); + compare(&prefixed); + for end in 0..=bytes.len() { + compare(&bytes[..end]); + } + // Exercise offsets, tags, types, counts and payloads independently, not random + // external data. The complete shared span must remain bounded for every edit. + for index in 0..bytes.len() { + for value in [0, 0xff, 0x80] { + let mut changed = bytes.clone(); + changed[index] = value; + compare(&changed); + } + } + } + + #[test] + fn preserves_partial_entry_and_zero_count_semantics_in_both_orders() { + for little in [true, false] { + let u16b = |v: u16| if little { v.to_le_bytes() } else { v.to_be_bytes() }; + let u32b = |v: u32| if little { v.to_le_bytes() } else { v.to_be_bytes() }; + for count in [0u32, 1, 2, u32::MAX] { + let mut bytes = if little { b"II".to_vec() } else { b"MM".to_vec() }; + bytes.extend(u16b(42)); + bytes.extend(u32b(8)); + bytes.extend(u16b(1)); + bytes.extend(u16b(0x8827)); + bytes.extend(u16b(3)); + bytes.extend(u32b(count)); + bytes.extend(u16b(200)); + compare(&bytes); // two inline bytes, no full 12-byte entry/next pointer + bytes.extend(u16b(0)); + compare(&bytes); + } + } + } +} diff --git a/crates/metadata/src/camera_fields_legacy_tests.rs b/crates/metadata/src/camera_fields_legacy_tests.rs new file mode 100644 index 000000000..1b560f91f --- /dev/null +++ b/crates/metadata/src/camera_fields_legacy_tests.rs @@ -0,0 +1,237 @@ +// Frozen differential oracle: PhotoCraft main abb30dff, exif.rs. Test-only, not a production fallback. +#![allow(dead_code)] +// Minimal EXIF reader for the camera settings the photography commands use (exposure, aperture, +// ISO, focal length, camera and lens names). Input is the raw TIFF-structured EXIF payload +// (`II*\0` / `MM\0*`, as stored in `Document.metadata.exif`); malformed data yields `None` +// fields, never a panic. Tag numbers are from the EXIF 2.32 specification (CIPA DC-008). + +/// Camera settings read from EXIF. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct CameraInfo { + pub make: Option, + pub model: Option, + pub lens: Option, + /// Seconds. + pub exposure_time: Option, + pub f_number: Option, + pub iso: Option, + /// Millimetres. + pub focal_length: Option, + /// 35 mm-equivalent focal length in millimetres. + pub focal_length_35mm: Option, + /// DateTimeOriginal as stored (`YYYY:MM:DD HH:MM:SS`). + pub date_taken: Option, +} + +impl CameraInfo { + /// Linear exposure factor `t · ISO / N²` (relative brightness of the image for one scene). + pub fn exposure_factor(&self) -> Option { + let t = self.exposure_time?; + let n = self.f_number.unwrap_or(1.0).max(0.1); + Some(t * self.iso.unwrap_or(100.0) / 100.0 / (n * n)) + } +} + +/// Text bytes decoded per EXIF read (real Make/Model/Lens/Date strings are a few dozen bytes). +const MAX_TEXT_BYTES: usize = 1 << 20; + +struct Tiff<'a> { + b: &'a [u8], + le: bool, + /// Text bytes this read may still decode: duplicate tags can share one large blank value. + text_left: std::cell::Cell, +} + +impl Tiff<'_> { + fn u16(&self, o: usize) -> Option { + let s = self.b.get(o..o.checked_add(2)?)?; + Some(if self.le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + } + fn u32(&self, o: usize) -> Option { + let s = self.b.get(o..o.checked_add(4)?)?; + let a = [s[0], s[1], s[2], s[3]]; + Some(if self.le { u32::from_le_bytes(a) } else { u32::from_be_bytes(a) }) + } + /// Entries of the IFD at `off`: (tag, type, count, value offset field position). + fn ifd(&self, off: usize) -> Vec<(u16, u16, u32, usize)> { + let Some(n) = self.u16(off) else { return Vec::new() }; + (0..n as usize) + .filter_map(|k| { + // Checked: offsets come from the file, and usize is 32 bits on wasm. + let e = off.checked_add(2)?.checked_add(k * 12)?; + Some((self.u16(e)?, self.u16(e.checked_add(2)?)?, self.u32(e.checked_add(4)?)?, e.checked_add(8)?)) + }) + .collect() + } + fn data_at(&self, typ: u16, count: u32, field: usize) -> Option { + let size = match typ { + 1 | 2 | 6 | 7 => 1usize, + 3 | 8 => 2, + 4 | 9 | 11 => 4, + 5 | 10 | 12 => 8, + _ => return None, + } + .checked_mul(usize::try_from(count).ok()?)?; + let at = if size <= 4 { field } else { usize::try_from(self.u32(field)?).ok()? }; + // The whole value must lie inside the payload, not just its first element. + self.b.get(at..at.checked_add(size)?)?; + Some(at) + } + fn number(&self, typ: u16, count: u32, field: usize) -> Option { + let at = self.data_at(typ, count, field)?; + match typ { + 3 => self.u16(at).map(f64::from), + 4 => self.u32(at).map(f64::from), + 5 => { + let (n, d) = (self.u32(at)?, self.u32(at.checked_add(4)?)?); + (d != 0).then(|| n as f64 / d as f64) + } + 10 => { + let (n, d) = (self.u32(at)? as i32, self.u32(at.checked_add(4)?)? as i32); + (d != 0).then(|| n as f64 / d as f64) + } + _ => None, + } + } + fn text(&self, typ: u16, count: u32, field: usize) -> Option { + #[cfg(test)] + TEXT_DECODES.with(|c| c.set(c.get() + 1)); + if typ != 2 { + return None; + } + let at = self.data_at(typ, count, field)?; + let n = usize::try_from(count).ok()?; + self.text_left.set(self.text_left.get().checked_sub(n)?); + let s = self.b.get(at..at.checked_add(n)?)?; + let s = String::from_utf8_lossy(s).trim_end_matches('\0').trim().to_string(); + (!s.is_empty()).then_some(s) + } +} + +#[cfg(test)] +thread_local!(static TEXT_DECODES: std::cell::Cell = const { std::cell::Cell::new(0) }); + +/// Reads [`CameraInfo`] from a TIFF-structured EXIF payload (an `Exif\0\0` prefix is skipped). +pub fn read(exif: &[u8]) -> CameraInfo { + let b = exif.strip_prefix(b"Exif\0\0").unwrap_or(exif); + let mut info = CameraInfo::default(); + let le = match b.get(..4) { + Some([b'I', b'I', 42, 0]) => true, + Some([b'M', b'M', 0, 42]) => false, + _ => return info, + }; + let t = Tiff { b, le, text_left: std::cell::Cell::new(MAX_TEXT_BYTES) }; + let Some(ifd0) = t.u32(4) else { return info }; + let mut queue = vec![ifd0 as usize]; + let mut seen = Vec::new(); + while let Some(off) = queue.pop() { + if seen.contains(&off) || seen.len() > 8 { + continue; + } + seen.push(off); + for (tag, typ, count, field) in t.ifd(off) { + match tag { + // Duplicate TIFF text tags can point at the same large payload. Keep the first + // recognized value and do not repeatedly decode/copy later duplicates. + 0x010F if info.make.is_none() => info.make = t.text(typ, count, field), + 0x0110 if info.model.is_none() => info.model = t.text(typ, count, field), + 0x829A => info.exposure_time = t.number(typ, count, field), + 0x829D => info.f_number = t.number(typ, count, field), + 0x8827 => info.iso = t.number(typ, count, field), + 0x920A => info.focal_length = t.number(typ, count, field), + 0xA405 => info.focal_length_35mm = t.number(typ, count, field).filter(|v| *v > 0.0), + 0xA434 if info.lens.is_none() => info.lens = t.text(typ, count, field), + 0x9003 if info.date_taken.is_none() => info.date_taken = t.text(typ, count, field), + 0x8769 => { + if let Some(p) = t.u32(field) { + queue.push(p as usize); + } + } + _ => {} + } + } + } + info +} + +/// Builds a little-endian EXIF payload (used by tests and by synthetic fixtures). +pub fn build(info: &CameraInfo) -> Vec { + // IFD0: Make, Model, ExifIFD pointer; Exif IFD: the rest. Data area after the IFDs. + let mut ifd0: Vec<(u16, u16, u32, Vec)> = Vec::new(); + let mut exif: Vec<(u16, u16, u32, Vec)> = Vec::new(); + let text = |s: &str| { + let mut v = s.as_bytes().to_vec(); + v.push(0); + v + }; + let rational = |v: f64| { + let d = 10000u32; + let mut b = ((v * d as f64).round() as u32).to_le_bytes().to_vec(); + b.extend_from_slice(&d.to_le_bytes()); + b + }; + if let Some(s) = &info.make { + ifd0.push((0x010F, 2, s.len() as u32 + 1, text(s))); + } + if let Some(s) = &info.model { + ifd0.push((0x0110, 2, s.len() as u32 + 1, text(s))); + } + if let Some(v) = info.exposure_time { + exif.push((0x829A, 5, 1, rational(v))); + } + if let Some(v) = info.f_number { + exif.push((0x829D, 5, 1, rational(v))); + } + if let Some(v) = info.iso { + exif.push((0x8827, 3, 1, (v as u16).to_le_bytes().to_vec())); + } + if let Some(s) = &info.date_taken { + exif.push((0x9003, 2, s.len() as u32 + 1, text(s))); + } + if let Some(v) = info.focal_length { + exif.push((0x920A, 5, 1, rational(v))); + } + if let Some(v) = info.focal_length_35mm { + exif.push((0xA405, 3, 1, (v as u16).to_le_bytes().to_vec())); + } + if let Some(s) = &info.lens { + exif.push((0xA434, 2, s.len() as u32 + 1, text(s))); + } + let ifd_len = |n: usize| 2 + 12 * n + 4; + let ifd0_off = 8usize; + let n0 = ifd0.len() + 1; + let exif_off = ifd0_off + ifd_len(n0); + let mut data_off = exif_off + ifd_len(exif.len()); + let mut out = b"II*\0".to_vec(); + out.extend_from_slice(&(ifd0_off as u32).to_le_bytes()); + let mut data = Vec::new(); + let mut write_ifd = |out: &mut Vec, entries: &[(u16, u16, u32, Vec)], extra: Option| { + let n = entries.len() + usize::from(extra.is_some()); + out.extend_from_slice(&(n as u16).to_le_bytes()); + for (tag, typ, count, bytes) in entries { + out.extend_from_slice(&tag.to_le_bytes()); + out.extend_from_slice(&typ.to_le_bytes()); + out.extend_from_slice(&count.to_le_bytes()); + if bytes.len() <= 4 { + let mut f = bytes.clone(); + f.resize(4, 0); + out.extend_from_slice(&f); + } else { + out.extend_from_slice(&(data_off as u32).to_le_bytes()); + data.extend_from_slice(bytes); + data_off += bytes.len(); + } + } + if let Some(p) = extra { + out.extend_from_slice(&0x8769u16.to_le_bytes()); + out.extend_from_slice(&4u16.to_le_bytes()); + out.extend_from_slice(&1u32.to_le_bytes()); + out.extend_from_slice(&p.to_le_bytes()); + } + out.extend_from_slice(&0u32.to_le_bytes()); + }; + write_ifd(&mut out, &ifd0, Some(exif_off as u32)); + write_ifd(&mut out, &exif, None); + out.extend_from_slice(&data); + out +} diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index 1bc9660b8..ff2c530fa 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -19,6 +19,7 @@ //! HEIF, PSD and MRW files are read by their own crates. Put the blocks they return into an //! [`Embedded`] and call [`from_embedded`]. +pub mod camera_fields; mod container; pub mod cr3; mod datetime; diff --git a/crates/pcraft-format/Cargo.toml b/crates/pcraft-format/Cargo.toml index ce1d6873f..f54bb0c82 100644 --- a/crates/pcraft-format/Cargo.toml +++ b/crates/pcraft-format/Cargo.toml @@ -16,3 +16,6 @@ serde = { workspace = true } # the nesting against `Limits::max_depth` before parsing. serde_json = { workspace = true, features = ["unbounded_depth"] } thiserror = { workspace = true } + +# Same pure-Rust backend as the PhotoCraft source codec. MIT OR Apache-2.0. +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } diff --git a/crates/pcraft-format/README.md b/crates/pcraft-format/README.md index 5d8869b74..f5b0dfa73 100644 --- a/crates/pcraft-format/README.md +++ b/crates/pcraft-format/README.md @@ -5,7 +5,7 @@ references, the entry paths, the limits for reading a manifest, and the runner f migrations. The document model stays in PhotoCraft and is a type parameter here. The crate does not read ZIP archives, open files or decode tiles. No `unsafe`, no panics on malformed input, and it builds for `wasm32-unknown-unknown`. The dependencies are `serde`, `serde_json` -and `thiserror`. +`thiserror`, and `flate2`. ```toml craft-pcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "" } @@ -120,3 +120,15 @@ limits.max_manifest_bytes = 16 << 20; `.github/workflows/pcraft-format.yml` runs fmt, clippy and the tests on Linux and Windows, and a `wasm32-unknown-unknown` check. + +## Brush-tip envelope + +`tip::encode`, `tip::decode` and `tip::decode_with_max_side` share PhotoCraft's PCTIP1 +brush-tip codec: a six-byte magic, one-byte depth, little-endian width/height and zlib +samples. Eight-bit-compatible samples use 8 bits, otherwise 16-bit little-endian. +Dimensions, sample count, exact inflated length and caller preview side limits are checked. +Preset group schemas, tip hashes and storage policy remain application-owned. + +Ported from `storytold/photocraft` commit `19aec5eb44131de44361189d64910e7b3e19e57c`, +`crates/engine/src/preset_store.rs`. Original codec by Brandon Thomas (PhotoCraft #82), +with later bounds/backend/storage changes by David Mann and FinnNk, under MIT OR Apache-2.0. diff --git a/crates/pcraft-format/src/lib.rs b/crates/pcraft-format/src/lib.rs index 18868da2d..f12bc5213 100644 --- a/crates/pcraft-format/src/lib.rs +++ b/crates/pcraft-format/src/lib.rs @@ -36,6 +36,9 @@ mod limits; mod migrate; mod read; +/// Lossless brush-tip envelope used by PhotoCraft preset libraries. +pub mod tip; + use serde::{Deserialize, Serialize}; pub use limits::{ diff --git a/crates/pcraft-format/src/tip.rs b/crates/pcraft-format/src/tip.rs new file mode 100644 index 000000000..9b971f5d9 --- /dev/null +++ b/crates/pcraft-format/src/tip.rs @@ -0,0 +1,102 @@ +//! PhotoCraft's PCTIP1 brush-tip envelope and lossless grayscale sample serialization. +//! +//! Extracted from PhotoCraft preset_store.rs at 19aec5eb44131de44361189d64910e7b3e19e57c. +//! Original implementation: Brandon Thomas; subsequent codec changes: David Mann and FinnNk. +//! Group schemas, file naming, content hashes and storage policy remain with the application. + +/// Largest accepted tip side. +pub const MAX_SIDE: u32 = 16384; +/// Largest accepted sample count. +pub const MAX_SAMPLES: u64 = 1 << 26; +const TIP_MAGIC: &[u8; 6] = b"PCTIP1"; +const TIP_HEADER: usize = 6 + 1 + 4 + 4; + +/// Decoded grayscale samples. Eight-bit samples expand losslessly by multiplication by 257. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Tip { + pub width: u32, + pub height: u32, + pub data: Vec, +} + +/// `PCTIP1`, bits (8|16), width, height (u32 LE), then the zlib-deflated samples. +/// Returns an empty vector for invalid dimensions/sample counts or compression failure. +pub fn encode(width: u32, height: u32, data: &[u16]) -> Vec { + use std::io::Write; + let count = u64::from(width) * u64::from(height); + if width == 0 || height == 0 || width > MAX_SIDE || height > MAX_SIDE || count > MAX_SAMPLES || count != data.len() as u64 { + return Vec::new(); + } + let eight = data.iter().all(|v| v % 257 == 0); + let mut out = Vec::with_capacity(TIP_HEADER + data.len() / 4); + out.extend_from_slice(TIP_MAGIC); + out.push(if eight { 8 } else { 16 }); + out.extend_from_slice(&width.to_le_bytes()); + out.extend_from_slice(&height.to_le_bytes()); + // Level 3: zlib-rs level 1 writes static Huffman blocks only, 13-30% bigger than miniz's level 1. + let mut z = flate2::write::ZlibEncoder::new(out, flate2::Compression::new(3)); + let mut buf = Vec::with_capacity(8192); + let mut ok = true; + for chunk in data.chunks(4096) { + buf.clear(); + if eight { + buf.extend(chunk.iter().map(|v| (v / 257) as u8)); + } else { + for v in chunk { + buf.extend_from_slice(&v.to_le_bytes()); + } + } + ok &= z.write_all(&buf).is_ok(); + } + match z.finish() { + Ok(v) if ok => v, + _ => Vec::new(), + } +} + +/// Inverse of [`encode`]; any malformed, truncated or oversized input is an error. +pub fn decode(bytes: &[u8]) -> Result { + decode_with_max_side(bytes, MAX_SIDE) +} + +/// [`decode`] refusing (before inflating anything) a bitmap with a side over `max_side`. +pub fn decode_with_max_side(bytes: &[u8], max_side: u32) -> Result { + let head = bytes.get(..TIP_HEADER).ok_or("truncated tip header")?; + if &head[..6] != TIP_MAGIC { + return Err("not a PhotoCraft tip".into()); + } + let bits = head[6]; + let u32_at = |i: usize| u32::from_le_bytes([head[i], head[i + 1], head[i + 2], head[i + 3]]); + let (w, h) = (u32_at(7), u32_at(11)); + if w == 0 || h == 0 || w > MAX_SIDE.min(max_side) || h > MAX_SIDE.min(max_side) { + return Err(format!("bad tip size {w}×{h}")); + } + let n = u64::from(w) * u64::from(h); + if n > MAX_SAMPLES { + return Err(format!("tip too large ({w}×{h})")); + } + let bytes_per = match bits { + 8 => 1u64, + 16 => 2, + b => return Err(format!("unsupported tip depth {b}")), + }; + let want = n * bytes_per; + // One byte beyond the declared size detects overlong output, without allocating for + // an attacker-controlled expansion. Require the zlib trailer, not merely enough samples. + let mut raw = Vec::with_capacity(want as usize + 1); + let status = flate2::Decompress::new(true) + .decompress_vec(&bytes[TIP_HEADER..], &mut raw, flate2::FlushDecompress::Finish) + .map_err(|e| format!("tip data: {e}"))?; + if status != flate2::Status::StreamEnd { + return Err("tip data: truncated or oversized zlib stream".into()); + } + if raw.len() as u64 != want { + return Err(format!("tip data has {} bytes, expected {want}", raw.len())); + } + let data: Vec = if bits == 8 { + raw.iter().map(|&v| u16::from(v) * 257).collect() + } else { + raw.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect() + }; + Ok(Tip { width: w, height: h, data }) +} diff --git a/crates/pcraft-format/tests/tip.rs b/crates/pcraft-format/tests/tip.rs new file mode 100644 index 000000000..e54b44ac8 --- /dev/null +++ b/crates/pcraft-format/tests/tip.rs @@ -0,0 +1,58 @@ +use craft_pcraft_format::tip::{MAX_SAMPLES, MAX_SIDE, decode, decode_with_max_side, encode}; +use std::io::Write; + +fn fixture(bits: u8, width: u32, height: u32, samples: &[u8]) -> Vec { + let mut out = b"PCTIP1".to_vec(); + out.push(bits); + out.extend(width.to_le_bytes()); + out.extend(height.to_le_bytes()); + let mut z = flate2::write::ZlibEncoder::new(out, flate2::Compression::new(3)); + z.write_all(samples).unwrap(); + z.finish().unwrap() +} + +#[test] +fn legacy_eight_and_sixteen_bit_fixtures_and_roundtrips() { + for (bits, raw, samples) in [(8, vec![0, 128, 255], vec![0, 32896, 65535]), (16, vec![1, 0, 52, 18, 255, 255], vec![1, 0x1234, 65535])] { + let legacy = fixture(bits, 3, 1, &raw); + let tip = decode(&legacy).unwrap(); + assert_eq!((tip.width, tip.height), (3, 1)); + assert_eq!(tip.data, samples); + let encoded = encode(3, 1, &samples); + assert_eq!(&encoded[..15], &legacy[..15]); + assert_eq!(decode(&encoded).unwrap(), tip); + } +} + +#[test] +fn checks_headers_limits_and_sample_lengths() { + let good = fixture(8, 1, 1, &[255]); + for n in 0..good.len() { + assert!(decode(&good[..n]).is_err(), "truncation at {n}"); + } + let mut bad = good.clone(); + bad[0] = b'X'; + assert!(decode(&bad).is_err()); + for (bits, w, h) in [(7, 1, 1), (8, 0, 1), (8, 1, 0), (8, MAX_SIDE + 1, 1), (8, MAX_SIDE, MAX_SIDE)] { + assert!(decode(&fixture(bits, w, h, &[])).is_err()); + } + assert!(MAX_SAMPLES < u64::from(MAX_SIDE) * u64::from(MAX_SIDE)); + for raw in [vec![], vec![0, 1], vec![0; 10000]] { + assert!(decode(&fixture(8, 1, 1, &raw)).is_err()); + } + assert!(decode(&fixture(16, 1, 1, &[1])).is_err()); + let mut checksum = good; + *checksum.last_mut().unwrap() ^= 1; + assert!(decode(&checksum).is_err()); + assert!(encode(0, 1, &[]).is_empty()); + assert!(encode(1, 1, &[]).is_empty()); + assert!(encode(MAX_SIDE + 1, 1, &[0]).is_empty()); +} + +#[test] +fn preview_side_limit_is_checked_before_inflation() { + let good = encode(2, 1, &[0, 65535]); + assert!(decode_with_max_side(&good, 1).is_err()); + assert!(decode_with_max_side(&good, 2).is_ok()); + assert!(decode_with_max_side(&good, 0).is_err()); +} diff --git a/crates/plist/Cargo.toml b/crates/plist/Cargo.toml new file mode 100644 index 000000000..7ae95aae0 --- /dev/null +++ b/crates/plist/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-plist" +description = "Bounded cross-platform binary Apple property-list reader" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +thiserror.workspace = true diff --git a/crates/plist/LICENSE-APACHE b/crates/plist/LICENSE-APACHE new file mode 100644 index 000000000..f433b1a53 --- /dev/null +++ b/crates/plist/LICENSE-APACHE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/crates/plist/LICENSE-MIT b/crates/plist/LICENSE-MIT new file mode 100644 index 000000000..8e6399401 --- /dev/null +++ b/crates/plist/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 ArtCraft Team and the Craft Anything contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/plist/README.md b/crates/plist/README.md new file mode 100644 index 000000000..dc5c52007 --- /dev/null +++ b/crates/plist/README.md @@ -0,0 +1,11 @@ +# craft-plist + +A cross-platform bounded binary (`bplist00`) reader, extracted from Craft Anything's existing implementation and hardening history. Typed dictionaries retain order and duplicate keys; `get` returns the first match. Strings retain the source reader's lossy decoding policy. Shared references expand under node and decoded-byte budgets, and cycles are rejected. + +`parse_binary_with_limits` accepts explicit input, object, expanded-node, nesting and decoded-byte limits. Recursion always has a hard ceiling of 256 even if a caller supplies a larger depth. Counts are checked before container reservation; allocations can return a resource error. Limits are per parse, not a promise of a precise process RSS cap. + +This first PR contains reading only. XML parsing/writing and keyed-archive interpretation are not implemented. JSON projection, date formatting, OS file access and preference/locale selection are not part of this crate. Existing updater/Photo streaming `plist` users remain unchanged. + +Initial consumers: PhotoCraft's AppleLanguages binary-reader path; later Craft Anything binary parsing and LightCraft XML presets (after XML support/parity). Photo must retain its 4 MiB input policy and first-language selection. + +Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a`, `crates/formats/src/plist.rs` and checked byte reader. Source license files retained. Imported changes retain Brandon Thomas, BFlat, RRFZ and Afloat as primary Git authors; imported regression tests are BFlat's. No external specification implementation copied. diff --git a/crates/plist/src/binary.rs b/crates/plist/src/binary.rs new file mode 100644 index 000000000..3fe6aed92 --- /dev/null +++ b/crates/plist/src/binary.rs @@ -0,0 +1,391 @@ +use crate::{FormatError, Limits, bytes::Reader}; + +#[derive(Debug, Clone, PartialEq)] +pub enum Value { + Dict(Vec<(String, Value)>), + Array(Vec), + String(String), + Integer(i128), + Real(f64), + Bool(bool), + /// Seconds since 2001-01-01T00:00:00Z (binary), or the ISO text (XML). + Date(DateValue), + Data(Vec), + /// A keyed-archive object reference. + Uid(u64), + Null, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum DateValue { + Seconds(f64), + Text(String), +} + +impl Value { + pub fn get(&self, key: &str) -> Option<&Value> { + match self { + Value::Dict(d) => d.iter().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + + pub fn as_str(&self) -> Option<&str> { + match self { + Value::String(s) => Some(s), + _ => None, + } + } + + pub fn as_array(&self) -> Option<&[Value]> { + match self { + Value::Array(a) => Some(a), + _ => None, + } + } + + pub fn as_uid(&self) -> Option { + match self { + Value::Uid(u) => Some(*u), + _ => None, + } + } + + pub fn as_int(&self) -> Option { + match self { + Value::Integer(i) => Some(*i), + _ => None, + } + } +} +struct Bin<'a> { + r: Reader<'a>, + offsets: Vec, + ref_size: usize, + nodes: usize, + /// Data and text bytes still allowed to be copied out. + bytes_left: usize, + limits: Limits, +} + +/// Parse a `bplist00` binary property list. +pub fn parse_binary(data: &[u8]) -> Result { + parse_binary_with_limits(data, Limits::default()) +} + +/// Parse a binary property list, with caller-selected input, object, expansion and nesting limits. +pub fn parse_binary_with_limits(data: &[u8], limits: Limits) -> Result { + let (mut b, top) = reader(data, limits)?; + let mut stack = reserved(limits.max_depth.min(256))?; + b.object(top, &mut stack) +} + +fn reader(data: &[u8], limits: Limits) -> Result<(Bin<'_>, usize), FormatError> { + if data.len() > limits.max_input_bytes { + return Err(FormatError::TooLarge("input bytes".into())); + } + if !data.starts_with(b"bplist") { + return Err(FormatError::Malformed("not a binary plist".into())); + } + if !data.starts_with(b"bplist00") { + return Err(FormatError::Unsupported("unknown binary plist version".into())); + } + let len = data.len(); + if len < 8 + 32 { + return Err(FormatError::Truncated("binary plist trailer".into())); + } + let r = Reader::new(data); + let t = len - 32; + let off_size = usize::from(r.u8(t + 6).unwrap_or(0)); + let ref_size = usize::from(r.u8(t + 7).unwrap_or(0)); + let num = r.u64_be(t + 8).unwrap_or(0); + let top = r.u64_be(t + 16).unwrap_or(0); + let table = r.u64_be(t + 24).unwrap_or(0); + if !(1..=8).contains(&off_size) || !(1..=8).contains(&ref_size) { + return Err(FormatError::Malformed("bad offset or reference size".into())); + } + let num = usize::try_from(num).map_err(|_| FormatError::Malformed("object count".into()))?; + let table = usize::try_from(table).map_err(|_| FormatError::Malformed("offset table".into()))?; + if num == 0 || num > len { + return Err(FormatError::Malformed(format!("{num} objects in a {len}-byte file"))); + } + if num > limits.max_objects { + return Err(FormatError::TooLarge("object count".into())); + } + let need = num.checked_mul(off_size).and_then(|n| n.checked_add(table)).ok_or_else(|| FormatError::Malformed("offset table".into()))?; + if need > t || table < 8 { + return Err(FormatError::Truncated("offset table runs past the trailer".into())); + } + let mut offsets = reserved(num)?; + for i in 0..num { + let o = read_be(&r, table + i * off_size, off_size).ok_or_else(|| FormatError::Truncated("offset table".into()))?; + let o = usize::try_from(o).map_err(|_| FormatError::Malformed("object offset".into()))?; + if o < 8 || o >= table { + return Err(FormatError::Malformed(format!("object {i} lies outside the object area"))); + } + offsets.push(o); + } + let top = usize::try_from(top).map_err(|_| FormatError::Malformed("top object".into()))?; + // Object payloads and references must not borrow bytes from the offset table or trailer. + let objects = data.get(..table).ok_or_else(|| FormatError::Truncated("object area".into()))?; + let b = Bin { r: Reader::new(objects), offsets, ref_size, nodes: 0, bytes_left: limits.max_expanded_bytes, limits }; + Ok((b, top)) +} + +fn read_be(r: &Reader<'_>, off: usize, n: usize) -> Option { + let s = r.slice(off, n)?; + Some(s.iter().fold(0u64, |acc, &b| (acc << 8) | u64::from(b))) +} + +/// UTF-8 size of `String::from_utf8_lossy(bytes)`: each damaged sequence becomes a U+FFFD. +fn lossy_utf8_len(bytes: &[u8]) -> usize { + bytes.utf8_chunks().fold(0usize, |n, c| { + let replacement = if c.invalid().is_empty() { 0 } else { char::REPLACEMENT_CHARACTER.len_utf8() }; + n.saturating_add(c.valid().len()).saturating_add(replacement) + }) +} + +/// UTF-8 size of `String::from_utf16_lossy` over big-endian code units: a lone surrogate becomes +/// a U+FFFD. +fn lossy_utf16_len(bytes: &[u8]) -> usize { + let units = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c)); + char::decode_utf16(units).fold(0usize, |n, c| n.saturating_add(c.map_or(char::REPLACEMENT_CHARACTER.len_utf8(), char::len_utf8))) +} + +impl Bin<'_> { + fn count(&self, at: usize, low: u8) -> Result<(usize, usize), FormatError> { + if low != 0xf { + return Ok((usize::from(low), at + 1)); + } + let m = self.r.u8(at + 1).ok_or_else(|| FormatError::Truncated("length".into()))?; + if m & 0xf0 != 0x10 { + return Err(FormatError::Malformed("bad extended length marker".into())); + } + if m & 0x0f > 3 { + return Err(FormatError::Malformed("extended length width exceeds 64 bits".into())); + } + let n = 1usize << (m & 0x0f); + let v = read_be(&self.r, at + 2, n).ok_or_else(|| FormatError::Truncated("length".into()))?; + let v = usize::try_from(v).map_err(|_| FormatError::Malformed("length".into()))?; + Ok((v, at + 2 + n)) + } + + fn reference(&self, at: usize, i: usize) -> Result { + let off = i.checked_mul(self.ref_size).and_then(|x| x.checked_add(at)).ok_or_else(|| FormatError::Malformed("reference".into()))?; + let v = read_be(&self.r, off, self.ref_size).ok_or_else(|| FormatError::Truncated("object reference".into()))?; + usize::try_from(v).map_err(|_| FormatError::Malformed("reference".into())) + } + + /// Refuse a container whose `children` would pass the node limit, before space is reserved for them. + fn room_for_nodes(&self, children: usize) -> Result<(), FormatError> { + if children > self.limits.max_nodes.saturating_sub(self.nodes) { + return Err(FormatError::TooLarge("expanded node count".into())); + } + Ok(()) + } + + /// Spend `n` bytes of the data and text budget before copying them out. + fn spend(&mut self, n: usize) -> Result<(), FormatError> { + self.bytes_left = + self.bytes_left.checked_sub(n).ok_or_else(|| FormatError::TooLarge("repeated data and text expand past the plist byte budget".into()))?; + Ok(()) + } + + /// Read object `idx`. `stack` holds the containers on the current path, so a cycle is refused. + fn object(&mut self, idx: usize, stack: &mut Vec) -> Result { + self.nodes += 1; + if self.nodes > self.limits.max_nodes { + return Err(FormatError::TooLarge("expanded node count".into())); + } + if stack.len() >= self.limits.max_depth.min(256) { + return Err(FormatError::Malformed("nesting too deep".into())); + } + if stack.contains(&idx) { + return Err(FormatError::Malformed(format!("object {idx} contains itself"))); + } + let at = *self.offsets.get(idx).ok_or_else(|| FormatError::Malformed(format!("reference to missing object {idx}")))?; + let m = self.r.u8(at).ok_or_else(|| FormatError::Truncated("object marker".into()))?; + let (hi, lo) = (m >> 4, m & 0x0f); + let v = match hi { + 0x0 => match lo { + 0x0 => Value::Null, + 0x8 => Value::Bool(false), + 0x9 => Value::Bool(true), + _ => return Err(FormatError::Malformed(format!("unknown simple plist object {m:#04x}"))), + }, + 0x1 => { + if lo > 4 { + return Err(FormatError::Malformed("integer width exceeds 128 bits".into())); + } + let n = 1usize << lo; + let bytes = self.r.slice(at + 1, n).ok_or_else(|| FormatError::Truncated("integer".into()))?; + let v = match n { + 8 => i128::from(i64::from_be_bytes(bytes.try_into().map_err(|_| FormatError::Truncated("integer".into()))?)), + 16 => i128::from_be_bytes(bytes.try_into().map_err(|_| FormatError::Truncated("integer".into()))?), + _ => i128::from(bytes.iter().fold(0u64, |acc, &b| (acc << 8) | u64::from(b))), + }; + Value::Integer(v) + } + 0x2 => { + let v = match lo { + 2 => self.r.u32_be(at + 1).map(|b| f64::from(f32::from_bits(b))), + 3 => self.r.u64_be(at + 1).map(f64::from_bits), + _ => None, + }; + Value::Real(v.ok_or_else(|| FormatError::Truncated("real".into()))?) + } + 0x3 if lo == 3 => { + Value::Date(DateValue::Seconds(self.r.u64_be(at + 1).map(f64::from_bits).ok_or_else(|| FormatError::Truncated("date".into()))?)) + } + 0x4 => { + let (n, s) = self.count(at, lo)?; + let bytes = self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("data".into()))?; + self.spend(bytes.len())?; + { + let mut out = reserved(bytes.len())?; + out.extend_from_slice(bytes); + Value::Data(out) + } + } + 0x5 => { + let (n, s) = self.count(at, lo)?; + let bytes = self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("string".into()))?; + let size = lossy_utf8_len(bytes); + self.spend(size)?; + let mut out = reserved_string(size)?; + for chunk in bytes.utf8_chunks() { + out.push_str(chunk.valid()); + if !chunk.invalid().is_empty() { + out.push(char::REPLACEMENT_CHARACTER); + } + } + Value::String(out) + } + 0x6 => { + let (n, s) = self.count(at, lo)?; + let bytes = self + .r + .slice(s, n.checked_mul(2).ok_or_else(|| FormatError::Malformed("string".into()))?) + .ok_or_else(|| FormatError::Truncated("string".into()))?; + let size = lossy_utf16_len(bytes); + self.spend(size)?; + let mut out = reserved_string(size)?; + let units = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c)); + for ch in char::decode_utf16(units) { + out.push(ch.unwrap_or(char::REPLACEMENT_CHARACTER)); + } + Value::String(out) + } + 0x8 => { + if lo > 7 { + return Err(FormatError::Malformed("uid width exceeds 64 bits".into())); + } + let n = usize::from(lo) + 1; + Value::Uid(read_be(&self.r, at + 1, n).ok_or_else(|| FormatError::Truncated("uid".into()))?) + } + 0xa | 0xc => { + let (n, s) = self.count(at, lo)?; + // Writers store equal values once, so an array may name one object many times: + // only its references have to fit in the file. the node limit and the byte budget bound + // the expansion. + if s.saturating_add(n.saturating_mul(self.ref_size)) > self.r.len() { + return Err(FormatError::Malformed("array larger than the file".into())); + } + self.room_for_nodes(n)?; + stack.push(idx); + let mut out = reserved(n)?; + for i in 0..n { + let child = self.reference(s, i)?; + out.push(self.object(child, stack)?); + } + stack.pop(); + Value::Array(out) + } + 0xd => { + let (n, s) = self.count(at, lo)?; + if s.saturating_add(n.saturating_mul(self.ref_size).saturating_mul(2)) > self.r.len() { + return Err(FormatError::Malformed("dictionary larger than the file".into())); + } + self.room_for_nodes(n.saturating_mul(2))?; + stack.push(idx); + let mut out = reserved(n)?; + for i in 0..n { + let k = self.reference(s, i)?; + let v = self.reference(s, i + n)?; + let key = match self.object(k, stack)? { + Value::String(s) => s, + _ => return Err(FormatError::Unsupported("non-string dictionary keys cannot be represented in this plist view".into())), + }; + out.push((key, self.object(v, stack)?)); + } + stack.pop(); + Value::Dict(out) + } + _ => return Err(FormatError::Malformed(format!("unknown object marker {m:#04x}"))), + }; + Ok(v) + } +} + +fn reserved(n: usize) -> Result, FormatError> { + let mut v = Vec::new(); + v.try_reserve_exact(n).map_err(|_| FormatError::TooLarge("allocation".into()))?; + Ok(v) +} + +fn reserved_string(n: usize) -> Result { + let mut s = String::new(); + s.try_reserve_exact(n).map_err(|_| FormatError::TooLarge("string allocation".into()))?; + Ok(s) +} + +/// Select the first string in one top-level dictionary array without expanding unrelated values. +/// First matching dictionary key wins. Missing/wrong-typed values return `None`; malformed +/// selected objects and resource exhaustion return errors. Unselected value graphs are not +/// validated. This permits preference readers to ignore unrelated future object kinds. +pub fn first_string_in_array(data: &[u8], key: &str, limits: Limits) -> Result, FormatError> { + let (mut b, top) = reader(data, limits)?; + let (kind, n, start) = b.header(top)?; + if kind != 0xd { + return Ok(None); + } + if n > limits.max_nodes { + return Err(FormatError::TooLarge("dictionary lookup entries".into())); + } + let bytes = n.checked_mul(2).and_then(|n| n.checked_mul(b.ref_size)).ok_or_else(|| FormatError::Malformed("dictionary references".into()))?; + b.r.slice(start, bytes).ok_or_else(|| FormatError::Truncated("dictionary references".into()))?; + for i in 0..n { + let k = b.reference(start, i)?; + if b.selected_string(k)?.as_deref() != Some(key) { + continue; + } + let value = b.reference(start, n + i)?; + let (kind, count, payload) = b.header(value)?; + if kind != 0xa || count == 0 { + return Ok(None); + } + let first = b.reference(payload, 0)?; + return b.selected_string(first); + } + Ok(None) +} +impl Bin<'_> { + fn header(&self, idx: usize) -> Result<(u8, usize, usize), FormatError> { + let at = *self.offsets.get(idx).ok_or_else(|| FormatError::Malformed("missing object".into()))?; + let marker = self.r.u8(at).ok_or_else(|| FormatError::Truncated("object marker".into()))?; + let kind = marker >> 4; + // Only containers and strings use an extended length. Unknown types are not traversed. + let (n, start) = if matches!(kind, 0x5 | 0x6 | 0xa | 0xd) { self.count(at, marker & 15)? } else { (usize::from(marker & 15), at + 1) }; + Ok((kind, n, start)) + } + fn selected_string(&mut self, idx: usize) -> Result, FormatError> { + let (kind, _, _) = self.header(idx)?; + if !matches!(kind, 0x5 | 0x6) { + return Ok(None); + } + match self.object(idx, &mut Vec::new())? { + Value::String(s) => Ok(Some(s)), + _ => Ok(None), + } + } +} diff --git a/crates/plist/src/bytes.rs b/crates/plist/src/bytes.rs new file mode 100644 index 000000000..bdfecc668 --- /dev/null +++ b/crates/plist/src/bytes.rs @@ -0,0 +1,37 @@ +//! Checked little/big-endian reads over untrusted bytes. Every read returns `None` when out of range. + +#[derive(Debug, Clone, Copy)] +pub struct Reader<'a> { + data: &'a [u8], +} + +impl<'a> Reader<'a> { + pub fn new(data: &'a [u8]) -> Self { + Reader { data } + } + + pub fn len(&self) -> usize { + self.data.len() + } + + pub fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> { + let end = off.checked_add(len)?; + self.data.get(off..end) + } + + pub fn u8(&self, off: usize) -> Option { + self.data.get(off).copied() + } + + fn arr(&self, off: usize) -> Option<[u8; N]> { + self.slice(off, N)?.try_into().ok() + } + + pub fn u32_be(&self, off: usize) -> Option { + self.arr(off).map(u32::from_be_bytes) + } + + pub fn u64_be(&self, off: usize) -> Option { + self.arr(off).map(u64::from_be_bytes) + } +} diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs new file mode 100644 index 000000000..f3af79ddc --- /dev/null +++ b/crates/plist/src/lib.rs @@ -0,0 +1,33 @@ +//! Bounded binary Apple property lists, independent of operating-system preferences. +//! XML and writing are intentionally not implemented in this first extraction. +//! File discovery, locale selection, bundle validation and application policy stay in callers. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +mod binary; +mod bytes; +pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; + +/// Caller-selected resource limits. Zero values refuse the corresponding resource. +#[derive(Clone, Copy, Debug)] +pub struct Limits { + pub max_input_bytes: usize, + pub max_objects: usize, + pub max_nodes: usize, + pub max_depth: usize, + pub max_expanded_bytes: usize, +} +impl Default for Limits { + fn default() -> Self { + Self { max_input_bytes: 16 << 20, max_objects: 100_000, max_nodes: 100_000, max_depth: 64, max_expanded_bytes: 64 << 20 } + } +} +#[derive(Debug, thiserror::Error)] +pub enum FormatError { + #[error("malformed property list: {0}")] + Malformed(String), + #[error("truncated property list: {0}")] + Truncated(String), + #[error("unsupported property list: {0}")] + Unsupported(String), + #[error("property list resource limit: {0}")] + TooLarge(String), +} diff --git a/crates/plist/tests/limits.rs b/crates/plist/tests/limits.rs new file mode 100644 index 000000000..dc1882698 --- /dev/null +++ b/crates/plist/tests/limits.rs @@ -0,0 +1,75 @@ +use craft_plist::{Limits, Value, parse_binary, parse_binary_with_limits}; +fn plist(objects: &[&[u8]]) -> Vec { + let mut b = b"bplist00".to_vec(); + let mut offsets = Vec::new(); + for obj in objects { + offsets.push(b.len() as u8); + b.extend_from_slice(obj); + } + let table = b.len() as u64; + b.extend_from_slice(&offsets); + b.extend_from_slice(&[0, 0, 0, 0, 0, 0, 1, 1]); + b.extend_from_slice(&(objects.len() as u64).to_be_bytes()); + b.extend_from_slice(&0u64.to_be_bytes()); + b.extend_from_slice(&table.to_be_bytes()); + b +} +#[test] +fn languages_and_every_truncation() { + let b = plist(&[&[0xd1, 1, 2], b"\x5eAppleLanguages", &[0xa1, 3], b"\x55ja-JP"]); + let p = parse_binary(&b).unwrap(); + assert_eq!(p.get("AppleLanguages").unwrap().as_array().unwrap()[0].as_str(), Some("ja-JP")); + for n in 0..b.len() { + assert!(parse_binary(&b[..n]).is_err(), "prefix {n}"); + } +} +#[test] +fn cycles_and_repeated_references() { + assert!(parse_binary(&plist(&[&[0xa1, 0]])).is_err()); + let b = plist(&[&[0xa3, 1, 1, 1], b"\x51x"]); + assert_eq!(parse_binary(&b).unwrap(), Value::Array(vec![Value::String("x".into()); 3])); + assert!(parse_binary_with_limits(&b, Limits { max_nodes: 3, ..Limits::default() }).is_err()); + assert!(parse_binary_with_limits(&b, Limits { max_expanded_bytes: 2, ..Limits::default() }).is_err()); +} +#[test] +fn explicit_limits_and_version() { + let b = plist(&[&[0xa1, 1], b"\x51x"]); + for l in [ + Limits { max_input_bytes: b.len() - 1, ..Limits::default() }, + Limits { max_objects: 1, ..Limits::default() }, + Limits { max_depth: 1, ..Limits::default() }, + Limits { max_nodes: 0, ..Limits::default() }, + ] { + assert!(parse_binary_with_limits(&b, l).is_err()); + } + let mut future = b; + future[7] = b'1'; + assert!(parse_binary(&future).is_err()); +} +#[test] +fn utf16_and_lossy_expansion_spend_budget() { + let b = plist(&[&[0x62, 0xd8, 0x3d, 0xde, 0x00]]); + assert_eq!(parse_binary(&b).unwrap().as_str(), Some("😀")); + assert!(parse_binary_with_limits(&b, Limits { max_expanded_bytes: 3, ..Limits::default() }).is_err()); + let bad = plist(&[&[0x51, 0xff]]); + assert_eq!(parse_binary(&bad).unwrap().as_str(), Some("�")); + assert!(parse_binary_with_limits(&bad, Limits { max_expanded_bytes: 2, ..Limits::default() }).is_err()); +} + +#[test] +fn selective_lookup_ignores_unrelated_graphs_and_preserves_first_match() { + use craft_plist::first_string_in_array; + let b = plist(&[&[0xd2, 1, 2, 3, 4], b"\x55Other", b"\x5eAppleLanguages", &[0xaf], &[0xa2, 5, 6], b"\x55ja-JP", &[0xff]]); + assert!(parse_binary(&b).is_err()); + assert_eq!(first_string_in_array(&b, "AppleLanguages", Limits::default()).unwrap().as_deref(), Some("ja-JP")); + let duplicate = plist(&[&[0xd2, 1, 1, 2, 3], b"\x5eAppleLanguages", &[0xa1, 4], &[0xa1, 5], b"\x55zh-TW", b"\x55ko-KR"]); + assert_eq!(first_string_in_array(&duplicate, "AppleLanguages", Limits::default()).unwrap().as_deref(), Some("zh-TW")); + assert!(first_string_in_array(&b, "AppleLanguages", Limits { max_expanded_bytes: 1, ..Limits::default() }).is_err()); +} + +#[test] +fn selective_lookup_requires_an_array_not_a_set() { + use craft_plist::first_string_in_array; + let b = plist(&[&[0xd1, 1, 2], b"\x5eAppleLanguages", &[0xc1, 3], b"\x55ja-JP"]); + assert_eq!(first_string_in_array(&b, "AppleLanguages", Limits::default()).unwrap(), None); +} diff --git a/crates/plist/tests/plist_marker_widths.rs b/crates/plist/tests/plist_marker_widths.rs new file mode 100644 index 000000000..5873371e7 --- /dev/null +++ b/crates/plist/tests/plist_marker_widths.rs @@ -0,0 +1,47 @@ +use craft_plist::{Value, parse_binary}; + +fn single_object(object: &[u8]) -> Vec { + let mut data = b"bplist00".to_vec(); + data.extend_from_slice(object); + let table = data.len() as u64; + data.push(8); + data.extend_from_slice(&[0, 0, 0, 0, 0, 0, 1, 1]); + data.extend_from_slice(&1u64.to_be_bytes()); + data.extend_from_slice(&0u64.to_be_bytes()); + data.extend_from_slice(&table.to_be_bytes()); + data +} + +#[test] +fn unsupported_integer_widths_are_not_clamped() { + for marker in 0x15..=0x1f { + let mut object = vec![marker]; + object.extend_from_slice(&[0; 16]); + assert!(parse_binary(&single_object(&object)).is_err(), "accepted {marker:#x}"); + } +} + +#[test] +fn extended_lengths_and_uids_do_not_lose_high_bytes() { + let mut length = vec![0x4f, 0x14]; + length.extend_from_slice(&[0; 16]); + assert!(parse_binary(&single_object(&length)).is_err()); + for marker in 0x88..=0x8f { + let mut object = vec![marker, 1]; + object.resize(usize::from(marker & 15) + 2, 0); + assert!(parse_binary(&single_object(&object)).is_err(), "accepted {marker:#x}"); + } +} + +#[test] +fn supported_maximum_widths_keep_their_values() { + let mut integer = vec![0x14]; + integer.extend_from_slice(&i128::MAX.to_be_bytes()); + assert!(matches!(parse_binary(&single_object(&integer)), Ok(Value::Integer(i)) if i == i128::MAX)); + let mut uid = vec![0x87]; + uid.extend_from_slice(&u64::MAX.to_be_bytes()); + assert!(matches!(parse_binary(&single_object(&uid)), Ok(Value::Uid(i)) if i == u64::MAX)); + let mut empty = vec![0x4f, 0x13]; + empty.extend_from_slice(&0u64.to_be_bytes()); + assert!(matches!(parse_binary(&single_object(&empty)), Ok(Value::Data(d)) if d.is_empty())); +} diff --git a/crates/plist/tests/plist_object_bounds.rs b/crates/plist/tests/plist_object_bounds.rs new file mode 100644 index 000000000..d35fd91b1 --- /dev/null +++ b/crates/plist/tests/plist_object_bounds.rs @@ -0,0 +1,31 @@ +use craft_plist::{Value, parse_binary}; + +fn single_object(object: &[u8], offset: u8) -> Vec { + let mut data = b"bplist00".to_vec(); + data.extend_from_slice(object); + let table = data.len() as u64; + data.push(offset); + data.extend_from_slice(&[0, 0, 0, 0, 0, 0, 1, 1]); + data.extend_from_slice(&1u64.to_be_bytes()); + data.extend_from_slice(&0u64.to_be_bytes()); + data.extend_from_slice(&table.to_be_bytes()); + data +} + +#[test] +fn binary_values_cannot_consume_the_offset_table_or_trailer() { + for object in [&[0x54, b'x'][..], &[0x43, 0x42], &[0x13], &[0x87], &[0x62, 0, b'x']] { + assert!(parse_binary(&single_object(object, 8)).is_err(), "accepted object {object:02x?}"); + } +} + +#[test] +fn binary_offsets_cannot_point_inside_the_file_header() { + assert!(parse_binary(&single_object(&[0x00], 0)).is_err()); + assert!(matches!(parse_binary(&single_object(&[0x00], 8)), Ok(Value::Null))); +} + +#[test] +fn objects_ending_at_the_table_boundary_are_valid() { + assert!(matches!(parse_binary(&single_object(&[0x51, b'x'], 8)), Ok(Value::String(s)) if s == "x")); +} diff --git a/crates/print/src/cups/mod.rs b/crates/print/src/cups/mod.rs new file mode 100644 index 000000000..1a0a9360b --- /dev/null +++ b/crates/print/src/cups/mod.rs @@ -0,0 +1,955 @@ +//! Based on the direct CUPS transport from contributor PR #156 (ahmedhosnypro), +//! kept separate from the broader network-printing registry in PR #116. +//! IPP/1.1 straight to the local CUPS server: printer discovery (`CUPS-Get-Printers`, +//! `CUPS-Get-Default`) and job submission (`Print-Job`) over HTTP/1.1, for when `lpstat`/`lp` +//! are missing (a sandboxed install, where the print dialog would otherwise have no printers). +//! Implemented from the public IPP encoding (RFC 2910/8010) and semantics (RFC 8011) and +//! CUPS's IPP documentation; the scheduler is reached on its socket or selected TCP endpoint. +//! +//! # Blocking and authorization +//! The caller authorizes endpoint selection. The absolute exchange deadline covers all request +//! writes and reply reads **after connection**. Standard-library DNS resolution and Unix socket +//! connection are blocking OS calls outside that deadline. TCP connection attempts have their +//! own timeout. This is not a whole-operation deadline API: do not call it on a UI thread or +//! promise bounded completion for arbitrary named hosts/Unix listeners. No detached timeout +//! thread is spawned; callers needing cancellation must supply a separately bounded transport. + +#[cfg(all(test, unix))] +mod tests; + +use super::{Duplex, Error, Job, Printer, Scaling}; +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; +#[cfg(unix)] +use std::os::unix::net::UnixStream; +use std::path::PathBuf; +use std::sync::atomic::{AtomicU32, Ordering}; +use std::time::{Duration, Instant}; + +/// Where the local scheduler listens: its socket, else its loopback port. +const CUPS_SOCKET: &str = "/run/cups/cups.sock"; + +/// Caps on what the server may hand back: a reply is read into memory before it is parsed. +const MAX_BODY: usize = 1 << 20; +const MAX_HEADERS: usize = 8 << 10; +const MAX_PRINTERS: usize = 256; +const MAX_NAME: usize = 256; +/// A job's title on the wire; IPP values are u16-length, and no real title is this long. +const MAX_JOB_NAME: usize = 255; + +/// A wedged scheduler must not hang the dialog; a job may carry megabytes and is given longer. +const CONNECT_TIMEOUT: Duration = Duration::from_secs(1); +const DISCOVERY_TIMEOUT: Duration = Duration::from_secs(2); +const JOB_TIMEOUT: Duration = Duration::from_secs(30); + +/// IPP operation codes: the standard Print-Job, and CUPS's discovery extensions. +const OPERATION_PRINT_JOB: u16 = 0x0002; +const OPERATION_CUPS_GET_DEFAULT: u16 = 0x4001; +const OPERATION_CUPS_GET_PRINTERS: u16 = 0x4002; + +/// IPP value tags and group delimiters (RFC 8010 §5.1); the ones this client sends or reads. +const GROUP_OPERATION: u8 = 0x01; +const GROUP_JOB: u8 = 0x02; +const GROUP_END: u8 = 0x03; +const GROUP_PRINTER: u8 = 0x04; +const TAG_INTEGER: u8 = 0x21; +const TAG_BOOLEAN: u8 = 0x22; +const TAG_NAME: u8 = 0x42; +const TAG_KEYWORD: u8 = 0x44; +const TAG_URI: u8 = 0x45; +const TAG_CHARSET: u8 = 0x47; +const TAG_LANGUAGE: u8 = 0x48; +const TAG_MIME: u8 = 0x49; + +static REQUEST_ID: AtomicU32 = AtomicU32::new(1); + +/// How the scheduler is reached: its unix socket, else its loopback port. +#[derive(Clone, Debug)] +pub enum Server { + Unix(PathBuf), + Tcp(SocketAddr), + /// A caller-authorized host. DNS is resolved once for each connection attempt. + Named(String, u16), +} + +impl Server { + /// Parse an explicitly selected CUPS_SERVER value, without connecting. No URLs, + /// credentials, control characters or HTTP authority delimiters are accepted. + pub fn parse(value: &str) -> Result { + if value.starts_with('/') { + return Ok(Self::Unix(value.into())); + } + if value.is_empty() || value.len() > 255 || value.bytes().any(|c| c.is_ascii_control() || c.is_ascii_whitespace() || b"/@?#\\".contains(&c)) { + return Err(Error::Spool("invalid CUPS server".into())); + } + let (host, port) = if let Some(rest) = value.strip_prefix('[') { + let (host, tail) = rest.split_once(']').ok_or_else(|| Error::Spool("invalid CUPS IPv6 host".into()))?; + if !host.contains(':') { + return Err(Error::Spool("bracketed CUPS host must be IPv6".into())); + } + let port = if tail.is_empty() { + 631 + } else { + tail.strip_prefix(':') + .and_then(|p| p.parse::().ok()) + .filter(|p| *p != 0) + .ok_or_else(|| Error::Spool("invalid CUPS port".into()))? + }; + (host, port) + } else if value.matches(':').count() > 1 { + // A bare IPv6 address is a host. A colon after its scope name may + // specify a port without cutting an address hextet in half. + if let Some((_, zone)) = value.split_once('%') + && zone.contains(':') + { + let (host, port) = value.rsplit_once(':').ok_or_else(|| Error::Spool("invalid CUPS endpoint".into()))?; + (host, port.parse::().ok().filter(|p| *p != 0).ok_or_else(|| Error::Spool("invalid CUPS port".into()))?) + } else { + (value, 631) + } + } else { + match value.rsplit_once(':') { + Some((host, port)) => (host, port.parse::().ok().filter(|p| *p != 0).ok_or_else(|| Error::Spool("invalid CUPS port".into()))?), + None => (value, 631), + } + }; + if host.is_empty() || host.contains(['[', ']']) { + return Err(Error::Spool("invalid CUPS host".into())); + } + if let Ok(ip) = host.parse::() { + return Ok(Self::Tcp(SocketAddr::new(ip, port))); + } + if host.contains(':') { + let (ip, zone) = host.split_once('%').ok_or_else(|| Error::Spool("invalid CUPS IPv6 host".into()))?; + if ip.parse::().is_err() + || zone.is_empty() + || !zone.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.')) + { + return Err(Error::Spool("invalid CUPS IPv6 scope".into())); + } + } else if host.contains('%') { + return Err(Error::Spool("invalid CUPS host".into())); + } + Ok(Self::Named(host.into(), port)) + } + + fn authority(&self) -> String { + match self { + Self::Unix(_) => "localhost".into(), + Self::Tcp(addr) => addr.to_string().replace('%', "%25"), + Self::Named(host, port) if host.contains(':') => format!("[{}]:{port}", host.replace('%', "%25")), + Self::Named(host, port) => format!("{host}:{port}"), + } + } +} + +/// Local scheduler candidates, honoring the user's explicit CUPS_SERVER setting. +/// No requests are made by this function; apps must authorize selection before use. +pub fn configured_servers() -> Result, Error> { + if let Some(value) = std::env::var_os("CUPS_SERVER").filter(|v| !v.is_empty()) { + return Ok(vec![Server::parse(&value.to_string_lossy())?]); + } + Ok(vec![ + Server::Unix(CUPS_SOCKET.into()), + Server::Unix("/var/run/cups/cups.sock".into()), + Server::Unix("/private/var/run/cupsd".into()), + Server::Tcp(([127, 0, 0, 1], 631).into()), + ]) +} + +fn endpoints() -> [Server; 2] { + [Server::Unix(PathBuf::from(CUPS_SOCKET)), Server::Tcp(([127, 0, 0, 1], 631).into())] +} + +/// Whether the local CUPS server is reachable: its socket, else its loopback port. A missing +/// scheduler is a normal `false`, not an error. +pub fn cups_reachable() -> bool { + endpoints().iter().any(reachable) +} + +/// Probe a caller-authorized endpoint without sending a print request. +/// DNS and Unix connect have the blocking limitations documented on this module. +pub fn reachable(server: &Server) -> bool { + connect(server).is_ok() +} + +fn connect(server: &Server) -> std::io::Result { + match server { + #[cfg(unix)] + Server::Unix(path) => UnixStream::connect(path).map(Stream::Unix), + #[cfg(not(unix))] + Server::Unix(_) => Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "Unix sockets unavailable")), + Server::Named(host, port) => { + // Validate even directly constructed public variants before resolving or writing headers. + let endpoint = if host.contains(':') { format!("[{host}]:{port}") } else { format!("{host}:{port}") }; + let parsed = Server::parse(&endpoint).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?; + if matches!(parsed, Server::Unix(_)) { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "a named CUPS host cannot be a Unix path")); + } + let addr = (host.as_str(), *port) + .to_socket_addrs()? + .next() + .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "CUPS host has no address"))?; + TcpStream::connect_timeout(&addr, CONNECT_TIMEOUT).map(Stream::Tcp) + } + // A loopback connect either answers promptly or the server is not there. + Server::Tcp(addr) => TcpStream::connect_timeout(addr, CONNECT_TIMEOUT).map(|s| { + let _ = s.set_nodelay(true); + Stream::Tcp(s) + }), + } +} + +/// A connection to the scheduler. +enum Stream { + #[cfg(unix)] + Unix(UnixStream), + Tcp(TcpStream), +} + +impl Stream { + fn set_timeouts(&self, timeout: Duration) -> std::io::Result<()> { + let (read, write) = match self { + #[cfg(unix)] + Stream::Unix(s) => (s.set_read_timeout(Some(timeout)), s.set_write_timeout(Some(timeout))), + Stream::Tcp(s) => (s.set_read_timeout(Some(timeout)), s.set_write_timeout(Some(timeout))), + }; + read.and(write) + } +} + +impl Read for Stream { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self { + #[cfg(unix)] + Stream::Unix(s) => s.read(buf), + Stream::Tcp(s) => s.read(buf), + } + } +} + +impl Write for Stream { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + #[cfg(unix)] + Stream::Unix(s) => s.write(buf), + Stream::Tcp(s) => s.write(buf), + } + } + fn flush(&mut self) -> std::io::Result<()> { + match self { + #[cfg(unix)] + Stream::Unix(s) => s.flush(), + Stream::Tcp(s) => s.flush(), + } + } +} + +/// The server's queues (CUPS-Get-Printers) and its default destination (CUPS-Get-Default), for +/// the Print dialog when `lpstat` is missing. A default the queue list lacks is appended, so +/// the dialog always has a selection to restore; a default that cannot be named still leaves +/// the queues usable. +pub fn printers() -> Result, Error> { + printers_at(&endpoints()) +} + +pub fn printers_at(endpoints: &[Server]) -> Result, Error> { + let queues = discover(endpoints, OPERATION_CUPS_GET_PRINTERS)?; + let default = discover(endpoints, OPERATION_CUPS_GET_DEFAULT).ok().and_then(|names| names.into_iter().next()); + let mut printers: Vec = Vec::new(); + for name in queues { + if printers.len() >= MAX_PRINTERS { + break; + } + if !printers.iter().any(|p| p.name == name) { + printers.push(Printer { description: String::new(), default: default.as_deref() == Some(name.as_str()), name, two_sided: None }); + } + } + if let Some(name) = default + && !printers.iter().any(|p| p.name == name) + { + printers.push(Printer { description: String::new(), name, default: true, two_sided: None }); + } + Ok(printers) +} + +/// The printer names a discovery reply carries: every queue (CUPS-Get-Printers), or the default +/// destination (CUPS-Get-Default). +fn discover(endpoints: &[Server], operation: u16) -> Result, Error> { + let reply = post(endpoints, "/", &discovery_request(operation), DISCOVERY_TIMEOUT, true)?; + let parsed = parse_reply(&reply)?; + if status_ok(parsed.status) { Ok(parsed.names) } else { Err(refused(parsed.status)) } +} + +/// Submit the PDF as a Print-Job to the printer (the job's, else the server's default); the +/// reply is the job message, like `lp` prints it (`request id is Office-12`). The driver's own +/// job options (PPD keywords) are not sent on this path: they have no IPP equivalents without +/// reading the printer's capabilities, which is another request. +pub fn print_job(pdf: &[u8], job: &Job) -> Result { + print_job_at(&endpoints(), pdf, job) +} + +pub(crate) fn print_job_at(endpoints: &[Server], pdf: &[u8], job: &Job) -> Result { + let queue = match &job.printer { + Some(name) => name.clone(), + None => discover(endpoints, OPERATION_CUPS_GET_DEFAULT)? + .into_iter() + .next() + .ok_or_else(|| Error::Spool("the print server has no default printer; choose one in the dialog".into()))?, + }; + if !is_queue_name(&queue) { + return Err(Error::Spool(format!("not a printer name: {queue}"))); + } + let reply = post(endpoints, &format!("/printers/{queue}"), &print_job_request(&queue, pdf, job), JOB_TIMEOUT, false)?; + let parsed = parse_reply(&reply)?; + if !status_ok(parsed.status) { + return Err(refused(parsed.status)); + } + Ok(match parsed.job_id { + Some(id) => format!("request id is {queue}-{id}"), + None => format!("the job was accepted by {queue}"), + }) +} + +/// A queue class can route a job to any member printer. Keep the destination kind +/// explicit rather than guessing it from a name shared by a printer and a class. +#[derive(Clone, Debug)] +pub struct Destination { + pub name: String, + pub is_class: bool, +} + +/// List both printer queues and classes from caller-selected CUPS endpoints. +/// The caller owns endpoint selection/authorization; this API does not discover network hosts. +pub fn destinations_at(endpoints: &[Server]) -> Result, Error> { + let mut out = queues_at(endpoints, false)?; + out.extend(queues_at(endpoints, true)?); + if out.len() > MAX_PRINTERS { + return Err(Error::Spool("too many CUPS destinations".into())); + } + let default = default_at(endpoints)?; + for queue in &mut out { + queue.is_default = default.as_deref() == Some(queue.name.as_str()); + } + Ok(out) +} + +/// List either printer queues or classes, without resolving defaults. Applications can +/// explicitly choose whether an optional class/default query failure should hide otherwise +/// usable printer queues. No error is swallowed here except CUPS's absent/unsupported list. +pub fn queues_at(endpoints: &[Server], is_class: bool) -> Result, Error> { + let operation = if is_class { 0x4005 } else { OPERATION_CUPS_GET_PRINTERS }; + let reply = post(endpoints, "/", &discovery_request(operation), DISCOVERY_TIMEOUT, true)?; + let parsed = parse_reply(&reply)?; + if !status_ok(parsed.status) { + if parsed.status == 0x0406 || (is_class && parsed.status == 0x0501) { + return Ok(Vec::new()); + } + return Err(refused(parsed.status)); + } + let mut queues = parsed.queues; + for queue in &mut queues { + queue.is_class = is_class; + } + Ok(queues) +} + +/// The scheduler's default queue; not-found means no default has been configured. +pub fn default_at(endpoints: &[Server]) -> Result, Error> { + let reply = post(endpoints, "/", &discovery_request(OPERATION_CUPS_GET_DEFAULT), DISCOVERY_TIMEOUT, true)?; + let parsed = parse_reply(&reply)?; + if parsed.status == 0x0406 { + return Ok(None); + } + if !status_ok(parsed.status) { + return Err(refused(parsed.status)); + } + Ok(parsed.names.into_iter().next()) +} + +/// Metadata from one CUPS printer group, without losing which fields belong together. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Queue { + pub name: String, + pub info: String, + pub state: u32, + pub accepting_jobs: bool, + pub is_default: bool, + pub is_class: bool, +} +impl Default for Queue { + fn default() -> Self { + Self { name: String::new(), info: String::new(), state: 3, accepting_jobs: true, is_default: false, is_class: false } + } +} + +/// Submit once to an explicitly selected queue/class, returning the numeric server job id. +/// An absent id is an uncertain accepted outcome, not permission to retry the document. +pub fn submit_at(endpoints: &[Server], destination: &Destination, pdf: &[u8], job: &Job) -> Result { + submit_as_at(endpoints, destination, pdf, job, "") +} + +/// As [`submit_at`], with the user name supplied by the application, never inferred from +/// process credentials. Once any request bytes may have been sent, no other endpoint is tried. +pub fn submit_as_at(endpoints: &[Server], destination: &Destination, pdf: &[u8], job: &Job, user: &str) -> Result { + let path = destination_path(destination)?; + if user.len() > MAX_JOB_NAME || user.chars().any(char::is_control) { + return Err(Error::Spool("invalid CUPS user name".into())); + } + if job.copies == 0 || job.copies > 999 { + return Err(Error::Spool("copies must be between 1 and 999".into())); + } + let mut last = Error::Spool("no CUPS endpoint selected".into()); + for endpoint in endpoints { + let mut stream = match connect(endpoint) { + Ok(stream) => stream, + Err(error) => { + last = Error::Spool(format!("CUPS connection failed: {error}")); + continue; + } + }; + let authority = endpoint.authority(); + let request = print_job_request_for(&authority, &path, pdf, job, user); + // Never retry exchange errors: the scheduler may already have accepted the job. + let reply = exchange(&mut stream, &authority, &path, &request, JOB_TIMEOUT)?; + let parsed = parse_reply(&reply)?; + if !status_ok(parsed.status) { + return Err(refused(parsed.status)); + } + return parsed + .job_id + .filter(|id| *id != 0 && *id <= i32::MAX as u32) + .ok_or_else(|| Error::Spool("CUPS accepted the job without a usable job id; do not resubmit".into())); + } + Err(last) +} + +/// Encode one destination as a URI path segment, never as routing syntax. Names from +/// discovery may include spaces or Unicode. Reject separators, controls and dot segments +/// rather than relying on a server to keep an encoded traversal request harmless. +fn destination_path(destination: &Destination) -> Result { + let name = &destination.name; + if name.is_empty() + || name.len() > MAX_NAME + || matches!(name.as_str(), "." | "..") + || name.chars().any(|c| c.is_control() || matches!(c, '/' | '\\')) + { + return Err(Error::Spool("invalid CUPS destination name".into())); + } + let kind = if destination.is_class { "classes" } else { "printers" }; + let mut path = format!("/{kind}/"); + for byte in name.bytes() { + if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') { + path.push(char::from(byte)); + } else { + use std::fmt::Write as _; + write!(&mut path, "%{byte:02X}").map_err(|_| Error::Spool("could not encode CUPS destination".into()))?; + } + } + Ok(path) +} + +/// A CUPS queue name: ASCII letters, digits and `_`, `-`, `.` — what the scheduler accepts, and +/// what cannot break the printer URI's path. +fn is_queue_name(name: &str) -> bool { + !name.is_empty() && name.len() <= MAX_NAME && name.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.')) +} + +/// One IPP request and its reply body. Discovery may try each endpoint in turn; a job is given +/// to the first endpoint that accepts a connection, once: a retry could print it twice. +pub(crate) fn post(endpoints: &[Server], path: &str, body: &[u8], timeout: Duration, retry: bool) -> Result, Error> { + let mut failed: Option = None; + for server in endpoints { + let mut stream = match connect(server) { + Ok(stream) => stream, + Err(e) => { + failed = Some(Error::Spool(format!("the print server could not be reached: {e}"))); + continue; + } + }; + match exchange(&mut stream, &server.authority(), path, body, timeout) { + Ok(reply) => return Ok(reply), + Err(e) => { + failed = Some(e); + if !retry { + break; + } + } + } + } + Err(failed.unwrap_or_else(|| Error::Spool("the print server could not be reached".into()))) +} + +/// Send one IPP request and read the reply's body. +fn exchange(stream: &mut Stream, authority: &str, path: &str, body: &[u8], timeout: Duration) -> Result, Error> { + let mut stream = DeadlineReader { stream, end: Instant::now() + timeout }; + let head = format!( + "POST {path} HTTP/1.1\r\nHost: {authority}\r\nContent-Type: application/ipp\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ); + stream + .write_all(head.as_bytes()) + .and_then(|()| stream.write_all(body)) + .and_then(|()| stream.flush()) + .map_err(|e| Error::Spool(format!("the request could not be sent to the print server: {e}")))?; + let reply = read_reply(&mut stream)?; + validate_reply_identity(body, &reply)?; + Ok(reply) +} + +/// One absolute deadline shared by request writes and reply reads after connection. +struct DeadlineReader<'a> { + stream: &'a mut Stream, + end: Instant, +} +impl Write for DeadlineReader<'_> { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + let left = self + .end + .checked_duration_since(Instant::now()) + .filter(|d| !d.is_zero()) + .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::TimedOut, "CUPS exchange deadline exceeded"))?; + self.stream.set_timeouts(left)?; + self.stream.write(buf) + } + fn flush(&mut self) -> std::io::Result<()> { + self.stream.flush() + } +} + +fn validate_reply_identity(request: &[u8], reply: &[u8]) -> Result<(), Error> { + if reply.get(..2) != Some(&[1, 1]) || request.get(4..8).is_none() || request.get(4..8) != reply.get(4..8) { + return Err(Error::Spool("CUPS reply version or request id does not match; do not resubmit".into())); + } + Ok(()) +} + +impl Read for DeadlineReader<'_> { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let left = self + .end + .checked_duration_since(Instant::now()) + .filter(|d| !d.is_zero()) + .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::TimedOut, "CUPS reply deadline exceeded"))?; + #[cfg(not(target_os = "macos"))] + self.stream.set_timeouts(left)?; + #[cfg(target_os = "macos")] + if let Err(error) = self.stream.set_timeouts(left) { + // Darwin rejects timeout updates on a disconnected Unix socket even + // when a complete reply is still buffered. Never fall back to a + // blocking read: drain at most this buffer, or fail on WouldBlock. + // Every subsequent read rechecks the same absolute deadline above. + if error.raw_os_error() == Some(22) + && let Stream::Unix(stream) = self.stream + { + stream.set_nonblocking(true)?; + let result = stream.read(buf); + let restored = stream.set_nonblocking(false); + return result.and_then(|count| restored.map(|()| count)); + } + return Err(error); + } + self.stream.read(buf) + } +} + +fn read_reply(stream: &mut impl Read) -> Result, Error> { + let mut head = Vec::new(); + let mut byte = [0u8; 1]; + loop { + if head.len() >= MAX_HEADERS { + return Err(Error::Spool("the print server sent a header block that is too large".into())); + } + match stream.read(&mut byte) { + Ok(0) => return Err(Error::Spool("the print server closed the connection before sending its reply".into())), + Ok(_) => head.push(byte[0]), + Err(e) => return Err(Error::Spool(format!("the print server's reply could not be read: {e}"))), + } + if head.ends_with(b"\r\n\r\n") { + break; + } + } + let head = String::from_utf8_lossy(&head); + let Some(status_line) = head.lines().next() else { return Err(Error::Spool("the print server sent an unreadable reply".into())) }; + let code = status_line + .strip_prefix("HTTP/") + .and_then(|rest| rest.split_once(' ')) + .and_then(|(_, code)| code.split_whitespace().next()) + .and_then(|code| code.parse::().ok()) + .ok_or_else(|| Error::Spool("the print server sent an unreadable reply".into()))?; + match code { + 200..=299 => {} + 401 => return Err(Error::Spool("the print server requires authentication".into())), + 426 => return Err(Error::Spool("the print server only accepts encrypted connections".into())), + _ => return Err(Error::Spool(format!("the print server refused the request (HTTP {code})"))), + } + let mut content_length: Option = None; + let mut chunked = false; + for line in head.lines().skip(1) { + // The empty line ends the header block; any other line without a colon is malformed. + if line.is_empty() { + continue; + } + let Some((name, value)) = line.split_once(':') else { + return Err(Error::Spool("the print server sent an unreadable header".into())); + }; + match name.trim().to_ascii_lowercase().as_str() { + "content-length" => { + let len = value.trim().parse::().map_err(|_| Error::Spool("invalid Content-Length".into()))?; + if content_length.replace(len).is_some() { + return Err(Error::Spool("duplicate Content-Length".into())); + } + } + // Only `identity` is not a transfer encoding to be decoded. + "transfer-encoding" => { + if chunked || !value.trim().eq_ignore_ascii_case("chunked") { + return Err(Error::Spool("unsupported Transfer-Encoding".into())); + } + chunked = true; + } + _ => {} + } + } + if chunked { + if content_length.is_some() { + return Err(Error::Spool("ambiguous reply framing".into())); + } + return read_chunks(stream); + } + let Some(len) = content_length else { return Err(Error::Spool("the print server sent no Content-Length".into())) }; + if len > MAX_BODY { + return Err(Error::Spool(format!("the print server's reply is too large ({len} bytes)"))); + } + let mut body = vec![0u8; len]; + stream.read_exact(&mut body).map_err(|e| Error::Spool(format!("the print server's reply was cut short: {e}")))?; + Ok(body) +} + +fn read_chunks(stream: &mut impl Read) -> Result, Error> { + fn line(stream: &mut impl Read) -> Result, Error> { + let mut out = Vec::new(); + for _ in 0..MAX_HEADERS { + let mut byte = [0]; + stream.read_exact(&mut byte).map_err(|e| Error::Spool(format!("truncated chunk: {e}")))?; + out.push(byte[0]); + if out.ends_with(b"\r\n") { + out.truncate(out.len() - 2); + return Ok(out); + } + } + Err(Error::Spool("chunk line exceeds limit".into())) + } + let mut body = Vec::new(); + for _ in 0..100_000 { + let header = line(stream)?; + let size = header.split(|b| *b == b';').next().unwrap_or_default(); + if size.is_empty() || !size.iter().all(u8::is_ascii_hexdigit) { + return Err(Error::Spool("invalid chunk size".into())); + } + let len = std::str::from_utf8(size) + .ok() + .and_then(|s| usize::from_str_radix(s, 16).ok()) + .ok_or_else(|| Error::Spool("invalid chunk size".into()))?; + if len == 0 { + let mut total = 0usize; + loop { + let trailer = line(stream)?; + total = total.saturating_add(trailer.len() + 2); + if total > MAX_HEADERS { + return Err(Error::Spool("chunk trailers exceed limit".into())); + } + if trailer.is_empty() { + return Ok(body); + } + if !trailer.contains(&b':') { + return Err(Error::Spool("invalid chunk trailer".into())); + } + } + } + let end = body.len().checked_add(len).filter(|n| *n <= MAX_BODY).ok_or_else(|| Error::Spool("chunked reply exceeds limit".into()))?; + let start = body.len(); + body.resize(end, 0); + stream.read_exact(&mut body[start..]).map_err(|e| Error::Spool(format!("truncated chunk: {e}")))?; + let mut crlf = [0; 2]; + stream.read_exact(&mut crlf).map_err(|e| Error::Spool(format!("truncated chunk: {e}")))?; + if crlf != *b"\r\n" { + return Err(Error::Spool("invalid chunk terminator".into())); + } + } + Err(Error::Spool("too many chunks".into())) +} + +/// An IPP request being built: a version, an operation and a request id, then attributes, then +/// the document. +struct Request { + bytes: Vec, +} + +impl Request { + fn new(operation: u16) -> Self { + // IPP 1.1: version, operation, a request id (any nonzero value). + let mut bytes = vec![1, 1]; + bytes.extend(operation.to_be_bytes()); + bytes.extend(REQUEST_ID.fetch_add(1, Ordering::Relaxed).max(1).to_be_bytes()); + Request { bytes } + } + + fn group(&mut self, tag: u8) { + self.bytes.push(tag); + } + + fn end(&mut self) { + self.bytes.push(GROUP_END); + } + + fn attribute(&mut self, tag: u8, name: &str, value: &[u8]) { + let (Ok(name_len), Ok(value_len)) = (u16::try_from(name.len()), u16::try_from(value.len())) else { + return; + }; + self.bytes.push(tag); + self.bytes.extend(name_len.to_be_bytes()); + self.bytes.extend(name.as_bytes()); + self.bytes.extend(value_len.to_be_bytes()); + self.bytes.extend(value); + } + + /// An additional value of the previous attribute (a `1setOf`): the name is empty. + fn additional(&mut self, tag: u8, value: &str) { + self.attribute(tag, "", value.as_bytes()); + } +} + +/// A discovery request: the operation's required attributes, then the names to be answered with. +fn discovery_request(operation: u16) -> Vec { + let mut r = Request::new(operation); + r.group(GROUP_OPERATION); + r.attribute(TAG_CHARSET, "attributes-charset", b"utf-8"); + r.attribute(TAG_LANGUAGE, "attributes-natural-language", b"en"); + r.attribute(TAG_URI, "printer-uri", b"ipp://localhost/"); + r.attribute(TAG_KEYWORD, "requested-attributes", b"printer-name"); + r.additional(TAG_KEYWORD, "printer-info"); + r.additional(TAG_KEYWORD, "printer-state"); + r.additional(TAG_KEYWORD, "printer-is-accepting-jobs"); + r.end(); + r.bytes +} + +/// A Print-Job request for `queue`: the job's options as IPP attributes, the PDF as the document +/// data that follows the end of attributes. +fn print_job_request(queue: &str, pdf: &[u8], job: &Job) -> Vec { + print_job_request_path(&format!("/printers/{queue}"), pdf, job) +} + +fn print_job_request_path(path: &str, pdf: &[u8], job: &Job) -> Vec { + print_job_request_for("localhost", path, pdf, job, "") +} + +fn print_job_request_for(authority: &str, path: &str, pdf: &[u8], job: &Job, user: &str) -> Vec { + let mut r = Request::new(OPERATION_PRINT_JOB); + r.group(GROUP_OPERATION); + r.attribute(TAG_CHARSET, "attributes-charset", b"utf-8"); + r.attribute(TAG_LANGUAGE, "attributes-natural-language", b"en"); + r.attribute(TAG_URI, "printer-uri", format!("ipp://{authority}{path}").as_bytes()); + if !user.is_empty() { + r.attribute(TAG_NAME, "requesting-user-name", user.as_bytes()); + } + r.attribute(TAG_NAME, "job-name", truncate_utf8(&job.title, MAX_JOB_NAME).as_bytes()); + r.attribute(TAG_MIME, "document-format", b"application/pdf"); + r.group(GROUP_JOB); + r.attribute(TAG_INTEGER, "copies", &job.copies.clamp(1, 999).to_be_bytes()); + r.attribute(TAG_BOOLEAN, "collate", &[u8::from(job.collate)]); + if let Some(duplex) = job.duplex { + r.attribute( + TAG_KEYWORD, + "sides", + match duplex { + Duplex::Off => "one-sided", + Duplex::LongEdge => "two-sided-long-edge", + Duplex::ShortEdge => "two-sided-short-edge", + } + .as_bytes(), + ); + } + if job.grayscale { + r.attribute(TAG_KEYWORD, "print-color-mode", b"monochrome"); + } + // PWG 5100.13 §6.2.5: `none` keeps every sheet at its size, `auto-fit` shrinks only + // oversized ones — the same choices `lp`'s fit-to-page and print-scaling options make. + r.attribute( + TAG_KEYWORD, + "print-scaling", + match job.scaling { + Scaling::Exact => "none", + Scaling::ShrinkToFit => "auto-fit", + } + .as_bytes(), + ); + r.end(); + r.bytes.extend(pdf); + r.bytes +} + +/// The string cut to at most `max` bytes, at a character boundary. +fn truncate_utf8(s: &str, max: usize) -> &str { + if s.len() <= max { + return s; + } + let mut end = max; + while !s.is_char_boundary(end) { + end -= 1; + } + // `end` is a checked boundary; nothing is the only name that cannot be cut. + s.get(..end).unwrap_or_default() +} + +/// The parsed parts of a reply: the IPP status, the printer names in its printer groups, and a +/// submission's job id. +struct Reply { + status: u16, + names: Vec, + queues: Vec, + job_id: Option, +} + +/// A cursor over a reply: `None` at the end or past a length, never an over-read. +struct Reader<'a> { + bytes: &'a [u8], + pos: usize, +} + +impl<'a> Reader<'a> { + fn take(&mut self, len: usize) -> Option<&'a [u8]> { + let end = self.pos.checked_add(len)?; + let taken = self.bytes.get(self.pos..end)?; + self.pos = end; + Some(taken) + } + + fn array(&mut self) -> Option<[u8; N]> { + <[u8; N]>::try_from(self.take(N)?).ok() + } + + fn u16(&mut self) -> Option { + self.array::<2>().map(u16::from_be_bytes) + } +} + +fn parse_reply(reply: &[u8]) -> Result { + let malformed = || Error::Spool("the print server sent a malformed reply".into()); + let mut r = Reader { bytes: reply, pos: 0 }; + let _version = r.array::<2>().ok_or_else(malformed)?; + let status = r.u16().ok_or_else(malformed)?; + let _request_id = r.array::<4>().ok_or_else(malformed)?; + let (mut names, mut job_id, mut group) = (Vec::new(), None, 0u8); + let mut queues = Vec::new(); + let mut queue = Queue::default(); + loop { + let Some(tag) = r.array::<1>().map(|t| t[0]) else { return Err(malformed()) }; + match tag { + GROUP_END => { + if !queue.name.is_empty() { + queues.push(queue); + } + break; + } + GROUP_OPERATION | GROUP_JOB | GROUP_PRINTER | 0x05 => { + if !queue.name.is_empty() { + if queues.len() >= MAX_PRINTERS { + return Err(malformed()); + } + queues.push(std::mem::take(&mut queue)); + } else { + queue = Queue::default(); + } + group = tag; + } + // An attribute: name and value, each u16-length. An empty name is an additional + // value of the previous one (the attributes read here are single-valued, so those + // are ignored). Group tags not in the registry and collections (the attributes + // requested here are never collections) are refused, not guessed. + 0x10..=0x33 | 0x35..=0x4A => { + let name_len = r.u16().ok_or_else(malformed)?; + if usize::from(name_len) > MAX_NAME { + return Err(malformed()); + } + let name_bytes = r.take(usize::from(name_len)).ok_or_else(malformed)?; + let name = std::str::from_utf8(name_bytes).map_err(|_| malformed())?; + let value_len = r.u16().ok_or_else(malformed)?; + let value = r.take(usize::from(value_len)).ok_or_else(malformed)?; + match (group, name, tag) { + (GROUP_PRINTER, "printer-name", TAG_NAME) => { + if let Some(printer) = std::str::from_utf8(value).ok().filter(|n| !n.is_empty() && n.len() <= MAX_NAME) { + if names.len() >= MAX_PRINTERS { + return Err(Error::Spool("too many CUPS destinations".into())); + } + if !queue.name.is_empty() { + return Err(malformed()); + } + queue.name = printer.to_string(); + names.push(printer.to_string()); + } + } + (GROUP_PRINTER, "printer-info", 0x41 | TAG_NAME) => { + // Human descriptions are text, not queue identifiers. Their u16 + // wire length and the whole-response MAX_BODY cap bound allocation. + queue.info = std::str::from_utf8(value).map_err(|_| malformed())?.to_owned(); + } + (GROUP_PRINTER, "printer-state", 0x23 | TAG_INTEGER) => { + queue.state = u32::from_be_bytes(value.try_into().map_err(|_| malformed())?); + } + (GROUP_PRINTER, "printer-is-accepting-jobs", TAG_BOOLEAN) => { + queue.accepting_jobs = match value { + [0] => false, + [1] => true, + _ => return Err(malformed()), + }; + } + (GROUP_JOB, "job-id", TAG_INTEGER) => { + if let Ok(id) = <[u8; 4]>::try_from(value) { + job_id = Some(u32::from_be_bytes(id)); + } + } + _ => {} + } + } + _ => return Err(malformed()), + } + } + Ok(Reply { status, names, queues, job_id }) +} + +/// Successful IPP status codes are `0x00xx` (RFC 8011 Appendix B). +fn status_ok(status: u16) -> bool { + status < 0x0100 +} + +/// The IPP status code's name, for the error the user sees. +fn status_name(status: u16) -> Option<&'static str> { + Some(match status { + 0x0400 => "bad request", + 0x0401 => "forbidden", + 0x0402 => "not authenticated", + 0x0403 => "not authorized", + 0x0404 => "not possible", + 0x0406 => "printer not found", + 0x0408 => "request too large", + 0x040A => "PDF is not a supported document format for this printer", + 0x0412 => "the document could not be accessed", + 0x0500 => "internal error", + 0x0501 => "operation not supported", + 0x0502 => "service unavailable", + 0x0503 => "IPP version not supported", + 0x0504 => "device error", + 0x0505 => "temporary error", + 0x0506 => "the printer is not accepting jobs", + 0x0507 => "the printer is busy", + _ => return None, + }) +} + +fn refused(status: u16) -> Error { + match status_name(status) { + Some(name) => Error::Spool(format!("the print server refused the request ({name})")), + None => Error::Spool(format!("the print server refused the request (IPP status 0x{status:04X})")), + } +} diff --git a/crates/print/src/cups/tests.rs b/crates/print/src/cups/tests.rs new file mode 100644 index 000000000..978246e56 --- /dev/null +++ b/crates/print/src/cups/tests.rs @@ -0,0 +1,593 @@ +//! The IPP client against a fake CUPS server (a unix socket or a loopback port): discovery, +//! Print-Job, and the malformed replies it must refuse without a panic. + +use super::*; +use crate as spool; +use crate::cups::{self as ipp, Server}; +use std::collections::VecDeque; +use std::io::{Read, Write}; +use std::sync::Mutex; +use std::sync::mpsc::{self, Receiver}; +use std::time::Duration; + +const TIMEOUT: Duration = Duration::from_secs(2); + +/// A fake CUPS server: each connection carries one request; the next canned reply goes back +/// (nothing, when the list is out — the client then sees a hang-up). Every request is recorded +/// for the assertions. +struct FakeCups { + server: Server, + requests: Receiver>, +} + +impl FakeCups { + fn on_unix(replies: Vec>) -> std::io::Result { + let dir = tempfile::tempdir()?; + let path = dir.path().join("cups.sock"); + let listener = std::os::unix::net::UnixListener::bind(&path)?; + // The socket must outlive this function; the folder is left for the process to reap. + std::mem::forget(dir); + let requests = listen_unix(listener, replies); + Ok(FakeCups { server: Server::Unix(path), requests }) + } + + fn on_tcp(replies: Vec>) -> std::io::Result { + let listener = std::net::TcpListener::bind("127.0.0.1:0")?; + let server = Server::Tcp(listener.local_addr()?); + let requests = listen_tcp(listener, replies); + Ok(FakeCups { server, requests }) + } + + fn request(&self) -> Result, mpsc::RecvTimeoutError> { + self.requests.recv_timeout(Duration::from_secs(5)) + } + + /// The server as the one-endpoint list the client functions take. + fn endpoint(&self) -> &[Server] { + std::slice::from_ref(&self.server) + } +} + +fn listen_unix(listener: std::os::unix::net::UnixListener, replies: Vec>) -> Receiver> { + let (tx, rx) = mpsc::channel(); + let replies = Mutex::new(VecDeque::from(replies)); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + serve(stream, &replies, &tx); + } + }); + rx +} + +fn listen_tcp(listener: std::net::TcpListener, replies: Vec>) -> Receiver> { + let (tx, rx) = mpsc::channel(); + let replies = Mutex::new(VecDeque::from(replies)); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + serve(stream, &replies, &tx); + } + }); + rx +} + +fn serve(mut stream: impl Read + Write, replies: &Mutex>>, requests: &mpsc::Sender>) { + let Some(request) = read_request(&mut stream) else { return }; + let Some(body) = ipp_body(&request) else { return }; + let request_id = body.get(4..8).map(|id| id.to_vec()); + let _ = requests.send(request); + // A list out of replies is a server that hangs up before answering. + let Ok(mut replies) = replies.lock() else { return }; + if let Some(mut reply) = replies.pop_front() { + if let Some(body) = reply.windows(4).position(|w| w == b"\r\n\r\n").map(|at| at + 4) + && reply.get(body..body + 2) == Some(&[1, 1]) + && let Some(id) = request_id + && let Some(destination) = reply.get_mut(body + 4..body + 8) + { + destination.copy_from_slice(&id); + } + let _ = stream.write_all(&reply); + let _ = stream.flush(); + } +} + +/// One HTTP request: the headers (bounded), then the `Content-Length` body it announces. +fn read_request(stream: &mut impl Read) -> Option> { + let mut buf = Vec::new(); + let mut byte = [0u8; 1]; + loop { + match stream.read(&mut byte) { + Ok(0) | Err(_) => return None, + Ok(_) => buf.push(byte[0]), + } + if buf.ends_with(b"\r\n\r\n") { + break; + } + if buf.len() > 1 << 16 { + return None; + } + } + let head = String::from_utf8_lossy(&buf); + let len: usize = head.lines().find_map(|l| l.strip_prefix("Content-Length:")).and_then(|v| v.trim().parse().ok())?; + let head_len = buf.len(); + buf.resize(head_len + len, 0); + stream.read_exact(&mut buf[head_len..]).ok()?; + Some(buf) +} + +/// An HTTP reply carrying `body`, as CUPS would send it. +fn http_reply(body: &[u8]) -> Vec { + let mut out = format!("HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nContent-Length: {}\r\n\r\n", body.len()).into_bytes(); + out.extend(body); + out +} + +/// An IPP reply body: `status`, then the attribute groups as raw bytes, then end of attributes. +fn ipp_reply(status: u16, groups: &[&[u8]]) -> Vec { + let mut out = vec![1, 1]; + out.extend(status.to_be_bytes()); + out.extend(1u32.to_be_bytes()); + for &group in groups { + out.extend(group); + } + out.push(0x03); + out +} + +fn group(tag: u8) -> Vec { + vec![tag] +} + +fn printer_name(name: &str) -> Vec { + attr(0x42, "printer-name", name.as_bytes()) +} + +/// One attribute, framed the way the client frames them, so `has` finds it in a request. +fn attr(tag: u8, name: &str, value: &[u8]) -> Vec { + let mut out = vec![tag]; + out.extend((name.len() as u16).to_be_bytes()); + out.extend(name.as_bytes()); + out.extend((value.len() as u16).to_be_bytes()); + out.extend(value); + out +} + +fn has(bytes: &[u8], needle: &[u8]) -> bool { + !needle.is_empty() && bytes.windows(needle.len()).any(|w| w == needle) +} + +/// The IPP part of a recorded request: everything after the HTTP head. +fn ipp_body(request: &[u8]) -> Option<&[u8]> { + let at = request.windows(4).position(|w| w == b"\r\n\r\n")?; + request.get(at + 4..) +} + +fn entry(name: &str, default: bool) -> spool::Printer { + spool::Printer { description: String::new(), name: name.into(), default, two_sided: None } +} + +#[test] +fn discovery_finds_queues_and_the_default_over_a_unix_socket() { + let fake = FakeCups::on_unix(vec![ + http_reply(&ipp_reply( + 0x0000, + &[ + &group(0x01), + &printer_name("Office_Laser"), + &group(0x04), + &printer_name("Office_Laser"), + &group(0x04), + &printer_name("Label_Writer"), + &group(0x04), + &printer_name("Office_Laser"), + ], + )), + http_reply(&ipp_reply(0x0000, &[&group(0x01), &group(0x04), &printer_name("Office_Laser")])), + ]) + .unwrap(); + let printers = ipp::printers_at(fake.endpoint()).unwrap(); + assert_eq!(printers, [entry("Office_Laser", true), entry("Label_Writer", false)], "each queue once, the default marked"); + let taken = fake.request().unwrap(); + let queues = ipp_body(&taken).unwrap(); + assert_eq!(&queues[..4], &[1, 1, 0x40, 0x02], "IPP 1.1, CUPS-Get-Printers"); + assert!(has(queues, b"ipp://localhost/")); + assert!(has(queues, &attr(0x44, "requested-attributes", b"printer-name"))); + assert!(has(queues, b"printer-is-accepting-jobs"), "only the attributes the dialog needs are asked for"); + let taken = fake.request().unwrap(); + let default = ipp_body(&taken).unwrap(); + assert_eq!(&default[..4], &[1, 1, 0x40, 0x01], "CUPS-Get-Default"); +} + +#[test] +fn a_print_job_carries_the_pdf_and_the_options() { + let pdf: Vec = b"%PDF-1.7 fake print job\n".iter().copied().cycle().take(1 << 20).collect(); + let job = spool::Job { + printer: Some("Office".into()), + copies: 2, + collate: false, + duplex: Some(spool::Duplex::LongEdge), + grayscale: true, + scaling: spool::Scaling::ShrinkToFit, + title: "memo.pdf".into(), + ..Job::default() + }; + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply( + 0x0000, + &[&group(0x01), &group(0x02), &attr(0x21, "job-id", &12u32.to_be_bytes()), &attr(0x23, "job-state", &[3u8])], + ))]) + .unwrap(); + let message = ipp::print_job_at(fake.endpoint(), &pdf, &job).unwrap(); + assert_eq!(message, "request id is Office-12", "the same message lp prints"); + let taken = fake.request().unwrap(); + let request = ipp_body(&taken).unwrap(); + assert_eq!(&request[..4], &[1, 1, 0x00, 0x02], "IPP 1.1, Print-Job"); + assert!(has(request, b"ipp://localhost/printers/Office")); + let mut copies = group(0x02); + copies.extend(attr(0x21, "copies", &2u32.to_be_bytes())); + assert!(has(request, &copies), "copies are a job attribute"); + assert!(has(request, &attr(0x22, "collate", &[0]))); + assert!(has(request, &attr(0x44, "sides", b"two-sided-long-edge"))); + assert!(has(request, &attr(0x44, "print-color-mode", b"monochrome"))); + assert!(has(request, &attr(0x44, "print-scaling", b"auto-fit"))); + assert!(has(request, &attr(0x49, "document-format", b"application/pdf"))); + assert!(has(request, &attr(0x42, "job-name", b"memo.pdf"))); + assert!(request.ends_with(&pdf), "the whole job is the document data"); +} + +#[test] +fn the_default_printer_is_used_when_the_job_has_none() { + let fake = FakeCups::on_tcp(vec![ + http_reply(&ipp_reply(0x0000, &[&group(0x01), &group(0x04), &printer_name("Office")])), + http_reply(&ipp_reply(0x0000, &[&group(0x01), &group(0x02), &attr(0x21, "job-id", &7u32.to_be_bytes())])), + ]) + .unwrap(); + let message = ipp::print_job_at(fake.endpoint(), b"%PDF-1.7\n", &Job::default()).unwrap(); + assert_eq!(message, "request id is Office-7"); + let default = fake.request().unwrap(); + assert_eq!(&ipp_body(&default).unwrap()[..4], &[1, 1, 0x40, 0x01], "the default comes from CUPS-Get-Default"); + let taken = fake.request().unwrap(); + assert!(has(ipp_body(&taken).unwrap(), b"ipp://localhost/printers/Office"), "the resolved default is the target"); +} + +#[test] +fn a_default_outside_the_queue_list_still_gets_its_marker() { + let fake = FakeCups::on_unix(vec![ + http_reply(&ipp_reply(0x0000, &[&group(0x01), &group(0x04), &printer_name("Office_Laser")])), + http_reply(&ipp_reply(0x0000, &[&group(0x01), &group(0x04), &printer_name("Label_Writer")])), + ]) + .unwrap(); + let printers = ipp::printers_at(fake.endpoint()).unwrap(); + assert_eq!( + printers, + [entry("Office_Laser", false), entry("Label_Writer", true)], + "the default joins the list, so the dialog has a selection to restore" + ); +} + +#[test] +fn no_default_printer_is_an_error_when_the_job_names_none() { + let fake = FakeCups::on_unix(vec![http_reply(&ipp_reply(0x0000, &[&group(0x01)]))]).unwrap(); + let err = ipp::print_job_at(fake.endpoint(), b"%PDF-1.7\n", &Job::default()).unwrap_err(); + assert!(err.to_string().contains("no default printer"), "{err}"); +} + +#[test] +fn a_printer_name_that_cannot_be_a_uri_path_is_refused() { + let fake = FakeCups::on_tcp(vec![]).unwrap(); + let mut long = "x".repeat(257); + long.push('y'); + for name in ["Office (east)", "../../admin", "", "a b", "Brother/HL", "Копир", long.as_str()] { + let job = spool::Job { printer: Some(name.into()), ..Job::default() }; + assert_eq!( + ipp::print_job_at(fake.endpoint(), b"pdf", &job), + Err(spool::Error::Spool(format!("not a printer name: {name}"))), + "{name:?} cannot go in the printer URI's path" + ); + } +} + +#[test] +fn an_ipp_refusal_is_an_error_that_names_the_problem() { + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply(0x0506, &[&group(0x01)]))]).unwrap(); + let job = spool::Job { printer: Some("Office".into()), ..Job::default() }; + assert_eq!( + ipp::print_job_at(fake.endpoint(), b"%PDF-1.7\n", &job), + Err(spool::Error::Spool("the print server refused the request (the printer is not accepting jobs)".into())) + ); +} + +#[test] +fn malformed_replies_are_errors_never_a_panic() { + let bodies: Vec<(&str, Vec)> = vec![ + // Shorter than an IPP header. + ("shorter than an IPP header", vec![1, 1, 0]), + // A reply that ends inside an attribute: the name is announced, the value never comes. + ("cut inside an attribute", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x04, 0x42, 0x00, 0x20, b'p']), + // A value tag the registry does not know. + ("unknown value tag", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x04, 0xFF, 0x00, 0x00, 0x00, 0x00]), + // A collection, which the attributes asked for never are. + ("collection", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x04, 0x34, 0x00, 0x00, 0x00, 0x00]), + // A group tag that is no delimiter in the registry. + ("reserved delimiter", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x06]), + // A length within the cap that runs past the reply, and one past the cap. + ("length past the reply", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x04, 0x42, 0x00, 0xFE, b'p']), + ("length past the cap", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x04, 0x42, 0xFF, 0xFF, b'p']), + // No end of attributes before the body runs out. + ("no end of attributes", vec![1, 1, 0, 0, 0, 0, 0, 1, 0x04, 0x42, 0x00, 0x01, b'x']), + ]; + for (why, body) in bodies { + let fake = FakeCups::on_unix(vec![http_reply(&body)]).unwrap(); + let result = ipp::printers_at(fake.endpoint()); + assert!(result.is_err(), "{why}: the reply must be an error, not a panic or a success"); + } +} + +#[test] +fn http_replies_that_cannot_carry_an_answer_are_refused() { + let replies: Vec<(&str, Vec)> = vec![ + ("requires authentication", b"HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n".to_vec()), + ("demands a TLS upgrade", b"HTTP/1.1 426 Upgrade Required\r\nContent-Length: 0\r\n\r\n".to_vec()), + ("chunked", b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n25\r\nthe body is chunked\r\n0\r\n\r\n".to_vec()), + ("no Content-Length", b"HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\n\r\n".to_vec()), + ("Content-Length past the cap", b"HTTP/1.1 200 OK\r\nContent-Length: 99999999\r\n\r\n".to_vec()), + ("unreadable status line", b"not HTTP at all\r\n\r\n".to_vec()), + ]; + for (why, reply) in replies { + let fake = FakeCups::on_unix(vec![reply]).unwrap(); + let result = ipp::post(&[fake.server], "/", b"ignored", TIMEOUT, true); + assert!(result.is_err(), "{why}: the reply must be an error, not a panic or a success"); + } + // A server that takes the request and hangs up. + let fake = FakeCups::on_unix(vec![]).unwrap(); + assert!(ipp::post(&[fake.server], "/", b"ignored", TIMEOUT, true).is_err(), "a hang-up is an error"); +} + +#[test] +fn a_server_that_answers_is_reachable_and_silence_is_not() { + let fake = FakeCups::on_unix(vec![]).unwrap(); + assert!(ipp::reachable(&fake.server), "a unix socket that accepts connections is reachable"); + let fake = FakeCups::on_tcp(vec![]).unwrap(); + assert!(ipp::reachable(&fake.server), "a loopback port that accepts connections is reachable"); + let absent = Server::Unix("/nonexistent/craft-print-test.sock".into()); + assert!(!ipp::reachable(&absent), "a socket nothing listens on is not reachable"); + let addr = { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.local_addr().unwrap() + }; + assert!(!ipp::reachable(&Server::Tcp(addr)), "a port nothing listens on is not reachable"); +} + +#[test] +fn chunked_reply_is_bounded_and_rejects_ambiguous_framing() { + let good = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n2\r\nde\r\n0\r\n\r\n"; + assert_eq!(super::read_reply(&mut &good[..]).unwrap(), b"abcde"); + for bad in [ + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Length: 0\r\n\r\n0\r\n\r\n".as_slice(), + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n100001\r\n", + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\na", + b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nContent-Length: 0\r\n\r\n", + ] { + assert!(super::read_reply(&mut &bad[..]).is_err()); + } +} + +#[test] +fn explicit_server_rejects_header_injection_and_malformed_port() { + for value in ["host\r\nInjected: yes", "host:0", "host:99999", "http://host", "user@host", ""] { + assert!(Server::parse(value).is_err(), "{value:?}"); + } + assert_eq!(Server::parse("[::1]:631").unwrap().authority(), "[::1]:631"); + assert_eq!(Server::parse("print.example:8631").unwrap().authority(), "print.example:8631"); +} + +#[test] +fn class_submission_preserves_authority_user_and_numeric_job_id() { + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply(0, &[&group(2), &attr(0x21, "job-id", &42u32.to_be_bytes())]))]).unwrap(); + let destination = super::Destination { name: "Studio".into(), is_class: true }; + assert_eq!(super::submit_as_at(fake.endpoint(), &destination, b"pdf", &Job::default(), "artist").unwrap(), 42); + let request = fake.request().unwrap(); + assert!(has(&request, b"POST /classes/Studio HTTP/1.1")); + assert!(has(&request, format!("Host: {}", fake.server.authority()).as_bytes())); + assert!(has(ipp_body(&request).unwrap(), format!("ipp://{}/classes/Studio", fake.server.authority()).as_bytes())); + assert!(has(ipp_body(&request).unwrap(), &attr(0x42, "requesting-user-name", b"artist"))); +} + +#[test] +fn uncertain_submission_never_tries_the_second_endpoint() { + let first = FakeCups::on_tcp(vec![]).unwrap(); + let second = FakeCups::on_tcp(vec![]).unwrap(); + let destination = super::Destination { name: "Studio".into(), is_class: false }; + assert!(super::submit_at(&[first.server.clone(), second.server.clone()], &destination, b"pdf", &Job::default()).is_err()); + let _ = first.request().unwrap(); + assert!(second.requests.recv_timeout(Duration::from_millis(100)).is_err()); +} + +#[test] +fn queue_metadata_stays_with_its_printer_group() { + let bytes = ipp_reply( + 0, + &[ + &group(4), + &attr(0x41, "printer-info", b"Studio printer"), + &printer_name("First"), + &attr(0x23, "printer-state", &5u32.to_be_bytes()), + &attr(0x22, "printer-is-accepting-jobs", &[0]), + &group(4), + &printer_name("Second"), + ], + ); + let reply = super::parse_reply(&bytes).unwrap(); + assert_eq!(reply.queues.len(), 2); + assert_eq!(reply.queues[0].info, "Studio printer"); + assert_eq!(reply.queues[0].state, 5); + assert!(!reply.queues[0].accepting_jobs); + assert!(reply.queues[1].info.is_empty()); + assert!(reply.queues[1].accepting_jobs); +} + +#[test] +fn reply_identity_and_metadata_request_are_checked() { + let request = super::discovery_request(0x4002); + assert!(has(&request, b"printer-info")); + let mut reply = vec![1, 1, 0, 0]; + reply.extend_from_slice(&request[4..8]); + reply.push(3); + assert!(super::validate_reply_identity(&request, &reply).is_ok()); + reply[7] ^= 1; + assert!(super::validate_reply_identity(&request, &reply).is_err()); + reply[4..8].copy_from_slice(&request[4..8]); + reply[0] = 9; + assert!(super::validate_reply_identity(&request, &reply).is_err()); +} + +#[test] +fn slow_reply_cannot_reset_the_absolute_deadline() { + let (client, mut peer) = std::os::unix::net::UnixStream::pair().unwrap(); + let producer = std::thread::spawn(move || { + // Finite work and a joined handle: no abandoned timeout worker. + for _ in 0..20 { + if peer.write_all(b"x").is_err() { + break; + } + std::thread::sleep(Duration::from_millis(20)); + } + }); + let start = std::time::Instant::now(); + let mut stream = super::Stream::Unix(client); + let mut reader = super::DeadlineReader { stream: &mut stream, end: start + Duration::from_millis(80) }; + let mut bytes = [0; 20]; + assert!(reader.read_exact(&mut bytes).is_err(), "trickle reads must not extend the absolute deadline"); + drop(stream); + producer.join().unwrap(); + assert!(start.elapsed() < Duration::from_secs(3)); +} + +#[test] +fn blocked_upload_obeys_the_same_absolute_deadline() { + let (client, _peer) = std::os::unix::net::UnixStream::pair().unwrap(); + let start = std::time::Instant::now(); + let mut stream = super::Stream::Unix(client); + let mut writer = super::DeadlineReader { stream: &mut stream, end: start + Duration::from_millis(80) }; + let chunk = [0u8; 64 * 1024]; + let mut timed_out = false; + // A peer that never drains cannot turn each partial write into a fresh timeout budget. + for _ in 0..512 { + if writer.write_all(&chunk).is_err() { + timed_out = true; + break; + } + } + assert!(timed_out, "blocked upload must terminate without a reader"); + assert!(start.elapsed() < Duration::from_secs(3)); +} + +#[test] +fn recorded_request_body_requires_complete_http_headers() { + assert!(ipp_body(b"POST / HTTP/1.1\r\n").is_none()); + assert_eq!(ipp_body(b"POST / HTTP/1.1\r\n\r\nbody"), Some(b"body".as_slice())); +} + +#[test] +fn named_destinations_encode_one_path_segment_on_both_wire_fields() { + for is_class in [false, true] { + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply(0, &[&group(2), &attr(0x21, "job-id", &42u32.to_be_bytes())]))]).unwrap(); + let destination = super::Destination { name: "Office café%20?#".into(), is_class }; + assert_eq!(super::submit_as_at(fake.endpoint(), &destination, b"pdf", &Job::default(), "artist").unwrap(), 42); + let kind = if is_class { "classes" } else { "printers" }; + let path = format!("/{kind}/Office%20caf%C3%A9%2520%3F%23"); + let request = fake.request().unwrap(); + assert!(has(&request, format!("POST {path} HTTP/1.1").as_bytes())); + assert!(has(ipp_body(&request).unwrap(), format!("ipp://{}{path}", fake.server.authority()).as_bytes())); + } +} + +#[test] +fn destination_path_rejects_routing_syntax_before_connecting() { + for name in ["", ".", "..", "../admin", "a/b", "a\\b", "a\r\nb", "a\0b"] { + let destination = super::Destination { name: name.into(), is_class: false }; + assert!(super::destination_path(&destination).is_err(), "{name:?}"); + } + let destination = super::Destination { name: "x".repeat(257), is_class: false }; + assert!(super::destination_path(&destination).is_err()); +} + +#[test] +fn separate_queue_query_preserves_optional_discovery_errors() { + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply(0x0403, &[&group(1)]))]).unwrap(); + assert!(super::queues_at(fake.endpoint(), true).is_err(), "authorization refusal is not an empty class list"); + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply(0x0501, &[&group(1)]))]).unwrap(); + assert!(super::queues_at(fake.endpoint(), true).unwrap().is_empty(), "unsupported class operation is absent"); + let fake = FakeCups::on_tcp(vec![http_reply(&ipp_reply(0, &[&group(1), &group(4), &printer_name("Pool")]))]).unwrap(); + let queues = super::queues_at(fake.endpoint(), true).unwrap(); + assert_eq!(queues.len(), 1); + assert!(queues[0].is_class); + assert!(!queues[0].is_default, "default resolution is caller-owned for this API"); + let request = fake.request().unwrap(); + assert_eq!(&ipp_body(&request).unwrap()[..4], &[1, 1, 0x40, 0x05]); +} + +#[test] +fn ipv6_endpoint_keeps_scope_for_resolution_and_escapes_wire_authority() { + assert_eq!(Server::parse("[::1]").unwrap().authority(), "[::1]:631"); + assert_eq!(Server::parse("::1").unwrap().authority(), "[::1]:631"); + let scoped = Server::parse("[fe80::1%en0]:8631").unwrap(); + assert!(matches!(&scoped, Server::Named(host, 8631) if host == "fe80::1%en0")); + assert_eq!(scoped.authority(), "[fe80::1%25en0]:8631"); + assert_eq!(Server::parse("fe80::1%en0:8631").unwrap().authority(), scoped.authority()); + assert_eq!(Server::parse("fe80::1%en0").unwrap().authority(), "[fe80::1%25en0]:631"); + assert!(Server::parse("[host]:631").is_err()); + assert_eq!(super::connect(&Server::Named("/invalid".into(), 631)).err().unwrap().kind(), std::io::ErrorKind::InvalidInput); + for bad in ["[::1]junk", "[::1]:0", "[::1]:abc", "[fe80::1%]:631", "[fe80::1%x%y]:631", "host%zone"] { + assert!(Server::parse(bad).is_err(), "{bad}"); + } +} + +#[test] +fn long_printer_descriptions_are_not_limited_like_queue_names() { + let description = "Studio ".repeat(200); + let body = ipp_reply(0, &[&group(1), &group(4), &printer_name("Studio"), &attr(0x41, "printer-info", description.as_bytes())]); + let parsed = super::parse_reply(&body).unwrap(); + assert_eq!(parsed.queues[0].info, description); +} + +#[test] +fn destination_limit_is_explicit_and_never_silently_truncates() { + let mut body = vec![1, 1, 0, 0, 0, 0, 0, 1]; + for index in 0..super::MAX_PRINTERS { + body.push(4); + body.extend(printer_name(&format!("Printer{index}"))); + } + body.push(3); + assert_eq!(super::parse_reply(&body).unwrap().queues.len(), super::MAX_PRINTERS); + assert_eq!(body.pop(), Some(3)); + body.push(4); + body.extend(printer_name("OneTooMany")); + body.push(3); + assert!(super::parse_reply(&body).err().unwrap().to_string().contains("too many CUPS destinations")); +} + +#[test] +fn closed_unix_peer_keeps_its_buffered_reply_readable_with_a_deadline() { + let (client, mut peer) = std::os::unix::net::UnixStream::pair().unwrap(); + let body = ipp_reply(0, &[&group(1), &group(4), &printer_name("Studio")]); + peer.write_all(&http_reply(&body)).unwrap(); + drop(peer); + let mut stream = super::Stream::Unix(client); + let mut reader = super::DeadlineReader { stream: &mut stream, end: std::time::Instant::now() + Duration::from_secs(1) }; + assert_eq!(super::read_reply(&mut reader).unwrap(), body); + let mut byte = [0]; + assert_eq!(reader.read(&mut byte).unwrap(), 0); + reader.end = std::time::Instant::now(); + assert_eq!(reader.read(&mut byte).unwrap_err().kind(), std::io::ErrorKind::TimedOut); +} + +#[test] +fn stalled_unix_peer_still_fails_within_the_absolute_read_deadline() { + let (client, _peer) = std::os::unix::net::UnixStream::pair().unwrap(); + let start = std::time::Instant::now(); + let mut stream = super::Stream::Unix(client); + let mut reader = super::DeadlineReader { stream: &mut stream, end: start + Duration::from_millis(80) }; + let mut byte = [0]; + let error = reader.read(&mut byte).unwrap_err(); + assert!(matches!(error.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock)); + assert!(start.elapsed() < Duration::from_secs(3)); +} diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs index dcc9c53f9..bf50e3a3f 100644 --- a/crates/print/src/lib.rs +++ b/crates/print/src/lib.rs @@ -8,6 +8,10 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +/// Direct local-CUPS transport, independent of command-line spooler selection. +#[cfg(not(target_arch = "wasm32"))] +pub mod cups; + #[cfg(any(test, windows))] mod progress; #[cfg(any(test, windows))] diff --git a/crates/psd/src/annotations.rs b/crates/psd/src/annotations.rs new file mode 100644 index 000000000..57b466843 --- /dev/null +++ b/crates/psd/src/annotations.rs @@ -0,0 +1,230 @@ +//! Bounded, borrowed views and serialization of PSD global `Anno` records. +//! +//! Framing follows PhotoCraft's annotation reader (Brandon Thomas, aa87012a), with +//! David Mann's minimum-length guard and the per-entry span fix from PhotoCraft +//! #11138. Coordinates, colour conversion, text decoding and popup defaults are +//! deliberately caller policy. Unknown records can be carried through byte-exact. + +use crate::{PsdError, Result}; + +/// Explicit limits shared by readers and writers. +#[derive(Clone, Copy, Debug)] +pub struct Limits { + /// Maximum complete block size. + pub bytes: usize, + /// Maximum annotation count. + pub records: usize, +} +impl Default for Limits { + fn default() -> Self { + Self { bytes: 16 * 1024 * 1024, records: 4096 } + } +} + +struct Reader<'a> { + d: &'a [u8], + p: usize, +} +impl<'a> Reader<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.p.checked_add(n).ok_or(PsdError::LimitExceeded("annotation offset"))?; + let s = self.d.get(self.p..end).ok_or(PsdError::UnexpectedEof { offset: self.p, needed: n })?; + self.p = end; + Ok(s) + } + fn u8(&mut self) -> Result { + Ok(self.take(1)?.first().copied().unwrap_or_default()) + } + fn u16(&mut self) -> Result { + Ok(u16::from_be_bytes(self.take(2)?.try_into().map_err(|_| PsdError::invalid("annotation u16"))?)) + } + fn u32(&mut self) -> Result { + Ok(u32::from_be_bytes(self.take(4)?.try_into().map_err(|_| PsdError::invalid("annotation u32"))?)) + } + fn key(&mut self) -> Result<[u8; 4]> { + self.take(4)?.try_into().map_err(|_| PsdError::invalid("annotation key")) + } + fn rect(&mut self) -> Result<[i32; 4]> { + Ok([self.u32()? as i32, self.u32()? as i32, self.u32()? as i32, self.u32()? as i32]) + } + fn pascal(&mut self) -> Result<&'a [u8]> { + let n = usize::from(self.u8()?); + let bytes = self.take(n)?; + if (n + 1) % 2 == 1 { + self.take(1)?; + } + Ok(bytes) + } +} + +/// A borrowed record, including its original length, padding and unknown fields. +#[derive(Clone, Copy, Debug)] +pub struct Record<'a> { + raw: &'a [u8], +} +impl<'a> Record<'a> { + /// Complete self-counting record, suitable for lossless passthrough. + pub fn raw(&self) -> &'a [u8] { + self.raw + } + /// Parse text records; other record kinds remain opaque. + pub fn text(&self) -> Result>> { + let mut r = Reader { d: self.raw, p: 4 }; + if r.key()? != *b"txtA" { + return Ok(None); + } + // Never read beyond this record, even if a text length points into its neighbour. + let open = r.u8()?; + let flags = r.u8()?; + let optional_blocks = r.u16()?; + let icon = r.rect()?; + let popup = r.rect()?; + let color_space = r.u16()?; + let color = [r.u16()?, r.u16()?, r.u16()?, r.u16()?]; + let author = r.pascal()?; + let name = r.pascal()?; + let modified = r.pascal()?; + let data_length = r.u32()?; + let data_key = r.key()?; + let size = usize::try_from(r.u32()?).map_err(|_| PsdError::LimitExceeded("annotation text"))?; + let data = r.take(size)?; + Ok(Some(Text { open, flags, optional_blocks, icon, popup, color_space, color, author, name, modified, data_length, data_key, data })) + } +} + +/// Wire-level text annotation. Text payload and Pascal strings remain bytes. +/// No text normalisation or application colour/coordinate policy is applied. +#[derive(Clone, Copy, Debug)] +pub struct Text<'a> { + /// Open state byte. + pub open: u8, + /// Flags byte. + pub flags: u8, + /// Optional block flags. + pub optional_blocks: u16, + /// Icon rectangle, top/left/bottom/right. + pub icon: [i32; 4], + /// Popup rectangle, top/left/bottom/right. + pub popup: [i32; 4], + /// PSD colour space. + pub color_space: u16, + /// Raw colour components. + pub color: [u16; 4], + /// Pascal author bytes, maximum 255. + pub author: &'a [u8], + /// Pascal name bytes, maximum 255. + pub name: &'a [u8], + /// Pascal modification-date bytes, maximum 255. + pub modified: &'a [u8], + /// Original self-counting data length, exposed for diagnostics. + /// The writer recomputes this field. + pub data_length: u32, + /// Usually `txtC`; retained without imposing application policy. + pub data_key: [u8; 4], + /// Payload, including any text byte-order mark. + pub data: &'a [u8], +} + +/// Parse complete framing before exposing records. Borrowed payloads do not allocate. +/// Trailing block bytes are accepted as in the existing PhotoCraft reader. +pub fn read(data: &[u8], limits: Limits) -> Result>> { + if data.len() > limits.bytes { + return Err(PsdError::LimitExceeded("annotation bytes")); + } + let mut r = Reader { d: data, p: 0 }; + r.u16()?; + r.u16()?; + let count = usize::try_from(r.u32()?).map_err(|_| PsdError::LimitExceeded("annotation count"))?; + if count > limits.records || count > data.len().saturating_sub(8) / 8 { + return Err(PsdError::LimitExceeded("annotation count")); + } + let mut out = Vec::new(); + out.try_reserve_exact(count).map_err(|_| PsdError::OutOfMemory("annotation records"))?; + for _ in 0..count { + let start = r.p; + let len = usize::try_from(r.u32()?).map_err(|_| PsdError::LimitExceeded("annotation length"))?; + // David Mann's zero-length guard: a self-counting record must advance. + if len < 8 { + return Err(PsdError::invalid("annotation record shorter than its header")); + } + r.p = start; + let record = Record { raw: r.take(len)? }; + record.text()?; // validate text spans, including data borrowed by later consumers + out.push(record); + } + Ok(out) +} + +fn append(out: &mut Vec, bytes: &[u8], limit: usize) -> Result<()> { + let len = out.len().checked_add(bytes.len()).ok_or(PsdError::LimitExceeded("annotation bytes"))?; + if len > limit { + return Err(PsdError::LimitExceeded("annotation bytes")); + } + out.try_reserve(bytes.len()).map_err(|_| PsdError::OutOfMemory("annotation bytes"))?; + out.extend_from_slice(bytes); + Ok(()) +} + +fn pascal(out: &mut Vec, bytes: &[u8], limit: usize) -> Result<()> { + let n = u8::try_from(bytes.len()).map_err(|_| PsdError::LimitExceeded("annotation Pascal string"))?; + append(out, &[n], limit)?; + append(out, bytes, limit)?; + if (bytes.len() + 1) % 2 == 1 { + append(out, &[0], limit)?; + } + Ok(()) +} + +/// Serialize one text record, computing both length fields. Caller supplies raw +/// text bytes and already-mapped colour/coordinates; nothing is truncated. +pub fn write_text(text: &Text<'_>, limits: Limits) -> Result> { + let mut out = Vec::new(); + append(&mut out, &[0; 4], limits.bytes)?; + append(&mut out, b"txtA", limits.bytes)?; + append(&mut out, &[text.open, text.flags], limits.bytes)?; + append(&mut out, &text.optional_blocks.to_be_bytes(), limits.bytes)?; + for value in text.icon.into_iter().chain(text.popup) { + append(&mut out, &value.to_be_bytes(), limits.bytes)?; + } + append(&mut out, &text.color_space.to_be_bytes(), limits.bytes)?; + for value in text.color { + append(&mut out, &value.to_be_bytes(), limits.bytes)?; + } + for value in [text.author, text.name, text.modified] { + pascal(&mut out, value, limits.bytes)?; + } + let n = u32::try_from(text.data.len()).map_err(|_| PsdError::LimitExceeded("annotation text"))?; + let data_length = n.checked_add(12).ok_or(PsdError::LimitExceeded("annotation data length"))?; + append(&mut out, &data_length.to_be_bytes(), limits.bytes)?; + append(&mut out, &text.data_key, limits.bytes)?; + append(&mut out, &n.to_be_bytes(), limits.bytes)?; + append(&mut out, text.data, limits.bytes)?; + let len = u32::try_from(out.len()).map_err(|_| PsdError::LimitExceeded("annotation record length"))?; + out.get_mut(..4).ok_or_else(|| PsdError::invalid("annotation header"))?.copy_from_slice(&len.to_be_bytes()); + Ok(out) +} + +/// Combine already-framed records into a version 2.1 block. Unknown record types +/// are preserved byte-exact; malformed text records are refused. +pub fn write(records: &[&[u8]], limits: Limits) -> Result> { + if records.len() > limits.records { + return Err(PsdError::LimitExceeded("annotation count")); + } + let count = u32::try_from(records.len()).map_err(|_| PsdError::LimitExceeded("annotation count"))?; + let mut out = Vec::new(); + append(&mut out, &[0, 2, 0, 1], limits.bytes)?; + append(&mut out, &count.to_be_bytes(), limits.bytes)?; + for raw in records { + let mut r = Reader { d: raw, p: 0 }; + let declared = usize::try_from(r.u32()?).map_err(|_| PsdError::LimitExceeded("annotation record length"))?; + if raw.len() < 8 || declared != raw.len() { + return Err(PsdError::invalid("annotation record length mismatch")); + } + Record { raw }.text()?; + append(&mut out, raw, limits.bytes)?; + } + Ok(out) +} + +#[cfg(test)] +mod tests; diff --git a/crates/psd/src/annotations/tests.rs b/crates/psd/src/annotations/tests.rs new file mode 100644 index 000000000..e207852d4 --- /dev/null +++ b/crates/psd/src/annotations/tests.rs @@ -0,0 +1,117 @@ +use super::*; + +fn note() -> Text<'static> { + Text { + open: 1, + flags: 0x1c, + optional_blocks: 1, + icon: [10, 20, 30, 36], + popup: [30, 40, 190, 280], + color_space: 0, + color: [65535, 1000, 3000, 0], + author: b"Writer", + name: b"", + modified: b"2026", + data_length: 0, + data_key: *b"txtC", + data: &[0xfe, 0xff, 0, 65, 0, 13, 0, 66], + } +} + +#[test] +fn text_and_unknown_sound_round_trip_without_copying_payloads() { + let limits = Limits::default(); + let text = write_text(¬e(), limits).unwrap(); + let sound = [0, 0, 0, 11, b's', b'n', b'd', b'A', 42, 3, 9]; + let block = write(&[&text, &sound], limits).unwrap(); + let parsed = read(&block, limits).unwrap(); + assert_eq!(parsed.len(), 2); + let view = parsed[0].text().unwrap().unwrap(); + assert_eq!(view.author, note().author); + assert_eq!(view.data, note().data); + assert_eq!(view.icon, note().icon); + assert_eq!(view.color, note().color); + assert_eq!(view.data_length, 20); + assert!(parsed[1].text().unwrap().is_none()); + assert_eq!(parsed[1].raw(), sound); + let slices: Vec<_> = parsed.iter().map(Record::raw).collect(); + assert_eq!(write(&slices, limits).unwrap(), block); + assert!(view.data.as_ptr() >= block.as_ptr()); + assert!(view.data.as_ptr() < block.as_ptr().wrapping_add(block.len())); +} + +#[test] +fn text_cannot_read_past_the_declared_entry_into_its_neighbour() { + // Regression from PhotoCraft #11138, without its app Note/Color policy. + let mut record = write_text(¬e(), Limits::default()).unwrap(); + let shortened = record.len() - note().data.len(); + record[..4].copy_from_slice(&(shortened as u32).to_be_bytes()); + let mut block = vec![0, 2, 0, 1, 0, 0, 0, 1]; + block.extend_from_slice(&record); + assert!(read(&block, Limits::default()).is_err()); + assert!(write(&[&record], Limits::default()).is_err()); +} + +#[test] +fn truncation_and_hostile_counts_lengths_fail_closed() { + let record = write_text(¬e(), Limits::default()).unwrap(); + let block = write(&[&record], Limits::default()).unwrap(); + for end in 0..block.len() { + assert!(read(&block[..end], Limits::default()).is_err(), "{end}"); + } + for len in [0u32, 1, 7, u32::MAX] { + let mut bad = block.clone(); + bad[8..12].copy_from_slice(&len.to_be_bytes()); + assert!(read(&bad, Limits::default()).is_err()); + } + let mut bad = block; + bad[4..8].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(read(&bad, Limits::default()).is_err()); +} + +#[test] +fn explicit_limits_and_pascal_length_are_not_silently_truncated() { + let record = write_text(¬e(), Limits::default()).unwrap(); + let block = write(&[&record], Limits::default()).unwrap(); + assert!(read(&block, Limits { records: 0, ..Limits::default() }).is_err()); + assert!(read(&block, Limits { bytes: block.len() - 1, ..Limits::default() }).is_err()); + assert!(write(&[&record], Limits { records: 0, ..Limits::default() }).is_err()); + assert!(write_text(¬e(), Limits { bytes: record.len() - 1, ..Limits::default() }).is_err()); + let long = [0; 256]; + let mut text = note(); + text.author = &long; + assert!(write_text(&text, Limits::default()).is_err()); +} + +#[test] +fn preserves_legacy_version_and_unused_data_header_acceptance() { + let record = write_text(¬e(), Limits::default()).unwrap(); + let mut block = write(&[&record], Limits::default()).unwrap(); + block[..4].copy_from_slice(&[0, 9, 0, 8]); + block.extend_from_slice(b"trailing"); + assert_eq!(read(&block, Limits::default()).unwrap()[0].raw(), record); +} + +#[test] +fn canonical_layout_matches_the_existing_photocraft_writer() { + // PhotoCraft annotations_map::write_note layout (Brandon Thomas, aa87012a). + // App RGB/position/text conversion happens before this shared wire writer. + let n = note(); + let mut expected = vec![0, 0, 0, 0]; + expected.extend_from_slice(b"txtA"); + expected.extend_from_slice(&[1, 0x1c, 0, 1]); + for value in [10i32, 20, 30, 36, 30, 40, 190, 280] { + expected.extend_from_slice(&value.to_be_bytes()); + } + for value in [0u16, 65535, 1000, 3000, 0] { + expected.extend_from_slice(&value.to_be_bytes()); + } + expected.extend_from_slice(b"\x06Writer\x00\x00\x00\x042026\x00"); + expected.extend_from_slice(&20u32.to_be_bytes()); + expected.extend_from_slice(b"txtC"); + expected.extend_from_slice(&8u32.to_be_bytes()); + expected.extend_from_slice(n.data); + let len = expected.len() as u32; + expected[..4].copy_from_slice(&len.to_be_bytes()); + assert_eq!(write_text(&n, Limits::default()).unwrap(), expected); +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 93acd5f39..8126437ff 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -54,6 +54,7 @@ pub mod aco; pub mod act; pub mod adjust; pub mod animation; +pub mod annotations; pub mod ase; pub mod asl; pub mod atn; diff --git a/crates/psd/src/smart.rs b/crates/psd/src/smart.rs index a8f778ccd..a2195bf5d 100644 --- a/crates/psd/src/smart.rs +++ b/crates/psd/src/smart.rs @@ -470,6 +470,55 @@ fn linked_file(item: &[u8], at: usize) -> Result { Ok(out) } +/// Header of an embedded (`liFD`) linked-layer item, without reading its payload. +/// Offsets are relative to the item (not its enclosing eight-byte length field). +/// A deferred reader can supply just the header, then validate the payload range +/// against the full record length before reading those bytes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct EmbeddedFileHeader { + /// Lossy UTF-8 identifier, matching Photoshop metadata display semantics. + pub uuid: String, + /// Lossy UTF-16 filename with trailing NULs removed. + pub name: String, + /// Start of the payload within the item. + pub data_offset: usize, + /// Declared payload length; the payload need not be resident. + pub data_len: usize, +} + +impl EmbeddedFileHeader { + /// Borrow the payload only when the supplied complete item contains it. + pub fn payload<'a>(&self, item: &'a [u8]) -> Option<&'a [u8]> { + item.get(self.data_offset..self.data_offset.checked_add(self.data_len)?) + } +} + +/// Read an embedded item's header from a possibly partial/deferred item buffer. +/// This deliberately does not require its declared payload to be present. A malformed +/// header or a non-embedded item is an error, not an empty embedded file. Callers walking +/// framed records may continue to the next record after such an error. +/// Ported from PhotoCraft's linked-item reader; record versions remain forward-compatible. +pub fn embedded_file_header(item: &[u8]) -> Result { + let mut r = Reader::new(item); + let kind = r.array::<4>()?; + let _version = r.u32()?; + let uuid = String::from_utf8_lossy(&read_pascal(&mut r, 1)?).into_owned(); + // A filename is metadata, never an unbounded allocation. Check before decoding. + if r.clone().u32()? > 32_768 { + return Err(PsdError::LimitExceeded("linked filename exceeds 32768 UTF-16 units")); + } + let name = UnicodeString::read(&mut r)?.to_string_lossy(); + r.skip(8)?; // file type and creator + let data_len = usize::try_from(r.u64()?).map_err(|_| PsdError::LimitExceeded("linked payload exceeds address space"))?; + if r.u8()? != 0 { + crate::descriptor::VersionedDescriptor::read(&mut r)?; + } + if kind != *b"liFD" { + return Err(PsdError::invalid("linked item is not embedded")); + } + Ok(EmbeddedFileHeader { uuid, name, data_offset: r.pos(), data_len }) +} + /// The four-character file type Photoshop records for an embedded file, from its first bytes: /// `8BPS` (`8BPB` for a PSB), `PNGf`, `JPEG`, `TIFF`, `GIFf`, or four spaces. pub fn file_type(bytes: &[u8]) -> [u8; 4] { @@ -519,7 +568,29 @@ pub struct LinkedRecord<'a> { pub raw: &'a [u8], } -impl LinkedRecord<'_> { +impl<'a> LinkedRecord<'a> { + /// The complete item, excluding framing and padding; absent for a damaged tail. + pub fn item(&self) -> Option<&'a [u8]> { + framed_record(self.raw).map(|(item, _)| item) + } + + /// Copy this complete record under a new ID, leaving the payload byte-identical. + /// Selection policy belongs to the caller; malformed tails cannot be copied. + pub fn copy_with_uuid(&self, new_uuid: &str) -> Option> { + let item = self.item()?; + let old_end = 9usize.checked_add(usize::from(*item.get(8)?))?; + let new_len = u8::try_from(new_uuid.len()).ok()?; + let mut copy = item.get(..8)?.to_vec(); + copy.push(new_len); + copy.extend_from_slice(new_uuid.as_bytes()); + copy.extend_from_slice(item.get(old_end..)?); + let mut out = Vec::with_capacity(copy.len() + 11); + out.put_u64(copy.len() as u64); + out.extend(copy); + out.resize(out.len().next_multiple_of(4), 0); + Some(out) + } + /// The unique id as text, when it is readable and valid UTF-8. pub fn uuid_str(&self) -> Option<&str> { std::str::from_utf8(self.uuid?).ok() @@ -545,17 +616,26 @@ fn record_uuid(item: &[u8]) -> Option<&[u8]> { /// The records of a linked layer block's data, in order, each with its unique id when its header /// is readable. A tail that is no complete record is returned as one record without an id. pub fn linked_records(data: &[u8]) -> Vec> { - let mut out = Vec::new(); + iter_linked_records(data).collect() +} + +/// Stream the same raw views as [`linked_records`] without allocating a record list. +/// Complete frames with malformed inner headers do not hide subsequent frames; +/// an incomplete tail is yielded once with no identifier. +pub fn iter_linked_records(data: &[u8]) -> impl Iterator> { let mut rest = data; - while !rest.is_empty() { + std::iter::from_fn(move || { + if rest.is_empty() { + return None; + } let Some((item, raw)) = framed_record(rest) else { - out.push(LinkedRecord { uuid: None, raw: rest }); - break; + let tail = rest; + rest = &[]; + return Some(LinkedRecord { uuid: None, raw: tail }); }; - out.push(LinkedRecord { uuid: record_uuid(item), raw }); rest = rest.get(raw.len()..).unwrap_or_default(); - } - out + Some(LinkedRecord { uuid: record_uuid(item), raw }) + }) } /// The unique ids of every record of a linked layer block's data that has a readable one. @@ -653,18 +733,7 @@ pub fn contains_linked_record(data: &[u8], record: &[u8]) -> bool { /// field and padding; the rest of the record (embedded, external or alias data) is kept. pub fn copy_linked_record(data: &[u8], uuid: &str, new_uuid: &str) -> Option> { let record = linked_records(data).into_iter().find(|r| r.uuid == Some(uuid.as_bytes()))?; - let (item, _) = framed_record(record.raw)?; - let old_end = 9usize.checked_add(usize::from(*item.get(8)?))?; - let new_len = u8::try_from(new_uuid.len()).ok()?; - let mut copy = item.get(..8)?.to_vec(); - copy.push(new_len); - copy.extend_from_slice(new_uuid.as_bytes()); - copy.extend_from_slice(item.get(old_end..)?); - let mut out = Vec::with_capacity(copy.len() + 11); - out.put_u64(copy.len() as u64); - out.extend(copy); - out.resize(out.len().next_multiple_of(4), 0); - Some(out) + record.copy_with_uuid(new_uuid) } impl PsdFile { @@ -1111,3 +1180,57 @@ mod tests { assert!(u64::from(w) * u64::from(h) <= MAX_BAKE_PIXELS); } } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod embedded_header_tests { + use super::*; + + #[test] + fn headers_do_not_require_resident_payload() { + let data = encode_embedded_file("id", "a.psd", b"payload"); + let record = iter_linked_records(&data).next().unwrap(); + let item = record.item().unwrap(); + let header = embedded_file_header(item).unwrap(); + assert_eq!(header.uuid, "id"); + assert_eq!(header.name, "a.psd"); + assert_eq!(header.payload(item), Some(&b"payload"[..])); + assert_eq!(embedded_file_header(&item[..header.data_offset]).unwrap(), header); + assert_eq!(header.payload(&item[..header.data_offset]), None); + for n in 0..header.data_offset { + assert!(embedded_file_header(&item[..n]).is_err()); + } + } + + #[test] + fn filename_limits_and_lossy_metadata_are_preserved() { + let data = encode_embedded_file("x", &"a".repeat(32767), b""); + let item = iter_linked_records(&data).next().unwrap().item().unwrap(); + assert_eq!(embedded_file_header(item).unwrap().name.len(), 32767); + let data = encode_embedded_file("x", &"a".repeat(32768), b""); + assert!(embedded_file_header(iter_linked_records(&data).next().unwrap().item().unwrap()).is_err()); + let data = encode_embedded_file("x", "y", b"payload"); + let mut item = iter_linked_records(&data).next().unwrap().item().unwrap().to_vec(); + item[9] = 0xff; + item[14..16].copy_from_slice(&0xd800u16.to_be_bytes()); + let h = embedded_file_header(&item).unwrap(); + assert_eq!(h.uuid, "\u{fffd}"); + assert_eq!(h.name, "\u{fffd}"); + item[0..4].copy_from_slice(b"liFE"); + assert!(embedded_file_header(&item).is_err()); + } + + #[test] + fn malformed_inner_records_do_not_hide_later_files() { + let mut data = vec![0; 8]; // A complete empty item, not a truncated frame. + data.extend(encode_embedded_file("wanted", "x", b"ok")); + data.extend([1, 2, 3]); // Incomplete tail retained, never interpreted as an item. + let records: Vec<_> = iter_linked_records(&data).collect(); + assert_eq!(records, linked_records(&data)); + assert!(embedded_file_header(records[0].item().unwrap()).is_err()); + let item = records[1].item().unwrap(); + assert_eq!(embedded_file_header(item).unwrap().payload(item), Some(&b"ok"[..])); + assert_eq!(records[2].item(), None); + assert_eq!(records[2].raw, &[1, 2, 3]); + } +} diff --git a/crates/video/src/av1/encode/README.md b/crates/video/src/av1/encode/README.md index 2df88ba64..0fd60c50f 100644 --- a/crates/video/src/av1/encode/README.md +++ b/crates/video/src/av1/encode/README.md @@ -8,9 +8,10 @@ no `unsafe`, builds for `wasm32-unknown-unknown`). The optional `av1-large-encode` feature exposes `LargeEncoder`, a tiled rav1e-backed encoder for 8-bit 4:2:0 frames up to 8192 samples per side. Use it when the clean-room `Encoder`'s one-tile limits (4096 samples per side and 2304 superblocks) would reject the frame. It supports constant -quantizer rate control and emits one low-latency packet per input frame. The fallback is native -only; the clean-room encoder remains available without the feature and supports 8- and 10-bit -input. +quantizer rate control. `push` returns packets available so far, which can be empty while frames +are buffered; `finish` returns delayed packets. Each packet includes its input frame number for +matching durations. The fallback is native only; the clean-room encoder remains available without +the feature and supports 8- and 10-bit input. ## What is implemented diff --git a/crates/video/src/av1/encode/large.rs b/crates/video/src/av1/encode/large.rs index 109a536dc..8ed26d52b 100644 --- a/crates/video/src/av1/encode/large.rs +++ b/crates/video/src/av1/encode/large.rs @@ -12,6 +12,14 @@ pub struct LargeEncoder { width: usize, height: usize, seq_header: Option>, + finished: bool, +} + +/// An encoded frame and the number of the input frame it represents. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct BufferedPacket { + pub input_frame_number: u64, + pub packet: Packet, } impl LargeEncoder { @@ -50,11 +58,13 @@ impl LargeEncoder { ec.max_key_frame_interval = cfg.keyint.clamp(1, 120) as u64; ec.tiles = std::thread::available_parallelism().map_or(4, |n| n.get()).clamp(2, 16); let ctx = Config::new().with_encoder_config(ec).new_context().map_err(|e| Error::InvalidConfig(format!("rav1e: {e}")))?; - Ok(Self { ctx, width: cfg.width as usize, height: cfg.height as usize, seq_header: None }) + Ok(Self { ctx, width: cfg.width as usize, height: cfg.height as usize, seq_header: None, finished: false }) } - /// Encodes one frame. The frame planes must match the configured 4:2:0 dimensions. - pub fn encode_with_key(&mut self, frame: &Frame<'_>, force_key: bool) -> Result { + fn submit(&mut self, frame: &Frame<'_>, force_key: bool) -> Result<(), Error> { + if self.finished { + return Err(Error::InvalidFrame("AV1 encoder was already finished".into())); + } if frame.y_stride < self.width || frame.uv_stride < self.width / 2 || frame.y.len() < frame.y_stride.saturating_mul(self.height) @@ -70,14 +80,54 @@ impl LargeEncoder { let params = FrameParameters { frame_type_override: if force_key { FrameTypeOverride::Key } else { FrameTypeOverride::No }, ..Default::default() }; self.ctx.send_frame((input, params)).map_err(|e| Error::InvalidFrame(format!("rav1e: {e}")))?; + Ok(()) + } + + fn packet(&mut self, packet: rav1e::prelude::Packet) -> Result { + let (data, seq) = sample_data(&packet.data)?; + if self.seq_header.is_none() { + self.seq_header = seq; + } + Ok(BufferedPacket { input_frame_number: packet.input_frameno, packet: Packet { data, keyframe: packet.frame_type == FrameType::KEY } }) + } + + fn drain(&mut self) -> Result, Error> { + let mut packets = Vec::new(); + loop { + match self.ctx.receive_packet() { + Ok(packet) => packets.push(self.packet(packet)?), + Err(rav1e::prelude::EncoderStatus::Encoded) => {} + Err(rav1e::prelude::EncoderStatus::NeedMoreData) if !self.finished => return Ok(packets), + Err(rav1e::prelude::EncoderStatus::LimitReached) if self.finished => return Ok(packets), + Err(e) => return Err(Error::InvalidFrame(format!("rav1e: {e}"))), + } + } + } + + /// Submit a frame and return all packets available now, which may be empty while the encoder + /// buffers input. Call [`Self::finish`] after the last frame to retrieve delayed packets. + pub fn push(&mut self, frame: &Frame<'_>, force_key: bool) -> Result, Error> { + self.submit(frame, force_key)?; + self.drain() + } + + /// Finish the stream and return its remaining packets. Repeated calls return no packets. + pub fn finish(&mut self) -> Result, Error> { + if !self.finished { + self.ctx.send_frame(None).map_err(|e| Error::InvalidFrame(format!("rav1e: {e}")))?; + self.finished = true; + } + self.drain() + } + + /// Encodes one frame if a packet is immediately available. The input is accepted even if + /// rav1e needs more frames and this returns an error; new callers should use [`Self::push`]. + pub fn encode_with_key(&mut self, frame: &Frame<'_>, force_key: bool) -> Result { + self.submit(frame, force_key)?; loop { match self.ctx.receive_packet() { Ok(packet) => { - let (data, seq) = sample_data(&packet.data)?; - if self.seq_header.is_none() { - self.seq_header = seq; - } - return Ok(Packet { data, keyframe: packet.frame_type == FrameType::KEY }); + return Ok(self.packet(packet)?.packet); } Err(rav1e::prelude::EncoderStatus::Encoded) => continue, Err(rav1e::prelude::EncoderStatus::NeedMoreData | rav1e::prelude::EncoderStatus::LimitReached) => { @@ -153,3 +203,67 @@ fn sample_data(data: &[u8]) -> Result<(Vec, Option>), Error> { } Ok((sample, sequence)) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn buffered_frames_are_returned_with_input_numbers_and_finish_drains() { + let cfg = EncoderConfig::new(64, 48, 24, 1); + let mut encoder = LargeEncoder::with_speed(&cfg, 10).unwrap(); + assert!(encoder.av1c().is_none()); + let y = vec![96; 64 * 48]; + let uv = vec![128; 32 * 24]; + let frame = Frame { y: &y, u: &uv, v: &uv, y_stride: 64, uv_stride: 32 }; + let mut packets = Vec::new(); + for i in 0..4 { + packets.extend(encoder.push(&frame, i == 2).unwrap()); + } + packets.extend(encoder.finish().unwrap()); + assert_eq!(packets.iter().map(|p| p.input_frame_number).collect::>(), [0, 1, 2, 3]); + assert!(packets[0].packet.keyframe); + assert!(packets[2].packet.keyframe, "requested key frame"); + assert!(encoder.av1c().is_some()); + #[cfg(feature = "av1-decode")] + { + let mut decoder = crate::av1::decode::Decoder::with_threads(1); + let mut frames = Vec::new(); + for packet in &packets { + frames.extend(decoder.decode(&packet.packet.data).unwrap()); + } + frames.extend(decoder.flush_result().unwrap()); + assert_eq!(frames.len(), 4); + assert!(frames.iter().all(|frame| (frame.width, frame.height) == (64, 48))); + } + assert!(encoder.finish().unwrap().is_empty()); + assert!(encoder.push(&frame, false).is_err()); + } + + #[test] + fn a_large_frame_can_be_flushed_even_when_the_first_push_is_buffered() { + let cfg = EncoderConfig::new(4112, 16, 30, 1); + let mut encoder = LargeEncoder::with_speed(&cfg, 10).unwrap(); + let y = vec![64; 4112 * 16]; + let uv = vec![128; 2056 * 8]; + let frame = Frame { y: &y, u: &uv, v: &uv, y_stride: 4112, uv_stride: 2056 }; + let mut packets = encoder.push(&frame, false).unwrap(); + packets.extend(encoder.finish().unwrap()); + assert_eq!(packets.len(), 1); + assert_eq!(packets[0].input_frame_number, 0); + assert!(packets[0].packet.keyframe); + assert!(!packets[0].packet.data.is_empty()); + assert!(encoder.av1c().is_some()); + } + + #[test] + fn invalid_frame_is_not_queued_and_empty_stream_finishes() { + let cfg = EncoderConfig::new(64, 48, 24, 1); + let mut encoder = LargeEncoder::with_speed(&cfg, 10).unwrap(); + let short = [0; 1]; + let frame = Frame { y: &short, u: &short, v: &short, y_stride: 64, uv_stride: 32 }; + assert!(encoder.push(&frame, false).is_err()); + assert!(encoder.finish().unwrap().is_empty()); + assert!(encoder.av1c().is_none()); + } +} diff --git a/crates/video/src/av1/encode/mod.rs b/crates/video/src/av1/encode/mod.rs index 3a954cef2..d6632a94d 100644 --- a/crates/video/src/av1/encode/mod.rs +++ b/crates/video/src/av1/encode/mod.rs @@ -23,7 +23,7 @@ mod writer; #[cfg(all(feature = "av1-large-encode", not(target_arch = "wasm32")))] mod large; #[cfg(all(feature = "av1-large-encode", not(target_arch = "wasm32")))] -pub use large::LargeEncoder; +pub use large::{BufferedPacket, LargeEncoder}; use bitw::obu; use craft_media::bitstream::BitWriter;