diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 4ae47be8..e91d516d 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9332,6 +9332,7 @@ struct wuffs_png__decoder__struct { bool f_seen_fctl; bool f_seen_gama; bool f_seen_iccp; + bool f_seen_idat; bool f_seen_plte; bool f_seen_srgb; bool f_seen_trns; @@ -35313,6 +35314,8 @@ const char wuffs_png__error__internal_error_zlib_decoder_did_not_exhaust_its_inp // ---------------- Private Consts +#define WUFFS_PNG__ANCILLARY_BIT 32 + static const uint8_t WUFFS_PNG__INTERLACING[8][6] WUFFS_BASE__POTENTIALLY_UNUSED = { { @@ -37252,11 +37255,11 @@ wuffs_png__decoder__decode_image_config( status = wuffs_base__make_status(wuffs_png__error__bad_header); goto exit; } - wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0)); self->private_impl.f_chunk_type_array[0] = 73; self->private_impl.f_chunk_type_array[1] = 72; self->private_impl.f_chunk_type_array[2] = 68; self->private_impl.f_chunk_type_array[3] = 82; + wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0)); wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4)); while (true) { v_mark = ((uint64_t)(iop_a_src - io0_a_src)); @@ -37326,19 +37329,24 @@ wuffs_png__decoder__decode_image_config( self->private_impl.f_chunk_length = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src))); self->private_impl.f_chunk_type = ((uint32_t)((wuffs_base__peek_u64le__no_bounds_check(iop_a_src) >> 32))); if (self->private_impl.f_chunk_type == 1413563465) { - if (self->private_impl.f_seen_actl && ! self->private_impl.f_seen_fctl) { - status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); - goto exit; + if ( ! self->private_impl.f_seen_actl || self->private_impl.f_seen_fctl) { + goto label__1__break; } - goto label__1__break; + self->private_impl.f_seen_idat = true; + } else if (self->private_impl.f_chunk_type == 1413571686) { + if (self->private_impl.f_seen_idat && self->private_impl.f_seen_fctl) { + goto label__1__break; + } + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; } iop_a_src += 8; - if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1163152464)) { + if ( ! self->private_impl.f_ignore_checksum && ((self->private_impl.f_chunk_type & 32) == 0)) { + self->private_impl.f_chunk_type_array[0] = ((uint8_t)(((self->private_impl.f_chunk_type >> 0) & 255))); + self->private_impl.f_chunk_type_array[1] = ((uint8_t)(((self->private_impl.f_chunk_type >> 8) & 255))); + self->private_impl.f_chunk_type_array[2] = ((uint8_t)(((self->private_impl.f_chunk_type >> 16) & 255))); + self->private_impl.f_chunk_type_array[3] = ((uint8_t)(((self->private_impl.f_chunk_type >> 24) & 255))); wuffs_base__ignore_status(wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_crc32, sizeof (wuffs_crc32__ieee_hasher), WUFFS_VERSION, 0)); - self->private_impl.f_chunk_type_array[0] = 80; - self->private_impl.f_chunk_type_array[1] = 76; - self->private_impl.f_chunk_type_array[2] = 84; - self->private_impl.f_chunk_type_array[3] = 69; wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4)); } while (true) { @@ -37353,7 +37361,7 @@ wuffs_png__decoder__decode_image_config( iop_a_src = a_src->data.ptr + a_src->meta.ri; } } - if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1163152464)) { + if ( ! self->private_impl.f_ignore_checksum && ((self->private_impl.f_chunk_type & 32) == 0)) { v_checksum_have = wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__io__since(v_mark, ((uint64_t)(iop_a_src - io0_a_src)), io0_a_src)); } if (wuffs_base__status__is_ok(&v_status)) { @@ -37397,7 +37405,7 @@ wuffs_png__decoder__decode_image_config( } v_checksum_want = t_5; } - if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1163152464) && (v_checksum_have != v_checksum_want)) { + if ( ! self->private_impl.f_ignore_checksum && ((self->private_impl.f_chunk_type & 32) == 0) && (v_checksum_have != v_checksum_want)) { status = wuffs_base__make_status(wuffs_png__error__bad_checksum); goto exit; } @@ -37839,6 +37847,11 @@ wuffs_png__decoder__decode_other_chunk( goto suspend; } self->private_impl.f_seen_plte = true; + } else if ((self->private_impl.f_chunk_type & 32) == 0) { + if (self->private_impl.f_chunk_type != 1413563465) { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; + } } else if (self->private_impl.f_chunk_type == 1280598881) { if (self->private_impl.f_seen_actl) { status = wuffs_base__make_status(wuffs_png__error__bad_chunk); @@ -37892,9 +37905,6 @@ wuffs_png__decoder__decode_other_chunk( goto suspend; } self->private_impl.f_seen_fctl = true; - } else if (self->private_impl.f_chunk_type == 1413571686) { - status = wuffs_base__make_status(wuffs_png__error__bad_chunk); - goto exit; } else if (self->private_impl.f_chunk_type == 1095582055) { if (self->private_impl.f_report_metadata_gama) { if (self->private_impl.f_seen_gama) { diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 91f0ecf6..3900cb83 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -33,6 +33,10 @@ pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 // wuffs_base__io_buffer passed to the decoder. pub const DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL : base.u64 = 8 +// ANCILLARY_BIT is the upper/lower case bit on the chunk type's first byte (in +// little-endian order). +pri const ANCILLARY_BIT : base.u32 = 0x0000_0020 + pub struct decoder? implements base.image_decoder( // The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but // it means that (width * height * bytes_per_pixel) doesn't overflow. @@ -109,6 +113,7 @@ pub struct decoder? implements base.image_decoder( seen_fctl : base.bool, seen_gama : base.bool, seen_iccp : base.bool, + seen_idat : base.bool, seen_plte : base.bool, seen_srgb : base.bool, seen_trns : base.bool, @@ -217,11 +222,11 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ if magic <> '\x00\x00\x00\x0DIHDR'le { return "#bad header" } - this.crc32.reset!() this.chunk_type_array[0] = 'I' this.chunk_type_array[1] = 'H' this.chunk_type_array[2] = 'D' this.chunk_type_array[3] = 'R' + this.crc32.reset!() this.crc32.update_u32!(x: this.chunk_type_array[..]) while true { @@ -243,7 +248,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ } } - // Read up until an IDAT chunk. + // Read up until an IDAT or fdAT chunk. // // By default, libpng "warns and discards" when seeing ancillary chunk // checksum failures (as opposed to critical chunk checksum failures) but @@ -268,28 +273,31 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ this.chunk_length = args.src.peek_u32be_as_u64() this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32 if this.chunk_type == 'IDAT'le { - // TODO: support APNG images whose first frame is not the - // IDAT-fallback frame. - if this.seen_actl and (not this.seen_fctl) { - return "#unsupported PNG file" + if (not this.seen_actl) or this.seen_fctl { + break } - break + this.seen_idat = true + } else if this.chunk_type == 'fdAT'le { + if this.seen_idat and this.seen_fctl { + break + } + return "#bad chunk" } args.src.skip_u32_fast!(actual: 8, worst_case: 8) - if (not this.ignore_checksum) and (this.chunk_type == 'PLTE'le) { + if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) { + this.chunk_type_array[0] = ((this.chunk_type >> 0) & 0xFF) as base.u8 + this.chunk_type_array[1] = ((this.chunk_type >> 8) & 0xFF) as base.u8 + this.chunk_type_array[2] = ((this.chunk_type >> 16) & 0xFF) as base.u8 + this.chunk_type_array[3] = ((this.chunk_type >> 24) & 0xFF) as base.u8 this.crc32.reset!() - this.chunk_type_array[0] = 'P' - this.chunk_type_array[1] = 'L' - this.chunk_type_array[2] = 'T' - this.chunk_type_array[3] = 'E' this.crc32.update_u32!(x: this.chunk_type_array[..]) } while true { mark = args.src.mark() status =? this.decode_other_chunk?(src: args.src) - if (not this.ignore_checksum) and (this.chunk_type == 'PLTE'le) { + if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) { checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark)) } if status.is_ok() { @@ -306,7 +314,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ } checksum_want = args.src.read_u32be?() - if (not this.ignore_checksum) and (this.chunk_type == 'PLTE'le) and + if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) and (checksum_have <> checksum_want) { return "#bad checksum" } @@ -536,6 +544,11 @@ pri func decoder.decode_other_chunk?(src: base.io_reader) { this.decode_plte?(src: args.src) this.seen_plte = true + } else if (this.chunk_type & ANCILLARY_BIT) == 0 { + if this.chunk_type <> 'IDAT'le { + return "#bad chunk" + } + } else if this.chunk_type == 'acTL'le { if this.seen_actl { return "#bad chunk" @@ -559,9 +572,6 @@ pri func decoder.decode_other_chunk?(src: base.io_reader) { this.decode_fctl?(src: args.src) this.seen_fctl = true - } else if this.chunk_type == 'fdAT'le { - return "#bad chunk" - } else if this.chunk_type == 'gAMA'le { if this.report_metadata_gama { if this.seen_gama { diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index fe395ba3..200f7476 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -20,6 +20,7 @@ fdcc644f test/data/artificial/gif-metadata-full.gif f50d1d50 test/data/artificial/gif-small-frame-interlaced.gif beaec397 test/data/artificial/gif-transparent-index.gif 6ec818c3 test/data/artificial/gif-zero-width-frame.gif +0564b364 test/data/artificial/png-apng-skip-idat.png 076cb375 test/data/bricks-color.bmp 076cb375 test/data/bricks-color.png f36c2e80 test/data/bricks-dither.bmp