extern crate alloc;
#[cfg(feature = "dec")]
mod decoder;
#[cfg(feature = "dec")]
mod model;
#[cfg(feature = "dec")]
mod rangecoder;
#[cfg(any(feature = "enc", feature = "dec"))]
use alloc::vec::Vec;
#[cfg(feature = "dec")]
use embark_format::Error;
#[cfg(feature = "enc")]
use lzma_rust2::{LzmaOptions, LzmaWriter, Write as _};
const HEADER_LEN: usize = 13;
#[cfg(feature = "enc")]
const PRESET: u32 = 9;
#[cfg(feature = "enc")]
fn dict_size(n: usize) -> u32 {
let base = u32::try_from(n).unwrap_or(u32::MAX).clamp(1 << 12, 1 << 27);
base.next_power_of_two()
}
#[cfg(feature = "enc")]
pub(crate) fn compress(input: &[u8]) -> Vec<u8> {
let mut options = LzmaOptions::with_preset(PRESET);
options.dict_size = dict_size(input.len());
let out = Vec::with_capacity(HEADER_LEN + input.len() / 2 + 64);
let mut writer = LzmaWriter::new(out, &options, true, false, Some(input.len() as u64))
.expect("lzma encoder rejected our own options");
writer.write_all(input).expect("lzma encode into a Vec");
writer.finish().expect("lzma flush into a Vec")
}
#[cfg(feature = "dec")]
pub(crate) fn decompress(input: &[u8], orig_len: usize) -> Result<Vec<u8>, Error> {
if input.len() < HEADER_LEN {
return Err(Error::Truncated);
}
let props = input[0];
if props >= 9 * 5 * 5 {
return Err(Error::Corrupt);
}
let lc = (props % 9) as u32;
let rem = props / 9;
let lp = (rem % 5) as u32;
let pb = (rem / 5) as u32;
let size = u64::from_le_bytes([
input[5], input[6], input[7], input[8], input[9], input[10], input[11], input[12],
]);
if size != u64::MAX && size != orig_len as u64 {
return Err(Error::Corrupt);
}
let stream = &input[HEADER_LEN..];
decoder::decode(lc, lp, pb, stream, orig_len)
}
#[cfg(all(test, feature = "enc", feature = "dec"))]
mod tests {
use super::*;
use alloc::vec;
use alloc::vec::Vec;
fn rt(data: &[u8]) {
let c = compress(data);
let back = decompress(&c, data.len()).expect("decompress");
assert_eq!(back, data, "roundtrip mismatch (len {})", data.len());
}
#[test]
fn roundtrip_empty() {
rt(b"");
}
#[test]
fn roundtrip_single_byte() {
rt(b"Q");
}
#[test]
fn roundtrip_repetitive() {
rt(&[0u8; 1000]);
rt(&b"abcabcabc".repeat(200));
rt(&b"The quick brown fox jumps over the lazy dog. ".repeat(50));
}
#[test]
fn roundtrip_all_bytes() {
let data: Vec<u8> = (0u8..=255).collect();
rt(&data);
}
#[test]
fn roundtrip_incompressible() {
let mut x: u32 = 0x1234_5678;
let mut data = vec![0u8; 4096];
for b in data.iter_mut() {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
*b = (x & 0xff) as u8;
}
rt(&data);
}
#[test]
fn roundtrip_text_kb() {
let mut s = Vec::new();
for i in 0u32..300 {
s.extend_from_slice(b"line ");
let mut digits = [0u8; 10];
let mut d = 9;
let mut v = i;
loop {
digits[d] = b'0' + (v % 10) as u8;
v /= 10;
if v == 0 {
break;
}
d -= 1;
}
s.extend_from_slice(&digits[d..]);
s.extend_from_slice(b": the lazy dog sleeps while the quick fox runs.\n");
}
rt(&s);
}
#[test]
fn too_short_header_is_truncated() {
assert!(matches!(decompress(&[0u8; 5], 0), Err(Error::Truncated)));
}
#[test]
fn bad_props_is_corrupt() {
let mut c = compress(b"hello world");
c[0] = 0xFF; assert!(matches!(decompress(&c, 11), Err(Error::Corrupt)));
}
#[test]
fn wrong_size_field_is_corrupt() {
let mut c = compress(b"hello world");
c[5] ^= 0x01; assert!(matches!(decompress(&c, 11), Err(Error::Corrupt)));
}
#[test]
fn truncated_body_errs() {
let c = compress(&b"The quick brown fox. ".repeat(40));
let orig = b"The quick brown fox. ".repeat(40).len();
let cut = HEADER_LEN + (c.len() - HEADER_LEN) / 2;
let _ = decompress(&c[..cut], orig);
}
#[test]
fn hostile_orig_len_does_not_over_allocate() {
let hostile_orig_len: usize = 1 << 40; let mut input = Vec::new();
input.push(0x5D); input.extend_from_slice(&0u32.to_le_bytes()); input.extend_from_slice(&(hostile_orig_len as u64).to_le_bytes());
input.extend_from_slice(&[0u8, 0, 0, 0, 0]); assert_eq!(
decompress(&input, hostile_orig_len),
Err(embark_format::Error::Truncated)
);
}
#[test]
fn garbage_does_not_panic() {
let mut x: u32 = 0xDEAD_BEEF;
let mut junk = vec![0u8; 200];
for b in junk.iter_mut() {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
*b = (x & 0xff) as u8;
}
let _ = decompress(&junk, 500);
}
}