diff --git a/Cargo.lock b/Cargo.lock index e59dfab5..ea78d5ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11478,6 +11478,15 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "ruzstd" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ff0cc5e135c8870a775d3320910cd9b564ec036b4dc0b8741629020be63f01" +dependencies = [ + "twox-hash", +] + [[package]] name = "ryu" version = "1.0.20" @@ -11676,6 +11685,7 @@ dependencies = [ "bitvec", "derive_more", "eyre", + "ruzstd", "scroll-alloy-consensus", "scroll-l1", "serde_json", @@ -13571,6 +13581,12 @@ dependencies = [ "utf-8", ] +[[package]] +name = "twox-hash" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea3136b675547379c4bd395ca6b938e5ad3c3d20fad76e7fe85f9e0d011419c" + [[package]] name = "typenum" version = "1.19.0" diff --git a/crates/codec/Cargo.toml b/crates/codec/Cargo.toml index 581f8f64..120ddd5b 100644 --- a/crates/codec/Cargo.toml +++ b/crates/codec/Cargo.toml @@ -19,11 +19,14 @@ bitvec.workspace = true derive_more = { version = "2.0", default-features = false } eyre = { workspace = true, optional = true } thiserror = { version = "2.0", default-features = false } -zstd = "=0.13.3" +zstd = { version = "=0.13.3", optional = true } +ruzstd = "0.8" [dev-dependencies] eyre.workspace = true serde_json = "1.0" [features] +default = ["zstd"] test-utils = ["dep:eyre", "scroll-l1/test-utils"] +zstd = ["dep:zstd"] diff --git a/crates/codec/src/decoding/v2/zstd.rs b/crates/codec/src/decoding/v2/zstd.rs index e5969031..4b580ead 100644 --- a/crates/codec/src/decoding/v2/zstd.rs +++ b/crates/codec/src/decoding/v2/zstd.rs @@ -2,14 +2,18 @@ use std::io::Read; -use zstd::Decoder; +#[cfg(feature = "zstd")] +use ruzstd as _; /// The ZSTD magic number for zstd compressed data header. const ZSTD_MAGIC_NUMBER: [u8; 4] = [0x28, 0xb5, 0x2f, 0xfd]; /// Uncompress the provided data. +#[cfg(feature = "zstd")] pub fn decompress_blob_data(data: &[u8]) -> Vec { + use zstd::Decoder; let mut header_data = ZSTD_MAGIC_NUMBER.to_vec(); + header_data.extend_from_slice(data); // init decoder and owned output data. @@ -31,3 +35,20 @@ pub fn decompress_blob_data(data: &[u8]) -> Vec { output } + +/// Uncompress the provided data. +#[cfg(not(feature = "zstd"))] +pub fn decompress_blob_data(data: &[u8]) -> Vec { + use ruzstd::decoding::StreamingDecoder; + + let mut header_data = ZSTD_MAGIC_NUMBER.to_vec(); + header_data.extend_from_slice(data); + + // init decoder and owned output data. + let mut decoder = StreamingDecoder::new(header_data.as_slice()).unwrap(); + // heuristic: use data length as the allocated output capacity. + let mut output = Vec::with_capacity(header_data.len()); + decoder.read_to_end(&mut output).unwrap(); + + output +}