Skip to content

Commit

Permalink
add lz4 compression and decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Feb 17, 2025
1 parent 7689493 commit fbf27cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
7 changes: 7 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 backhand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ solana-nohash-hasher = "0.2.1"
libz-sys = { version = "1.1.20", features = ["zlib-ng-no-cmake-experimental-community-maintained"], default-features = false }
# Temporary workaround for https://github.com/rust-lang/libz-sys/issues/225
libz-ng-sys = { version = "<1.1.20", optional = true }
lz4_flex = { version = "0.11.3", optional = true, default-features = false }

[features]
default = ["xz", "gzip", "zstd"]
default = ["xz", "gzip", "zstd", "lz4"]
## Enables xz compression inside library and binaries
xz = ["dep:xz2"]
## Enables xz compression and forces static build inside library and binaries
Expand All @@ -47,6 +48,8 @@ gzip-zlib-ng = ["any-flate2", "any-gzip", "dep:flate2", "flate2?/zlib-ng"]
lzo = ["dep:rust-lzo"]
## Enables zstd compression inside library and binaries
zstd = ["dep:zstd", "dep:zstd-safe"]
## Enables Lz4 compression
lz4 = ["dep:lz4_flex"]
## Internal only
any-gzip = []
## Internal only
Expand Down
8 changes: 8 additions & 0 deletions backhand/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ impl CompressionAction for DefaultCompressor {
let mut decoder = zstd::bulk::Decompressor::new().unwrap();
decoder.decompress_to_buffer(bytes, out)?;
}
#[cfg(feature = "lz4")]
Compressor::Lz4 => {
out.resize(out.capacity(), 0u8);
let out_size = lz4_flex::decompress_into(bytes, out.as_mut_slice()).unwrap();
out.truncate(out_size);
}
_ => return Err(BackhandError::UnsupportedCompression(compressor)),
}
Ok(())
Expand Down Expand Up @@ -351,6 +357,8 @@ impl CompressionAction for DefaultCompressor {
encoder.compress_to_buffer(bytes, &mut buf)?;
Ok(buf)
}
#[cfg(feature = "lz4")]
(Compressor::Lz4, _option, _) => Ok(lz4_flex::compress(bytes)),
_ => Err(BackhandError::UnsupportedCompression(fc.id)),
}
}
Expand Down

0 comments on commit fbf27cf

Please sign in to comment.