From ad80df5bbb747a63d6fa2cc6379be8dec0fedea2 Mon Sep 17 00:00:00 2001 From: Toka Date: Mon, 14 Jun 2021 18:06:53 +0900 Subject: [PATCH] compression fix (#170) * compression fix * use miniz_oxide * fix Error::Compression --- libafl/Cargo.toml | 2 +- libafl/src/bolts/compress.rs | 21 ++++++++++----------- libafl/src/lib.rs | 11 ++--------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/libafl/Cargo.toml b/libafl/Cargo.toml index 9a5585d503..f5d36ef0a7 100644 --- a/libafl/Cargo.toml +++ b/libafl/Cargo.toml @@ -63,7 +63,7 @@ postcard = { version = "0.5.1", features = ["alloc"] } # no_std compatible serde static_assertions = "1.1.0" ctor = "0.1.20" serde_json = { version = "1.0", optional = true, default-features = false, features = ["alloc"] } # an easy way to debug print SerdeAnyMap -compression = { version = "0.1.5" } +miniz_oxide = "0.4.4" core_affinity = { version = "0.5", git = "https://github.com/s1341/core_affinity_rs" } num_enum = "0.5.1" hostname = "^0.3" # Is there really no gethostname in the stdlib? diff --git a/libafl/src/bolts/compress.rs b/libafl/src/bolts/compress.rs index 58d0bd0c45..cd4c4e3cc4 100644 --- a/libafl/src/bolts/compress.rs +++ b/libafl/src/bolts/compress.rs @@ -4,8 +4,10 @@ #[cfg(feature = "llmp_compression")] use crate::Error; use alloc::vec::Vec; -use compression::prelude::*; use core::fmt::Debug; +use miniz_oxide::{ + deflate::compress_to_vec, deflate::CompressionLevel, inflate::decompress_to_vec, +}; /// Compression for your stream compression needs. #[derive(Debug)] @@ -30,11 +32,7 @@ impl GzipCompressor { pub fn compress(&self, buf: &[u8]) -> Result>, Error> { if buf.len() >= self.threshold { //compress if the buffer is large enough - let compressed = buf - .iter() - .copied() - .encode(&mut GZipEncoder::new(), Action::Finish) - .collect::, _>>()?; + let compressed = compress_to_vec(buf, CompressionLevel::BestSpeed as u8); Ok(Some(compressed)) } else { Ok(None) @@ -45,11 +43,12 @@ impl GzipCompressor { /// Flag is used to indicate if it's compressed or not #[allow(clippy::unused_self)] pub fn decompress(&self, buf: &[u8]) -> Result, Error> { - Ok(buf - .iter() - .copied() - .decode(&mut GZipDecoder::new()) - .collect::, _>>()?) + let decompressed = decompress_to_vec(buf); + + match decompressed { + Ok(buf) => Ok(buf), + Err(_) => Err(Error::Compression), + } } } diff --git a/libafl/src/lib.rs b/libafl/src/lib.rs index bfa254ce3d..63627511c0 100644 --- a/libafl/src/lib.rs +++ b/libafl/src/lib.rs @@ -52,7 +52,7 @@ pub enum Error { Serialize(String), /// Compression error #[cfg(feature = "llmp_compression")] - Compression(String), + Compression, /// File related error #[cfg(feature = "std")] File(io::Error), @@ -83,7 +83,7 @@ impl fmt::Display for Error { match self { Self::Serialize(s) => write!(f, "Error in Serialization: `{0}`", &s), #[cfg(feature = "llmp_compression")] - Self::Compression(s) => write!(f, "Error in Compression: `{0}`", &s), + Self::Compression => write!(f, "Error in decompression"), #[cfg(feature = "std")] Self::File(err) => write!(f, "File IO failed: {:?}", &err), Self::EmptyOptional(s) => write!(f, "Optional value `{0}` was not set", &s), @@ -109,13 +109,6 @@ impl From for Error { } } -#[cfg(feature = "llmp_compression")] -impl From for Error { - fn from(err: compression::prelude::CompressionError) -> Self { - Self::Compression(format!("{:?}", err)) - } -} - /// Stringify the json serializer error #[cfg(feature = "std")] impl From for Error {