diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index f157315c..89c73f19 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9601,6 +9601,7 @@ extern const char wuffs_jpeg__error__bad_sof_marker[]; extern const char wuffs_jpeg__error__bad_sos_marker[]; extern const char wuffs_jpeg__error__bad_header[]; extern const char wuffs_jpeg__error__bad_marker[]; +extern const char wuffs_jpeg__error__bad_scan_count[]; extern const char wuffs_jpeg__error__missing_huffman_table[]; extern const char wuffs_jpeg__error__missing_quantization_table[]; extern const char wuffs_jpeg__error__truncated_input[]; @@ -9845,6 +9846,7 @@ struct wuffs_jpeg__decoder__struct { uint32_t f_bitstream_ri; uint32_t f_bitstream_wi; bool f_bitstream_is_closed; + bool f_expect_multiple_scans; uint32_t f_bitstream_padding; uint16_t f_quant_tables[4][64]; uint16_t f_saved_quant_tables[4][64]; @@ -43660,6 +43662,7 @@ const char wuffs_jpeg__error__bad_sof_marker[] = "#jpeg: bad SOF marker"; const char wuffs_jpeg__error__bad_sos_marker[] = "#jpeg: bad SOS marker"; const char wuffs_jpeg__error__bad_header[] = "#jpeg: bad header"; const char wuffs_jpeg__error__bad_marker[] = "#jpeg: bad marker"; +const char wuffs_jpeg__error__bad_scan_count[] = "#jpeg: bad scan count"; const char wuffs_jpeg__error__missing_huffman_table[] = "#jpeg: missing Huffman table"; const char wuffs_jpeg__error__missing_quantization_table[] = "#jpeg: missing Quantization table"; const char wuffs_jpeg__error__truncated_input[] = "#jpeg: truncated input"; @@ -47714,6 +47717,9 @@ wuffs_jpeg__decoder__decode_sos( if (self->private_impl.f_scan_count >= 64u) { status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_scan_count); goto exit; + } else if ((self->private_impl.f_scan_count > 0u) && ! self->private_impl.f_expect_multiple_scans) { + status = wuffs_base__make_status(wuffs_jpeg__error__bad_scan_count); + goto exit; } WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_jpeg__decoder__prepare_scan(self, a_src); @@ -47937,6 +47943,9 @@ wuffs_jpeg__decoder__prepare_scan( } v_i += 1u; } + if (self->private_impl.f_scan_count == 0u) { + self->private_impl.f_expect_multiple_scans = ((self->private_impl.f_sof_marker >= 194u) || (self->private_impl.f_scan_num_components < self->private_impl.f_num_components)); + } if (self->private_impl.f_sof_marker < 194u) { self->private_data.s_prepare_scan.scratch = 3u; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); @@ -48776,6 +48785,7 @@ wuffs_jpeg__decoder__restart_frame( } self->private_impl.f_call_sequence = 40u; self->private_impl.f_bitstream_is_closed = false; + self->private_impl.f_expect_multiple_scans = false; self->private_impl.f_frame_config_io_position = a_io_position; self->private_impl.f_scan_count = 0u; self->private_impl.f_restart_interval = self->private_impl.f_saved_restart_interval; diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index 338f0ca0..f0deae2b 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -15,6 +15,7 @@ pub status "#bad SOF marker" pub status "#bad SOS marker" pub status "#bad header" pub status "#bad marker" +pub status "#bad scan count" pub status "#missing Huffman table" pub status "#missing Quantization table" pub status "#truncated input" @@ -278,6 +279,8 @@ pub struct decoder? implements base.image_decoder( bitstream_is_closed : base.bool, + expect_multiple_scans : base.bool, + // bitstream_padding is the number (per restart) of implicit zeroes // remaining at the end of the bitstream. Its initial 12345 value is // arbitrary. @@ -1256,6 +1259,8 @@ pri func decoder.decode_sos?(src: base.io_reader, workbuf: slice base.u8) { if this.scan_count >= 64 { return "#unsupported scan count" + } else if (this.scan_count > 0) and (not this.expect_multiple_scans) { + return "#bad scan count" } this.prepare_scan?(src: args.src) @@ -1431,6 +1436,12 @@ pri func decoder.prepare_scan?(src: base.io_reader) { i += 1 } + if this.scan_count == 0 { + this.expect_multiple_scans = + (this.sof_marker >= 0xC2) or + (this.scan_num_components < this.num_components) + } + if this.sof_marker < 0xC2 { // At this point, for sequential (not progressive) JPEGs, we should // check that (Ss, Se, Ah, Al) is (0, 63, 0, 0) but libjpeg treats @@ -2060,6 +2071,7 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta this.call_sequence = 0x28 this.bitstream_is_closed = false + this.expect_multiple_scans = false this.frame_config_io_position = args.io_position this.scan_count = 0 this.restart_interval = this.saved_restart_interval