#![cfg(feature = "deflate64")]
use compcol::deflate64::{Decoder, DecoderConfig, Deflate64, Encoder, EncoderConfig};
use compcol::{Algorithm, Decoder as _, Encoder as _, Error, Status};
fn encode_chunked(enc: &mut Encoder, input: &[u8], in_chunk: usize, out_chunk: usize) -> Vec<u8> {
let mut encoded = Vec::new();
let mut buf = vec![0u8; out_chunk.max(1)];
let mut i = 0;
while i < input.len() {
let end = (i + in_chunk).min(input.len());
let chunk = &input[i..end];
let mut consumed = 0;
while consumed < chunk.len() {
let (p, status) = enc.encode(&chunk[consumed..], &mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
match status {
Status::InputEmpty | Status::StreamEnd => break,
Status::OutputFull => continue,
}
}
i = end;
}
loop {
let (p, status) = enc.finish(&mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("deflate64 encoder finish stalled");
}
}
}
}
encoded
}
fn decode_chunked(encoded: &[u8], in_chunk: usize, out_chunk: usize) -> Result<Vec<u8>, Error> {
let mut dec = Decoder::new();
decode_chunked_with(&mut dec, encoded, in_chunk, out_chunk)
}
fn decode_chunked_with(
dec: &mut Decoder,
encoded: &[u8],
in_chunk: usize,
out_chunk: usize,
) -> Result<Vec<u8>, Error> {
let mut decoded = Vec::new();
let mut buf = vec![0u8; out_chunk.max(1)];
let mut i = 0;
while i < encoded.len() {
let end = (i + in_chunk).min(encoded.len());
let chunk = &encoded[i..end];
let mut consumed = 0;
while consumed < chunk.len() {
let (p, status) = dec.decode(&chunk[consumed..], &mut buf)?;
decoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
match status {
Status::StreamEnd => break,
Status::InputEmpty => break,
Status::OutputFull => continue,
}
}
i = end;
}
loop {
let (p, _status) = dec.decode(&[], &mut buf)?;
decoded.extend_from_slice(&buf[..p.written]);
if p.written == 0 {
break;
}
}
loop {
let (p, status) = dec.finish(&mut buf)?;
decoded.extend_from_slice(&buf[..p.written]);
match status {
Status::StreamEnd => break,
Status::OutputFull | Status::InputEmpty => {
if p.written == 0 {
panic!("deflate64 decoder finish stalled");
}
}
}
}
Ok(decoded)
}
fn encode_all(input: &[u8]) -> Vec<u8> {
let mut enc = Encoder::new();
encode_chunked(&mut enc, input, input.len().max(1), 4096)
}
fn round_trip(input: &[u8]) {
let encoded = encode_all(input);
let decoded = decode_chunked(&encoded, 4096, 4096).unwrap();
assert_eq!(decoded, input, "round-trip mismatch len {}", input.len());
}
#[test]
fn name_is_deflate64() {
assert_eq!(<Deflate64 as Algorithm>::NAME, "deflate64");
}
#[test]
fn default_config_is_level_6() {
assert_eq!(EncoderConfig::default().level, 6);
}
#[test]
fn round_trip_empty() {
round_trip(b"");
}
#[test]
fn round_trip_single_byte() {
round_trip(b"X");
}
#[test]
fn round_trip_hello_world() {
round_trip(b"hello world");
}
#[test]
fn round_trip_short_repeating() {
let input = vec![b'a'; 1024];
round_trip(&input);
}
#[test]
fn round_trip_64kib_repeating() {
let input = vec![b'a'; 64 * 1024];
round_trip(&input);
}
#[test]
fn round_trip_mixed_corpus() {
let mut state: u32 = 0xDEADBEEF;
let mut input = Vec::with_capacity(32 * 1024);
let phrases: &[&[u8]] = &[
b"the_quick_brown_fox_jumps_over_the_lazy_dog ",
b"compcol deflate64 round trip ",
b"length code 285 exercises matches up to 65538 bytes ",
];
let mut p = 0;
while input.len() < 24 * 1024 {
for _ in 0..32 {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
input.push((state >> 16) as u8);
}
input.extend_from_slice(phrases[p % phrases.len()]);
p += 1;
}
round_trip(&input);
}
#[test]
fn round_trip_long_distance() {
let header = b"deflate64-long-distance-marker-phrase-XYZ";
let mut input = Vec::with_capacity(48 * 1024);
input.extend_from_slice(header);
let mut state: u32 = 0x12345678;
while input.len() < 40 * 1024 + header.len() {
state = state.wrapping_mul(1_103_515_245).wrapping_add(12345);
input.extend_from_slice(&(state.to_le_bytes())[..1]);
}
input.extend_from_slice(header);
round_trip(&input);
}
#[test]
fn round_trip_long_match() {
let mut block = Vec::with_capacity(1024);
for i in 0..1024 {
block.push((i * 31 + 7) as u8);
}
let mut input = block.clone();
input.extend_from_slice(&block);
round_trip(&input);
}
#[test]
fn round_trip_streaming_one_byte() {
let input = b"streaming deflate64 one byte at a time".to_vec();
let mut enc = Encoder::new();
let encoded = encode_chunked(&mut enc, &input, 1, 1);
let decoded = decode_chunked(&encoded, 1, 1).unwrap();
assert_eq!(decoded, input);
}
#[test]
fn round_trip_all_levels() {
let input = b"compcol deflate64 level sweep test corpus aaabbbccc xyzxyzxyz ";
let mut input_v = Vec::new();
while input_v.len() < 4096 {
input_v.extend_from_slice(input);
}
for level in 1..=9u8 {
let mut enc = Encoder::with_config(EncoderConfig { level });
let encoded = encode_chunked(&mut enc, &input_v, 4096, 4096);
let decoded = decode_chunked(&encoded, 4096, 4096).unwrap();
assert_eq!(decoded, input_v, "level {} round-trip mismatch", level);
}
}
#[test]
fn encoder_reset_allows_reuse() {
let input_a = b"alpha alpha alpha alpha alpha".as_slice();
let input_b = b"bravo bravo bravo bravo bravo".as_slice();
let mut enc = Encoder::with_config(EncoderConfig { level: 6 });
let encoded_a = encode_chunked(&mut enc, input_a, 4096, 4096);
enc.reset();
let encoded_b = encode_chunked(&mut enc, input_b, 4096, 4096);
assert_eq!(decode_chunked(&encoded_a, 4096, 4096).unwrap(), input_a);
assert_eq!(decode_chunked(&encoded_b, 4096, 4096).unwrap(), input_b);
}
#[test]
fn decoder_reset_allows_reuse() {
let mut enc = Encoder::new();
let encoded_a = encode_chunked(&mut enc, b"hello", 4096, 4096);
enc.reset();
let encoded_b = encode_chunked(&mut enc, b"world", 4096, 4096);
let mut dec = Decoder::new();
assert_eq!(
decode_chunked_with(&mut dec, &encoded_a, 4096, 4096).unwrap(),
b"hello"
);
dec.reset();
assert_eq!(
decode_chunked_with(&mut dec, &encoded_b, 4096, 4096).unwrap(),
b"world"
);
}
const FIXTURE_XS300: &[u8] = &[0x8b, 0x20, 0x1e, 0x8c, 0x78, 0x00, 0x00];
const FIXTURE_XS300_DECODED: &[u8] = &[b'X'; 300];
#[test]
fn decode_fixture_xs300() {
let decoded = decode_chunked(FIXTURE_XS300, 4096, 4096).unwrap();
assert_eq!(decoded, FIXTURE_XS300_DECODED);
}
const FIXTURE_ARUN: &[u8] = &[
0x73, 0x1c, 0xf1, 0x60, 0xc4, 0x83, 0x11, 0x0f, 0x46, 0x02, 0x00, 0x00,
];
#[test]
fn decode_fixture_arun() {
let decoded = decode_chunked(FIXTURE_ARUN, 4096, 4096).unwrap();
let expected: Vec<u8> = vec![b'A'; 1000];
assert_eq!(decoded, expected);
}
const FIXTURE_MEDIUM: &[u8] = &[
0xe5, 0xca, 0xc9, 0x15, 0x80, 0x20, 0x10, 0x44, 0xc1, 0xbb, 0x51, 0x74, 0x04, 0xe6, 0xe4, 0x82,
0xbb, 0x8e, 0xa2, 0xa8, 0x10, 0x3d, 0x8f, 0x38, 0xfe, 0xb9, 0xea, 0x99, 0x9c, 0xae, 0x30, 0x77,
0xab, 0x5a, 0x6f, 0xdf, 0xa1, 0xc1, 0x7e, 0x2d, 0x61, 0x3f, 0x6f, 0xd9, 0xeb, 0xbc, 0x0a, 0x6f,
0x4d, 0x8a, 0xea, 0x6d, 0xac, 0x05, 0xc8, 0x80, 0x0c, 0xc8, 0xb0, 0x8c, 0xcf, 0xf8, 0x8c, 0xcf,
0xf8, 0x8c, 0xcf, 0x55, 0x06,
];
#[test]
fn decode_fixture_medium() {
let decoded = decode_chunked(FIXTURE_MEDIUM, 4096, 4096).unwrap();
let mut expected = Vec::with_capacity(2251);
for _ in 0..50 {
expected.extend_from_slice(b"the quick brown fox jumps over the lazy dog. ");
}
expected.push(b'\n');
assert_eq!(decoded.len(), 2251, "decoded length");
assert_eq!(decoded, expected);
}
const FIXTURE_LONGMATCH: &[u8] = &[
0xe5, 0xd1, 0x83, 0x81, 0x18, 0x01, 0x00, 0x00, 0xb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xff, 0x03, 0xfe, 0x1f, 0xf0, 0xff, 0x80, 0xff, 0x07, 0xfc, 0x3f, 0xe0, 0xff, 0x01, 0xff, 0x0f,
0xf8, 0xff, 0xff,
];
#[test]
fn decode_fixture_longmatch() {
let decoded = decode_chunked(FIXTURE_LONGMATCH, 4096, 4096).unwrap();
let base: Vec<u8> = (0u8..=255u8).chain((0u8..=255u8).rev()).collect();
let mut expected = Vec::with_capacity(4096);
for _ in 0..8 {
expected.extend_from_slice(&base);
}
assert_eq!(decoded.len(), 4096, "decoded length");
assert_eq!(decoded, expected);
}
#[test]
fn decode_fixture_longmatch_byte_at_a_time() {
let decoded = decode_chunked(FIXTURE_LONGMATCH, 1, 1).unwrap();
assert_eq!(decoded.len(), 4096);
}
#[test]
fn truncated_stream_rejected() {
let encoded = encode_all(b"some payload bytes that produce a few output bytes");
let truncated = &encoded[..encoded.len() / 2];
let err = decode_chunked(truncated, 4096, 4096).unwrap_err();
assert!(
matches!(
err,
Error::UnexpectedEnd | Error::Corrupt | Error::InvalidDistance
),
"unexpected error: {:?}",
err
);
}
#[test]
fn invalid_block_type_rejected() {
let bad: &[u8] = &[0b0000_0111];
let mut dec = Decoder::new();
let mut buf = [0u8; 16];
let err = dec.decode(bad, &mut buf).unwrap_err();
assert_eq!(err, Error::InvalidBlockType);
}
#[test]
fn stored_block_with_bad_nlen_rejected() {
let bad: &[u8] = &[0b0000_0001, 0x01, 0x00, 0x00, 0x00];
let mut dec = Decoder::new();
let mut buf = [0u8; 16];
let err = dec.decode(bad, &mut buf).unwrap_err();
assert_eq!(err, Error::Corrupt);
}
#[test]
fn algorithm_encoder_decoder_round_trip() {
let mut enc = <Deflate64 as Algorithm>::encoder();
let mut dec = <Deflate64 as Algorithm>::decoder();
let input = b"compcol Algorithm trait roundtrip!";
let mut encoded = Vec::new();
let mut buf = vec![0u8; 256];
let mut consumed = 0;
while consumed < input.len() {
let (p, status) = enc.encode(&input[consumed..], &mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
if matches!(status, Status::InputEmpty) {
break;
}
}
loop {
let (p, status) = enc.finish(&mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
if matches!(status, Status::StreamEnd) {
break;
}
if p.written == 0 {
panic!("finish stalled");
}
}
let mut decoded = Vec::new();
let mut consumed = 0;
loop {
let (p, status) = dec.decode(&encoded[consumed..], &mut buf).unwrap();
decoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
if matches!(status, Status::StreamEnd | Status::InputEmpty) {
break;
}
}
let (_, status) = dec.finish(&mut buf).unwrap();
assert!(matches!(status, Status::StreamEnd));
assert_eq!(decoded, input);
}
#[test]
fn decoder_dictionary_preload() {
let dictionary = b"compcol shared dictionary content used as preset history".to_vec();
let payload = b"shared dictionary content used as preset history again";
let cfg = DecoderConfig {
dictionary: dictionary.clone(),
};
let dec = Decoder::with_config(cfg);
drop(dec);
let encoded = encode_all(payload);
let decoded = decode_chunked(&encoded, 4096, 4096).unwrap();
assert_eq!(decoded, payload);
}
#[cfg(feature = "factory")]
mod factory {
use compcol::Status;
use compcol::factory;
#[test]
fn lookup_known() {
assert!(factory::encoder_by_name("deflate64").is_some());
assert!(factory::decoder_by_name("deflate64").is_some());
}
#[test]
fn names_contains_deflate64() {
assert!(factory::names().contains(&"deflate64"));
}
#[test]
fn extension_is_deflate64() {
assert_eq!(factory::extension("deflate64"), Some("deflate64"));
}
#[test]
fn level_factory_clamps() {
assert!(factory::encoder_by_name_with_level("deflate64", 0).is_some());
assert!(factory::encoder_by_name_with_level("deflate64", 99).is_some());
}
#[test]
fn boxed_round_trip() {
let mut enc = factory::encoder_by_name("deflate64").unwrap();
let mut dec = factory::decoder_by_name("deflate64").unwrap();
let input = b"factory boxed round-trip for deflate64";
let mut encoded = Vec::new();
let mut buf = vec![0u8; 256];
let mut consumed = 0;
while consumed < input.len() {
let (p, status) = enc.encode(&input[consumed..], &mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
if matches!(status, Status::InputEmpty) {
break;
}
}
loop {
let (p, status) = enc.finish(&mut buf).unwrap();
encoded.extend_from_slice(&buf[..p.written]);
if matches!(status, Status::StreamEnd) {
break;
}
if p.written == 0 {
panic!("finish stalled");
}
}
let mut decoded = Vec::new();
let mut consumed = 0;
loop {
let (p, status) = dec.decode(&encoded[consumed..], &mut buf).unwrap();
decoded.extend_from_slice(&buf[..p.written]);
consumed += p.consumed;
if matches!(status, Status::StreamEnd | Status::InputEmpty) {
break;
}
}
let (_, status) = dec.finish(&mut buf).unwrap();
assert!(matches!(status, Status::StreamEnd));
assert_eq!(&decoded[..], input);
}
}