From 646fa2e454b4ce6e71857972b34bd83e08d13ed0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:50:57 -0500 Subject: [PATCH] Use shared USD Crate LZ4 decompression Preserve TextureCraft scene/material policy, integer packing and writing; replace only bounded LZ4 framing reads. Depends on craft-libs #1738. Co-authored-by: bflatastic --- Cargo.lock | 10 +++++++++ Cargo.toml | 1 + crates/formats/Cargo.toml | 1 + crates/formats/src/mesh_import/usd/codec.rs | 23 +-------------------- crates/formats/src/mesh_import/usd/tests.rs | 15 ++++++++++++++ 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d63a2ff2..b821ec91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -903,6 +903,15 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-usd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=556d02941ad3b97b1d109e9c08d4477d647deb1f#556d02941ad3b97b1d109e9c08d4477d647deb1f" +dependencies = [ + "lz4_flex", + "thiserror 2.0.21", +] + [[package]] name = "craft-window" version = "0.1.0" @@ -4417,6 +4426,7 @@ dependencies = [ "craft-materialx", "craft-mcmat", "craft-mesh", + "craft-usd", "craft-zip", "exr", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 65ce6e99..f9890f38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/texturecraft" [workspace.dependencies] +craft-usd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "556d02941ad3b97b1d109e9c08d4477d647deb1f" } # internal texturecraft-core = { path = "crates/core" } texturecraft-texel = { path = "crates/texel" } diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index 756b2a78..21cf0443 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -9,6 +9,7 @@ license.workspace = true workspace = true [dependencies] +craft-usd.workspace = true craft-materialx = { workspace = true } craft-mesh = { workspace = true } craft-mcmat = { workspace = true } diff --git a/crates/formats/src/mesh_import/usd/codec.rs b/crates/formats/src/mesh_import/usd/codec.rs index a336db33..f1e0e7ee 100644 --- a/crates/formats/src/mesh_import/usd/codec.rs +++ b/crates/formats/src/mesh_import/usd/codec.rs @@ -104,28 +104,7 @@ pub(crate) fn numeric(t: u8) -> Option<(Scalar, usize)> { /// Decompress Crate's LZ4 framing into at most `max_out` bytes. pub(crate) fn decompress(input: &[u8], max_out: usize) -> Result> { - let bad = |e: lz4_flex::block::DecompressError| malformed(KIND, format!("LZ4 data: {e}")); - let (&chunks, mut rest) = input.split_first().ok_or_else(|| malformed(KIND, "empty compressed block"))?; - // LZ4 expands at most ~255×: never allocate more than the input can produce. - let mut out = vec![0u8; max_out.min(input.len().saturating_mul(256).saturating_add(64))]; - let written = if chunks == 0 { - lz4_flex::block::decompress_into(rest, &mut out).map_err(bad)? - } else { - if chunks > 127 { - return Err(malformed(KIND, "bad LZ4 chunk count")); - } - let mut total = 0usize; - for _ in 0..chunks { - let size = rest.get(..4).and_then(|b| b.try_into().ok()).map(i32::from_le_bytes).ok_or_else(|| malformed(KIND, "truncated LZ4 chunk"))?; - let size = usize::try_from(size).map_err(|_| malformed(KIND, "negative LZ4 chunk size"))?; - let chunk = rest.get(4..4usize.saturating_add(size)).ok_or_else(|| malformed(KIND, "truncated LZ4 chunk"))?; - rest = rest.get(4 + size..).unwrap_or_default(); - total += lz4_flex::block::decompress_into(chunk, out.get_mut(total..).unwrap_or_default()).map_err(bad)?; - } - total - }; - out.truncate(written); - Ok(out) + craft_usd::decompress(input, max_out).map_err(|e| malformed(KIND, e.to_string())) } /// Crate's LZ4 framing for one chunk (inputs are far below LZ4's ~2 GB chunk limit). diff --git a/crates/formats/src/mesh_import/usd/tests.rs b/crates/formats/src/mesh_import/usd/tests.rs index 1797093b..8b85a611 100644 --- a/crates/formats/src/mesh_import/usd/tests.rs +++ b/crates/formats/src/mesh_import/usd/tests.rs @@ -420,6 +420,21 @@ fn integer_coding_round_trips() { assert!(codec::decompress(&[0, 0xF0], 10).is_err()); } +#[test] +fn shared_crate_lz4_retains_chunk_and_budget_policy() { + let mut frame = vec![2]; + for raw in [b"abc".as_slice(), b"def".as_slice()] { + let block = lz4_flex::block::compress(raw); + frame.extend((block.len() as i32).to_le_bytes()); + frame.extend(block); + } + frame.extend(b"old reader ignores this tail"); + assert_eq!(codec::decompress(&frame, 6).unwrap(), b"abcdef"); + assert!(codec::decompress(&frame, 5).is_err()); + assert!(codec::decompress(&[1, 255, 255, 255, 255], 64).is_err()); + assert!(codec::decompress(&[128], 64).is_err()); +} + #[test] fn malformed_usd_never_panics() { // A small two-material mesh keeps the byte loops fast.