use super::api::{Filter, MAX_DECODED_BYTES};
use crate::error::{Error, Result};
#[derive(Debug, Default, Clone, Copy)]
pub struct Lz4;
impl Filter for Lz4 {
fn name(&self) -> &'static str {
"lz4"
}
fn forward(&self, input: &[u8]) -> Result<Vec<u8>> {
Ok(lz4_flex::compress_prepend_size(input))
}
fn reverse(&self, input: &[u8]) -> Result<Vec<u8>> {
let declared = input
.get(..4)
.and_then(|b| <[u8; 4]>::try_from(b).ok())
.map(u32::from_le_bytes)
.ok_or_else(|| Error::Corrupt("lz4 payload has no length prefix".into()))?
as usize;
if declared > MAX_DECODED_BYTES {
return Err(Error::Corrupt(format!(
"lz4 payload declares {declared} bytes, over the {MAX_DECODED_BYTES} byte limit"
)));
}
lz4_flex::decompress_size_prepended(input)
.map_err(|e| Error::Corrupt(format!("lz4 decompression failed: {e}")))
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct Crc32;
impl Filter for Crc32 {
fn name(&self) -> &'static str {
"crc32"
}
fn forward(&self, input: &[u8]) -> Result<Vec<u8>> {
let sum = crc32fast::hash(input);
let mut out = Vec::with_capacity(input.len() + 4);
out.extend_from_slice(input);
out.extend_from_slice(&sum.to_le_bytes());
Ok(out)
}
fn reverse(&self, input: &[u8]) -> Result<Vec<u8>> {
let body = verify(input)?;
Ok(body.to_vec())
}
fn forward_owned(&self, mut input: Vec<u8>) -> Result<Vec<u8>> {
let sum = crc32fast::hash(&input);
input.extend_from_slice(&sum.to_le_bytes());
Ok(input)
}
fn reverse_owned(&self, mut input: Vec<u8>) -> Result<Vec<u8>> {
let kept = verify(&input)?.len();
input.truncate(kept);
Ok(input)
}
}
fn verify(input: &[u8]) -> Result<&[u8]> {
if input.len() < 4 {
return Err(Error::Corrupt("value is too short to carry a CRC32".into()));
}
let split = input.len() - 4;
let (body, tail) = input.split_at(split);
let expected = u32::from_le_bytes([tail[0], tail[1], tail[2], tail[3]]);
let actual = crc32fast::hash(body);
if expected != actual {
return Err(Error::Corrupt(format!(
"CRC32 mismatch: stored {expected:#010x}, computed {actual:#010x}"
)));
}
Ok(body)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crc32_owned_and_borrowed_paths_agree() {
for len in [0usize, 1, 4, 5, 63, 64, 65, 4096] {
let payload: Vec<u8> = (0..len).map(|i| (i * 31 % 251) as u8).collect();
let borrowed = Crc32.forward(&payload).unwrap();
let owned = Crc32.forward_owned(payload.clone()).unwrap();
assert_eq!(borrowed, owned, "forward disagreed at {len} bytes");
assert_eq!(
owned.len(),
len + 4,
"a CRC32 adds exactly four bytes, at {len} bytes"
);
let back_borrowed = Crc32.reverse(&owned).unwrap();
let back_owned = Crc32.reverse_owned(owned).unwrap();
assert_eq!(back_borrowed, payload, "reverse lost bytes at {len}");
assert_eq!(back_owned, payload, "reverse_owned lost bytes at {len}");
}
}
#[test]
fn crc32_reverse_owned_still_refuses_a_bad_checksum() {
let mut sealed = Crc32.forward(b"payload").unwrap();
let last = sealed.len() - 1;
sealed[last] ^= 0xFF;
assert!(Crc32.reverse(&sealed).is_err());
assert!(
Crc32.reverse_owned(sealed).is_err(),
"the in-place path must verify before it truncates"
);
}
#[test]
fn lz4_round_trips_compressible_data() {
let payload = vec![7u8; 100_000];
let packed = Lz4.forward(&payload).unwrap();
assert!(
packed.len() < payload.len(),
"repetitive data should shrink"
);
assert_eq!(Lz4.reverse(&packed).unwrap(), payload);
}
#[test]
fn lz4_round_trips_incompressible_data() {
let payload: Vec<u8> = (0..2048u32)
.map(|i| (i.wrapping_mul(2654435761) >> 13) as u8)
.collect();
let packed = Lz4.forward(&payload).unwrap();
assert_eq!(Lz4.reverse(&packed).unwrap(), payload);
}
#[test]
fn lz4_round_trips_empty() {
let packed = Lz4.forward(b"").unwrap();
assert_eq!(Lz4.reverse(&packed).unwrap(), b"");
}
#[test]
fn lz4_rejects_an_absurd_declared_size_without_allocating() {
let mut bogus = u32::MAX.to_le_bytes().to_vec();
bogus.extend_from_slice(b"garbage");
match Lz4.reverse(&bogus) {
Err(Error::Corrupt(msg)) => assert!(msg.contains("limit"), "unexpected: {msg}"),
other => panic!("expected a corrupt-size error, got {other:?}"),
}
}
#[test]
fn crc32_round_trips_and_adds_four_bytes() {
let out = Crc32.forward(b"hello").unwrap();
assert_eq!(out.len(), 9);
assert_eq!(Crc32.reverse(&out).unwrap(), b"hello");
}
#[test]
fn crc32_catches_a_single_flipped_bit() {
let mut out = Crc32.forward(b"important payload").unwrap();
out[3] ^= 0b0000_0001;
assert!(matches!(Crc32.reverse(&out), Err(Error::Corrupt(_))));
}
#[test]
fn crc32_catches_a_corrupted_checksum() {
let mut out = Crc32.forward(b"payload").unwrap();
let last = out.len() - 1;
out[last] ^= 0xFF;
assert!(matches!(Crc32.reverse(&out), Err(Error::Corrupt(_))));
}
#[test]
fn crc32_rejects_a_truncated_value() {
assert!(matches!(Crc32.reverse(&[1, 2]), Err(Error::Corrupt(_))));
}
#[test]
fn crc32_round_trips_empty() {
let out = Crc32.forward(b"").unwrap();
assert_eq!(out.len(), 4);
assert_eq!(Crc32.reverse(&out).unwrap(), b"");
}
}