use alloc::vec::Vec;
use crate::{
error::{Error, Result},
format::constants::{COMPRESS_LZO, COMPRESS_NONE, COMPRESS_ZLIB, COMPRESS_ZSTD},
};
#[cfg(feature = "lzo")]
mod lzo;
#[cfg(feature = "zlib")]
mod zlib;
#[cfg(feature = "zstd")]
mod zstd;
#[cfg(any(feature = "zlib", feature = "zstd", feature = "lzo"))]
pub(crate) use crate::format::constants::MAX_DECOMPRESSED_EXTENT_BYTES;
pub(crate) fn decode(algorithm: u8, src: &[u8], dst: &mut Vec<u8>) -> Result<()> {
match algorithm {
COMPRESS_NONE => {
dst.extend_from_slice(src);
Ok(())
}
COMPRESS_ZLIB => decode_zlib(src, dst),
COMPRESS_LZO => decode_lzo(src, dst),
COMPRESS_ZSTD => decode_zstd(src, dst),
_ => Err(Error::BadCompression {
algorithm: "comp_unknown",
}),
}
}
#[cfg(feature = "zlib")]
fn decode_zlib(src: &[u8], dst: &mut Vec<u8>) -> Result<()> {
zlib::decode(src, dst)
}
#[cfg(not(feature = "zlib"))]
fn decode_zlib(_src: &[u8], _dst: &mut Vec<u8>) -> Result<()> {
Err(Error::BadCompression {
algorithm: "comp_zlib",
})
}
#[cfg(feature = "zstd")]
fn decode_zstd(src: &[u8], dst: &mut Vec<u8>) -> Result<()> {
zstd::decode(src, dst)
}
#[cfg(not(feature = "zstd"))]
fn decode_zstd(_src: &[u8], _dst: &mut Vec<u8>) -> Result<()> {
Err(Error::BadCompression {
algorithm: "comp_zstd",
})
}
#[cfg(feature = "lzo")]
fn decode_lzo(src: &[u8], dst: &mut Vec<u8>) -> Result<()> {
lzo::decode(src, dst)
}
#[cfg(not(feature = "lzo"))]
fn decode_lzo(_src: &[u8], _dst: &mut Vec<u8>) -> Result<()> {
Err(Error::BadCompression {
algorithm: "comp_lzo",
})
}