Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions src/codecs/ico/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::color::ColorType;
use crate::error::{
DecodingError, ImageError, ImageResult, UnsupportedError, UnsupportedErrorKind,
};
use crate::io::image_reader_type::SpecCompliance;
use crate::io::{
DecodedAnimationAttributes, DecodedImageAttributes, DecoderPreparedImage, FormatAttributes,
};
Expand Down Expand Up @@ -113,6 +114,7 @@ pub struct IcoDecoder<R: BufRead + Seek> {
selected_entry: DirEntry,
reader_offset: u64,
inner_decoder: InnerDecoder<R>,
spec_strictness: SpecCompliance,
}

enum InnerDecoder<R: BufRead + Seek> {
Expand Down Expand Up @@ -147,7 +149,15 @@ struct DirEntry {

impl<R: BufRead + Seek> IcoDecoder<R> {
/// Create a new decoder that decodes from the stream ```r```
pub fn new(mut r: R) -> ImageResult<IcoDecoder<R>> {
pub fn new(r: R) -> ImageResult<IcoDecoder<R>> {
Self::with_spec_compliance(r, SpecCompliance::default())
}

/// Create a new decoder with the given spec compliance mode.
pub(crate) fn with_spec_compliance(
mut r: R,
spec: SpecCompliance,
) -> ImageResult<IcoDecoder<R>> {
let reader_offset = r.stream_position()?;
let entries = read_entries(&mut r)?;
let entry = best_entry(entries)?;
Expand All @@ -157,6 +167,7 @@ impl<R: BufRead + Seek> IcoDecoder<R> {
selected_entry: entry,
reader_offset,
inner_decoder: decoder,
spec_strictness: spec,
})
}
}
Expand Down Expand Up @@ -261,12 +272,7 @@ impl DirEntry {
self.seek_to_start(&mut r, reader_offset)?;

if is_png {
let limits = crate::Limits {
max_image_width: Some(self.real_width().into()),
max_image_height: Some(self.real_height().into()),
max_alloc: Some(256 * 256 * 4 * 2), // width * height * 4 bytes per pixel * safety factor of 2
};
Ok(Png(Box::new(PngDecoder::with_limits(r, limits))))
Ok(Png(Box::new(PngDecoder::new(r))))
} else {
Ok(Bmp(BmpDecoder::new_with_ico_format(r)?))
}
Expand Down Expand Up @@ -307,7 +313,9 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
let layout = decoder.prepare_image()?;
// Check if the image dimensions match the ones in the image data.
let (width, height) = layout.layout.dimensions();
if !self.selected_entry.matches_dimensions(width, height) {
if self.spec_strictness == SpecCompliance::Strict
&& !self.selected_entry.matches_dimensions(width, height)
{
return Err(DecoderError::ImageEntryDimensionMismatch {
format: IcoEntryImageFormat::Png,
entry: (
Expand All @@ -330,7 +338,9 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
Bmp(decoder) => {
let layout = decoder.prepare_image()?;
let (width, height) = layout.layout.dimensions();
if !self.selected_entry.matches_dimensions(width, height) {
if self.spec_strictness == SpecCompliance::Strict
&& !self.selected_entry.matches_dimensions(width, height)
{
return Err(DecoderError::ImageEntryDimensionMismatch {
format: IcoEntryImageFormat::Bmp,
entry: (
Expand Down Expand Up @@ -531,4 +541,32 @@ mod test {
let mut buf = vec![0; usize::try_from(bytes).unwrap()];
assert!(decoder.read_image(&mut buf).is_err());
}

#[test]
fn dimension_mismatch_strict_vs_lenient() {
// Minimal 2x2 32-bit BMP inside an ICO where the directory entry says 3x3.
let data = vec![
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x03, 0x03, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00,
0x40, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x02, 0x00,
0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];

let mut decoder =
IcoDecoder::with_spec_compliance(std::io::Cursor::new(&data), SpecCompliance::Lenient)
.unwrap();
let bytes = decoder.prepare_image().unwrap().total_bytes();
let mut buf = vec![0; usize::try_from(bytes).unwrap()];
assert!(decoder.read_image(&mut buf).is_ok());

let mut decoder =
IcoDecoder::with_spec_compliance(std::io::Cursor::new(&data), SpecCompliance::Strict)
.unwrap();
let bytes = decoder.prepare_image().unwrap().total_bytes();
let mut buf = vec![0; usize::try_from(bytes).unwrap()];
assert!(decoder.read_image(&mut buf).is_err());
}
}
5 changes: 4 additions & 1 deletion src/io/image_reader_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ impl<'a, R: 'a + BufRead + Seek> ImageReaderOptions<R> {
spec_compliance,
)?),
#[cfg(feature = "ico")]
ImageFormat::Ico => Box::new(ico::IcoDecoder::new(reader)?),
ImageFormat::Ico => Box::new(ico::IcoDecoder::with_spec_compliance(
reader,
spec_compliance,
)?),
#[cfg(feature = "hdr")]
ImageFormat::Hdr => Box::new(hdr::HdrDecoder::with_spec_compliance(
reader,
Expand Down
Loading