fn main() {
let inputs: Vec<&[u8]> = vec![
b"ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD",
b"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.",
b"aaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccdddd",
];
for input in inputs {
println!(
"\n=== Testing: \"{}...\" ({} bytes) ===",
String::from_utf8_lossy(&input[..30.min(input.len())]),
input.len()
);
let ref_compressed = zstd::encode_all(input, 1).unwrap();
let fhd = ref_compressed[4];
let single_segment = (fhd & 0x20) != 0;
let fcs_size = match fhd >> 6 {
0 => {
if single_segment {
1
} else {
0
}
}
1 => 2,
2 => 4,
3 => 8,
_ => 0,
};
let pos = if single_segment {
5 + fcs_size
} else {
5 + 1 + fcs_size };
if pos + 3 > ref_compressed.len() {
println!(" Frame too short");
continue;
}
let bh = u32::from_le_bytes([
ref_compressed[pos],
ref_compressed[pos + 1],
ref_compressed[pos + 2],
0,
]);
let block_type = (bh >> 1) & 0x3;
let block_size = (bh >> 3) as usize;
let block_type_name = match block_type {
0 => "Raw",
1 => "RLE",
2 => "Compressed",
_ => "Reserved",
};
println!(
" Reference uses {} block ({} bytes)",
block_type_name, block_size
);
if block_type == 2 {
println!(" FOUND Compressed block from reference!");
println!(" Reference hex: {:02x?}", &ref_compressed[..]);
let compressor = haagenti_zstd::compress::SpeculativeCompressor::new();
let our_compressed = compressor.compress(input).unwrap();
println!(" Ours hex: {:02x?}", &our_compressed[..]);
println!("\n Cross-decode tests:");
match zstd::decode_all(&our_compressed[..]) {
Ok(d) if d == input => println!(" Ref decodes ours: ✓"),
Ok(_) => println!(" Ref decodes ours: WRONG DATA"),
Err(e) => println!(" Ref decodes ours: ✗ {}", e),
}
match haagenti_zstd::decompress::decompress_frame(&ref_compressed) {
Ok(d) if d == input => println!(" We decode ref: ✓"),
Ok(_) => println!(" We decode ref: WRONG DATA"),
Err(e) => println!(" We decode ref: ✗ {:?}", e),
}
match haagenti_zstd::decompress::decompress_frame(&our_compressed) {
Ok(d) if d == input => println!(" We decode ours: ✓"),
Ok(_) => println!(" We decode ours: WRONG DATA"),
Err(e) => println!(" We decode ours: ✗ {:?}", e),
}
}
}
}