2024-05-12 13:11:42 +02:00
|
|
|
#[cfg(feature = "compression")]
|
2024-05-11 23:16:41 +02:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
#[cfg(feature = "compression-bz")]
|
2024-05-11 23:28:08 +02:00
|
|
|
use bzip2::read::{BzDecoder, BzEncoder};
|
2024-05-11 23:16:41 +02:00
|
|
|
#[cfg(feature = "compression-gz")]
|
2024-05-11 23:28:08 +02:00
|
|
|
use flate2::read::{GzDecoder, GzEncoder};
|
2024-05-11 23:16:41 +02:00
|
|
|
#[cfg(feature = "compression-lz")]
|
2024-05-11 23:28:08 +02:00
|
|
|
use lz4::{Decoder as Lz4Decoder, EncoderBuilder as Lz4EncoderBuilder};
|
2024-05-11 23:16:41 +02:00
|
|
|
#[cfg(feature = "compression-zs")]
|
2024-05-11 23:28:08 +02:00
|
|
|
use zstd::{Decoder as ZstdDecoder, Encoder as ZstdEncoder};
|
|
|
|
|
|
|
|
use crate::{CompressionCode, Payload};
|
2024-05-11 23:16:41 +02:00
|
|
|
|
2024-05-11 23:28:08 +02:00
|
|
|
pub(crate) fn into_decompressed(
|
|
|
|
kind: CompressionCode,
|
|
|
|
payload: Payload,
|
|
|
|
) -> Option<Payload> {
|
2024-05-11 23:16:41 +02:00
|
|
|
match kind {
|
2024-05-12 13:11:42 +02:00
|
|
|
CompressionCode::Uncompressed => Some(payload),
|
2024-05-11 23:16:41 +02:00
|
|
|
#[cfg(feature = "compression-gz")]
|
|
|
|
CompressionCode::Gz => {
|
|
|
|
let mut decoder = GzDecoder::new(&*payload);
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut decompressed = vec![];
|
2024-05-11 23:16:41 +02:00
|
|
|
match decoder.read_to_end(&mut decompressed) {
|
|
|
|
Err(_) => None,
|
2024-05-11 23:28:08 +02:00
|
|
|
Ok(_) => Some(decompressed),
|
2024-05-11 23:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "compression-bz")]
|
|
|
|
CompressionCode::Bz => {
|
|
|
|
let mut decoder = BzDecoder::new(&*payload);
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut decompressed = vec![];
|
2024-05-11 23:16:41 +02:00
|
|
|
match decoder.read_to_end(&mut decompressed) {
|
|
|
|
Err(_) => None,
|
2024-05-11 23:28:08 +02:00
|
|
|
Ok(_) => Some(decompressed),
|
2024-05-11 23:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "compression-lz")]
|
|
|
|
CompressionCode::Lz => {
|
|
|
|
let mut decoder = match Lz4Decoder::new(&*payload) {
|
|
|
|
Err(_) => return None,
|
2024-05-11 23:28:08 +02:00
|
|
|
Ok(value) => value,
|
2024-05-11 23:16:41 +02:00
|
|
|
};
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut decompressed = vec![];
|
2024-05-11 23:16:41 +02:00
|
|
|
match decoder.read_to_end(&mut decompressed) {
|
|
|
|
Err(_) => None,
|
2024-05-11 23:28:08 +02:00
|
|
|
Ok(_) => Some(decompressed),
|
2024-05-11 23:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "compression-zs")]
|
|
|
|
CompressionCode::Zs => {
|
|
|
|
let mut decoder = match ZstdDecoder::new(&*payload) {
|
|
|
|
Err(_) => return None,
|
2024-05-11 23:28:08 +02:00
|
|
|
Ok(value) => value,
|
2024-05-11 23:16:41 +02:00
|
|
|
};
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut decompressed = vec![];
|
2024-05-11 23:16:41 +02:00
|
|
|
match decoder.read_to_end(&mut decompressed) {
|
|
|
|
Err(_) => None,
|
2024-05-11 23:28:08 +02:00
|
|
|
Ok(_) => Some(decompressed),
|
2024-05-11 23:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 23:28:08 +02:00
|
|
|
pub(crate) fn into_compressed(
|
|
|
|
kind: CompressionCode,
|
|
|
|
payload: Payload,
|
|
|
|
) -> Payload {
|
2024-05-11 23:16:41 +02:00
|
|
|
match kind {
|
2024-05-12 13:11:42 +02:00
|
|
|
CompressionCode::Uncompressed => payload,
|
2024-05-11 23:16:41 +02:00
|
|
|
#[cfg(feature = "compression-gz")]
|
|
|
|
CompressionCode::Gz => {
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut encoder =
|
|
|
|
GzEncoder::new(&*payload, flate2::Compression::fast());
|
|
|
|
let mut compressed = vec![];
|
2024-05-11 23:16:41 +02:00
|
|
|
match encoder.read_to_end(&mut compressed) {
|
|
|
|
Err(err) => panic!("could not compress payload: {}", err),
|
|
|
|
Ok(_) => compressed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "compression-bz")]
|
|
|
|
CompressionCode::Bz => {
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut encoder =
|
|
|
|
BzEncoder::new(&*payload, bzip2::Compression::fast());
|
|
|
|
let mut compressed = vec![];
|
2024-05-11 23:16:41 +02:00
|
|
|
match encoder.read_to_end(&mut compressed) {
|
|
|
|
Err(err) => panic!("could not compress payload: {}", err),
|
|
|
|
Ok(_) => compressed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "compression-lz")]
|
|
|
|
CompressionCode::Lz => {
|
|
|
|
let mut encoder = Lz4EncoderBuilder::new()
|
2024-05-11 23:28:08 +02:00
|
|
|
.build(vec![])
|
2024-05-11 23:16:41 +02:00
|
|
|
.expect("could not create encoder");
|
2024-05-11 23:28:08 +02:00
|
|
|
encoder.write(&*payload).expect("could not write payload");
|
2024-05-11 23:16:41 +02:00
|
|
|
let (payload, _) = encoder.finish();
|
|
|
|
payload
|
|
|
|
}
|
|
|
|
#[cfg(feature = "compression-zs")]
|
|
|
|
CompressionCode::Zs => {
|
2024-05-11 23:28:08 +02:00
|
|
|
let mut encoder =
|
|
|
|
ZstdEncoder::new(vec![], zstd::DEFAULT_COMPRESSION_LEVEL)
|
|
|
|
.expect("could not create encoder");
|
|
|
|
encoder
|
|
|
|
.write(&*payload)
|
2024-05-11 23:16:41 +02:00
|
|
|
.expect("could not compress payload");
|
2024-05-11 23:28:08 +02:00
|
|
|
encoder.finish().expect("could not finish encoding")
|
2024-05-11 23:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|