Skip to content

Commit aebe13b

Browse files
authored
Merge pull request #3001 from mpospelova/ico-lenient-mode
Add lenient mode to ICO decoder
2 parents 58b1ff5 + e246e85 commit aebe13b

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎src/codecs/ico/decoder.rs‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::color::ColorType;
66
use crate::error::{
77
DecodingError, ImageError, ImageResult, UnsupportedError, UnsupportedErrorKind,
88
};
9+
use crate::io::image_reader_type::SpecCompliance;
910
use crate::io::{
1011
DecodedAnimationAttributes, DecodedImageAttributes, DecoderPreparedImage, FormatAttributes,
1112
};
@@ -113,6 +114,7 @@ pub struct IcoDecoder<R: BufRead + Seek> {
113114
selected_entry: DirEntry,
114115
reader_offset: u64,
115116
inner_decoder: InnerDecoder<R>,
117+
spec_strictness: SpecCompliance,
116118
}
117119

118120
enum InnerDecoder<R: BufRead + Seek> {
@@ -147,7 +149,15 @@ struct DirEntry {
147149

148150
impl<R: BufRead + Seek> IcoDecoder<R> {
149151
/// Create a new decoder that decodes from the stream ```r```
150-
pub fn new(mut r: R) -> ImageResult<IcoDecoder<R>> {
152+
pub fn new(r: R) -> ImageResult<IcoDecoder<R>> {
153+
Self::with_spec_compliance(r, SpecCompliance::default())
154+
}
155+
156+
/// Create a new decoder with the given spec compliance mode.
157+
pub(crate) fn with_spec_compliance(
158+
mut r: R,
159+
spec: SpecCompliance,
160+
) -> ImageResult<IcoDecoder<R>> {
151161
let reader_offset = r.stream_position()?;
152162
let entries = read_entries(&mut r)?;
153163
let entry = best_entry(entries)?;
@@ -157,6 +167,7 @@ impl<R: BufRead + Seek> IcoDecoder<R> {
157167
selected_entry: entry,
158168
reader_offset,
159169
inner_decoder: decoder,
170+
spec_strictness: spec,
160171
})
161172
}
162173
}
@@ -261,12 +272,7 @@ impl DirEntry {
261272
self.seek_to_start(&mut r, reader_offset)?;
262273

263274
if is_png {
264-
let limits = crate::Limits {
265-
max_image_width: Some(self.real_width().into()),
266-
max_image_height: Some(self.real_height().into()),
267-
max_alloc: Some(256 * 256 * 4 * 2), // width * height * 4 bytes per pixel * safety factor of 2
268-
};
269-
Ok(Png(Box::new(PngDecoder::with_limits(r, limits))))
275+
Ok(Png(Box::new(PngDecoder::new(r))))
270276
} else {
271277
Ok(Bmp(BmpDecoder::new_with_ico_format(r)?))
272278
}
@@ -307,7 +313,9 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
307313
let layout = decoder.prepare_image()?;
308314
// Check if the image dimensions match the ones in the image data.
309315
let (width, height) = layout.layout.dimensions();
310-
if !self.selected_entry.matches_dimensions(width, height) {
316+
if self.spec_strictness == SpecCompliance::Strict
317+
&& !self.selected_entry.matches_dimensions(width, height)
318+
{
311319
return Err(DecoderError::ImageEntryDimensionMismatch {
312320
format: IcoEntryImageFormat::Png,
313321
entry: (
@@ -330,7 +338,9 @@ impl<R: BufRead + Seek> ImageDecoder for IcoDecoder<R> {
330338
Bmp(decoder) => {
331339
let layout = decoder.prepare_image()?;
332340
let (width, height) = layout.layout.dimensions();
333-
if !self.selected_entry.matches_dimensions(width, height) {
341+
if self.spec_strictness == SpecCompliance::Strict
342+
&& !self.selected_entry.matches_dimensions(width, height)
343+
{
334344
return Err(DecoderError::ImageEntryDimensionMismatch {
335345
format: IcoEntryImageFormat::Bmp,
336346
entry: (
@@ -537,6 +547,34 @@ mod test {
537547
assert!(decoder.read_image(&mut buf).is_err());
538548
}
539549

550+
#[test]
551+
fn dimension_mismatch_strict_vs_lenient() {
552+
// Minimal 2x2 32-bit BMP inside an ICO where the directory entry says 3x3.
553+
let data = vec![
554+
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x03, 0x03, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00,
555+
0x40, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x02, 0x00,
556+
0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
557+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
558+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
559+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
560+
0x00, 0x00,
561+
];
562+
563+
let mut decoder =
564+
IcoDecoder::with_spec_compliance(std::io::Cursor::new(&data), SpecCompliance::Lenient)
565+
.unwrap();
566+
let bytes = decoder.prepare_image().unwrap().total_bytes();
567+
let mut buf = vec![0; usize::try_from(bytes).unwrap()];
568+
assert!(decoder.read_image(&mut buf).is_ok());
569+
570+
let mut decoder =
571+
IcoDecoder::with_spec_compliance(std::io::Cursor::new(&data), SpecCompliance::Strict)
572+
.unwrap();
573+
let bytes = decoder.prepare_image().unwrap().total_bytes();
574+
let mut buf = vec![0; usize::try_from(bytes).unwrap()];
575+
assert!(decoder.read_image(&mut buf).is_err());
576+
}
577+
540578
// Verify that the AND mask is ignored for 32bpp BMP images in ICO files.
541579
#[test]
542580
fn bmp_32bpp_and_mask_ignored() {

‎src/io/image_reader_type.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ impl<'a, R: 'a + BufRead + Seek> ImageReaderOptions<R> {
220220
spec_compliance,
221221
)?),
222222
#[cfg(feature = "ico")]
223-
ImageFormat::Ico => Box::new(ico::IcoDecoder::new(reader)?),
223+
ImageFormat::Ico => Box::new(ico::IcoDecoder::with_spec_compliance(
224+
reader,
225+
spec_compliance,
226+
)?),
224227
#[cfg(feature = "hdr")]
225228
ImageFormat::Hdr => Box::new(hdr::HdrDecoder::with_spec_compliance(
226229
reader,

0 commit comments

Comments
 (0)