mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-19 14:00:43 +01:00
Merge pull request #531 from storytold/refactor/shared-usd-lz4
Use shared bounded USD Crate LZ4 decoding
This commit is contained in:
5 files changed
+28
-22
No files matched your search
Generated
+10
@@ -923,6 +923,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"
|
||||
@@ -4449,6 +4458,7 @@ dependencies = [
|
||||
"craft-mcmat",
|
||||
"craft-mesh",
|
||||
"craft-tcraft",
|
||||
"craft-usd",
|
||||
"craft-zip",
|
||||
"exr",
|
||||
"flate2",
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -9,6 +9,7 @@ license.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-usd.workspace = true
|
||||
# ICC v2 serialization prerequisite: craft-libs#1735; app colour policy stays local.
|
||||
craft-cms-icc-v2 = { package = "craft-cms", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "272a095b61257917693da3d09f2f8ecccd7f0295" }
|
||||
craft-materialx = { workspace = true }
|
||||
|
||||
@@ -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<Vec<u8>> {
|
||||
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).
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user