mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 13:17:41 +00:00
image-codecs: reject a JPEG-LS restart marker that follows another segment
While a restart interval is in effect, the decoder now rejects a restart marker anywhere outside a scan's data, including one that comes after a comment or another segment. Before, only a marker directly after the scan was rejected, and one after a comment was skipped and the image decoded. With an interval of 0 a stray restart marker is still skipped. `detect` documents that a buffer of only `FF D8` matches no format, and the tests check the error kind of a malformed DRI segment and a DRI change between the component scans of an RGB file.
This commit is contained in:
@@ -673,7 +673,13 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
if marker == 0xD9 {
|
||||
return finish(frame, planes, scans_done);
|
||||
}
|
||||
if marker == 0x00 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 {
|
||||
let restart = (0xD0..=0xD7).contains(&marker);
|
||||
// A restart marker comes only between two intervals inside a scan, and every scan the
|
||||
// decoder reads fits in one interval, so none may follow it, before or after other segments.
|
||||
if restart && restart_interval != 0 {
|
||||
return Err(bad("a restart marker outside a scan's restart intervals"));
|
||||
}
|
||||
if marker == 0x00 || restart || marker == 0x01 {
|
||||
pos += 2;
|
||||
continue;
|
||||
}
|
||||
@@ -758,11 +764,6 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
samples.try_reserve_exact(f.width * f.height).map_err(|_| bad("pixel buffer is too large"))?;
|
||||
samples.resize(f.width * f.height, 0u16);
|
||||
decode_scan(&b[start..end], f.width, f.height, ¶ms, &mut samples)?;
|
||||
// A restart marker comes only between two intervals, so none follows a scan that
|
||||
// fits in one. Fill bytes (`0xFF`) may come before the marker.
|
||||
if restart_interval != 0 && b[end..].iter().find(|&&v| v != 0xFF).is_some_and(|m| (0xD0..=0xD7).contains(m)) {
|
||||
return Err(bad("a restart marker after the last restart interval"));
|
||||
}
|
||||
planes[idx] = samples;
|
||||
scans_done += 1;
|
||||
pos = end;
|
||||
|
||||
@@ -411,8 +411,9 @@ impl PrefixDetector {
|
||||
/// magic and is recognized last, by header plausibility.
|
||||
///
|
||||
/// JPEG-LS is told from JPEG by the frame header that follows the marker segments, so `bytes`
|
||||
/// must reach that header; a buffer that ends before it is reported as [`Format::Jpeg`]. Use
|
||||
/// [`detect_prefix`] on a buffer that may hold only the start of a file.
|
||||
/// must reach that header; a buffer that ends before it is reported as [`Format::Jpeg`] when it
|
||||
/// starts with `FF D8 FF`, and a buffer of only `FF D8` matches no format. Use [`detect_prefix`] on
|
||||
/// a buffer that may hold only the start of a file: it asks for 4 bytes after `FF D8`.
|
||||
pub fn detect(bytes: &[u8]) -> Option<Format> {
|
||||
let b = bytes;
|
||||
if b.starts_with(b"\x89PNG\r\n\x1a\n") {
|
||||
|
||||
@@ -333,6 +333,7 @@ fn a_cut_off_jpeg_asks_for_more_bytes() {
|
||||
assert_eq!(detect_prefix(&bytes[..5]), Detection::NeedBytes(6));
|
||||
assert_eq!(detect_prefix(&bytes[..3]), Detection::NeedBytes(4));
|
||||
assert_eq!(detect_prefix(&[0xFF, 0xD8]), Detection::NeedBytes(4));
|
||||
assert_eq!(detect(&[0xFF, 0xD8]), None);
|
||||
assert_eq!(detect(&bytes[..50]), Some(Format::Jpeg));
|
||||
assert!(decode_as(Format::JpegLs, &bytes[..50]).is_err());
|
||||
// A byte that is not a marker where one should be ends the walk: no frame header follows.
|
||||
@@ -366,7 +367,12 @@ fn a_restart_interval_before_the_frame_header_is_jpeg_ls() {
|
||||
bytes.extend(std::iter::repeat_n(0, size));
|
||||
bytes.extend_from_slice(&plain[2..]);
|
||||
assert_eq!(detect(&bytes), Some(Format::JpegLs));
|
||||
assert_eq!(decode(&bytes).is_ok(), ok, "{size} interval bytes");
|
||||
let decoded = decode(&bytes);
|
||||
if ok {
|
||||
assert_eq!(decoded.unwrap().data(), img.data(), "{size} interval bytes");
|
||||
} else {
|
||||
assert!(matches!(decoded, Err(CodecError::Malformed { .. })), "{size} interval bytes: {decoded:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,6 +402,28 @@ fn the_restart_interval_in_effect_at_the_scan_decides_decoding() {
|
||||
assert!(matches!(decode(&with_dri(&[0, 2])), Err(CodecError::Unsupported { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_component_scan_uses_the_restart_interval_in_effect_at_it() {
|
||||
// The 5x3 RGB file codes each component in its own scan.
|
||||
let img = Image::from_u8(5, 3, ChannelLayout::Rgb, (0..45).collect()).unwrap();
|
||||
let plain = encode(&img, Format::JpegLs, &EncodeOptions::default()).unwrap();
|
||||
let scans: Vec<usize> = plain.windows(2).enumerate().filter(|(_, w)| *w == [0xFF, 0xDA]).map(|(i, _)| i).collect();
|
||||
assert_eq!(scans.len(), 3);
|
||||
// A DRI segment with `first` before the first scan and one with `second` before the second.
|
||||
let with_dri = |first: u16, second: u16| {
|
||||
let mut bytes = plain[..scans[0]].to_vec();
|
||||
for (interval, part) in [(first, &plain[scans[0]..scans[1]]), (second, &plain[scans[1]..])] {
|
||||
bytes.extend_from_slice(&[0xFF, 0xDD, 0, 4]);
|
||||
bytes.extend_from_slice(&interval.to_be_bytes());
|
||||
bytes.extend_from_slice(part);
|
||||
}
|
||||
bytes
|
||||
};
|
||||
assert_eq!(decode(&with_dri(3, 3)).unwrap().data(), img.data());
|
||||
assert_eq!(decode(&with_dri(3, 0)).unwrap().data(), img.data());
|
||||
assert!(matches!(decode(&with_dri(3, 2)), Err(CodecError::Unsupported { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_restart_marker_after_a_scan_of_one_interval_is_an_error() {
|
||||
// With an interval of 3 lines the 5x3 scan is one interval, which no restart marker follows.
|
||||
@@ -414,6 +442,27 @@ fn a_restart_marker_after_a_scan_of_one_interval_is_an_error() {
|
||||
assert!(decode(&bytes).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_restart_marker_after_a_comment_is_an_error_while_an_interval_is_set() {
|
||||
let (img, plain) = jpeg_ls_file();
|
||||
let eoi = plain.len() - 2;
|
||||
// `after` goes between the scan data and the EOI of the 5x3 file with an interval of `dri`.
|
||||
let with = |dri: u8, after: &[u8]| {
|
||||
let mut bytes = plain[..15].to_vec();
|
||||
bytes.extend_from_slice(&[0xFF, 0xDD, 0, 4, 0, dri]);
|
||||
bytes.extend_from_slice(&plain[15..eoi]);
|
||||
bytes.extend_from_slice(after);
|
||||
bytes.extend_from_slice(&plain[eoi..]);
|
||||
bytes
|
||||
};
|
||||
const COMMENT: [u8; 4] = [0xFF, 0xFE, 0, 2];
|
||||
assert_eq!(decode(&with(3, &COMMENT)).unwrap().data(), img.data());
|
||||
let decoded = decode(&with(3, &[COMMENT.as_slice(), &[0xFF, 0xD0]].concat()));
|
||||
assert!(matches!(decoded, Err(CodecError::Malformed { .. })), "{decoded:?}");
|
||||
// With restarts off, a stray restart marker is skipped as before.
|
||||
assert_eq!(decode(&with(0, &[COMMENT.as_slice(), &[0xFF, 0xD0]].concat())).unwrap().data(), img.data());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stuffed_zero_where_a_marker_should_be_is_jpeg() {
|
||||
// `FF 00` is not a marker, so the walk ends there without asking for a length field.
|
||||
|
||||
Reference in New Issue
Block a user