#[path = "support/synth.rs"]
mod synth;
use rust_j2k::{DecodeOptions, Error, decode_with};
const MESSAGE: &str = "a packet header runs past the end of its `PPM` or `PPT` packed headers";
fn read(name: &str) -> Vec<u8> {
synth::read_file(synth::fixtures("packed_overrun").join(name))
}
fn options(strict: bool, reduction: u8) -> DecodeOptions {
DecodeOptions::default()
.with_strict(strict)
.with_resolution_reduction(reduction)
}
fn assert_refused(bytes: &[u8], what: &str) {
for strict in [true, false] {
match decode_with(bytes, &options(strict, 0)) {
Err(Error::Codestream(message)) => assert_eq!(message.message(), MESSAGE, "{what}"),
other => panic!("{what}, strict {strict}: expected Codestream, got {other:?}"),
}
}
}
#[test]
fn a_ppt_header_one_bit_short_is_refused() {
assert_refused(&read("ppt_short.j2k"), "ppt_short");
}
#[test]
fn a_ppt_header_ending_in_ff_without_its_stuffed_byte_is_refused() {
assert_refused(&read("ppt_ff_nostuff.j2k"), "ppt_ff_nostuff");
}
#[test]
fn a_ppm_header_one_bit_short_is_refused() {
assert_refused(&read("ppm_short.j2k"), "ppm_short");
}
#[test]
fn the_same_header_whole_decodes_as_openjpeg() {
for name in ["ppt_whole.j2k", "ppm_whole.j2k"] {
let bytes = read(name);
let strict =
decode_with(&bytes, &options(true, 0)).unwrap_or_else(|e| panic!("{name}: {e}"));
let partial =
decode_with(&bytes, &options(false, 0)).unwrap_or_else(|e| panic!("{name}: {e}"));
assert_eq!(strict.components[0].samples, [176], "{name}");
assert_eq!(strict, partial, "{name}");
assert!(partial.report.is_complete(), "{name}");
}
}
#[test]
fn a_packed_header_that_faults_past_its_end_is_refused() {
let mut bytes = read("ppt_short.j2k");
let ppt = bytes.windows(2).position(|w| w == [0xFF, 0x61]).unwrap();
bytes[ppt + 5] = 0xFE;
assert_refused(&bytes, "0xFE");
}