#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Algorithm {
Zlib,
Lzo,
Zstd,
}
impl Algorithm {
pub(crate) const fn name(self) -> &'static str {
match self {
Algorithm::Zlib => "zlib",
Algorithm::Lzo => "lzo",
Algorithm::Zstd => "zstd",
}
}
pub(crate) const fn available(self) -> bool {
match self {
Algorithm::Zlib => cfg!(feature = "zlib"),
Algorithm::Lzo => cfg!(feature = "lzo"),
Algorithm::Zstd => cfg!(feature = "zstd"),
}
}
}
#[cfg_attr(
not(any(feature = "zlib", feature = "lzo", feature = "zstd")),
allow(dead_code)
)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
#[non_exhaustive]
pub(crate) enum Error {
#[error("{} is an algorithm this build does not decode", .0.name())]
Unavailable(Algorithm),
#[error("the {} stream does not decode: {fault}", .algorithm.name())]
#[non_exhaustive]
Malformed {
algorithm: Algorithm,
fault: &'static str,
},
#[error(
"the {} stream expands past the {expected} bytes the record declares",
.algorithm.name()
)]
#[non_exhaustive]
Overrun {
algorithm: Algorithm,
expected: usize,
},
}
#[cfg_attr(
not(any(feature = "zlib", feature = "lzo", feature = "zstd")),
allow(unused_variables)
)]
pub(crate) fn decompress(
algorithm: Algorithm,
input: &[u8],
out: &mut [u8],
) -> Result<usize, Error> {
match algorithm {
#[cfg(feature = "zlib")]
Algorithm::Zlib => zlib::decompress(input, out),
#[cfg(feature = "lzo")]
Algorithm::Lzo => lzo::decompress(input, out),
#[cfg(feature = "zstd")]
Algorithm::Zstd => zstd::decompress(input, out),
#[allow(unreachable_patterns)]
other => Err(Error::Unavailable(other)),
}
}
#[cfg(feature = "zlib")]
mod zlib {
use super::{Algorithm, Error};
pub(super) fn decompress(input: &[u8], out: &mut [u8]) -> Result<usize, Error> {
use miniz_oxide::inflate::TINFLStatus;
miniz_oxide::inflate::decompress_slice_iter_to_slice(
out,
core::iter::once(input),
true,
false,
)
.map_err(|status| match status {
TINFLStatus::HasMoreOutput => Error::Overrun {
algorithm: Algorithm::Zlib,
expected: out.len(),
},
TINFLStatus::NeedsMoreInput | TINFLStatus::FailedCannotMakeProgress => {
Error::Malformed {
algorithm: Algorithm::Zlib,
fault: "the stream ends mid-symbol",
}
}
TINFLStatus::BadParam => Error::Malformed {
algorithm: Algorithm::Zlib,
fault: "the stream's own parameters are not ones the format defines",
},
TINFLStatus::Adler32Mismatch => Error::Malformed {
algorithm: Algorithm::Zlib,
fault: "the stream's own checksum does not cover what it decoded to",
},
_ => Error::Malformed {
algorithm: Algorithm::Zlib,
fault: "the stream is not well-formed",
},
})
}
}
#[cfg(feature = "zstd")]
mod zstd {
use std::io::Read as _;
use super::{Algorithm, Error};
const MIN_WINDOW: u64 = 1 << 10;
pub(super) fn decompress(input: &[u8], out: &mut [u8]) -> Result<usize, Error> {
let malformed = |fault| Error::Malformed {
algorithm: Algorithm::Zstd,
fault,
};
let window_cap = (out.len() as u64).next_power_of_two().max(MIN_WINDOW);
let mut decoder =
ruzstd::decoding::StreamingDecoder::new_with_max_window_size(input, window_cap)
.map_err(|error| match error {
ruzstd::decoding::errors::FrameDecoderError::WindowSizeTooBig { .. } => {
malformed("the frame declares a window larger than what it decodes into")
}
_ => malformed("the frame header is not one the format defines"),
})?;
let mut filled = 0;
while filled < out.len() {
match decoder.read(&mut out[filled..]) {
Ok(0) => return Ok(filled),
Ok(n) => filled += n,
Err(_) => return Err(malformed("the frame does not decode")),
}
}
let mut past = [0u8; 1];
match decoder.read(&mut past) {
Ok(0) => Ok(filled),
Ok(_) => Err(Error::Overrun {
algorithm: Algorithm::Zstd,
expected: out.len(),
}),
Err(_) => Err(malformed("the frame does not decode")),
}
}
}
#[cfg(feature = "lzo")]
pub(crate) mod lzo;
#[cfg(all(test, any(feature = "lzo", feature = "zlib", feature = "zstd")))]
pub(crate) fn sweep_arbitrary_bytes<E>(
salt: u32,
mut decode: impl FnMut(&[u8], &mut [u8]) -> Result<usize, E>,
) {
const GUARD: u8 = 0xee;
for seed in 0..2048u32 {
let mut bytes = Vec::new();
let mut x = seed.wrapping_mul(2_654_435_761).wrapping_add(salt);
for _ in 0..(seed as usize % 160) + 1 {
x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345);
bytes.push((x >> 16) as u8);
}
let mut out = [GUARD; 512];
let room = out.len() - 64;
if let Ok(produced) = decode(&bytes, &mut out[..room]) {
assert!(
produced <= room,
"seed {seed} reported {produced} of {room}"
);
}
assert!(
out[room..].iter().all(|&b| b == GUARD),
"seed {seed} wrote past the buffer it was given"
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_algorithm_is_named_whether_or_not_this_build_decodes_it() {
for (algorithm, name) in [
(Algorithm::Zlib, "zlib"),
(Algorithm::Lzo, "lzo"),
(Algorithm::Zstd, "zstd"),
] {
assert_eq!(algorithm.name(), name);
assert_eq!(
Error::Unavailable(algorithm).to_string(),
format!("{name} is an algorithm this build does not decode")
);
}
}
#[test]
fn an_algorithm_this_build_does_not_carry_is_refused_rather_than_attempted() {
for algorithm in [Algorithm::Zlib, Algorithm::Lzo, Algorithm::Zstd] {
let mut out = [0u8; 8];
let refused = decompress(algorithm, b"", &mut out);
assert_eq!(
matches!(refused, Err(Error::Unavailable(_))),
!algorithm.available(),
"{algorithm:?} reported itself {} and answered {refused:?}",
if algorithm.available() {
"available"
} else {
"unavailable"
}
);
}
}
#[cfg(feature = "zlib")]
#[test]
fn a_zlib_stream_decodes_to_what_it_stands_for() {
const STREAM: &[u8] = &[
0x78, 0xda, 0x4b, 0x4b, 0x2d, 0x2a, 0xca, 0x2f, 0xae, 0x2c, 0x4e, 0x23, 0x93, 0x06,
0x00, 0x88, 0x65, 0x1b, 0xe9,
];
let want = b"ferrosys".repeat(8);
let mut out = vec![0u8; want.len()];
assert_eq!(
decompress(Algorithm::Zlib, STREAM, &mut out),
Ok(want.len())
);
assert_eq!(out, want);
let mut small = vec![0u8; want.len() - 1];
assert!(matches!(
decompress(Algorithm::Zlib, STREAM, &mut small),
Err(Error::Overrun { .. })
));
let mut out = vec![0u8; 64];
assert!(matches!(
decompress(Algorithm::Zlib, b"not a stream", &mut out),
Err(Error::Malformed { .. })
));
}
#[cfg(feature = "zstd")]
#[test]
fn a_zstd_frame_declaring_a_giant_window_is_refused_at_the_header() {
const FRAME: &[u8] = &[0x28, 0xb5, 0x2f, 0xfd, 0x00, 0x80];
let mut out = vec![0u8; 4096];
assert_eq!(
decompress(Algorithm::Zstd, FRAME, &mut out),
Err(Error::Malformed {
algorithm: Algorithm::Zstd,
fault: "the frame declares a window larger than what it decodes into",
})
);
}
#[cfg(feature = "zstd")]
#[test]
fn a_zstd_frame_decodes_to_what_it_stands_for() {
const FRAME: &[u8] = &[
0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x40, 0x7d, 0x00, 0x00, 0x48, 0x66, 0x65, 0x72, 0x72,
0x6f, 0x73, 0x79, 0x73, 0x66, 0x01, 0x00, 0x7c, 0xde, 0x23, 0x80, 0xc5, 0xf4, 0x04,
];
let want = b"ferrosys".repeat(8);
let mut out = vec![0u8; want.len()];
assert_eq!(decompress(Algorithm::Zstd, FRAME, &mut out), Ok(want.len()));
assert_eq!(out, want);
let mut small = vec![0u8; want.len() - 1];
assert!(matches!(
decompress(Algorithm::Zstd, FRAME, &mut small),
Err(Error::Overrun { .. })
));
let mut out = vec![0u8; 64];
assert!(matches!(
decompress(Algorithm::Zstd, b"not a frame at all", &mut out),
Err(Error::Malformed { .. })
));
}
#[cfg(feature = "zlib")]
#[test]
fn arbitrary_bytes_are_a_zlib_stream_that_refuses_or_fits() {
sweep_arbitrary_bytes(2, |bytes, out| decompress(Algorithm::Zlib, bytes, out));
}
#[cfg(feature = "zstd")]
#[test]
fn arbitrary_bytes_are_a_zstd_frame_that_refuses_or_fits() {
sweep_arbitrary_bytes(3, |bytes, out| decompress(Algorithm::Zstd, bytes, out));
}
}