use std::io::{Read, copy};
use anyhow::{Context, Result};
use flate2::bufread::{GzDecoder, GzEncoder};
use versatiles_derive::context;
use super::super::{LimitedWriter, max_decompressed_bytes};
use crate::Blob;
#[context("Compressing blob ({} bytes) using Gzip with highest quality settings", blob.len())]
pub fn compress_gzip(blob: &Blob) -> Result<Blob> {
let mut encoder = GzEncoder::new(blob.as_slice(), flate2::Compression::best());
let mut compressed_data = Vec::new();
encoder
.read_to_end(&mut compressed_data)
.context("Failed to compress data using Gzip")?;
Ok(Blob::from(compressed_data))
}
#[context("Compressing blob ({} bytes) using Gzip with fast compression settings", blob.len())]
pub fn compress_gzip_fast(blob: &Blob) -> Result<Blob> {
let mut encoder = GzEncoder::new(blob.as_slice(), flate2::Compression::fast());
let mut compressed_data = Vec::new();
encoder
.read_to_end(&mut compressed_data)
.context("Failed to compress data using Gzip (fast)")?;
Ok(Blob::from(compressed_data))
}
#[context("Decompressing blob ({} bytes) using Gzip", blob.len())]
pub fn decompress_gzip(blob: &Blob) -> Result<Blob> {
decompress_gzip_bounded(blob, max_decompressed_bytes())
}
fn decompress_gzip_bounded(blob: &Blob, limit: u64) -> Result<Blob> {
let mut decoder = GzDecoder::new(blob.as_slice());
let mut writer = LimitedWriter::new(limit);
copy(&mut decoder, &mut writer).context("Failed to decompress data using Gzip")?;
Ok(Blob::from(writer.into_vec()))
}
#[cfg(test)]
mod tests {
#[test]
fn decompression_stops_at_the_limit() -> Result<()> {
let bomb = compress_gzip(&Blob::from(vec![0u8; 1_000_000]))?;
assert!(
bomb.len() < 100_000,
"the test bomb did not compress: {} bytes",
bomb.len()
);
let error = decompress_gzip_bounded(&bomb, 64 * 1024).unwrap_err();
assert!(
format!("{error:#}").contains("exceeds the limit"),
"unexpected error: {error:#}"
);
assert_eq!(decompress_gzip_bounded(&bomb, 2_000_000)?.len(), 1_000_000);
assert_eq!(decompress_gzip_bounded(&bomb, 0)?.len(), 1_000_000);
Ok(())
}
use super::{super::super::test_utils::generate_test_data, *};
#[test]
fn should_compress_and_decompress_gzip_correctly() -> Result<()> {
let data = generate_test_data(100_000);
let compressed = compress_gzip(&data)?;
let decompressed = decompress_gzip(&compressed)?;
assert_eq!(data, decompressed, "Gzip compression and decompression failed");
Ok(())
}
#[test]
fn should_compress_and_decompress_gzip_fast_correctly() -> Result<()> {
let data = generate_test_data(100_000);
let compressed = compress_gzip_fast(&data)?;
let decompressed = decompress_gzip(&compressed)?;
assert_eq!(data, decompressed, "Fast Gzip compression and decompression failed");
Ok(())
}
}