diff --git a/Cargo.lock b/Cargo.lock index 345553bd9..8e12a6995 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", @@ -2667,6 +2686,7 @@ version = "0.1.0" dependencies = [ "craft-image-codecs", "craft-xml", + "data-url", "flate2", "kurbo 0.13.1", "resvg 0.48.1", diff --git a/Cargo.toml b/Cargo.toml index 77b20da79..5d0bbbc66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,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 +89,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 0e8767dc6..b7cc99bf3 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -534,6 +534,23 @@ pub fn write_cube(l: &LutFile) -> String { cube_rows(s, l.data.as_chunks::<3>().0.iter().copied()) } +/// 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"); + cube_rows(s, l.data.as_chunks::<3>().0.iter().copied()) +} + fn cube_rows(mut s: String, rows: impl Iterator) -> String { for c in rows { s.push_str(&format!("{:.6} {:.6} {:.6}\n", c[0], c[1], c[2])); @@ -625,6 +642,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/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/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/svg/Cargo.toml b/crates/svg/Cargo.toml index c3912696d..3615a719c 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -27,9 +27,11 @@ usvg = { version = "=0.48.1", default-features = false, optional = true } # so callers that only need bounded SVG text/tree parsing do not pull in raster backends. resvg = { version = "=0.48.1", default-features = false, features = ["text", "raster-images"], optional = true } +data-url = { version = "0.3.2", optional = true } + [features] # `craft_svg::tree`: usvg behind the bounded entry point. -tree = ["dep:usvg"] +tree = ["dep:usvg", "dep:data-url"] # Text converted to paths (needs a font database in the options). tree-text = ["tree", "usvg/text"] # The system's fonts in `usvg::fontdb`. diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index 2f67edbda..cb33137b8 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -371,6 +371,16 @@ impl ImageResolver { self.used.xml_depth.store(depth, Ordering::Relaxed); } + /// Account for malformed image data URLs before usvg discards them without calling + /// its image resolver. Call once for the outer document, before `parse_text`. + /// Nested SVGs are checked automatically. The scan shares the normal XML limits and + /// streaming-validates URL encoding without allocating another decoded image buffer. + pub fn check_document(&self, svg: &str) -> Result<(), Error> { + let xml = checked(svg, &self.limits)?; + account_invalid_data_urls(&xml, &self.used); + Ok(()) + } + /// Snapshot accounting after parsing has finished. pub fn usage(&self) -> ImageUsage { ImageUsage { @@ -387,6 +397,27 @@ impl ImageResolver { } } +// usvg's data-URL decoder returns before resolve_data for invalid base64. Count +// these omissions separately, including feImage, without reading linked resources. +fn account_invalid_data_urls(xml: &roxmltree::Document<'_>, used: &Used) { + for node in xml.descendants().filter(|n| n.is_element() && matches!(n.tag_name().name(), "image" | "feImage")) { + let href = node.attribute(("http://www.w3.org/1999/xlink", "href")).or_else(|| node.attribute("href")); + let Some(href) = href else { continue }; + let url = match data_url::DataUrl::process(href) { + Ok(url) => url, + // Use the parser's normalized scheme recognition, including whitespace. + Err(data_url::DataUrlError::NoComma) => { + used.invalid_raster.fetch_add(1, Ordering::Relaxed); + continue; + } + Err(data_url::DataUrlError::NotADataUrl) => continue, + }; + if url.decode(|_| Ok::<(), std::convert::Infallible>(())).is_err() { + used.invalid_raster.fetch_add(1, Ordering::Relaxed); + } + } +} + fn raster_format(data: &[u8]) -> Option { match craft_image_codecs::detect(data)? { format @ (craft_image_codecs::Format::Png @@ -432,6 +463,24 @@ fn raster_image(used: &Used, limits: &Limits, data: Arc>, format: craft_ } } drop(seen); + // A valid header is not proof of valid pixels. resvg can silently omit a damaged + // image later; decode now under explicit limits so omission accounting is honest. + let decode_limits = craft_image_codecs::Limits::new(width, height, pixels, limits.embedded_raster_pixels.saturating_mul(8)); + let options = craft_image_codecs::DecodeOptions { limits: decode_limits, keep_orientation: true }; + if let Err(error) = craft_image_codecs::decode_as_with(format, &data, &options) { + // No tree retains this allocation on failure. Forget its address before it + // can be reused, but keep the pixel charge for the decode work already spent. + used.raster_data.lock().unwrap_or_else(PoisonError::into_inner).remove(&pointer); + match error { + craft_image_codecs::CodecError::LimitExceeded(_) => { + used.raster_limit.fetch_add(1, Ordering::Relaxed); + } + _ => { + used.invalid_raster.fetch_add(1, Ordering::Relaxed); + } + } + return None; + } Some(match format { craft_image_codecs::Format::Png => usvg::ImageKind::PNG(data), craft_image_codecs::Format::Jpeg => usvg::ImageKind::JPEG(data), @@ -501,6 +550,7 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' let previous_depth = used.xml_depth.swap(total_depth, Ordering::Relaxed); let depth = used.depth.fetch_add(1, Ordering::Relaxed).saturating_add(1); used.svg_depth.fetch_max(depth, Ordering::Relaxed); + account_invalid_data_urls(&xml, used); let tree = usvg::Tree::from_xmltree(&xml, options); used.xml_depth.store(previous_depth, Ordering::Relaxed); used.depth.fetch_sub(1, Ordering::Relaxed); diff --git a/crates/svg/src/tree/tests.rs b/crates/svg/src/tree/tests.rs index 80681cad4..103c0fe7c 100644 --- a/crates/svg/src/tree/tests.rs +++ b/crates/svg/src/tree/tests.rs @@ -492,3 +492,95 @@ mod draw_checks { assert!(!refusal.memory && refusal.message.contains("clip-paths"), "{refusal:?}"); } } + +mod invalid_image_accounting { + use super::*; + + fn usage(body: &str, limits: Limits) -> (Vec, ImageUsage) { + let text = svg(body); + let resolver = ImageResolver::new(limits); + resolver.check_document(&text).unwrap(); + let options = usvg::Options { image_href_resolver: resolver.resolver(), ..Default::default() }; + let tree = parse_text(&text, &limits, &options).unwrap(); + let mut found = Vec::new(); + images(tree.root(), &mut found); + (found, resolver.usage()) + } + + fn png() -> Vec { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image, SampleType, encode}; + let image = Image::new(1, 1, ChannelLayout::Rgba, SampleType::U8).unwrap(); + encode(&image, Format::Png, &EncodeOptions::default()).unwrap() + } + + fn image(data: &[u8]) -> String { + let encoded: String = data.iter().map(|b| format!("%{b:02X}")).collect(); + format!(r#""#) + } + + #[test] + fn malformed_base64_is_counted_before_usvg_discards_it() { + let bad = r#""#; + let (found, report) = usage(bad, shallow()); + assert!(found.is_empty()); + assert_eq!(report.invalid_raster_images, 1); + let (found, report) = usage(&embed(&svg(bad)), shallow()); + assert_eq!(found, [true]); + assert_eq!(report.invalid_raster_images, 1); + } + + #[test] + fn malformed_data_url_without_comma_is_counted() { + for href in ["data:image/png;base64", " data:image/png;base64", "da ta:image/png;base64", "da ta:image/png;base64"] { + let bad = format!(r#""#); + let (found, report) = usage(&bad, shallow()); + assert!(found.is_empty(), "{href}"); + assert_eq!(report.invalid_raster_images, 1, "{href}"); + let (found, report) = usage(&embed(&svg(&bad)), shallow()); + assert_eq!(found, [true], "{href}"); + assert_eq!(report.invalid_raster_images, 1, "{href}"); + } + } + + #[test] + fn failed_decode_forgets_address_but_preserves_spent_budget() { + let valid = png(); + let idat = valid.windows(4).position(|b| b == b"IDAT").unwrap(); + let invalid = Arc::new(valid[..idat + 4].to_vec()); + let pointer = Arc::as_ptr(&invalid) as usize; + let limits = Limits { embedded_raster_pixels: 3, ..shallow() }; + let resolver = ImageResolver::new(limits); + let used = &resolver.used; + let format = craft_image_codecs::Format::Png; + assert!(raster_image(used, &limits, invalid, format).is_none()); + assert!(!used.raster_data.lock().unwrap().contains(&pointer)); + assert_eq!(resolver.usage().raster_pixels, 1); + let first_data = Arc::new(valid.clone()); + let first = raster_image(used, &limits, first_data.clone(), format).unwrap(); + assert_eq!(resolver.usage().raster_pixels, 2); + let repeated = raster_image(used, &limits, first_data, format).unwrap(); + assert_eq!(resolver.usage().raster_pixels, 2); + let second = raster_image(used, &limits, Arc::new(valid.clone()), format).unwrap(); + assert_eq!(resolver.usage().raster_pixels, 3); + assert!(raster_image(used, &limits, Arc::new(valid), format).is_none()); + assert_eq!(resolver.usage().raster_limit_images, 1); + drop((first, repeated, second)); + } + + #[test] + fn corrupt_pixels_do_not_pass_a_valid_header_probe() { + let valid = png(); + let (found, report) = usage(&image(&valid), shallow()); + assert_eq!(found, [false]); + assert_eq!(report.invalid_raster_images, 0); + let idat = valid.windows(4).position(|b| b == b"IDAT").unwrap(); + let corrupt = &valid[..idat + 4]; + assert_eq!(craft_image_codecs::probe_allocation_size(corrupt).unwrap(), (1, 1)); + let (found, report) = usage(&image(corrupt), shallow()); + assert!(found.is_empty()); + assert_eq!(report.invalid_raster_images, 1); + let (found, report) = usage(&image(&valid), Limits { embedded_raster_pixels: 0, ..shallow() }); + assert!(found.is_empty()); + assert_eq!(report.raster_limit_images, 1); + } +} 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;