mod adler32;
mod copy;
pub mod deflate;
mod inflate;
pub mod zstd;
use std::fmt;
use std::path::Path;
pub use adler32::{ADLER32_INIT, adler32, adler32_combine, adler32_update};
pub use inflate::{inflate_into, zlib_decompress_into};
pub use zstd::zstd_decompress_into;
use crate::error::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DecodeError {
pub offset: usize,
pub what: &'static str,
}
impl DecodeError {
#[must_use]
pub const fn new(offset: usize, what: &'static str) -> Self {
Self { offset, what }
}
#[must_use]
pub const fn shifted(self, base: usize) -> Self {
Self {
offset: self.offset.saturating_add(base),
what: self.what,
}
}
#[cold]
#[must_use]
pub fn into_error(self, file: &Path, member: Option<&str>, base: u64) -> Error {
Error::Malformed {
file: file.to_path_buf(),
member: member.map(str::to_owned),
offset: base.saturating_add(u64::try_from(self.offset).unwrap_or(u64::MAX)),
what: self.what.to_owned(),
}
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "malformed {} at offset {:#x}", self.what, self.offset)
}
}
impl std::error::Error for DecodeError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Codec {
Zlib,
Zstd,
}
impl Codec {
pub fn decompress_into(self, input: &[u8], out: &mut [u8]) -> Result<(), DecodeError> {
match self {
Self::Zlib => zlib_decompress_into(input, out),
Self::Zstd => zstd_decompress_into(input, out),
}
}
}