Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crates/codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
23 changes: 22 additions & 1 deletion crates/codec/src/decoding/v2/zstd.rs
Comment thread
Thegaram marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

use std::io::Read;

use zstd::Decoder;
#[cfg(feature = "zstd")]
use ruzstd as _;
Comment thread
lightsing marked this conversation as resolved.
Outdated
Comment thread
lightsing marked this conversation as resolved.
Outdated

/// 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<u8> {
use zstd::Decoder;
let mut header_data = ZSTD_MAGIC_NUMBER.to_vec();

header_data.extend_from_slice(data);

// init decoder and owned output data.
Expand All @@ -31,3 +35,20 @@ pub fn decompress_blob_data(data: &[u8]) -> Vec<u8> {

output
}

/// Uncompress the provided data.
#[cfg(not(feature = "zstd"))]
pub fn decompress_blob_data(data: &[u8]) -> Vec<u8> {
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();
Comment thread
lightsing marked this conversation as resolved.
Outdated

output
}
Loading