diff --git a/Cargo.lock b/Cargo.lock index add90f8c2..281540f7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1843,6 +1843,13 @@ dependencies = [ "image", ] +[[package]] +name = "craft-gzip" +version = "0.1.0" +dependencies = [ + "flate2", +] + [[package]] name = "craft-hdr" version = "0.1.0" @@ -2383,13 +2390,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-plist" -version = "0.1.0" -dependencies = [ - "thiserror 2.0.21", -] - [[package]] name = "craft-platform" version = "0.1.0" @@ -2407,6 +2407,13 @@ dependencies = [ "zbus", ] +[[package]] +name = "craft-plist" +version = "0.1.0" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-plotter" version = "0.1.0" @@ -2704,6 +2711,7 @@ dependencies = [ "data-url", "flate2", "kurbo 0.13.1", + "quick-xml 0.41.0", "resvg 0.48.1", "roxmltree 0.21.2", "thiserror 2.0.21", @@ -2720,6 +2728,16 @@ dependencies = [ "kurbo 0.13.1", ] +[[package]] +name = "craft-tcraft" +version = "0.1.0" +dependencies = [ + "craft-zip", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-text" version = "0.1.0" @@ -2849,6 +2867,7 @@ name = "craft-vectorcraft-format" version = "0.1.0" dependencies = [ "flate2", + "proptest", "serde", "serde_json", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index eae758e17..0c9729e00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ hayro-syntax = { version = "=0.7.2", default-features = false, features = ["std" bytemuck = "1" kurbo = "0.13" thiserror = "2" +craft-gzip = { path = "crates/gzip" } craft-aaf = { path = "crates/aaf" } craft-atomic = { path = "crates/atomic" } craft-captions = { path = "crates/captions" } @@ -146,6 +147,7 @@ craft-stencilcraft-document = { path = "crates/stencilcraft-document" } craft-svg-paths = { path = "crates/svg-paths" } craft-stream-codecs = { path = "crates/stream-codecs" } craft-text = { path = "crates/text" } +craft-tcraft = { path = "crates/tcraft" } craft-texture = { path = "crates/texture" } craft-tga = { path = "crates/tga" } craft-timeline = { path = "crates/timeline" } diff --git a/crates/gzip/Cargo.toml b/crates/gzip/Cargo.toml new file mode 100644 index 000000000..3d4f04603 --- /dev/null +++ b/crates/gzip/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-gzip" +description = "Streaming gzip writer with stored and compressed segments" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +flate2 = { version = "1", default-features = false, features = ["rust_backend"] } diff --git a/crates/gzip/src/lib.rs b/crates/gzip/src/lib.rs new file mode 100644 index 000000000..33f2a5ac1 --- /dev/null +++ b/crates/gzip/src/lib.rs @@ -0,0 +1,119 @@ +//! Streaming gzip output with optional stored segments. +//! +//! Copyright (c) 2026 ArtCraft Team and the VectorCraft contributors. +//! Extracted from VectorCraft, licensed under MIT OR Apache-2.0. +//! +//! `flush` does not emit a deflate block; call [`Gz::finish`] to complete and flush the +//! stream. After an I/O error, discard the writer: the output may be incomplete. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use flate2::{Compress, Compression, Crc, FlushCompress, Status}; +use std::io::{self, Write}; + +/// Bytes of compressed output gathered before they go to the writer. +const OUT_CHUNK: usize = 64 << 10; + +/// A gzip stream, written as [`flate2::write::GzEncoder`] at the default level writes it (the +/// same header and the same data; the deflate blocks are the same too where flate2 deflates with +/// miniz_oxide, not with zlib-rs, which writes them by how the data is fed), except that data which +/// is compressed already ([`Gz::packed`]) is stored as it is: the default level spends a second +/// or more per photo on it for next to nothing, and makes it slower to unpack. Where the level +/// changes the deflate blocks so far end with a sync flush and a fresh compressor writes on, +/// which makes one valid deflate stream that every reader unpacks. +pub struct Gz { + out: W, + deflate: Compress, + /// What `deflate` writes: stored blocks (packed data), else the default level. + stored: bool, + crc: Crc, + buf: Vec, + /// Bytes written (unpacked). + total: u64, +} + +impl Gz { + /// Write a gzip header and construct a writer. + pub fn new(mut out: W) -> io::Result { + // flate2's header for the default level: no name, no time, unknown OS. + out.write_all(&[0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 255])?; + Ok(Self { + out, + deflate: Compress::new(Compression::default(), false), + stored: false, + crc: Crc::new(), + buf: Vec::with_capacity(OUT_CHUNK), + total: 0, + }) + } + + /// Run the compressor over `input` with `flush` until it has taken it all (and, flushing, + /// written everything out). + fn deflate(&mut self, mut input: &[u8], flush: FlushCompress) -> io::Result<()> { + loop { + if self.buf.len() == self.buf.capacity() { + self.out.write_all(&self.buf)?; + self.buf.clear(); + } + let (before_in, before_out) = (self.deflate.total_in(), self.deflate.total_out()); + let status = self.deflate.compress_vec(input, &mut self.buf, flush).map_err(io::Error::other)?; + let taken = usize::try_from(self.deflate.total_in() - before_in).unwrap_or(input.len()).min(input.len()); + input = input.get(taken..).unwrap_or_default(); + let made = self.deflate.total_out() - before_out; + let full = self.buf.len() == self.buf.capacity(); + match flush { + FlushCompress::None if input.is_empty() => return Ok(()), + FlushCompress::Finish if status == Status::StreamEnd => return Ok(()), + // A flush is done once it left room in the output. + FlushCompress::Sync if input.is_empty() && !full => return Ok(()), + _ => {} + } + if taken == 0 && made == 0 && !full { + return Err(io::Error::other("the compressor made no progress")); + } + } + } + + /// Return the number of uncompressed bytes supplied. + pub fn written(&self) -> u64 { + self.total + } + + /// What is written next is compressed already (stored as it is), or not (compressed at the + /// default level). + pub fn packed(&mut self, packed: bool) -> io::Result<()> { + if packed == self.stored { + return Ok(()); + } + self.deflate(&[], FlushCompress::Sync)?; + self.deflate = Compress::new(if packed { Compression::none() } else { Compression::default() }, false); + self.stored = packed; + Ok(()) + } + + /// Complete the gzip trailer and flush the underlying writer. + pub fn finish(mut self) -> io::Result<()> { + self.deflate(&[], FlushCompress::Finish)?; + self.out.write_all(&self.buf)?; + self.out.write_all(&self.crc.sum().to_le_bytes())?; + self.out.write_all(&self.crc.amount().to_le_bytes())?; + self.out.flush() + } +} + +impl Write for Gz { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.write_all(buf)?; + Ok(buf.len()) + } + + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + self.total = self.total.checked_add(buf.len() as u64).ok_or_else(|| io::Error::other("uncompressed byte count overflow"))?; + self.crc.update(buf); + self.deflate(buf, FlushCompress::None) + } + + fn flush(&mut self) -> io::Result<()> { + // Flushing would end a deflate block: the stream is flushed when it is finished. + Ok(()) + } +} diff --git a/crates/gzip/tests/writer.rs b/crates/gzip/tests/writer.rs new file mode 100644 index 000000000..38981ad0a --- /dev/null +++ b/crates/gzip/tests/writer.rs @@ -0,0 +1,77 @@ +use std::io::{self, Read, Write}; + +use craft_gzip::Gz; +use flate2::{Compression, read::GzDecoder, write::GzEncoder}; + +fn decode(bytes: &[u8]) -> Vec { + let mut result = Vec::new(); + GzDecoder::new(bytes).read_to_end(&mut result).unwrap(); + result +} + +#[test] +fn matches_flate2_without_mode_changes() { + let data = b"The quick brown fox jumps over the lazy dog. ".repeat(10000); + let mut expected = GzEncoder::new(Vec::new(), Compression::default()); + let mut actual = Vec::new(); + { + let mut gz = Gz::new(&mut actual).unwrap(); + for chunk in data.chunks(131) { + expected.write_all(chunk).unwrap(); + gz.write_all(chunk).unwrap(); + } + assert_eq!(gz.written(), data.len() as u64); + gz.finish().unwrap(); + } + assert_eq!(actual, expected.finish().unwrap()); + assert_eq!(decode(&actual), data); +} + +#[test] +fn mixed_segments_round_trip_and_stored_bytes_remain_uncompressed() { + let plain = b"repeat me ".repeat(20000); + let packed: Vec = (0..200_000).map(|i| (i * 31) as u8).collect(); + let mut bytes = Vec::new(); + { + let mut gz = Gz::new(&mut bytes).unwrap(); + gz.write_all(&plain).unwrap(); + gz.packed(true).unwrap(); + gz.packed(true).unwrap(); + gz.write_all(&packed).unwrap(); + gz.packed(false).unwrap(); + gz.write_all(&plain).unwrap(); + assert_eq!(gz.written(), (plain.len() * 2 + packed.len()) as u64); + gz.finish().unwrap(); + } + let expected = [plain.as_slice(), packed.as_slice(), plain.as_slice()].concat(); + assert_eq!(decode(&bytes), expected); + assert!(bytes.windows(1024).any(|window| window == &packed[..1024])); + assert!(bytes.len() < expected.len()); +} + +struct FailAfter { + remaining: usize, +} +impl Write for FailAfter { + fn write(&mut self, data: &[u8]) -> io::Result { + if self.remaining == 0 { + return Err(io::Error::other("injected failure")); + } + let n = self.remaining.min(data.len()); + self.remaining -= n; + Ok(n) + } + fn flush(&mut self) -> io::Result<()> { + Err(io::Error::other("injected flush failure")) + } +} + +#[test] +fn propagates_header_body_and_flush_failures() { + assert!(Gz::new(FailAfter { remaining: 0 }).is_err()); + let mut body = Gz::new(FailAfter { remaining: 10 }).unwrap(); + body.packed(true).unwrap(); + assert!(body.write_all(&vec![42; 200_000]).is_err()); + let empty = Gz::new(FailAfter { remaining: usize::MAX }).unwrap(); + assert_eq!(empty.finish().unwrap_err().to_string(), "injected flush failure"); +} diff --git a/crates/image-codecs/src/codecs/png.rs b/crates/image-codecs/src/codecs/png.rs index 4ceb75403..9965dfc30 100644 --- a/crates/image-codecs/src/codecs/png.rs +++ b/crates/image-codecs/src/codecs/png.rs @@ -141,12 +141,12 @@ impl> PngDecodeRows { /// Reads the header and refuses what [`decode`] refuses before any pixel is decoded: sizes /// past `limits`, and a file too small for the image it declares. pub fn new(bytes: B, limits: &Limits) -> Result { - Self::open(bytes, limits, true) + Self::open(bytes, limits, true, false) } /// [`PngDecodeRows::new`], optionally without reserving the whole image: a caller that hands /// the rows on as they come ([`decode_rows`]) never holds them all. - fn open(bytes: B, limits: &Limits, reserve: bool) -> Result { + fn open(bytes: B, limits: &Limits, reserve: bool, bounded_stream: bool) -> Result { let at = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0)); let bytes = SharedInput(std::sync::Arc::new(bytes)); let input = Tracked { cursor: Cursor::new(bytes.clone()), at: at.clone() }; @@ -187,7 +187,14 @@ impl> PngDecodeRows { let (width, height) = info.frame_control.map_or_else(|| info.size(), |f| (f.width, f.height)); let line = reader.output_line_size(width).ok_or_else(|| err("image too large"))?; // The output layout is known: charge its real bytes per pixel, not RGBA16's worst case. - limits.check(width, height, layout, sample)?; + if bounded_stream { + limits.check_size(width, height)?; + if line > limits.alloc_usize() { + return Err(CodecError::LimitExceeded("one PNG row exceeds max_alloc".into())); + } + } else { + limits.check(width, height, layout, sample)?; + } let height = usize::try_from(height).map_err(|_| err("image too large"))?; if width == 0 || height == 0 { return Err(err("the image is empty")); @@ -234,11 +241,35 @@ impl> PngDecodeRows { /// decode the whole image after all (an interlaced file) calls /// [`PngDecodeRows::reserve_output`] before [`PngDecodeRows::step`]. pub fn new_streaming(bytes: B, limits: &Limits) -> Result { - Self::open(bytes, limits, false) + Self::open(bytes, limits, false, false) + } + + /// Opens a bounded row stream without charging a full image that is never retained. + /// Dimension, pixel-count, plausibility, decoder and metadata limits remain unchanged; + /// `max_alloc` also bounds each row group. It is not a total process-memory limit. + /// Unlike [`Self::new_streaming`], a streamable image may exceed the full-image budget. + /// Non-streamable images still require that budget, as do [`Self::step`] and + /// [`Self::reserve_output`]. The caller must bound any output it retains in its sink. + pub fn new_bounded_streaming(bytes: B, limits: &Limits) -> Result { + let decoder = Self::open(bytes, limits, false, true)?; + if !decoder.streamable() { + decoder.check_full_output()?; + } + Ok(decoder) + } + + fn check_full_output(&self) -> Result<(), CodecError> { + let height = u32::try_from(self.height).map_err(|_| err("image too large"))?; + self.limits.check(self.width, height, self.layout, self.sample)?; + if self.size > self.limits.alloc_usize() { + return Err(CodecError::LimitExceeded("PNG output exceeds max_alloc".into())); + } + Ok(()) } /// Reserves the buffer [`PngDecodeRows::step`] decodes the whole image into. pub fn reserve_output(&mut self) -> Result<(), CodecError> { + self.check_full_output()?; let size = self.size; self.buf.try_reserve_exact(size).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {size} bytes for the image"))) } @@ -271,6 +302,7 @@ impl> PngDecodeRows { /// Decodes at least one row (the whole image if it is interlaced), then goes on while `more()` /// returns true. `Some` once the image is complete, after which the decoder is spent. pub fn step(&mut self, more: &mut dyn FnMut() -> bool) -> Result, CodecError> { + self.check_full_output()?; if self.done { return Err(err("the image was already decoded")); } @@ -338,7 +370,9 @@ impl> PngDecodeRows { /// `more()` returns true; `Some` with the extras once the file is read to its end, with the /// resolution a format that lacks one takes from its EXIF or XMP. A damaged file fails with /// the error [`decode`] gives, after however many rows were delivered. An orientation (an - /// `eXIf` after the image data may set one) is for the caller to apply. + /// `eXIf` after the image data may set one) is for the caller to apply. A new group size + /// must exceed any partial group retained from an earlier yield; otherwise this returns + /// an error without consuming another row. pub fn step_stream>( &mut self, group: u32, @@ -348,12 +382,29 @@ impl> PngDecodeRows { if self.done { return Err(err("the image was already decoded").into()); } - let group = usize::try_from(group.max(1)).unwrap_or(usize::MAX); + // Tail EXIF may report an orientation after rows have already been delivered; + // that belongs in the returned extras, not a rejection of metadata continuation. + if self.rows == 0 && !self.tail && !self.streamable() { + return Err(err("this PNG requires whole-image decoding").into()); + } + let group = usize::try_from(group.max(1)).unwrap_or(usize::MAX).min(self.height); + if self.rows - self.group_start >= group { + return Err(err("PNG row group is smaller than rows held from the previous step").into()); + } + let group_bytes = group.checked_mul(self.line).ok_or_else(|| err("PNG row group is too large"))?; + if group_bytes > self.limits.alloc_usize() { + return Err(CodecError::LimitExceeded("PNG row group exceeds max_alloc".into()).into()); + } if !self.tail { while self.rows < self.height { let held = self.rows - self.group_start; let at = held.checked_mul(self.line).ok_or_else(|| err("image too large"))?; let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; + // A caller may change its group size between yields. Bound the actual held + // bytes as well as the requested group before growing the row buffer. + if end > self.limits.alloc_usize() { + return Err(CodecError::LimitExceeded("PNG row group exceeds max_alloc".into()).into()); + } if self.buf.len() < end { self.buf .try_reserve_exact(end - self.buf.len()) @@ -368,7 +419,7 @@ impl> PngDecodeRows { to_native_endian(row); } self.rows += 1; - if self.rows - self.group_start == group || self.rows == self.height { + if self.rows - self.group_start >= group || self.rows == self.height { let data = self.buf.get(..end).ok_or_else(|| err("invalid PNG row group"))?; sink(u32::try_from(self.group_start).map_err(|_| err("image too large"))?, data)?; self.group_start = self.rows; diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 961a04327..a62de6e61 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -28,6 +28,7 @@ mod inspection; mod lab; mod options; pub mod orientation; +pub mod png_probe; pub mod resolution; pub mod source_settings; pub mod web; diff --git a/crates/image-codecs/src/png_probe.rs b/crates/image-codecs/src/png_probe.rs new file mode 100644 index 000000000..73f1199eb --- /dev/null +++ b/crates/image-codecs/src/png_probe.rs @@ -0,0 +1,190 @@ +//! Header-only PNG inspection without inflating image data. +use crate::{CodecError, Format}; +fn err(message: &str) -> CodecError { + CodecError::malformed(Format::Png, message) +} + +/// Dimensions declared in a PNG IHDR prefix, without checking the rest of the file. +/// Useful only for an early allocation limit; use [`header`] for structural inspection. +pub fn declared_dimensions(b: &[u8]) -> Option<(u32, u32)> { + if !b.starts_with(b"\x89PNG\r\n\x1a\n") || b.get(12..16) != Some(b"IHDR") { + return None; + } + let w = u32::from_be_bytes(b.get(16..20)?.try_into().ok()?); + let h = u32::from_be_bytes(b.get(20..24)?.try_into().ok()?); + Some((w, h)) +} + +/// Stored dimensions and EXIF orientation. This does not validate CRCs or compressed pixels. +pub fn header(b: &[u8]) -> Result<(u32, u32, u16), CodecError> { + header_with_max_pixels(b, u64::MAX) +} + +/// As [`header`], refusing images over `max_pixels` before scanning their chunks. +pub fn header_with_max_pixels(b: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), CodecError> { + if !b.starts_with(b"\x89PNG\r\n\x1a\n") { + return Err(err("bad signature")); + } + let be32 = |p: usize| b.get(p..p.checked_add(4)?).map(|v| u32::from_be_bytes([v[0], v[1], v[2], v[3]])); + if be32(8) != Some(13) || b.get(12..16) != Some(b"IHDR") { + return Err(err("missing IHDR")); + } + let ihdr = b.get(16..29).ok_or_else(|| err("truncated IHDR"))?; + let (w, h) = (u32::from_be_bytes([ihdr[0], ihdr[1], ihdr[2], ihdr[3]]), u32::from_be_bytes([ihdr[4], ihdr[5], ihdr[6], ihdr[7]])); + let (depth, color, compression, filter, interlace) = (ihdr[8], ihdr[9], ihdr[10], ihdr[11], ihdr[12]); + if w > i32::MAX as u32 || h > i32::MAX as u32 { + return Err(err("dimensions out of range")); + } + if w == 0 || h == 0 { + return Err(err("zero dimension")); + } + if u64::from(w) * u64::from(h) > max_pixels { + return Err(CodecError::LimitExceeded("PNG pixel count".into())); + } + let depth_ok = match color { + 0 => matches!(depth, 1 | 2 | 4 | 8 | 16), + 3 => matches!(depth, 1 | 2 | 4 | 8), + 2 | 4 | 6 => matches!(depth, 8 | 16), + _ => false, + }; + if !depth_ok || compression != 0 || filter != 0 || interlace > 1 { + return Err(err("invalid IHDR")); + } + let (mut p, mut palette, mut idat, mut after_idat, mut exif) = (8usize, false, false, false, None); + while let Some(len) = be32(p) { + let Some(kind) = p.checked_add(4).and_then(|k| b.get(k..k.checked_add(4)?)) else { break }; + if idat && kind != b"IDAT" { + // a chunk follows the image data: the IDAT run is complete + after_idat = true; + } + if kind == b"IEND" { + break; + } + let start = p.saturating_add(8); + let data = (len <= i32::MAX as u32).then(|| start.checked_add(len as usize).and_then(|end| b.get(start..end))).flatten(); + let Some(data) = data else { + if after_idat { + break; // a damaged chunk after the image data: a decode ignores it too + } + return Err(err("truncated")); + }; + match kind { + b"PLTE" => palette = true, + b"IDAT" if after_idat => {} // a stray IDAT after other chunks: the first run is the image + b"IDAT" => { + if color == 3 && !palette { + return Err(err("indexed image without a palette")); + } + idat = true; + } + b"eXIf" if exif.is_none() => exif = Some(data.strip_prefix(b"Exif\0\0").unwrap_or(data)), + _ => {} + } + p = start.saturating_add(data.len()).saturating_add(4); + } + if !idat { + return Err(err("no image data")); + } + if !after_idat { + return Err(err("truncated image data")); + } + Ok((w, h, exif.map_or(1, craft_metadata::compat_orientation::orientation))) +} + +/// Container color declarations preceding image data. Raw values leave color policy to callers. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct ColorChunks { + pub srgb: bool, + pub gamma: Option, + pub chromaticities: Option<[f64; 8]>, +} + +/// Read bounded color chunks before image data; malformed tails are ignored. +pub fn color_chunks(b: &[u8]) -> Option { + if !b.starts_with(b"\x89PNG\r\n\x1a\n") { + return None; + } + let mut result = ColorChunks::default(); + let mut p = 8usize; + while let Some(length) = b.get(p..p.checked_add(4)?) { + let len = u32::from_be_bytes(length.try_into().ok()?) as usize; + let start = p.checked_add(8)?; + let Some(kind) = b.get(p.checked_add(4)?..start) else { break }; + let Some(payload) = start.checked_add(len).and_then(|end| b.get(start..end)) else { break }; + match kind { + b"sRGB" => result.srgb = true, + b"gAMA" if payload.len() == 4 => { + let v = u32::from_be_bytes(payload.try_into().ok()?) as f32 / 100_000.0; + if v > 0.0 && v.is_finite() { + result.gamma = Some(v); + } + } + b"cHRM" if payload.len() == 32 => { + let mut vals = [0.0; 8]; + for (i, dst) in vals.iter_mut().enumerate() { + let raw = i32::from_be_bytes(payload[i * 4..i * 4 + 4].try_into().ok()?); + *dst = f64::from(raw) / 100_000.0; + } + result.chromaticities = Some(vals); + } + b"IDAT" | b"IEND" => break, + _ => {} + } + let Some(next) = start.checked_add(len).and_then(|end| end.checked_add(4)) else { break }; + p = next; + } + Some(result) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn chunk(out: &mut Vec, kind: &[u8; 4], data: &[u8]) { + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + out.extend_from_slice(kind); + out.extend_from_slice(data); + out.extend_from_slice(&[0; 4]); // Header inspection deliberately does not validate CRCs. + } + + fn png(color: u8) -> Vec { + let mut bytes = b"\x89PNG\r\n\x1a\n".to_vec(); + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&2u32.to_be_bytes()); + ihdr.extend_from_slice(&3u32.to_be_bytes()); + ihdr.extend_from_slice(&[8, color, 0, 0, 0]); + chunk(&mut bytes, b"IHDR", &ihdr); + bytes + } + + #[test] + fn header_checks_structure_and_reads_late_orientation_without_crc_validation() { + let mut bytes = png(2); + chunk(&mut bytes, b"IDAT", &[1]); + let exif = b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00"; + chunk(&mut bytes, b"eXIf", exif); + chunk(&mut bytes, b"IEND", &[]); + assert_eq!(header(&bytes).ok(), Some((2, 3, 6))); + assert!(matches!(header_with_max_pixels(&bytes, 5), Err(CodecError::LimitExceeded(_)))); + assert_eq!(declared_dimensions(&bytes), Some((2, 3))); + assert!(header(&bytes[..34]).is_err()); + let mut indexed = png(3); + chunk(&mut indexed, b"IDAT", &[1]); + chunk(&mut indexed, b"IEND", &[]); + assert!(header(&indexed).is_err()); + indexed[25] = 7; + assert!(header(&indexed).is_err()); + } + + #[test] + fn reads_color_chunks_only_before_image_data() { + let mut bytes = png(2); + chunk(&mut bytes, b"gAMA", &45455u32.to_be_bytes()); + chunk(&mut bytes, b"IDAT", &[1]); + chunk(&mut bytes, b"sRGB", &[0]); + let color = color_chunks(&bytes).unwrap(); + assert_eq!(color.gamma, Some(0.45455)); + assert!(!color.srgb); + assert!(color_chunks(b"not png").is_none()); + } +} diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs index 6b213ea83..07c57bf24 100644 --- a/crates/image-codecs/tests/it/png_jpeg.rs +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -665,3 +665,97 @@ fn step_stream_gives_the_pixels_and_extras_of_decode() { fn dec_line(w: u32, layout: ChannelLayout, sample: SampleType) -> usize { w as usize * layout.channels() * sample.bytes() } + +#[test] +fn bounded_png_stream_does_not_charge_unretained_full_frame() { + let image = synth(256, 1024, ChannelLayout::Rgba, SampleType::U16, 77, 0.5); + let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap(); + let limits = Limits { max_alloc: 512 * 1024, ..Limits::default() }; + assert!(image.data().len() > limits.max_alloc as usize); + assert!(PngDecodeRows::new(bytes.as_slice(), &limits).is_err()); + assert!(PngDecodeRows::new_streaming(bytes.as_slice(), &limits).is_err()); + let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &limits).unwrap(); + assert!(decoder.streamable()); + // Opening a stream must not become a way to bypass whole-image allocation guards. + assert!(decoder.reserve_output().is_err()); + assert!(decoder.step(&mut || true).is_err()); + let mut next = 0; + let mut sink = |first: u32, rows: &[u8]| -> Result<(), CodecError> { + assert_eq!(first as usize, next); + assert!(rows.len() <= 3 * 256 * 8); + let start = next * 256 * 8; + assert_eq!(rows, &image.data()[start..start + rows.len()]); + next += rows.len() / (256 * 8); + Ok(()) + }; + while decoder.step_stream(3, &mut || false, &mut sink).unwrap().is_none() {} + assert_eq!(next, 1024); +} + +#[test] +fn bounded_png_stream_keeps_group_dimension_and_fallback_limits() { + let image = synth(256, 1024, ChannelLayout::Rgba, SampleType::U16, 81, 0.5); + let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap(); + let limits = Limits { max_alloc: 512 * 1024, ..Limits::default() }; + let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &limits).unwrap(); + assert!(decoder.step_stream::(1024, &mut || true, &mut |_, _| panic!("oversized group reached sink")).is_err()); + for strict in [ + Limits { max_width: 255, ..limits }, + Limits { max_height: 1023, ..limits }, + Limits { max_pixels: 256 * 1024 - 1, ..limits }, + Limits { max_alloc: 1024, ..limits }, + ] { + assert!(PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &strict).is_err()); + } + let adam7 = interlaced(&image); + assert!(PngDecodeRows::new_bounded_streaming(adam7.as_slice(), &limits).is_err()); +} + +#[test] +fn bounded_png_stream_rejects_shrinking_below_buffered_rows() { + let image = synth(16, 8, ChannelLayout::Rgba, SampleType::U16, 83, 0.5); + let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap(); + let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &Limits::default()).unwrap(); + for _ in 0..3 { + assert!(decoder.step_stream::(4, &mut || false, &mut |_, _| panic!("group is incomplete")).unwrap().is_none()); + } + assert!(decoder.step_stream::(1, &mut || true, &mut |_, _| panic!("changed group reached sink")).is_err()); + let mut output = Vec::new(); + decoder + .step_stream::(4, &mut || true, &mut |_, rows| { + output.extend_from_slice(rows); + Ok(()) + }) + .unwrap(); + assert_eq!(output, image.data()); +} + +#[test] +fn bounded_png_stream_preserves_late_exif_across_metadata_yields() { + let image = synth(16, 8, ChannelLayout::Rgba, SampleType::U16, 89, 0.5); + let bytes = encode(&image, Format::Png, &EncodeOptions::default()).unwrap(); + let mut exif = sample_exif(); + exif[18..22].copy_from_slice(&6u32.to_le_bytes()); + let mut chunks = vec![png_chunk(b"eXIf", &exif)]; + for i in 0..150 { + chunks.push(png_chunk(b"tEXt", format!("key{i}\0value").as_bytes())); + } + let bytes = with_chunks_before_iend(&bytes, &chunks); + let mut decoder = PngDecodeRows::new_bounded_streaming(bytes.as_slice(), &Limits::default()).unwrap(); + assert!(decoder.streamable()); + let mut output = Vec::new(); + let extras = loop { + if let Some(extras) = decoder + .step_stream::(2, &mut || false, &mut |_, rows| { + output.extend_from_slice(rows); + Ok(()) + }) + .unwrap() + { + break extras; + } + }; + assert_eq!(output, image.data()); + assert_eq!(extras.orientation, 6); + assert_eq!(extras.meta.text.len(), 150); +} diff --git a/crates/metadata/src/compat_orientation.rs b/crates/metadata/src/compat_orientation.rs new file mode 100644 index 000000000..3590b7c48 --- /dev/null +++ b/crates/metadata/src/compat_orientation.rs @@ -0,0 +1,52 @@ +//! Compatibility orientation reading for legacy clients that accepted loosely formed classic EXIF. +//! The ordinary `exif_orientation` API remains strict; use this only to retain older behavior. + +/// Read the last IFD0 orientation entry as older lightweight readers did. Entry count is bounded; +/// a complete entry table is required, but an absent next-IFD pointer is accepted. BYTE, UNDEFINED, +/// SHORT, LONG and IFD values are read inline regardless of count. Invalid/missing values give 1. +pub fn orientation(bytes: &[u8]) -> u16 { + let b = bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes); + let little = match b.get(..2) { + Some(b"II") => true, + Some(b"MM") => false, + _ => return 1, + }; + let u16_at = + |at: usize| b.get(at..at.checked_add(2)?).map(|v| if little { u16::from_le_bytes([v[0], v[1]]) } else { u16::from_be_bytes([v[0], v[1]]) }); + let u32_at = |at: usize| { + b.get(at..at.checked_add(4)?) + .map(|v| if little { u32::from_le_bytes([v[0], v[1], v[2], v[3]]) } else { u32::from_be_bytes([v[0], v[1], v[2], v[3]]) }) + }; + if u16_at(2) != Some(42) { + return 1; + } + let Some(ifd) = u32_at(4).and_then(|v| usize::try_from(v).ok()) else { return 1 }; + let Some(count) = u16_at(ifd).map(usize::from).filter(|n| *n <= 4096) else { return 1 }; + let Some(start) = ifd.checked_add(2) else { return 1 }; + let Some(end) = count.checked_mul(12).and_then(|n| start.checked_add(n)).filter(|end| *end <= b.len()) else { return 1 }; + let mut orientation = None; + for at in (start..end).step_by(12) { + if u16_at(at) == Some(0x0112) { + let value = match u16_at(at + 2) { + Some(3) => u16_at(at + 8).map(u32::from), + Some(4 | 13) => u32_at(at + 8), + Some(1 | 7) => b.get(at + 8).copied().map(u32::from), + _ => None, + }; + orientation = value.map(|v| v as u16).filter(|v| (1..=8).contains(v)); + } + } + orientation.unwrap_or(1) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn accepts_legacy_count_zero_but_bounds_the_table() { + let mut b = b"II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\0\0\0\0\x06\0\0\0".to_vec(); + assert_eq!(orientation(&b), 6); + b.truncate(21); + assert_eq!(orientation(&b), 1); + } +} diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index ff2c530fa..05f9b81b4 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -20,6 +20,7 @@ //! [`Embedded`] and call [`from_embedded`]. pub mod camera_fields; +pub mod compat_orientation; mod container; pub mod cr3; mod datetime; diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index 3615a719c..3b778da47 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -12,6 +12,7 @@ workspace = true [dependencies] craft-xml = { workspace = true } +quick-xml = { version = "0.41", default-features = false } craft-image-codecs = { workspace = true } # SVGZ (gzip), pure-Rust backend. flate2 = { version = "1", default-features = false, features = ["rust_backend"] } diff --git a/crates/svg/src/drawing_write.rs b/crates/svg/src/drawing_write.rs new file mode 100644 index 000000000..91b94d790 --- /dev/null +++ b/crates/svg/src/drawing_write.rs @@ -0,0 +1,56 @@ +//! SVG polyline and triangle path attributes for display-list exporters. +use crate::write::num; +use std::fmt::{self, Display}; + +/// A fixed-precision polyline attribute streamed into an SVG writer. +pub struct Points { + iter: I, + decimals: usize, +} +/// Format points without allocating an intermediate attribute string. Precision is capped at 32. +pub fn points + Clone>(iter: I, decimals: usize) -> Points { + Points { iter, decimals: decimals.min(32) } +} +impl + Clone> Display for Points { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for [x, y] in self.iter.clone() { + write!(f, "{},{} ", num(x, self.decimals), num(y, self.decimals))?; + } + Ok(()) + } +} +/// A fixed-precision sequence of closed triangle subpaths. +pub struct Triangles { + iter: I, + decimals: usize, +} +/// Format independent triangles without allocating an intermediate attribute string. Precision is capped at 32. +pub fn triangles + Clone>(iter: I, decimals: usize) -> Triangles { + Triangles { iter, decimals: decimals.min(32) } +} +impl + Clone> Display for Triangles { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for [[ax, ay], [bx, by], [cx, cy]] in self.iter.clone() { + write!( + f, + "M{} {}L{} {}L{} {}Z", + num(ax, self.decimals), + num(ay, self.decimals), + num(bx, self.decimals), + num(by, self.decimals), + num(cx, self.decimals), + num(cy, self.decimals) + )?; + } + Ok(()) + } +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn fixed_precision_attributes_keep_independent_triangles() { + assert_eq!(points([[1.0, 2.0], [3.0, 4.0]].into_iter(), 2).to_string(), "1.00,2.00 3.00,4.00 "); + assert_eq!(triangles([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]].into_iter(), 1).to_string(), "M1.0 2.0L3.0 4.0L5.0 6.0Z"); + } +} diff --git a/crates/svg/src/guard.rs b/crates/svg/src/guard.rs index 073bfe34c..f3573e443 100644 --- a/crates/svg/src/guard.rs +++ b/crates/svg/src/guard.rs @@ -178,6 +178,10 @@ pub fn too_wide(svg: &str, most: usize, most_work: usize) -> bool { pub struct Prescan<'s> { /// Whether the elements nest deeper than the cap. The pass stops there. pub too_deep: bool, + /// Deepest element nesting observed (markup entities count ten times). + pub depth: usize, + /// Deepest stack of unclosed source elements, excluding self-closing leaves. + pub open_depth: usize, /// The text with each data URL in an element's attribute values left out: a `data:` value's /// quotes kept, a `url(…)`'s parentheses and quotes. Borrowed when there is none, and the /// whole text when `too_deep`. An embedded picture is a data URL and most of the text when @@ -250,7 +254,7 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { (b.len(), false) }; // (now, deepest) of the elements, and of the markup inside the document type. - let (mut elements, mut entities) = ((0usize, 0usize), (0usize, 0usize)); + let (mut elements, mut entities, mut open_depth) = ((0usize, 0usize), (0usize, 0usize), 0usize); let mut doctype_end = 0; let mut i = 0; while let Some(at) = b.get(i..).and_then(|rest| rest.iter().position(|c| *c == b'<')).map(|at| i + at) { @@ -287,8 +291,14 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { end }; if elements.1.saturating_add(entities.1.saturating_mul(10)) > most { - return Prescan { too_deep: true, markers: Cow::Borrowed(svg) }; + return Prescan { + too_deep: true, + depth: elements.1.saturating_add(entities.1.saturating_mul(10)), + open_depth, + markers: Cow::Borrowed(svg), + }; } + open_depth = open_depth.max(elements.0); } let markers = if data_values.is_empty() { Cow::Borrowed(svg) @@ -303,7 +313,7 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { text.push_str(svg.get(from..).unwrap_or_default()); Cow::Owned(text) }; - Prescan { too_deep: false, markers } + Prescan { too_deep: false, depth: elements.1.saturating_add(entities.1.saturating_mul(10)), open_depth, markers } } #[cfg(test)] diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index 982e4f82b..25bf880cd 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -18,7 +18,10 @@ #![forbid(unsafe_code)] pub mod color; +pub mod drawing_write; pub mod guard; +pub mod neutral_document; +pub mod path_data; pub mod paths; pub mod preflight; pub mod sniff; diff --git a/crates/svg/src/neutral_document.rs b/crates/svg/src/neutral_document.rs new file mode 100644 index 000000000..17f352058 --- /dev/null +++ b/crates/svg/src/neutral_document.rs @@ -0,0 +1,407 @@ +//! A bounded SVG document walk that preserves geometric primitive kinds for CAD import. +//! +//! Coordinates remain in SVG user units. Each shape carries the composed transform into +//! millimetres with the page Y axis pointing up; consumers map shapes into their own model. +use crate::path_data::{self, Point, Subpath}; +use quick_xml::events::{BytesStart, Event as XmlEvent}; +use quick_xml::{Reader, XmlVersion}; +use std::collections::BTreeMap; + +const MAX_DEPTH: usize = 64; +const MAX_SHAPES: usize = 500_000; +const MAX_POINTS: usize = 8_000_000; +const MAX_POLY_POINTS: usize = 100_001; +const SKIP: &[&[u8]] = &[ + b"defs", + b"symbol", + b"clipPath", + b"mask", + b"pattern", + b"marker", + b"linearGradient", + b"radialGradient", + b"filter", + b"style", + b"text", + b"image", + b"title", + b"desc", + b"metadata", +]; +const CONTAINERS: &[&[u8]] = &[b"g", b"svg", b"a", b"switch"]; + +/// Input failure or resource bound. +#[derive(Debug, thiserror::Error)] +#[error("{0}")] +pub struct Error(pub String); +/// A shape's original geometry before its affine transform. +#[derive(Debug, Clone, PartialEq)] +pub enum Primitive { + Rect { x: f64, y: f64, width: f64, height: f64 }, + Circle { center: Point, radius: f64 }, + Ellipse { center: Point, rx: f64, ry: f64 }, + Line { a: Point, b: Point }, + Polyline { points: Vec, closed: bool }, + Path(Vec), +} +/// A primitive with its layer and SVG affine matrix `[a,b,c,d,e,f]`. +#[derive(Debug, Clone, PartialEq)] +pub struct Shape { + pub primitive: Primitive, + pub transform: [f64; 6], + pub layer: String, +} +/// The shapes, encountered layer names, and skipped-feature counts. `visit` streams shapes +/// to its sink, so its returned `shapes` is empty; `import` collects them. +#[derive(Debug, Default, PartialEq)] +pub struct Document { + pub shapes: Vec, + pub layers: Vec, + pub skipped: BTreeMap, +} +/// Features left out by this geometry-only import. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum Skipped { + Text, + Image, + Use, + Style, + RoundedCorners, +} + +fn attr(e: &BytesStart<'_>, name: &[u8]) -> Option { + for a in e.attributes() { + let Ok(a) = a else { return None }; + if a.key.into_inner() == name { + return a.normalized_value(XmlVersion::Explicit1_0).ok().map(|v| v.into_owned()); + } + } + None +} +fn hidden(e: &BytesStart<'_>) -> bool { + attr(e, b"display").as_deref() == Some("none") || attr(e, b"style").is_some_and(|s| s.replace(' ', "").contains("display:none")) +} +fn length(s: &str) -> Option { + let s = s.trim(); + let cut = s.find(|c: char| !(c.is_ascii_digit() || matches!(c, '.' | '-' | '+' | 'e' | 'E'))).unwrap_or(s.len()); + let (n, unit) = s.split_at(cut); + let v: f64 = n.trim().parse().ok()?; + if !v.is_finite() { + return None; + } + Some( + v * match unit.trim() { + "" | "px" | "mm" => 1.0, + "cm" => 10.0, + "in" => 25.4, + "pt" => 25.4 / 72.0, + "pc" => 25.4 / 6.0, + _ => 25.4 / 96.0, + }, + ) +} +fn num(e: &BytesStart<'_>, name: &[u8], default: f64) -> f64 { + attr(e, name).as_deref().and_then(length).unwrap_or(default) +} +const ID: [f64; 6] = [1., 0., 0., 1., 0., 0.]; +fn mul(a: [f64; 6], b: [f64; 6]) -> [f64; 6] { + [ + a[0] * b[0] + a[2] * b[1], + a[1] * b[0] + a[3] * b[1], + a[0] * b[2] + a[2] * b[3], + a[1] * b[2] + a[3] * b[3], + a[0] * b[4] + a[2] * b[5] + a[4], + a[1] * b[4] + a[3] * b[5] + a[5], + ] +} +fn translate(x: f64, y: f64) -> [f64; 6] { + [1., 0., 0., 1., x, y] +} +fn scale(x: f64, y: f64) -> [f64; 6] { + [x, 0., 0., y, 0., 0.] +} +fn rotate(a: f64) -> [f64; 6] { + let (s, c) = a.to_radians().sin_cos(); + [c, s, -s, c, 0., 0.] +} +fn numbers(s: &str) -> impl Iterator + '_ { + path_data::Numbers::new(s) +} +fn transform(s: &str) -> Option<[f64; 6]> { + let mut out = None; + let mut rest = s.trim(); + while !rest.is_empty() { + let open = rest.find('(')?; + let name = rest[..open].trim(); + let close = rest[open..].find(')')? + open; + // No transform function accepts more than six arguments. + let args: Vec<_> = numbers(&rest[open + 1..close]).take(7).collect(); + let m = match (name, args.as_slice()) { + ("matrix", [a, b, c, d, e, f]) => [*a, *b, *c, *d, *e, *f], + ("translate", [x]) => translate(*x, 0.), + ("translate", [x, y]) => translate(*x, *y), + ("scale", [x]) => scale(*x, *x), + ("scale", [x, y]) => scale(*x, *y), + ("rotate", [a]) => rotate(*a), + ("rotate", [a, x, y]) => mul(mul(translate(*x, *y), rotate(*a)), translate(-x, -y)), + ("skewX", [a]) => [1., 0., a.to_radians().tan(), 1., 0., 0.], + ("skewY", [a]) => [1., a.to_radians().tan(), 0., 1., 0., 0.], + _ => return None, + }; + out = Some(out.map_or(m, |prev| mul(prev, m))); + rest = rest[close + 1..].trim_start_matches(|c: char| c == ',' || c.is_whitespace()); + } + out +} +fn viewport(e: &BytesStart<'_>) -> [f64; 6] { + let vb = attr(e, b"viewBox").and_then(|raw| { + let n: Vec<_> = numbers(&raw).take(5).collect(); + match n.as_slice() { + [x, y, w, h] if *w > 0. && *h > 0. => Some((*x, *y, *w, *h)), + _ => None, + } + }); + let root = |name: &[u8]| { + let raw = attr(e, name)?; + let mm = length(&raw)?; + let unit = raw.trim().trim_start_matches(|c: char| c.is_ascii_digit() || matches!(c, '.' | '-' | '+' | 'e' | 'E')).trim(); + Some(if matches!(unit, "" | "px") { mm * 25.4 / 96. } else { mm }) + }; + let unit = match (vb, root(b"width"), root(b"height")) { + (Some((_, _, w, _)), Some(x), _) if x > 0. => x / w, + (Some((_, _, _, h)), _, Some(y)) if y > 0. => y / h, + _ => 25.4 / 96., + }; + let (x, y, h) = vb.map_or((0., 0., 0.), |(x, y, _, h)| (x, y, h)); + [unit, 0., 0., -unit, -x * unit, (y + h) * unit] +} +fn layer_name(raw: &str) -> String { + let s: String = raw.trim().chars().map(|c| if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '$') { c } else { '_' }).take(128).collect(); + let s = s.trim_matches('_'); + if s.is_empty() { "SVG".into() } else { s.into() } +} +/// A layer declaration or a drawable primitive in document order. +pub enum Event { + Layer(String), + Shape(Shape), +} + +struct Walker<'a> { + doc: Document, + stack: Vec<[f64; 6]>, + layer: String, + skip: usize, + points: usize, + shapes: usize, + sink: &'a mut dyn FnMut(Event) -> Result<(), Error>, +} +impl Walker<'_> { + fn current(&self) -> [f64; 6] { + self.stack.last().copied().unwrap_or(ID) + } + fn note(&mut self, what: Skipped) { + *self.doc.skipped.entry(what).or_default() += 1; + } + fn skipped(name: &[u8]) -> Option { + match name { + b"text" => Some(Skipped::Text), + b"image" => Some(Skipped::Image), + _ => None, + } + } + fn start(&mut self, name: &[u8], e: &BytesStart<'_>) -> Result<(), Error> { + if name == b"style" { + self.note(Skipped::Style); + } + if self.skip > 0 { + self.skip += 1; + return Ok(()); + } + if hidden(e) { + self.skip = 1; + return Ok(()); + } + if SKIP.contains(&name) { + if let Some(s) = Self::skipped(name) { + self.note(s); + } + self.skip = 1; + return Ok(()); + } + if !CONTAINERS.contains(&name) { + return self.element(name, e); + } + if name == b"svg" + && self.stack.len() == 1 + && let Some(root) = self.stack.first_mut() + { + *root = viewport(e); + } + if name == b"g" + && attr(e, b"inkscape:groupmode").as_deref() == Some("layer") + && let Some(raw) = attr(e, b"inkscape:label").or_else(|| attr(e, b"id")) + { + let layer = layer_name(&raw); + if layer == "0" || self.doc.layers.contains(&layer) { + self.layer = layer; + } else if self.doc.layers.len() < 255 { + self.doc.layers.push(layer.clone()); + (self.sink)(Event::Layer(layer.clone()))?; + self.layer = layer; + } + } + if self.stack.len() >= MAX_DEPTH { + return Err(Error(format!("SVG: more than {MAX_DEPTH} nested groups"))); + } + let next = attr(e, b"transform").as_deref().and_then(transform).map_or(self.current(), |t| mul(self.current(), t)); + self.stack.push(next); + Ok(()) + } + fn element(&mut self, name: &[u8], e: &BytesStart<'_>) -> Result<(), Error> { + if self.skip > 0 { + return Ok(()); + } + if SKIP.contains(&name) || hidden(e) { + if !hidden(e) + && let Some(s) = Self::skipped(name) + { + self.note(s); + } + return Ok(()); + } + let p = |x, y| Point::new(num(e, x, 0.), num(e, y, 0.)); + let primitive = match name { + b"use" => { + self.note(Skipped::Use); + return Ok(()); + } + b"rect" => { + let (x, y, w, h) = (num(e, b"x", 0.), num(e, b"y", 0.), num(e, b"width", 0.), num(e, b"height", 0.)); + if w <= 0. || h <= 0. { + return Ok(()); + } + if num(e, b"rx", 0.) > 0. || num(e, b"ry", 0.) > 0. { + self.note(Skipped::RoundedCorners); + } + Primitive::Rect { x, y, width: w, height: h } + } + b"circle" => { + let r = num(e, b"r", 0.); + if r <= 0. { + return Ok(()); + } + Primitive::Circle { center: p(b"cx", b"cy"), radius: r } + } + b"ellipse" => { + let (rx, ry) = (num(e, b"rx", 0.), num(e, b"ry", 0.)); + if rx <= 0. || ry <= 0. { + return Ok(()); + } + Primitive::Ellipse { center: p(b"cx", b"cy"), rx, ry } + } + b"line" => Primitive::Line { a: p(b"x1", b"y1"), b: p(b"x2", b"y2") }, + b"polyline" | b"polygon" => { + let Some(raw) = attr(e, b"points") else { return Ok(()) }; + let n: Vec<_> = numbers(&raw).take(MAX_POLY_POINTS * 2).collect(); + let pts: Vec<_> = n.as_chunks::<2>().0.iter().map(|[x, y]| Point::new(*x, *y)).collect(); + if pts.len() < 2 { + return Ok(()); + } + Primitive::Polyline { points: pts, closed: name == b"polygon" } + } + b"path" => { + let Some(raw) = attr(e, b"d") else { return Ok(()) }; + Primitive::Path(path_data::parse_path(&raw).map_err(|e| Error(e.to_string()))?) + } + _ => return Ok(()), + }; + let cost = match &primitive { + Primitive::Polyline { points, .. } => points.len(), + Primitive::Path(paths) => paths.iter().map(|p| p.segs.len()).sum(), + _ => 1, + }; + self.points = self.points.checked_add(cost).filter(|n| *n <= MAX_POINTS).ok_or_else(|| Error("SVG: too much generated geometry".into()))?; + if self.shapes >= MAX_SHAPES { + return Err(Error(format!("SVG: more than {MAX_SHAPES} elements"))); + } + let matrix = attr(e, b"transform").as_deref().and_then(transform).map_or(self.current(), |t| mul(self.current(), t)); + self.shapes += 1; + (self.sink)(Event::Shape(Shape { primitive, transform: matrix, layer: self.layer.clone() })) + } +} +/// Walk supported SVG geometry without retaining a second copy of the document's shapes. +/// The sink receives each layer before its first shape and each shape in input order. +pub fn visit(text: &str, sink: &mut dyn FnMut(Event) -> Result<(), Error>) -> Result { + let mut walker = Walker { doc: Document::default(), stack: vec![ID], layer: "0".into(), skip: 0, points: 0, shapes: 0, sink }; + let mut reader = Reader::from_str(text); + reader.config_mut().trim_text(true); + loop { + match reader.read_event().map_err(|e| Error(format!("SVG: {e}")))? { + XmlEvent::Start(e) => walker.start(e.name().into_inner(), &e)?, + XmlEvent::Empty(e) => walker.element(e.name().into_inner(), &e)?, + XmlEvent::End(e) => { + if walker.skip > 0 { + walker.skip -= 1; + } else if CONTAINERS.contains(&e.name().into_inner()) && walker.stack.len() > 1 { + walker.stack.pop(); + } + } + XmlEvent::Eof => break, + _ => {} + } + } + Ok(walker.doc) +} +/// Read the supported geometry into an owned document. +pub fn import(text: &str) -> Result { + let mut shapes = Vec::new(); + let mut doc = visit(text, &mut |event| { + if let Event::Shape(shape) = event { + shapes.push(shape); + } + Ok(()) + })?; + doc.shapes = shapes; + Ok(doc) +} +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + #[test] + fn keeps_primitives_transform_layers_and_skips() { + let svg = r##"x"##; + let d = import(svg).unwrap(); + assert_eq!(d.layers, ["cut"]); + assert_eq!(d.shapes.len(), 3); + assert_eq!(d.shapes[0].primitive, Primitive::Circle { center: Point::new(3., 4.), radius: 2. }); + assert_eq!(d.shapes[0].transform, [1., 0., 0., -1., 2., 9.]); + assert!(matches!(d.shapes[1].primitive, Primitive::Ellipse { .. })); + assert!(matches!(&d.shapes[2].primitive,Primitive::Path(p) if matches!(p[0].segs[0],path_data::Segment::Arc { .. }))); + assert_eq!(d.skipped.get(&Skipped::Text), Some(&1)); + assert_eq!(d.skipped.get(&Skipped::Use), Some(&1)); + } + #[test] + fn streaming_delivers_layers_before_shapes_and_stops_on_error() { + let s = r#""#; + let mut events = Vec::new(); + let result = visit(s, &mut |event| { + match event { + Event::Layer(name) => events.push(name), + Event::Shape(_) => { + events.push("shape".into()); + return Err(Error("stop".into())); + } + } + Ok(()) + }); + assert!(result.is_err()); + assert_eq!(events, ["cut", "shape"]); + } + + #[test] + fn refuses_excessive_nesting() { + let s = format!("{}{}", "".repeat(100), "".repeat(100)); + assert!(import(&s).is_err()); + } +} diff --git a/crates/svg/src/path_data.rs b/crates/svg/src/path_data.rs new file mode 100644 index 000000000..96d0db771 --- /dev/null +++ b/crates/svg/src/path_data.rs @@ -0,0 +1,456 @@ +//! SVG path data as neutral line, cubic and elliptical arc segments. +//! +//! Relative commands and smooth controls are resolved, while arcs remain arcs. +//! The caller chooses how to map the result to its own geometry model. +use std::ops::{Add, Mul, Sub}; + +/// A point in the path's original user coordinate system. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Point { + /// Horizontal coordinate. + pub x: f64, + /// Vertical coordinate. + pub y: f64, +} +impl Point { + /// The origin. + pub const ZERO: Self = Self { x: 0.0, y: 0.0 }; + /// Construct a point. + pub const fn new(x: f64, y: f64) -> Self { + Self { x, y } + } +} +impl Add for Point { + type Output = Self; + fn add(self, rhs: Self) -> Self { + Self::new(self.x + rhs.x, self.y + rhs.y) + } +} +impl Sub for Point { + type Output = Self; + fn sub(self, rhs: Self) -> Self { + Self::new(self.x - rhs.x, self.y - rhs.y) + } +} +impl Mul for Point { + type Output = Self; + fn mul(self, rhs: f64) -> Self { + Self::new(self.x * rhs, self.y * rhs) + } +} + +/// Invalid or oversized path data. +#[derive(Debug, thiserror::Error)] +#[error("{0}")] +pub struct Error(pub String); +type Result = std::result::Result; +const MAX_PATH_SEGMENTS: usize = 100_000; +const MAX_CURVE_POINTS: usize = 100_001; + +pub(crate) struct Numbers<'a> { + s: &'a [u8], + i: usize, +} + +impl<'a> Numbers<'a> { + pub(crate) fn new(s: &'a str) -> Self { + Self { s: s.as_bytes(), i: 0 } + } + fn skip_seps(&mut self) { + while matches!(self.s.get(self.i), Some(b' ' | b'\t' | b'\r' | b'\n' | b',')) { + self.i += 1; + } + } + + fn next_num(&mut self) -> Option { + self.skip_seps(); + let start = self.i; + if matches!(self.s.get(self.i), Some(b'+' | b'-')) { + self.i += 1; + } + let mut digits = 0; + let mut dot = false; + let mut exp = false; + while let Some(c) = self.s.get(self.i) { + match c { + b'0'..=b'9' => { + digits += 1; + self.i += 1; + } + b'.' if !dot && !exp => { + dot = true; + self.i += 1; + } + b'e' | b'E' if !exp && digits > 0 => { + exp = true; + self.i += 1; + if matches!(self.s.get(self.i), Some(b'+' | b'-')) { + self.i += 1; + } + } + _ => break, + } + } + if digits == 0 { + self.i = start; + return None; + } + std::str::from_utf8(&self.s[start..self.i]).ok()?.parse::().ok().filter(|v| v.is_finite()) + } +} + +impl Iterator for Numbers<'_> { + type Item = f64; + fn next(&mut self) -> Option { + self.next_num() + } +} + +/// One `M …` run of a `d` attribute. +#[derive(Debug, Clone, PartialEq)] +pub struct Subpath { + /// Start point. + pub start: Point, + /// Segments in drawing order. + pub segs: Vec, + /// Whether a close-path command closed this subpath. + pub closed: bool, +} + +/// A path segment; arc values retain the SVG endpoint parameterization. +#[derive(Debug, Clone, PartialEq)] +pub enum Segment { + Line(Point), + Cubic(Point, Point, Point), + Arc { rx: f64, ry: f64, rot: f64, large: bool, sweep: bool, end: Point }, +} + +/// Tokens of a `d` attribute: command letters and numbers, in order. +enum Tok { + Cmd(u8), + Num(f64), +} + +struct Tokenizer<'a> { + s: &'a [u8], + i: usize, + /// Start of the most recently returned token. + tok_start: usize, +} + +impl<'a> Tokenizer<'a> { + fn new(s: &'a str) -> Self { + Tokenizer { s: s.as_bytes(), i: 0, tok_start: 0 } + } + + /// Read an arc flag: a single `0` or `1` character, which needs no separator after it. + fn next_flag(&mut self) -> Option { + let mut nums = Numbers { s: self.s, i: self.i }; + nums.skip_seps(); + let v = match self.s.get(nums.i)? { + b'0' => 0.0, + b'1' => 1.0, + _ => return None, + }; + self.i = nums.i + 1; + Some(v) + } + + fn next_tok(&mut self) -> Option { + let mut nums = Numbers { s: self.s, i: self.i }; + nums.skip_seps(); + self.i = nums.i; + self.tok_start = self.i; + let c = self.s.get(self.i).copied()?; + if c.is_ascii_alphabetic() { + self.i += 1; + return Some(Tok::Cmd(c)); + } + let tok = nums.next_num().map(Tok::Num); + self.i = nums.i; + tok + } +} + +/// Split `d` into subpaths, resolving relative commands and the S/T control reflections. +pub fn parse_path(d: &str) -> Result> { + let mut tk = Tokenizer::new(d); + let mut lookahead: Option = None; + let mut cmd = 0u8; + let mut cur = Point::ZERO; + let mut start = Point::ZERO; + let mut segs: Vec = Vec::new(); + let mut out: Vec = Vec::new(); + let closed = false; + let mut prev_cubic: Option = None; + let mut prev_quad: Option = None; + let mut segments = 0usize; + + let next_num = |tk: &mut Tokenizer, look: &mut Option| -> Result { + match look.take() { + Some(Tok::Num(v)) => Ok(v), + Some(other @ Tok::Cmd(_)) => { + *look = Some(other); + Err(Error("SVG: path command is missing coordinates".into())) + } + None => match tk.next_tok() { + Some(Tok::Num(v)) => Ok(v), + Some(other @ Tok::Cmd(_)) => { + *look = Some(other); + Err(Error("SVG: path command is missing coordinates".into())) + } + None => Err(Error("SVG: path command is missing coordinates".into())), + }, + } + }; + + while let Some(tok) = lookahead.take().or_else(|| tk.next_tok()) { + let rel = match tok { + Tok::Cmd(c) => { + if c.eq_ignore_ascii_case(&b'z') { + if !segs.is_empty() { + out.push(Subpath { start, segs, closed: true }); + segs = Vec::new(); + } + cur = start; + cmd = 0; + continue; + } + cmd = c; + c.is_ascii_lowercase() + } + tok @ Tok::Num(_) => { + if cmd == 0 { + return Err(Error("SVG: path data does not start with a command".into())); + } + lookahead = Some(tok); + cmd.is_ascii_lowercase() + } + }; + let abs = cmd.to_ascii_uppercase(); + let count = match abs { + b'M' | b'L' | b'T' => 2, + b'H' | b'V' => 1, + b'C' => 6, + b'S' | b'Q' => 4, + b'A' => 7, + _ => return Err(Error(format!("SVG: unknown path command `{}`", cmd as char))), + }; + let mut args = [0.0f64; 7]; + for (idx, slot) in args.iter_mut().enumerate().take(count) { + if abs == b'A' && (idx == 3 || idx == 4) { + // Flags are single characters; re-scan a pending number token from its start. + if matches!(lookahead, Some(Tok::Num(_))) { + lookahead = None; + tk.i = tk.tok_start; + } + if lookahead.is_none() { + *slot = tk.next_flag().ok_or_else(|| Error("SVG: path command is missing coordinates".into()))?; + continue; + } + } + *slot = next_num(&mut tk, &mut lookahead)?; + } + let point = |x: f64, y: f64| if rel { cur + Point::new(x, y) } else { Point::new(x, y) }; + match abs { + b'M' => { + if !segs.is_empty() { + out.push(Subpath { start, segs, closed: false }); + segs = Vec::new(); + } + cur = point(args[0], args[1]); + start = cur; + cmd = if rel { b'l' } else { b'L' }; + prev_cubic = None; + prev_quad = None; + } + b'L' => { + cur = point(args[0], args[1]); + segs.push(Segment::Line(cur)); + prev_cubic = None; + prev_quad = None; + } + b'H' => { + cur = Point::new(if rel { cur.x + args[0] } else { args[0] }, cur.y); + segs.push(Segment::Line(cur)); + prev_cubic = None; + prev_quad = None; + } + b'V' => { + cur = Point::new(cur.x, if rel { cur.y + args[0] } else { args[0] }); + segs.push(Segment::Line(cur)); + prev_cubic = None; + prev_quad = None; + } + b'C' => { + let (c1, c2, p) = (point(args[0], args[1]), point(args[2], args[3]), point(args[4], args[5])); + segs.push(Segment::Cubic(c1, c2, p)); + prev_cubic = Some(c2); + prev_quad = None; + cur = p; + } + b'S' => { + let c1 = prev_cubic.map_or(cur, |c2| cur * 2.0 - c2); + let (c2, p) = (point(args[0], args[1]), point(args[2], args[3])); + segs.push(Segment::Cubic(c1, c2, p)); + prev_cubic = Some(c2); + prev_quad = None; + cur = p; + } + b'Q' => { + let (q, p) = (point(args[0], args[1]), point(args[2], args[3])); + segs.push(Segment::Cubic(cur + (q - cur) * (2.0 / 3.0), p + (q - p) * (2.0 / 3.0), p)); + prev_quad = Some(q); + prev_cubic = None; + cur = p; + } + b'T' => { + let reflect = prev_quad.map_or(cur, |q| cur * 2.0 - q); + let p = point(args[0], args[1]); + segs.push(Segment::Cubic(cur + (reflect - cur) * (2.0 / 3.0), p + (reflect - p) * (2.0 / 3.0), p)); + prev_quad = Some(reflect); + prev_cubic = None; + cur = p; + } + b'A' => { + let end = point(args[5], args[6]); + segs.push(Segment::Arc { rx: args[0].abs(), ry: args[1].abs(), rot: args[2], large: args[3] != 0.0, sweep: args[4] != 0.0, end }); + cur = end; + prev_cubic = None; + prev_quad = None; + } + _ => return Err(Error("SVG: unsupported path command".into())), + } + segments += 1; + if segments > MAX_PATH_SEGMENTS { + return Err(Error("SVG: path has too many segments".into())); + } + if segs.len() > MAX_CURVE_POINTS { + return Err(Error("SVG: path has too many segments".into())); + } + } + if !segs.is_empty() { + out.push(Subpath { start, segs, closed }); + } + Ok(out) +} +/// The centre and signed sweep of a circular arc from `p0` to `p1` in a Y-up frame, per the +/// SVG endpoint parameterisation (F.6.5). Radii that cannot span the chord are scaled up, +/// as the specification requires. +pub fn arc_center_circular(p0: Point, p1: Point, r: f64, large: bool, sweep: bool) -> Option<(Point, f64)> { + if r <= 0.0 || !r.is_finite() { + return None; + } + let d = p1 - p0; + let dist = d.x.hypot(d.y); + let r = if dist > 2.0 * r { dist / 2.0 } else { r }; + if dist <= f64::EPSILON { + return None; + } + let mid = (p0 + p1) * 0.5; + let h = (r * r - (dist * dist) / 4.0).max(0.0).sqrt(); + let n = Point::new(-d.y / dist, d.x / dist); + let sign = if large != sweep { 1.0 } else { -1.0 }; + let center = mid + n * (h * sign); + let a0 = (p0.y - center.y).atan2(p0.x - center.x); + let a1 = (p1.y - center.y).atan2(p1.x - center.x); + let ccw = (a1 - a0).rem_euclid(std::f64::consts::TAU); + Some((center, pick_sweep(ccw, large, sweep))) +} + +/// The signed sweep (CCW positive) for a chord sweep `ccw` in `[0, 2π)`: the centre is +/// already chosen so that the magnitude matches `large`, so only the direction is left. +fn pick_sweep(ccw: f64, large: bool, sweep: bool) -> f64 { + let tau = std::f64::consts::TAU; + let eps = f64::EPSILON; + if sweep { + if ccw <= eps { if large { tau } else { eps } } else { ccw } + } else if ccw >= tau - eps { + if large { -tau } else { -eps } + } else { + ccw - tau + } +} + +/// Endpoint → centre parameterisation for a rotated elliptical arc (SVG F.6.5), in the +/// frame the points are given in. Returns the centre, the start parameter and the signed +/// sweep. +pub fn elliptical_params(p0: Point, p1: Point, rx: f64, ry: f64, phi: f64, large: bool, sweep: bool) -> Option<(Point, f64, f64)> { + if rx <= 0.0 || ry <= 0.0 { + return None; + } + let (sphi, cphi) = phi.sin_cos(); + let dx = (p0.x - p1.x) / 2.0; + let dy = (p0.y - p1.y) / 2.0; + let x1 = cphi * dx + sphi * dy; + let y1 = -sphi * dx + cphi * dy; + let (rx, ry) = { + let lambda = x1 * x1 / (rx * rx) + y1 * y1 / (ry * ry); + if lambda > 1.0 { + let s = lambda.sqrt(); + (rx * s, ry * s) + } else { + (rx, ry) + } + }; + let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1; + let den = rx * rx * y1 * y1 + ry * ry * x1 * x1; + let coef = if sweep != large { 1.0 } else { -1.0 } * (num.max(0.0) / den.max(f64::MIN_POSITIVE)).sqrt(); + let cxp = coef * rx * y1 / ry; + let cyp = -coef * ry * x1 / rx; + let center = Point::new(cphi * cxp - sphi * cyp + (p0.x + p1.x) / 2.0, sphi * cxp + cphi * cyp + (p0.y + p1.y) / 2.0); + let angle = |ux: f64, uy: f64, vx: f64, vy: f64| { + let dot = ux * vx + uy * vy; + let det = ux * vy - uy * vx; + let denom = (ux.hypot(uy) * vx.hypot(vy)).max(f64::MIN_POSITIVE); + (dot / denom).clamp(-1.0, 1.0).acos().copysign(det) + }; + let (ux, uy) = ((x1 - cxp) / rx, (y1 - cyp) / ry); + let (vx, vy) = ((-x1 - cxp) / rx, (-y1 - cyp) / ry); + let theta = angle(1.0, 0.0, ux, uy); + let ccw = angle(ux, uy, vx, vy).rem_euclid(std::f64::consts::TAU); + Some((center, theta, pick_sweep(ccw, large, sweep))) +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + + #[test] + fn resolves_relative_and_smooth_commands_without_flattening_arcs() { + let paths = parse_path("M1 2 l3 4 C5 6 7 8 9 10 s2 3 4 5 A6 7 30 0110 12 z").unwrap(); + assert_eq!(paths.len(), 1); + assert!(paths[0].closed); + assert_eq!(paths[0].start, Point::new(1.0, 2.0)); + assert_eq!(paths[0].segs[0], Segment::Line(Point::new(4.0, 6.0))); + assert_eq!(paths[0].segs[2], Segment::Cubic(Point::new(11.0, 12.0), Point::new(11.0, 13.0), Point::new(13.0, 15.0))); + assert_eq!(paths[0].segs[3], Segment::Arc { rx: 6.0, ry: 7.0, rot: 30.0, large: false, sweep: true, end: Point::new(10.0, 12.0) }); + } + + #[test] + fn smooth_quadratic_consumes_both_coordinates() { + let paths = parse_path("M0 0 Q3 3 6 0 T12 0").unwrap(); + assert_eq!(paths[0].segs[1], Segment::Cubic(Point::new(8.0, -2.0), Point::new(10.0, -2.0), Point::new(12.0, 0.0))); + } + + #[test] + fn arc_centers_keep_radius_correction_and_sweep() { + let (center, delta) = arc_center_circular(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, false, true).unwrap(); + assert!((center.x - 5.0).abs() < 1e-10); + assert!(center.y.abs() < 1e-10); + assert!((delta - std::f64::consts::PI).abs() < 1e-10); + let (center, start, sweep) = elliptical_params(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, 1.0, 0.0, false, true).unwrap(); + assert!((center.x - 5.0).abs() < 1e-10); + assert!(center.y.abs() < 1e-10 && start.is_finite()); + assert!((sweep - std::f64::consts::PI).abs() < 1e-10); + } + + #[test] + fn rejects_invalid_flags_and_limits_segments() { + assert!(parse_path("M0 0 A1 1 0 5 1 2 2").is_err()); + assert!(parse_path("M0 0 L").is_err()); + let excessive = format!("M0 0{}", " L1 1".repeat(MAX_PATH_SEGMENTS)); + assert!(parse_path(&excessive).is_err()); + } +} diff --git a/crates/svg/src/preflight.rs b/crates/svg/src/preflight.rs index 3ab381ff3..0c2d3a7b1 100644 --- a/crates/svg/src/preflight.rs +++ b/crates/svg/src/preflight.rs @@ -71,6 +71,12 @@ pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result, Error> /// Parses already-decoded SVG text and checks it against `limits`. pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result, Error> { + parse_text_with_depth(svg, limits).map(|(xml, _, _)| xml) +} + +/// [`parse_text`], with the source nesting: the deepest element nesting (markup entities count +/// ten times) and the deepest stack of open elements (a self-closing leaf opens none). +pub fn parse_text_with_depth<'a>(svg: &'a str, limits: &Limits) -> Result<(roxmltree::Document<'a>, usize, usize), Error> { if svg.len() > limits.bytes { return Err(Error::Text(crate::svgz::Error::TooLarge(limits.bytes))); } @@ -83,9 +89,38 @@ pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result Result<(), Error> { + let limits = Limits { depth: 4, ..Default::default() }; + let (document, depth, open_depth) = parse_text_with_depth("", &limits)?; + assert!(document.root_element().has_tag_name("svg")); + // Every element is a level (svg, g, path); a self-closing leaf opens none (svg, g). + assert_eq!(depth, 3); + assert_eq!(open_depth, 2); + let simple = parse_text("", &limits)?; + assert!(simple.root_element().has_tag_name("svg")); + Ok(()) + } + + #[test] + fn source_depth_and_xml_errors_keep_their_specific_kind() { + let shallow = Limits { depth: 1, ..Default::default() }; + assert!(matches!(parse_text_with_depth("", &shallow), Err(Error::TooDeep(1)))); + + let normal = Limits::default(); + assert!(matches!(parse_text_with_depth("", &normal), Err(Error::Xml(_)))); + } } diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index cb33137b8..9cf85c3bd 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -112,13 +112,19 @@ pub enum Error { /// usvg's own error. #[error("invalid SVG: {0}")] Parse(String), + /// The root viewport is missing or has dimensions this SVG helper cannot preserve. + #[error("invalid SVG root viewport: {0}")] + Viewport(String), + /// The rewritten SVG would exceed the caller's output byte budget. + #[error("rewritten SVG exceeds {0} bytes")] + OutputTooLarge(usize), } /// The SVG text of the bytes of an SVG or SVGZ file, checked against `limits`: what [`parse`] /// hands usvg. For a caller that edits the text before it parses ([`parse_text`]). pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result, Error> { let text = svgz::text(bytes, limits.bytes)?; - checked(&text, limits)?; + preflight(&text, limits)?; Ok(text) } @@ -130,8 +136,7 @@ pub fn parse(bytes: &[u8], limits: &Limits, options: &usvg::Options<'_>) -> Resu /// [`parse`] for SVG text the caller already holds. pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Result { - let xml = checked(svg, limits)?; - usvg::Tree::from_xmltree(&xml, options).map_err(|e| Error::Parse(e.to_string())) + preflight(svg, limits)?.parse(options) } /// Read the root SVG's unitless width and height without constructing a rendering tree. @@ -139,8 +144,8 @@ pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Re /// Lengths with units, percentages, missing dimensions, and non-SVG roots return `Ok(None)`; /// malformed or over-limit XML returns the same bounded parsing error as [`parse_text`]. pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result, Error> { - let xml = checked(svg, limits)?; - let root = xml.root_element(); + let checked = preflight(svg, limits)?; + let root = checked.xml.root_element(); if root.tag_name().name() != "svg" { return Ok(None); } @@ -152,8 +157,30 @@ pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result(svg: &'s str, limits: &Limits) -> Result, Error> { +/// SVG text after shared byte, entity, attribute, XML and expansion-depth checks. +/// Keep it opaque so consumers use the same checked tree and don't reparse XML locally. +pub struct Preflight<'s> { + xml: roxmltree::Document<'s>, + depth: usize, + /// [`height`]: the nesting usvg converts, references followed. + height: usize, +} + +impl Preflight<'_> { + /// The deepest stack of open source elements, before references are followed; a + /// self-closing leaf opens no level (`` is 2). + pub fn depth(&self) -> usize { + self.depth + } + + /// Converts this already-checked XML into the configured usvg tree. + pub fn parse(self, options: &usvg::Options<'_>) -> Result { + usvg::Tree::from_xmltree(&self.xml, options).map_err(|e| Error::Parse(e.to_string())) + } +} + +/// Checks already-decoded SVG text once, retaining the parsed XML for both depth policy and tree conversion. +pub fn preflight<'s>(svg: &'s str, limits: &Limits) -> Result, Error> { let preflight_limits = crate::preflight::Limits { bytes: limits.bytes, depth: limits.depth, @@ -163,7 +190,7 @@ fn checked<'s>(svg: &'s str, limits: &Limits) -> Result, attribute_work: limits.attribute_work, xml_nodes: limits.xml_nodes, }; - let xml = crate::preflight::parse_text(svg, &preflight_limits).map_err(|e| match e { + let (xml, _, depth) = crate::preflight::parse_text_with_depth(svg, &preflight_limits).map_err(|e| match e { crate::preflight::Error::Text(e) => Error::Text(e), crate::preflight::Error::Entities => Error::Entities, crate::preflight::Error::EntityMarkup => Error::EntityMarkup, @@ -171,10 +198,69 @@ fn checked<'s>(svg: &'s str, limits: &Limits) -> Result, crate::preflight::Error::TooWide(attributes) => Error::TooWide(attributes), crate::preflight::Error::Xml(msg) => Error::Xml(msg), })?; - if height(&xml, limits.tree_depth)?.is_none() { + let Some(height) = height(&xml, limits.tree_depth)? else { return Err(Error::TooDeep(limits.tree_depth)); + }; + Ok(Preflight { xml, depth, height }) +} + +/// Dimensions of an SVG viewport in SVG user units. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Viewport { + pub width: f64, + pub height: f64, +} + +/// Wraps a validated SVG root in a new viewport while preserving the original root as content. +/// The original root must have finite, positive pixel or point dimensions, and the wrapped +/// output must still pass [`preflight`] under `limits`. +pub fn rewrite_root_viewport(svg: &str, target: Viewport, limits: &Limits, output_limit: usize) -> Result { + if !target.width.is_finite() || !target.height.is_finite() || target.width <= 0.0 || target.height <= 0.0 { + return Err(Error::Viewport("target dimensions must be finite and positive".into())); } - Ok(xml) + let checked = preflight(svg, limits)?; + let root = checked.xml.root_element(); + if root.tag_name().name() != "svg" { + return Err(Error::Viewport("root element is not svg".into())); + } + let root_dimension = |key: &str| { + let value = root.attribute(key)?; + let value = value.trim(); + let (number, scale) = match value.strip_suffix("pt") { + Some(points) => (points, 4.0 / 3.0), + None => (value.strip_suffix("px").unwrap_or(value), 1.0), + }; + number.parse::().ok().map(|n| n * scale).filter(|n| n.is_finite() && *n > 0.0) + }; + let (width, height) = root_dimension("width") + .zip(root_dimension("height")) + .ok_or_else(|| Error::Viewport("source has no finite pixel or point dimensions".into()))?; + let range = root.range(); + let preamble = svg.get(..range.start).ok_or_else(|| Error::Viewport("invalid XML preamble range".into()))?; + let inner = svg.get(range).ok_or_else(|| Error::Viewport("invalid root element range".into()))?; + let prefix = format!( + r#""#, + target.width, target.height, width, height + ); + let len = preamble + .len() + .checked_add(prefix.len()) + .and_then(|n| n.checked_add(inner.len())) + .and_then(|n| n.checked_add(6)) + .ok_or(Error::OutputTooLarge(output_limit))?; + if len > output_limit { + return Err(Error::OutputTooLarge(output_limit)); + } + let mut output = String::new(); + output.try_reserve_exact(len).map_err(|_| Error::OutputTooLarge(output_limit))?; + output.push_str(preamble); + output.push_str(&prefix); + output.push_str(inner); + output.push_str(""); + // The wrapper adds a level, so a source at the depth limit can come out over it: check the + // output under the limits `parse_text` applies rather than return text it would refuse. + preflight(&output, limits)?; + Ok(output) } /// Properties whose value an element's descendants use too: the paint server they name is @@ -376,8 +462,8 @@ impl ImageResolver { /// 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); + let checked = preflight(svg, &self.limits)?; + account_invalid_data_urls(&checked.xml, &self.used); Ok(()) } @@ -520,8 +606,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' return None; } used.bytes.fetch_add(text.len(), Ordering::Relaxed); - let xml = match checked(&text, limits) { - Ok(xml) => xml, + let checked = match preflight(&text, limits) { + Ok(checked) => checked, // Decoding UTF-16 or Latin-1 can exceed the text cap after the // raw/inflated bytes fitted. This is a resource refusal, not invalid XML. Err(Error::Text(svgz::Error::TooLarge(_))) => { @@ -538,10 +624,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' return None; } }; - let Some(xml_depth) = height(&xml, limits.tree_depth).ok().flatten() else { - used.svg_limit.fetch_add(1, Ordering::Relaxed); - return None; - }; + // The nesting usvg converts, references followed, not the source depth alone. + let xml_depth = checked.height; let total_depth = used.xml_depth.load(Ordering::Relaxed).checked_add(xml_depth); let Some(total_depth) = total_depth.filter(|depth| *depth <= limits.tree_depth) else { used.svg_limit.fetch_add(1, Ordering::Relaxed); @@ -550,8 +634,8 @@ 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); + account_invalid_data_urls(&checked.xml, used); + let tree = checked.parse(options); used.xml_depth.store(previous_depth, Ordering::Relaxed); used.depth.fetch_sub(1, Ordering::Relaxed); match tree { diff --git a/crates/svg/src/tree/tests.rs b/crates/svg/src/tree/tests.rs index 103c0fe7c..35cb13a2e 100644 --- a/crates/svg/src/tree/tests.rs +++ b/crates/svg/src/tree/tests.rs @@ -292,7 +292,7 @@ mod resource_limit_usage { assert_eq!(svgz::inflate(&data, cap).unwrap().len(), cap, "raw bytes fit exactly"); assert_eq!(svgz::text(&data, cap).unwrap().len(), decoded.len(), "decoding itself succeeds"); let limits = Limits { bytes: cap, embedded_bytes: decoded.len(), ..shallow() }; - assert!(matches!(checked(&decoded, &limits), Err(Error::Text(svgz::Error::TooLarge(n))) if n == cap)); + assert!(matches!(preflight(&decoded, &limits), Err(Error::Text(svgz::Error::TooLarge(n))) if n == cap)); let image = data_image(&data); let (found, usage) = resolved(&svg(&image), limits, 1); assert!(found.is_empty(), "{encoding}, gzip {gzip}"); @@ -312,7 +312,7 @@ mod resource_limit_usage { let decoded = svgz::decode(&raw).unwrap(); let limits = Limits { bytes: decoded.len(), embedded_bytes: raw.len(), ..shallow() }; assert!(decoded.len() > limits.embedded_bytes); - checked(&decoded, &limits).unwrap(); // The independent per-image cap allows this complete XML. + preflight(&decoded, &limits).unwrap(); // The independent per-image cap allows this complete XML. for gzip in [false, true] { let data = if gzip { svgz::compress(&raw) } else { raw.clone() }; assert_eq!(svgz::inflate(&data, limits.embedded_bytes).unwrap().len(), raw.len()); @@ -584,3 +584,104 @@ mod invalid_image_accounting { assert_eq!(report.raster_limit_images, 1); } } + +#[test] +fn preflight_keeps_source_depth_and_parses_the_checked_tree() { + let text = svg(""); + let checked = preflight(&text, &Limits::default()).unwrap(); + // svg and g are open; the self-closing rect opens no level. + assert_eq!(checked.depth(), 2); + let tree = checked.parse(&options()).unwrap(); + assert_eq!(tree.size().width(), 10.0); + assert!(matches!(preflight(&svg(&groups(30)), &shallow()), Err(Error::TooDeep(24)))); +} + +#[test] +fn preflight_matches_parse_text_refusals() { + for text in [""] { + assert!(matches!(preflight(text, &Limits::default()), Err(Error::Xml(_)))); + assert!(matches!(parse_text(text, &Limits::default(), &options()), Err(Error::Xml(_)))); + } +} + +#[test] +fn rewrite_root_viewport_wraps_the_root_and_keeps_the_preamble() { + let source = concat!( + "\n]>\n", + "", + "" + ); + let out = rewrite_root_viewport(source, Viewport { width: 40.0, height: 30.0 }, &Limits::default(), 4096).unwrap(); + assert!(out.starts_with("\n]>\n")); + let tree = parse_text(&out, &Limits::default(), &options()).unwrap(); + assert_eq!((tree.size().width(), tree.size().height()), (40.0, 30.0)); +} + +#[test] +fn rewrite_root_viewport_reads_points_and_pixels() { + let points = rewrite_root_viewport( + "", + Viewport { width: 1.0, height: 1.0 }, + &Limits::default(), + 4096, + ) + .unwrap(); + assert!(points.contains("viewBox=\"0 0 96 48\"")); + let pixels = rewrite_root_viewport( + "", + Viewport { width: 1.0, height: 1.0 }, + &Limits::default(), + 4096, + ) + .unwrap(); + assert!(pixels.contains("viewBox=\"0 0 12 8\"")); +} + +#[test] +fn rewrite_root_viewport_refusals() { + let limits = Limits::default(); + let one = Viewport { width: 1.0, height: 1.0 }; + let sized = ""; + for target in [ + Viewport { width: f64::NAN, height: 1.0 }, + Viewport { width: 1.0, height: f64::INFINITY }, + Viewport { width: 0.0, height: 1.0 }, + Viewport { width: 1.0, height: -2.0 }, + ] { + assert!(matches!(rewrite_root_viewport(sized, target, &limits, 4096), Err(Error::Viewport(_)))); + } + assert!(matches!(rewrite_root_viewport("", one, &limits, 4096), Err(Error::Viewport(_)))); + for source in [ + "", + "", + "", + "", + "", + ] { + assert!(matches!(rewrite_root_viewport(source, one, &limits, 4096), Err(Error::Viewport(_))), "{source}"); + } + assert!(matches!(rewrite_root_viewport(" assert!(parse_text(&out, &limits, &options()).is_ok(), "{n} groups"), + Err(Error::TooDeep(_)) => refused_at_the_limit = true, + Err(e) => panic!("{n} groups: {e:?}"), + } + } + assert!(refused_at_the_limit, "a source at the depth limit is refused, not wrapped past it"); +} diff --git a/crates/svg/src/write.rs b/crates/svg/src/write.rs index 469a4f8fa..66cb1d6a0 100644 --- a/crates/svg/src/write.rs +++ b/crates/svg/src/write.rs @@ -86,18 +86,36 @@ impl Attribute { pub fn translate_scale(x: f64, y: f64, scale_x: f64, scale_y: f64) -> Self { Self { name: "transform".into(), value: Value::TranslateScale { x, y, scale_x, scale_y } } } + /// A translate/scale transform with three decimal places for positions and four for scales. + pub fn translate_scale_fixed(x: f64, y: f64, scale_x: f64, scale_y: f64) -> Self { + Self::text("transform", format!("translate({} {}) scale({} {})", num(x, 3), num(y, 3), num(scale_x, 4), num(scale_y, 4))) + } } impl Scene { /// Serialize the scene as an SVG 1.1 document. pub fn write(&self) -> String { + self.write_impl(false) + } + + /// Serialize the root viewBox with three decimal places for stable fixed-width output. + pub fn write_fixed_view_box(&self) -> String { + self.write_impl(true) + } + + fn write_impl(&self, fixed: bool) -> String { let mut writer = SvgWriter::new(); + let view_box = if fixed { + format!("{} {} {} {}", num(self.view_box[0], 3), num(self.view_box[1], 3), num(self.view_box[2], 3), num(self.view_box[3], 3)) + } else { + view_box_data(self.view_box) + }; writer .root() .attr("version", "1.1") .attr("width", format!("{}mm", num(self.width_mm, 3))) .attr("height", format!("{}mm", num(self.height_mm, 3))) - .attr("viewBox", view_box_data(self.view_box)) + .attr("viewBox", view_box) .open(); write_elements(&mut writer, &self.elements, 1); writer.line(0); @@ -307,6 +325,14 @@ mod tests { p } + #[test] + fn scene_can_keep_fixed_view_box_without_changing_default_output() { + let scene = Scene { width_mm: 100.0, height_mm: 150.0, view_box: [0.0, 0.0, 100.0, 150.0], elements: vec![] }; + assert!(scene.write().contains("viewBox=\"0 0 100 150\"")); + assert!(scene.write_fixed_view_box().contains("viewBox=\"0.000 0.000 100.000 150.000\"")); + assert_eq!(Attribute::translate_scale_fixed(6.0, 46.0, 0.5, -0.5).value, Value::Text("translate(6.000 46.000) scale(0.5000 -0.5000)".into())); + } + #[test] fn numbers_have_fixed_decimals_and_are_never_non_finite() { assert_eq!(num(1.5, 3).to_string(), "1.500"); diff --git a/crates/tcraft/Cargo.toml b/crates/tcraft/Cargo.toml new file mode 100644 index 000000000..3ce35abfb --- /dev/null +++ b/crates/tcraft/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "craft-tcraft" +description = "Streaming binary payloads of TextureCraft project archives" +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/tcraft/README.md b/crates/tcraft/README.md new file mode 100644 index 000000000..e5831ef98 --- /dev/null +++ b/crates/tcraft/README.md @@ -0,0 +1,14 @@ +# craft-tcraft + +Streaming neutral `TCTX` binary payloads used inside TextureCraft `.tcraft` projects. Sparse +paint data stores 64×64 tiles of little-endian `u16` components; baked maps store dense +little-endian `f32` values. This crate does not yet define the project JSON schema or ZIP +envelope and does not depend on TextureCraft's app types. + +`read_tiles` validates the expected dimensions and components, bounds tile count, rejects +out-of-range and repeated tile positions, and delivers one owned tile at a time. `read_baked` +validates dimensions and declared length and decodes in bounded chunks. Both consume the same +caller-owned `craft_zip::Budget` used for the surrounding project and reject trailing data. +`write_tiles` and `write_baked` stream their bodies rather than copying an entire image; a caller +must discard an incomplete output if a write fails. Limits and ownership rules beyond this +binary format remain with the project reader. diff --git a/crates/tcraft/src/lib.rs b/crates/tcraft/src/lib.rs new file mode 100644 index 000000000..d7af3e8a6 --- /dev/null +++ b/crates/tcraft/src/lib.rs @@ -0,0 +1,428 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] +//! Streaming `TCTX` tile and baked-map payloads in TextureCraft `.tcraft` archives. +//! +//! This codec handles the neutral binary payload, not the application's project model or ZIP +//! envelope. Reads use a caller-owned budget shared with other archive entries. No reader opens +//! a file or reserves memory based on a claimed payload before checking it. + +use std::collections::{BTreeMap, HashSet}; +use std::io::{self, Read, Write}; + +use craft_zip::Budget; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +pub const TILE_SIDE: u32 = 64; +pub const TILE_AREA: usize = 64 * 64; +pub const HEADER_BYTES: u64 = 24; +pub const VERSION: u32 = 1; +const MAGIC: &[u8; 4] = b"TCTX"; +const CHUNK: usize = 1 << 16; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("invalid TCTX payload: {0}")] + Invalid(String), + #[error("TCTX version {0} is newer than supported")] + NewerVersion(u32), + #[error("TCTX budget: {0}")] + Budget(#[from] craft_zip::Exceeded), + #[error("TCTX limit: {0}")] + Limit(String), + #[error("TCTX I/O: {0}")] + Io(#[from] io::Error), +} + +/// Neutral top-level project schema. Nested app-domain values and unknown fields remain JSON +/// so they round-trip without requiring TextureCraft's in-memory model. +#[derive(Clone, Debug, PartialEq, Deserialize)] +pub struct ProjectDocument { + pub format_version: u32, + pub name: String, + pub mesh: String, + pub normal_format: Value, + pub texture_sets: Vec, + pub layers: BTreeMap, + pub next_id: u64, + pub display: Value, + #[serde(default)] + pub cameras: Vec, + #[serde(flatten)] + pub extra: BTreeMap, +} + +impl Serialize for ProjectDocument { + fn serialize(&self, serializer: S) -> Result { + use serde::ser::{Error as _, SerializeMap as _}; + const RESERVED: &[&str] = &["format_version", "name", "mesh", "normal_format", "texture_sets", "layers", "next_id", "display", "cameras"]; + if self.extra.keys().any(|key| RESERVED.contains(&key.as_str())) { + return Err(S::Error::custom("an extra project field duplicates a known field")); + } + let mut map = serializer.serialize_map(Some(9 + self.extra.len()))?; + map.serialize_entry("format_version", &self.format_version)?; + map.serialize_entry("name", &self.name)?; + map.serialize_entry("mesh", &self.mesh)?; + map.serialize_entry("normal_format", &self.normal_format)?; + map.serialize_entry("texture_sets", &self.texture_sets)?; + map.serialize_entry("layers", &self.layers)?; + map.serialize_entry("next_id", &self.next_id)?; + map.serialize_entry("display", &self.display)?; + map.serialize_entry("cameras", &self.cameras)?; + for (key, value) in &self.extra { + map.serialize_entry(key, value)?; + } + map.end() + } +} + +/// Read only the version before an application applies its own recovery policy. Serde's +/// structured parse rejects a duplicate or invalid version field, like the project reader. +pub fn project_version(bytes: &[u8]) -> Result { + #[derive(Deserialize)] + struct Version { + format_version: u32, + } + Ok(serde_json::from_slice::(bytes)?.format_version) +} + +/// Width, height and number of components expected by the enclosing project schema. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Layout { + pub width: u32, + pub height: u32, + pub components: usize, +} + +#[derive(Clone, Copy, Debug)] +struct Header { + layout: Layout, + count: u32, +} + +fn charge(budget: &mut Budget, n: u64) -> Result<(), Error> { + budget.charge(n).map_err(Error::from) +} + +fn read_header(reader: &mut (impl Read + ?Sized), budget: &mut Budget) -> Result { + charge(budget, HEADER_BYTES)?; + let mut bytes = [0u8; HEADER_BYTES as usize]; + reader.read_exact(&mut bytes)?; + if bytes.get(..4) != Some(MAGIC.as_slice()) { + return Err(Error::Invalid("bad magic".into())); + } + let word = |i: usize| bytes.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])).unwrap_or(0); + let version = word(1); + if version > VERSION { + return Err(Error::NewerVersion(version)); + } + Ok(Header { layout: Layout { width: word(2), height: word(3), components: word(4) as usize }, count: word(5) }) +} + +fn write_header(writer: &mut (impl Write + ?Sized), layout: Layout, count: u32) -> Result<(), Error> { + let components = u32::try_from(layout.components).map_err(|_| Error::Limit("too many components".into()))?; + writer.write_all(MAGIC)?; + for value in [VERSION, layout.width, layout.height, components, count] { + writer.write_all(&value.to_le_bytes())?; + } + Ok(()) +} + +fn expect_end(reader: &mut (impl Read + ?Sized)) -> Result<(), Error> { + let mut byte = [0u8; 1]; + if reader.read(&mut byte)? != 0 { + return Err(Error::Invalid("trailing data".into())); + } + Ok(()) +} + +fn tile_dimensions(layout: Layout) -> Result<(u32, u32, usize), Error> { + if layout.width == 0 || layout.height == 0 || layout.components == 0 { + return Err(Error::Invalid("empty tile layout".into())); + } + let x = layout.width.div_ceil(TILE_SIDE); + let y = layout.height.div_ceil(TILE_SIDE); + let bytes = TILE_AREA.checked_mul(layout.components).and_then(|n| n.checked_mul(2)).ok_or_else(|| Error::Limit("tile is too large".into()))?; + Ok((x, y, bytes)) +} + +/// Decode tiles one at a time, transferring each tile to the callback. +/// Positions and duplicate tiles are checked before delivery; the entry must end exactly. +pub fn read_tiles( + reader: &mut (impl Read + ?Sized), + expected: Layout, + max_texels: u64, + budget: &mut Budget, + mut tile: impl FnMut(u32, u32, Vec) -> Result<(), Error>, +) -> Result<(), Error> { + let header = read_header(reader, budget)?; + let texels = u64::from(expected.width) * u64::from(expected.height); + if texels == 0 || texels > max_texels { + return Err(Error::Limit("tile image exceeds its texel limit".into())); + } + if header.layout != expected { + return Err(Error::Invalid("tile layout differs from the project".into())); + } + let (tiles_x, tiles_y, bytes_per_tile) = tile_dimensions(expected)?; + if u64::from(header.count) > u64::from(tiles_x) * u64::from(tiles_y) { + return Err(Error::Invalid("more tiles than the image holds".into())); + } + let mut bytes = Vec::new(); + let mut seen = HashSet::new(); + for _ in 0..header.count { + let payload = u64::try_from(bytes_per_tile).map_err(|_| Error::Limit("tile is too large".into()))?.saturating_add(8); + charge(budget, payload)?; + if bytes.is_empty() { + bytes.try_reserve_exact(bytes_per_tile).map_err(|_| Error::Limit("tile allocation failed".into()))?; + bytes.resize(bytes_per_tile, 0); + } + let mut pos = [0u8; 8]; + reader.read_exact(&mut pos)?; + let x = u32::from_le_bytes([pos[0], pos[1], pos[2], pos[3]]); + let y = u32::from_le_bytes([pos[4], pos[5], pos[6], pos[7]]); + if x >= tiles_x || y >= tiles_y || seen.contains(&(x, y)) { + return Err(Error::Invalid(format!("bad or repeated tile ({x}, {y})"))); + } + seen.try_reserve(1).map_err(|_| Error::Limit("tile index allocation failed".into()))?; + seen.insert((x, y)); + reader.read_exact(&mut bytes)?; + let mut values = Vec::new(); + values.try_reserve_exact(bytes_per_tile / 2).map_err(|_| Error::Limit("tile allocation failed".into()))?; + values.extend(bytes.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b))); + tile(x, y, values)?; + } + expect_end(reader) +} + +/// Write tiles in caller order, without buffering the image. A failure may leave a partial +/// entry; callers writing a project should discard the incomplete output. +pub fn write_tiles<'a>( + writer: &mut (impl Write + ?Sized), + layout: Layout, + count: u32, + tiles: impl IntoIterator, +) -> Result<(), Error> { + let (tiles_x, tiles_y, bytes_per_tile) = tile_dimensions(layout)?; + if u64::from(count) > u64::from(tiles_x) * u64::from(tiles_y) { + return Err(Error::Invalid("more tiles than the image holds".into())); + } + write_header(writer, layout, count)?; + let mut seen = HashSet::new(); + let mut bytes = Vec::new(); + let mut written = 0u32; + for (x, y, values) in tiles { + if written >= count || x >= tiles_x || y >= tiles_y || seen.contains(&(x, y)) || values.len() != bytes_per_tile / 2 { + return Err(Error::Invalid("bad, repeated or incorrectly sized tile".into())); + } + seen.try_reserve(1).map_err(|_| Error::Limit("tile index allocation failed".into()))?; + seen.insert((x, y)); + if bytes.is_empty() { + bytes.try_reserve_exact(bytes_per_tile).map_err(|_| Error::Limit("tile allocation failed".into()))?; + } + bytes.clear(); + bytes.extend(values.iter().flat_map(|v| v.to_le_bytes())); + writer.write_all(&x.to_le_bytes())?; + writer.write_all(&y.to_le_bytes())?; + writer.write_all(&bytes)?; + written += 1; + } + if written != count { + return Err(Error::Invalid("tile count differs from its data".into())); + } + Ok(()) +} + +/// Read a dense f32 map, validating declared size and charging its shared budget first. +pub fn read_baked( + reader: &mut (impl Read + ?Sized), + declared_bytes: u64, + components: usize, + max_texels: u64, + budget: &mut Budget, +) -> Result<(Layout, Vec), Error> { + let header = read_header(reader, budget)?; + let layout = header.layout; + let texels = u64::from(layout.width) * u64::from(layout.height); + if texels == 0 || texels > max_texels || layout.components != components || components == 0 { + return Err(Error::Invalid("bad baked-map dimensions or components".into())); + } + let count = texels.checked_mul(components as u64).ok_or_else(|| Error::Limit("too many map values".into()))?; + let payload = count.checked_mul(4).ok_or_else(|| Error::Limit("map is too large".into()))?; + if declared_bytes != HEADER_BYTES.checked_add(payload).ok_or_else(|| Error::Limit("map is too large".into()))? { + return Err(Error::Invalid(format!("{declared_bytes} bytes for a {}×{}×{} map", layout.width, layout.height, layout.components))); + } + charge(budget, payload)?; + let count = usize::try_from(count).map_err(|_| Error::Limit("too many map values".into()))?; + let mut values = Vec::new(); + let chunk_bytes = CHUNK.min(count) * 4; + let mut bytes = Vec::new(); + bytes.try_reserve_exact(chunk_bytes).map_err(|_| Error::Limit("map chunk allocation failed".into()))?; + bytes.resize(chunk_bytes, 0); + while values.len() < count { + let n = CHUNK.min(count - values.len()); + let chunk = bytes.get_mut(..n * 4).ok_or_else(|| Error::Invalid("bad map chunk".into()))?; + reader.read_exact(chunk)?; + values.try_reserve(n).map_err(|_| Error::Limit("map allocation failed".into()))?; + values.extend(chunk.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b))); + } + expect_end(reader)?; + Ok((layout, values)) +} + +/// Write a dense f32 map in bounded chunks without constructing a second copy of its payload. +pub fn write_baked(writer: &mut (impl Write + ?Sized), layout: Layout, values: &[f32]) -> Result<(), Error> { + let count = u64::from(layout.width).checked_mul(u64::from(layout.height)).and_then(|n| n.checked_mul(layout.components as u64)); + if layout.width == 0 || layout.height == 0 || layout.components == 0 || count != Some(values.len() as u64) { + return Err(Error::Invalid("map dimensions differ from its data".into())); + } + write_header(writer, layout, 0)?; + let mut bytes = Vec::new(); + bytes.try_reserve_exact(CHUNK * 4).map_err(|_| Error::Limit("map chunk allocation failed".into()))?; + for chunk in values.chunks(CHUNK) { + bytes.clear(); + bytes.extend(chunk.iter().flat_map(|v| v.to_le_bytes())); + writer.write_all(&bytes)?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + fn header(layout: Layout, count: u32) -> Vec { + let mut bytes = Vec::new(); + write_header(&mut bytes, layout, count).unwrap(); + bytes + } + + #[test] + fn project_schema_keeps_nested_and_future_fields() { + let json = serde_json::json!({ + "format_version": 2, "name": "Stone", "mesh": "mesh.obj", "normal_format": "openGl", + "texture_sets": [{"name": "main", "futureSet": {"keep": true}}], + "layers": {"1": {"kind": "paint", "futureLayer": [1, 2]}}, "next_id": 2, + "display": {"futureDisplay": 4}, "mesh_file": "source.obj", "futureProject": [3] + }); + let bytes = serde_json::to_vec(&json).unwrap(); + assert_eq!(project_version(&bytes).unwrap(), 2); + let document: ProjectDocument = serde_json::from_slice(&bytes).unwrap(); + let mut expected = json; + expected["cameras"] = serde_json::json!([]); + assert_eq!(serde_json::to_value(&document).unwrap(), expected); + let mut duplicate = document; + duplicate.extra.insert("mesh".into(), serde_json::json!("other.obj")); + assert!(serde_json::to_vec(&duplicate).is_err()); + assert!(project_version(br#"{"format_version":1,"format_version":2}"#).is_err()); + assert!(project_version(br#"{"format_version":-1}"#).is_err()); + } + + #[test] + fn baked_map_matches_the_native_little_endian_wire_layout() { + let layout = Layout { width: 2, height: 1, components: 1 }; + let mut expected = b"TCTX".to_vec(); + for value in [1u32, 2, 1, 1, 0] { + expected.extend_from_slice(&value.to_le_bytes()); + } + expected.extend_from_slice(&1.0f32.to_le_bytes()); + expected.extend_from_slice(&(-0.5f32).to_le_bytes()); + let mut actual = Vec::new(); + write_baked(&mut actual, layout, &[1.0, -0.5]).unwrap(); + assert_eq!(actual, expected); + let (decoded, values) = read_baked(&mut Cursor::new(&expected), expected.len() as u64, 1, 2, &mut Budget::new("project", 32)).unwrap(); + assert_eq!(decoded, layout); + assert_eq!(values, [1.0, -0.5]); + expected[4..8].copy_from_slice(&2u32.to_le_bytes()); + assert!(matches!(read_baked(&mut Cursor::new(&expected), 32, 1, 2, &mut Budget::new("project", 32)), Err(Error::NewerVersion(2)))); + } + + #[test] + fn dense_maps_round_trip_bit_exactly_in_bounded_chunks() { + let layout = Layout { width: 300, height: 300, components: 1 }; + let mut values = vec![0.25; 90_000]; + values[123] = f32::from_bits(0x7fc0_1234); + let mut bytes = Vec::new(); + write_baked(&mut bytes, layout, &values).unwrap(); + let mut budget = Budget::new("project", bytes.len() as u64); + let (decoded_layout, decoded) = read_baked(&mut Cursor::new(&bytes), bytes.len() as u64, 1, 100_000, &mut budget).unwrap(); + assert_eq!(decoded_layout, layout); + assert!(decoded.iter().zip(values.iter()).all(|(a, b)| a.to_bits() == b.to_bits())); + assert_eq!(budget.remaining(), 0); + } + + #[test] + fn dense_maps_refuse_false_lengths_limits_and_trailing_data() { + let layout = Layout { width: 2, height: 2, components: 1 }; + let mut bytes = Vec::new(); + write_baked(&mut bytes, layout, &[1.0; 4]).unwrap(); + let mut budget = Budget::new("project", 100); + assert!(read_baked(&mut Cursor::new(&bytes), 999, 1, 4, &mut budget).is_err()); + let mut budget = Budget::new("project", 39); + assert!(matches!(read_baked(&mut Cursor::new(&bytes), 40, 1, 4, &mut budget), Err(Error::Budget(_)))); + let mut extra = bytes.clone(); + extra.push(0); + let mut budget = Budget::new("project", 100); + assert!(read_baked(&mut Cursor::new(extra), 40, 1, 4, &mut budget).is_err()); + } + + #[test] + fn hostile_headers_and_tiles_are_rejected_without_trusting_counts() { + let layout = Layout { width: 64, height: 64, components: 1 }; + assert!(matches!( + read_tiles(&mut Cursor::new(header(layout, 0)), layout, 4095, &mut Budget::new("project", 100), |_, _, _| Ok(())), + Err(Error::Limit(_)) + )); + let mut bad_magic = header(layout, 0); + bad_magic[0] = b'X'; + assert!(read_tiles(&mut Cursor::new(bad_magic), layout, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err()); + assert!(read_tiles(&mut Cursor::new(header(layout, 2)), layout, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err()); + let other = Layout { width: 65, ..layout }; + assert!(read_tiles(&mut Cursor::new(header(layout, 0)), other, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err()); + let mut outside = header(layout, 1); + outside.extend_from_slice(&1u32.to_le_bytes()); + outside.extend_from_slice(&0u32.to_le_bytes()); + assert!(read_tiles(&mut Cursor::new(outside), layout, u64::MAX, &mut Budget::new("project", 10_000), |_, _, _| Ok(())).is_err()); + let mut truncated = header(layout, 1); + truncated.extend_from_slice(&[0; 8]); + assert!(read_tiles(&mut Cursor::new(truncated), layout, u64::MAX, &mut Budget::new("project", 10_000), |_, _, _| Ok(())).is_err()); + let mut extra = header(layout, 0); + extra.push(1); + assert!(read_tiles(&mut Cursor::new(extra), layout, u64::MAX, &mut Budget::new("project", 100), |_, _, _| Ok(())).is_err()); + } + + #[test] + fn sparse_tiles_stream_and_reject_duplicates_and_short_budget() { + let layout = Layout { width: 128, height: 64, components: 1 }; + let mut bytes = header(layout, 2); + for x in [0u32, 1] { + bytes.extend_from_slice(&x.to_le_bytes()); + bytes.extend_from_slice(&0u32.to_le_bytes()); + for _ in 0..TILE_AREA { + bytes.extend_from_slice(&(x as u16 + 1).to_le_bytes()); + } + } + let first = vec![1u16; TILE_AREA]; + let second = vec![2u16; TILE_AREA]; + let mut written = Vec::new(); + write_tiles(&mut written, layout, 2, [(0, 0, first.as_slice()), (1, 0, second.as_slice())]).unwrap(); + assert_eq!(written, bytes, "the streamed writer keeps the native wire layout"); + assert!(write_tiles(&mut Vec::new(), layout, 2, [(0, 0, first.as_slice())]).is_err()); + let mut seen = Vec::new(); + let mut budget = Budget::new("project", bytes.len() as u64); + read_tiles(&mut Cursor::new(&bytes), layout, u64::MAX, &mut budget, |x, y, tile| { + seen.push((x, y, tile.first().copied())); + Ok(()) + }) + .unwrap(); + assert_eq!(seen, [(0, 0, Some(1)), (1, 0, Some(2))]); + let mut duplicate = bytes.clone(); + let second = HEADER_BYTES as usize + 8 + TILE_AREA * 2; + duplicate[second..second + 4].copy_from_slice(&0u32.to_le_bytes()); + assert!(read_tiles(&mut Cursor::new(duplicate), layout, u64::MAX, &mut Budget::new("project", u64::MAX), |_, _, _| Ok(())).is_err()); + assert!(matches!( + read_tiles(&mut Cursor::new(bytes), layout, u64::MAX, &mut Budget::new("project", HEADER_BYTES), |_, _, _| Ok(())), + Err(Error::Budget(_)) + )); + } +} diff --git a/crates/vectorcraft-format/Cargo.toml b/crates/vectorcraft-format/Cargo.toml index e85d90886..c10435289 100644 --- a/crates/vectorcraft-format/Cargo.toml +++ b/crates/vectorcraft-format/Cargo.toml @@ -15,3 +15,6 @@ flate2 = { version = "1", default-features = false, features = ["rust_backend"] serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/vectorcraft-format/src/lib.rs b/crates/vectorcraft-format/src/lib.rs index 051c31a70..b180c046c 100644 --- a/crates/vectorcraft-format/src/lib.rs +++ b/crates/vectorcraft-format/src/lib.rs @@ -34,6 +34,7 @@ //! [`sniff`] reads: a reader of a compressed file decompresses it with its own limits first. pub mod base64; +pub mod stream; use std::collections::{BTreeMap, HashMap}; use std::fmt; @@ -769,10 +770,20 @@ impl<'a> Tail<'a> { /// Write what follows the JSON: [`BLOB_MAGIC`] and the entries, or nothing when there are none. pub fn write_to(&self, mut w: impl Write) -> io::Result<()> { + self.write_to_with(&mut w, |_, _| Ok(())) + } + + /// Write the marker and parts, calling `before_part` before each payload. A compressed + /// writer can use the MIME type to switch between compressed and stored deflate blocks. + pub fn write_to_with(&self, mut w: W, mut before_part: impl FnMut(&mut W, &str) -> io::Result<()>) -> io::Result<()> { if self.parts.is_empty() { return Ok(()); } w.write_all(BLOB_MAGIC)?; - self.parts.iter().try_for_each(|p| w.write_all(p.bytes)) + for part in &self.parts { + before_part(&mut w, &part.mime)?; + w.write_all(part.bytes)?; + } + Ok(()) } } diff --git a/crates/vectorcraft-format/src/stream.rs b/crates/vectorcraft-format/src/stream.rs new file mode 100644 index 000000000..2e8b0695e --- /dev/null +++ b/crates/vectorcraft-format/src/stream.rs @@ -0,0 +1,302 @@ +//! Streaming helpers for the native JSON envelope. +//! +//! Field values are emitted in bounded chunks. The decoded JSON and tail framing stay the same +//! as VectorCraft's previous writer, but compressed bytes can differ when a deflate backend's +//! output depends on input chunking. Gzip readers still validate the CRC and size trailer. +//! +//! Copyright (c) 2026 ArtCraft Team and the VectorCraft contributors. +//! Extracted from VectorCraft, licensed under MIT OR Apache-2.0. + +use serde::Serialize; +use std::collections::{BTreeMap, HashSet}; +use std::io::{self, Write}; + +/// Native file metadata for streaming a JSON envelope. Asset entries should be prepared using +/// the same [`crate::Tail`] whose parts will be written after the JSON. +pub struct FileHeader<'a> { + /// Native format version to write. + pub version: u32, + /// Application generator string. + pub generator: &'a str, + /// Optional saved PDF compatibility preference. + pub pdf_compatible: Option, + /// Optional inline preview. + pub preview: Option<&'a crate::Image>, + /// Embedded profiles keyed by profile name. + pub profiles: &'a BTreeMap<&'a str, crate::Image>, + /// Optional PDF entry. + pub pdf: Option<&'a crate::Image>, +} + +/// Write the native JSON envelope, streaming the document through `document`. The callback +/// returns the image entries after serializing the document, allowing it to collect image keys +/// without keeping a copy of the document JSON. It must write one complete JSON value. +pub fn write_file_json<'a>( + mut out: impl Write, + header: &FileHeader<'a>, + pretty: bool, + document: impl FnOnce(&mut dyn Write) -> io::Result>, +) -> io::Result<()> { + if !(crate::MIN_VERSION..=crate::VERSION).contains(&header.version) { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "unsupported native file version")); + } + let mut env = Envelope::new(pretty); + let format = if header.version < 3 { crate::LEGACY_EXTENSION } else { crate::EXTENSION }; + env.field(&mut out, "format", &format)?; + env.field(&mut out, "version", &header.version)?; + env.field(&mut out, "generator", &header.generator)?; + if let Some(compatible) = header.pdf_compatible { + env.field(&mut out, "pdfCompatible", &compatible)?; + } + if let Some(preview) = header.preview { + env.field(&mut out, "preview", preview)?; + } + if !header.profiles.is_empty() { + env.field(&mut out, "profiles", header.profiles)?; + } + if let Some(pdf) = header.pdf { + env.field(&mut out, "pdf", pdf)?; + } + env.key(&mut out, "document")?; + let images = document(&mut Indent { inner: &mut out, on: pretty })?; + env.field(&mut out, "images", &images)?; + env.finish(&mut out) +} + +/// Indents JSON text written through it one level (two spaces after each newline), as a member +/// of the file's top-level object; newlines only occur between tokens, inside strings they are +/// escaped. +pub struct Indent { + pub inner: W, + pub on: bool, +} + +impl Write for Indent { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.write_all(buf)?; + Ok(buf.len()) + } + + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + if !self.on { + return self.inner.write_all(buf); + } + let mut lines = buf.split(|&b| b == b'\n'); + self.inner.write_all(lines.next().unwrap_or_default())?; + for line in lines { + self.inner.write_all(b"\n ")?; + self.inner.write_all(line)?; + } + Ok(()) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +/// The member that names an image. +const TAG: &[u8] = b"\"key\":"; + +/// Passes JSON text through, collecting the string values of every `"key": "…"` member at any +/// depth (what the images a document uses are named by). Quotes inside JSON strings are escaped, +/// so every match is a real member. +pub struct KeyScan { + pub inner: W, + pub keys: HashSet, + /// Looking for keys (else it only passes the text on). + pub active: bool, + /// How much of [`TAG`] the last bytes matched. + matched: usize, + /// Right after a tag: skipping whitespace to its value. + after_tag: bool, + /// The value string so far, quotes included. + value: Option>, + escaped: bool, +} + +impl KeyScan { + pub fn new(inner: W) -> Self { + Self { inner, keys: HashSet::new(), active: true, matched: 0, after_tag: false, value: None, escaped: false } + } + + fn scan(&mut self, buf: &[u8]) { + if !self.active { + return; + } + let mut i = 0; + while let Some(&b) = buf.get(i) { + // Nothing in progress: skip to the next quote. + if self.matched == 0 && !self.after_tag && self.value.is_none() { + match buf.get(i..).and_then(|r| r.iter().position(|&c| c == b'"')) { + Some(at) => i += at, + None => return, + } + self.matched = 1; + i += 1; + continue; + } + self.step(b); + i += 1; + } + } + + fn step(&mut self, b: u8) { + if let Some(v) = self.value.as_mut() { + v.push(b); + if self.escaped { + self.escaped = false; + } else if b == b'\\' { + self.escaped = true; + } else if b == b'"' + && let Some(v) = self.value.take() + && let Ok(s) = serde_json::from_slice::(&v) + { + self.keys.insert(s); + } + } else if self.after_tag && !b.is_ascii_whitespace() { + self.after_tag = false; + if b == b'"' { + self.value = Some(vec![b]); + } + } + if TAG.get(self.matched) == Some(&b) { + self.matched += 1; + if self.matched == TAG.len() { + self.matched = 0; + self.after_tag = true; + self.value = None; + self.escaped = false; + } + } else { + self.matched = usize::from(b == b'"'); + } + } +} + +impl Write for KeyScan { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.write_all(buf)?; + Ok(buf.len()) + } + + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + self.scan(buf); + self.inner.write_all(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +/// The file's top-level object, written member by member (the document is streamed in). +pub struct Envelope { + pretty: bool, + first: bool, +} + +impl Envelope { + /// Start an object with optional indentation. + pub fn new(pretty: bool) -> Self { + Self { pretty, first: true } + } + + /// Serialize a member directly to the output without buffering the full value. + pub fn field(&mut self, out: &mut impl Write, key: &str, value: &impl Serialize) -> io::Result<()> { + self.key(out, key)?; + // serde_json emits small writes; keep compression and underlying I/O efficient while + // bounding temporary memory independently of the serialized table size. + let mut buffered = io::BufWriter::with_capacity(64 << 10, out); + let indented = Indent { inner: &mut buffered, on: self.pretty }; + if self.pretty { serde_json::to_writer_pretty(indented, value) } else { serde_json::to_writer(indented, value) }.map_err(io::Error::other)?; + buffered.into_inner().map(|_| ()).map_err(|e| e.into_error()) + } + + /// The start of member `key` (a plain name): its value follows, indented one level when + /// pretty (as serde_json would: see [`Indent`]). + pub fn key(&mut self, out: &mut impl Write, key: &str) -> io::Result<()> { + let sep: &[u8] = if std::mem::take(&mut self.first) { b"{" } else { b"," }; + let indent: &[u8] = if self.pretty { b"\n " } else { b"" }; + out.write_all(sep)?; + out.write_all(indent)?; + serde_json::to_writer(&mut *out, key).map_err(io::Error::other)?; + out.write_all(if self.pretty { b": " } else { b":" }) + } + + /// Close the object. + pub fn finish(self, out: &mut impl Write) -> io::Result<()> { + if self.first { + return out.write_all(b"{}"); + } + out.write_all(if self.pretty { b"\n}" } else { b"}" }) + } +} + +#[cfg(test)] +mod tests { + use proptest::prelude::*; + + use super::*; + + /// How the keys were found before saves were streamed: in the whole JSON text at once. + fn string_members(json: &[u8]) -> HashSet { + let mut out = HashSet::new(); + let mut rest = json; + while let Some(at) = rest.windows(TAG.len()).position(|w| w == TAG) { + rest = rest.get(at + TAG.len()..).unwrap_or_default(); + let value = rest.trim_ascii_start(); + if value.first() == Some(&b'"') + && let Some(Ok(s)) = serde_json::Deserializer::from_slice(value).into_iter::().next() + { + out.insert(s); + } + } + out + } + + /// The keys `KeyScan` finds in `json` written in pieces of `chunk` bytes. + fn scanned(json: &[u8], chunk: usize) -> HashSet { + let mut scan = KeyScan::new(Vec::new()); + for piece in json.chunks(chunk.max(1)) { + scan.write_all(piece).unwrap(); + } + assert_eq!(scan.inner, json, "the text passes through"); + scan.keys + } + + #[test] + fn finds_the_keys_the_whole_text_search_found() { + let v = serde_json::json!({"key": "a", "x": [{"key": "b \"q\" \\ é"}, {"monkey": "no"}, {"k": "key"}], "s": "\"key\": \"no\"", "key2": "n", "o": {"key": 3}}); + for json in [serde_json::to_vec(&v).unwrap(), serde_json::to_vec_pretty(&v).unwrap()] { + let want = string_members(&json); + assert_eq!(want, HashSet::from(["a".to_string(), "b \"q\" \\ é".to_string()])); + for chunk in [1, 2, 3, 7, 64, json.len()] { + assert_eq!(scanned(&json, chunk), want, "chunk {chunk}"); + } + } + } + + fn arb_json() -> impl Strategy { + let key = prop::sample::select(vec!["key", "k", "kkey", "key ", "x"]).prop_map(str::to_string); + let leaf = prop_oneof![ + Just(serde_json::Value::Null), + any::().prop_map(serde_json::Value::from), + "[a-z\"\\\\: ké\n]{0,8}".prop_map(serde_json::Value::from), + ]; + leaf.prop_recursive(4, 32, 6, move |inner| { + prop_oneof![ + prop::collection::vec(inner.clone(), 0..5).prop_map(serde_json::Value::from), + prop::collection::vec((key.clone(), inner), 0..5).prop_map(|m| serde_json::Value::Object(m.into_iter().collect())), + ] + }) + } + + proptest! { + #[test] + fn scanning_in_pieces_finds_what_the_whole_text_search_found(v in arb_json(), chunk in 1usize..40, pretty in any::()) { + let json = if pretty { serde_json::to_vec_pretty(&v) } else { serde_json::to_vec(&v) }.unwrap(); + prop_assert_eq!(scanned(&json, chunk), string_members(&json)); + } + } +} diff --git a/crates/vectorcraft-format/tests/stream.rs b/crates/vectorcraft-format/tests/stream.rs new file mode 100644 index 000000000..98f4685e2 --- /dev/null +++ b/crates/vectorcraft-format/tests/stream.rs @@ -0,0 +1,182 @@ +use craft_vectorcraft_format::stream::{Envelope, Indent, KeyScan}; +use std::io::{self, Write}; + +#[test] +fn envelope_streams_nested_json_and_escapes_keys() { + for pretty in [false, true] { + let mut bytes = Vec::new(); + let mut env = Envelope::new(pretty); + env.field(&mut bytes, "quoted\"key", &vec![1, 2]).unwrap(); + env.key(&mut bytes, "document").unwrap(); + let mut output = Indent { inner: &mut bytes, on: pretty }; + if pretty { + serde_json::to_writer_pretty(&mut output, &serde_json::json!({"nested": [3,4]})).unwrap(); + } else { + serde_json::to_writer(&mut output, &serde_json::json!({"nested": [3,4]})).unwrap(); + } + env.finish(&mut bytes).unwrap(); + let value: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(value, serde_json::json!({"quoted\"key": [1, 2], "document": {"nested": [3,4]}})); + let mut expected = Vec::new(); + let empty = Envelope::new(pretty); + empty.finish(&mut expected).unwrap(); + assert_eq!(expected, b"{}"); + } +} + +#[test] +fn scan_survives_every_chunk_boundary_and_forwards_data() { + let json = br#"{"key":"one","nested":[{"key":"two \\\"quoted\\\""},{"key":42}],"other":"x"}"#; + for chunk in 1..=json.len() { + let mut scan = KeyScan::new(Vec::new()); + for part in json.chunks(chunk) { + scan.write_all(part).unwrap(); + } + assert_eq!(scan.inner, json); + assert!(scan.keys.contains("one")); + assert_eq!(scan.keys.len(), 2); + } +} + +struct Broken; +impl Write for Broken { + fn write(&mut self, _: &[u8]) -> io::Result { + Err(io::Error::other("failure")) + } + fn flush(&mut self) -> io::Result<()> { + Err(io::Error::other("failure")) + } +} +#[test] +fn forwards_writer_errors() { + assert!(Envelope::new(false).field(&mut Broken, "x", &1).is_err()); + assert!(KeyScan::new(Broken).write_all(b"{}").is_err()); + assert!(Indent { inner: Broken, on: true }.write_all(b"x\ny").is_err()); +} + +#[test] +fn tail_calls_hook_in_order_and_propagates_errors() { + use craft_vectorcraft_format::{BLOB_MAGIC, INLINE_MAX, Tail}; + let first = vec![1; INLINE_MAX]; + let second = vec![2; INLINE_MAX]; + let mut tail = Tail::new(4); + tail.image("image/png", &first, false); + tail.image("application/pdf", &second, false); + let mut out = Vec::new(); + let mut seen = Vec::new(); + tail.write_to_with(&mut out, |_, mime| { + seen.push(mime.to_owned()); + Ok(()) + }) + .unwrap(); + assert_eq!(seen, ["image/png", "application/pdf"]); + assert_eq!(out, [BLOB_MAGIC, &first, &second].concat()); + assert!(tail.write_to_with(Vec::new(), |_, _| Err(io::Error::other("failure"))).is_err()); + let mut called = false; + Tail::new(4) + .write_to_with(Vec::new(), |_, _| { + called = true; + Ok(()) + }) + .unwrap(); + assert!(!called); +} + +#[test] +fn native_schema_order_and_versions() { + use craft_vectorcraft_format::stream::{FileHeader, write_file_json}; + let profiles = Default::default(); + for (version, name) in [(1, "drawcraft"), (4, "vectorcraft")] { + for pretty in [false, true] { + let header = FileHeader { version, generator: "test", pdf_compatible: Some(false), preview: None, profiles: &profiles, pdf: None }; + let mut out = Vec::new(); + write_file_json(&mut out, &header, pretty, |w| { + w.write_all(if pretty { b"{\n \"key\": \"image\"\n}" } else { b"{\"key\":\"image\"}" })?; + Ok(Default::default()) + }) + .unwrap(); + let value: serde_json::Value = serde_json::from_slice(&out).unwrap(); + assert_eq!(value["format"], name); + assert_eq!(value["pdfCompatible"], false); + let text = std::str::from_utf8(&out).unwrap(); + assert!(text.find("\"format\"").unwrap() < text.find("\"version\"").unwrap()); + assert!(text.find("\"document\"").unwrap() < text.find("\"images\"").unwrap()); + } + } + let header = FileHeader { version: 0, generator: "test", pdf_compatible: None, preview: None, profiles: &profiles, pdf: None }; + assert!(write_file_json(Vec::new(), &header, false, |_| Ok(Default::default())).is_err()); +} + +// Oracle for the previous app writer: each member was first serialized to a Vec, then +// indented and appended. Compare decoded file bytes, not backend-dependent gzip bytes. +#[test] +fn streamed_native_file_matches_previous_framing() { + use craft_vectorcraft_format::{ + Tail, + stream::{FileHeader, write_file_json}, + }; + use flate2::{Compression, read::GzDecoder, write::GzEncoder}; + use std::collections::BTreeMap; + use std::io::Read; + + fn old_field(out: &mut Vec, first: &mut bool, pretty: bool, key: &str, value: &impl serde::Serialize) { + out.extend_from_slice(if std::mem::take(first) { b"{" } else { b"," }); + if pretty { + out.extend_from_slice(b"\n "); + } + out.extend_from_slice(format!("\"{key}\"{}", if pretty { ": " } else { ":" }).as_bytes()); + let json = if pretty { serde_json::to_vec_pretty(value) } else { serde_json::to_vec(value) }.unwrap(); + let mut indent = Indent { inner: out, on: pretty }; + indent.write_all(&json).unwrap(); + } + + let payload = vec![42; craft_vectorcraft_format::INLINE_MAX]; + for version in [1, 3, 4] { + for pretty in [false, true] { + let mut tail = Tail::new(version); + let image = tail.image("image/png", &payload, false); + let profile = tail.image("application/vnd.iccprofile", &payload, false); + let pdf = tail.image("application/pdf", &payload, false); + let profiles = BTreeMap::from([("profile", profile)]); + let images = BTreeMap::from([("image", image)]); + let document = serde_json::json!({"layers": [{"key": "image", "name": "A"}]}); + let header = FileHeader { + version, + generator: "VectorCraft test", + pdf_compatible: Some(true), + preview: None, + profiles: &profiles, + pdf: Some(&pdf), + }; + let mut actual = Vec::new(); + write_file_json(&mut actual, &header, pretty, |w| { + if pretty { serde_json::to_writer_pretty(w, &document) } else { serde_json::to_writer(w, &document) }.map_err(io::Error::other)?; + Ok(images.clone()) + }) + .unwrap(); + tail.write_to(&mut actual).unwrap(); + + let mut old = Vec::new(); + let mut first = true; + let format = if version < 3 { "drawcraft" } else { "vectorcraft" }; + old_field(&mut old, &mut first, pretty, "format", &format); + old_field(&mut old, &mut first, pretty, "version", &version); + old_field(&mut old, &mut first, pretty, "generator", &"VectorCraft test"); + old_field(&mut old, &mut first, pretty, "pdfCompatible", &true); + old_field(&mut old, &mut first, pretty, "profiles", &profiles); + old_field(&mut old, &mut first, pretty, "pdf", &pdf); + old_field(&mut old, &mut first, pretty, "document", &document); + old_field(&mut old, &mut first, pretty, "images", &images); + old.extend_from_slice(if pretty { b"\n}" } else { b"}" }); + tail.write_to(&mut old).unwrap(); + assert_eq!(actual, old, "version {version}, pretty {pretty}"); + for bytes in [&old, &actual] { + let mut gzip = GzEncoder::new(Vec::new(), Compression::default()); + gzip.write_all(bytes).unwrap(); + let mut decoded = Vec::new(); + GzDecoder::new(gzip.finish().unwrap().as_slice()).read_to_end(&mut decoded).unwrap(); + assert_eq!(decoded, old); + } + } + } +}