Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add igzip implementation #12

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ jobs:
- uses: Swatinem/rust-cache@v2
name: Rust Cache

- name: Install nasm
if: runner.os != 'macOS'
uses: ilammy/setup-nasm@v1

- name: Install build deps (OSX)
if: runner.os == 'macOS'
run: brew install automake autoconf coreutils libtool nasm

- name: Set MSVC developer prompt
if: runner.os == 'Windows'
uses: ilammy/[email protected]

- name: Audit
if: |
!matrix.codec && matrix.capi
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[package]
name = "libcramjam"
version = "0.4.6"
version = "0.5.0"
edition = "2021"
license = "MIT"
description = "Compression library combining a plethora of algorithms in a similar as possible API"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["snappy", "lz4", "bzip2", "brotli", "xz", "zstd", "gzip", "deflate", "blosc2"]
default = ["snappy", "lz4", "bzip2", "brotli", "xz", "zstd", "gzip", "deflate", "blosc2", "igzip"]
capi = ["dep:libc"]
snappy = ["dep:snap"]
lz4 = ["dep:lz4"]
bzip2 = ["dep:bzip2"]
brotli = ["dep:brotli"]
zstd = ["dep:zstd"]
igzip = ["dep:isal-rs"]

gzip = ["gzip-static"]
gzip-static = ["dep:libdeflater", "dep:libdeflate-sys", "dep:flate2"]
Expand Down Expand Up @@ -47,6 +48,7 @@ libdeflate-sys = { version = "<1.20.0", optional = true } # TODO: requires gcc>
blosc2-rs = { version = "0.3.1+2.15.1", optional = true, default-features = false }
zstd = { version = "^0.13", optional = true }
xz2 = { version = "0.1.7", optional = true }
isal-rs = { version = "0.3.0+496255c", optional = true }

[build-dependencies]
cbindgen = "^0.24"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Features:
- `lz4`
- `bzip2`
- `brotli`
- `zstd`
- `igzip`
- `xz`
- `xz-static`
- `xz-shared`
- `zstd`
- `gzip`
- `gzip-static`
- `gzip-shared`
Expand Down
45 changes: 45 additions & 0 deletions src/igzip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! igzip de/compression interface
pub use isal;
use std::io::prelude::*;
use std::io::Error;

const DEFAULT_COMPRESSION_LEVEL: u32 = 3;

/// Decompress igzip data
#[inline(always)]
pub fn decompress<W: Write + ?Sized, R: Read>(input: R, output: &mut W) -> Result<usize, Error> {
let mut decoder = isal::igzip::read::Decoder::new(input);
let nbytes = std::io::copy(&mut decoder, output)?;
Ok(nbytes as usize)
}

/// Compress igzip data
#[inline(always)]
pub fn compress<W: Write + ?Sized, R: Read>(
input: R,
output: &mut W,
level: Option<u32>,
) -> Result<usize, Error> {
let level = level.unwrap_or_else(|| DEFAULT_COMPRESSION_LEVEL);
let level = isal::igzip::CompressionLevel::try_from(level as isize)?;
let mut encoder = isal::igzip::read::Encoder::new(input, level, true);
let n_bytes = std::io::copy(&mut encoder, output)?;
Ok(n_bytes as usize)
}

#[cfg(test)]
mod tests {

#[test]
fn test_gzip_multiple_streams() {
let mut out1 = vec![];
let mut out2 = vec![];
super::compress(b"foo".to_vec().as_slice(), &mut out1, None).unwrap();
super::compress(b"bar".to_vec().as_slice(), &mut out2, None).unwrap();

let mut out3 = vec![];
out1.extend_from_slice(&out2);
super::decompress(out1.as_slice(), &mut out3).unwrap();
assert_eq!(out3, b"foobar".to_vec());
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ mod capi;
pub mod deflate;
#[cfg(any(feature = "gzip", feature = "gzip-static", feature = "gzip-shared"))]
pub mod gzip;
#[cfg(feature = "igzip")]
pub mod igzip;
#[cfg(feature = "lz4")]
pub mod lz4;
#[cfg(feature = "snappy")]
Expand Down Expand Up @@ -57,6 +59,7 @@ mod tests {
crate::$variant::compress(&mut Cursor::new(data.as_slice()), &mut Cursor::new(&mut compressed) $(, $args)*).unwrap()
};

println!("Compressed size: {}", compressed_size);
compressed.truncate(compressed_size);

let mut decompressed = Vec::new();
Expand Down Expand Up @@ -97,6 +100,9 @@ mod tests {
#[cfg(feature = "gzip")]
test_variant!(gzip, None);

#[cfg(feature = "igzip")]
test_variant!(igzip, None);

#[cfg(feature = "brotli")]
test_variant!(brotli, None);

Expand Down
Loading