forked from AFLplusplus/LibAFL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add compression * modify event/llmp.rs * rename to LLMP_TAG_COMPRESS * remove compression code from bolts/llmp.rs * add compress.rs * handle compress & decompress in GzipCompress struct, compress if the size is large enough * add code for benchmark * remove LLMP_TAG_COMPRESS, use a flag instead * cargo fmt * rm test.sh * passes the test * comment benchmarks code out * add recv_buf_with_flag() * add the llmp_compress feature * add send_buf, do not compile compression code if it's not used * fix warning * merged dev * add error handling code * doc for compress.rs * remove tag from decompress * rename every flag to flags * fix some clippy.sh errors * simplify recv_buf * delete benchmark printf code * cargo fmt * fix doc Co-authored-by: Dominik Maier <domenukk@gmail.com>
- Loading branch information
Showing
8 changed files
with
207 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Compression of events passed between a broker and clients. | ||
//! Currently we use the gzip compression algorithm for its fast decompression performance. | ||
#[cfg(feature = "llmp_compress")] | ||
use crate::{ | ||
bolts::llmp::{Flag, LLMP_FLAG_COMPRESSED}, | ||
Error, | ||
}; | ||
use alloc::vec::Vec; | ||
use compression::prelude::*; | ||
use core::fmt::Debug; | ||
|
||
#[derive(Debug)] | ||
pub struct GzipCompressor { | ||
threshold: usize, | ||
} | ||
|
||
impl GzipCompressor { | ||
/// If the buffer is larger than the threshold value, we compress the buffer. | ||
pub fn new(threshold: usize) -> Self { | ||
GzipCompressor { threshold } | ||
} | ||
} | ||
|
||
impl GzipCompressor { | ||
/// Compression. | ||
/// The buffer is compressed with the gzip algo | ||
pub fn compress(&self, buf: &[u8]) -> Result<Option<Vec<u8>>, Error> { | ||
if buf.len() > self.threshold { | ||
//compress if the buffer is large enough | ||
let compressed = buf | ||
.iter() | ||
.cloned() | ||
.encode(&mut GZipEncoder::new(), Action::Finish) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
Ok(Some(compressed)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
/// Decompression. | ||
/// Flag is used to indicate if it's compressed or not | ||
pub fn decompress(&self, flags: Flag, buf: &[u8]) -> Result<Option<Vec<u8>>, Error> { | ||
if flags & LLMP_FLAG_COMPRESSED == LLMP_FLAG_COMPRESSED { | ||
let decompressed: Vec<u8> = buf | ||
.iter() | ||
.cloned() | ||
.decode(&mut GZipDecoder::new()) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
Ok(Some(decompressed)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.