use crate::bit_writer::BitWriter;
use crate::util::EncodeError;
const MAX_META_BLOCK: usize = 1 << 24;
pub trait BrotliCompression: Send + Sync {
fn compress(&self, data: &[u8]) -> Result<Vec<u8>, EncodeError>;
}
impl std::fmt::Debug for dyn BrotliCompression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("dyn BrotliCompression")
}
}
pub(crate) fn brotli_store(data: &[u8]) -> Vec<u8> {
let mut w = BitWriter::new();
w.write(1, 0);
let mut offset = 0usize;
while offset < data.len() {
let len = (data.len() - offset).min(MAX_META_BLOCK);
let chunk = &data[offset..offset + len];
w.write(1, 0);
let mlen_minus_1 = (len - 1) as u64;
let nibbles: u32 = if mlen_minus_1 < (1 << 16) {
4
} else if mlen_minus_1 < (1 << 20) {
5
} else {
6
};
w.write(2, (nibbles - 4) as u64);
w.write(nibbles as usize * 4, mlen_minus_1);
w.write(1, 1);
w.zero_pad_to_byte();
for &b in chunk {
w.write(8, b as u64);
}
offset += len;
}
w.write(1, 1);
w.write(1, 1);
w.zero_pad_to_byte();
w.into_bytes()
}
pub(crate) fn compress(
data: &[u8],
compressor: Option<&dyn BrotliCompression>,
) -> Result<Vec<u8>, EncodeError> {
match compressor {
Some(compressor) => compressor.compress(data),
None => Ok(brotli_store(data)),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn roundtrip(data: &[u8]) {
use std::io::Write;
use std::process::{Command, Stdio};
let encoded = brotli_store(data);
let Ok(mut child) = Command::new("brotli")
.args(["-d", "-c"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
else {
return;
};
child.stdin.take().unwrap().write_all(&encoded).unwrap();
let Ok(out) = child.wait_with_output() else {
return;
};
assert!(
out.status.success(),
"brotli rejected our stream ({} bytes in): {}",
data.len(),
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(
out.stdout, data,
"brotli decoded our stream to different bytes"
);
}
#[test]
fn stores_empty() {
roundtrip(&[]);
}
#[test]
fn stores_short() {
roundtrip(b"Exif\0\0MM\0*");
}
#[test]
fn stores_incompressible() {
let mut state = 0x12345678u32;
let data: Vec<u8> = (0..70_000)
.map(|_| {
state = state.wrapping_mul(1664525).wrapping_add(1013904223);
(state >> 24) as u8
})
.collect();
roundtrip(&data);
}
#[test]
fn stores_across_nibble_widths() {
for len in [1usize, 0xFFFF, 0x1_0000, 0x1_0001] {
roundtrip(&vec![0xABu8; len]);
}
}
#[test]
fn uses_the_supplied_compressor() {
struct Prefix;
impl BrotliCompression for Prefix {
fn compress(&self, data: &[u8]) -> Result<Vec<u8>, EncodeError> {
let mut out = b"custom:".to_vec();
out.extend_from_slice(data);
Ok(out)
}
}
assert_eq!(
compress(b"payload", Some(&Prefix)).unwrap(),
b"custom:payload"
);
}
}