From 9af2666aecd59b19d982f4e7a2f2a6aa5568583f Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 9 Nov 2021 22:30:24 +1100 Subject: [PATCH] Move std/png first_frame_io_position earlier --- release/c/wuffs-unsupported-snapshot.c | 197 +++++++++++++++---------- std/png/decode_png.wuffs | 36 ++++- test/c/std/png.c | 63 +++++++- 3 files changed, 213 insertions(+), 83 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 357c8d80..01c187bb 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9388,6 +9388,9 @@ struct wuffs_png__decoder__struct { uint32_t v_n; uint64_t scratch; } s_decode_trns[1]; + struct { + uint64_t scratch; + } s_decode_frame[1]; struct { uint32_t v_checksum_have; uint64_t scratch; @@ -37234,65 +37237,21 @@ wuffs_png__decoder__decode_image_config( } } while (true) { - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); - uint64_t t_4; - if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_4 = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src))); - iop_a_src += 4; - } else { - self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); - while (true) { - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; - } - uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_4 = ((uint32_t)(*scratch & 0xFF)); - *scratch >>= 8; - *scratch <<= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_4); - if (num_bits_4 == 24) { - t_4 = ((uint64_t)(*scratch >> 32)); - break; - } - num_bits_4 += 8; - *scratch |= ((uint64_t)(num_bits_4)); - } + while (((uint64_t)(io2_a_src - iop_a_src)) < 8) { + if (a_src && a_src->meta.closed) { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; } - self->private_impl.f_chunk_length = t_4; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8); } - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); - uint32_t t_5; - if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_5 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); - iop_a_src += 4; - } else { - self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); - while (true) { - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; - } - uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_5 = ((uint32_t)(*scratch >> 56)); - *scratch <<= 8; - *scratch >>= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_5; - if (num_bits_5 == 24) { - t_5 = ((uint32_t)(*scratch)); - break; - } - num_bits_5 += 8; - *scratch |= ((uint64_t)(num_bits_5)) << 56; - } - } - self->private_impl.f_chunk_type = t_5; + 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) { + goto label__1__break; } - if ( ! self->private_impl.f_ignore_checksum && ((self->private_impl.f_chunk_type == 1413563465) || (self->private_impl.f_chunk_type == 1163152464))) { + iop_a_src += 8; + if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1163152464)) { 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] = ((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))); @@ -37300,17 +37259,14 @@ wuffs_png__decoder__decode_image_config( self->private_impl.f_chunk_type_array[3] = ((uint8_t)(((self->private_impl.f_chunk_type >> 24) & 255))); wuffs_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4)); } - if (self->private_impl.f_chunk_type == 1413563465) { - goto label__1__break; - } while (true) { v_mark = ((uint64_t)(iop_a_src - io0_a_src)); { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } - wuffs_base__status t_6 = wuffs_png__decoder__decode_other_chunk(self, a_src); - v_status = t_6; + wuffs_base__status t_4 = wuffs_png__decoder__decode_other_chunk(self, a_src); + v_status = t_4; if (a_src) { iop_a_src = a_src->data.ptr + a_src->meta.ri; } @@ -37322,7 +37278,7 @@ wuffs_png__decoder__decode_image_config( goto label__2__break; } status = v_status; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(12); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9); } label__2__break:; if (self->private_impl.f_metadata_fourcc != 0) { @@ -37331,33 +37287,33 @@ wuffs_png__decoder__decode_image_config( goto ok; } { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13); - uint32_t t_7; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); + uint32_t t_5; if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { - t_7 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src); + t_5 = wuffs_base__peek_u32be__no_bounds_check(iop_a_src); iop_a_src += 4; } else { self->private_data.s_decode_image_config[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); while (true) { if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { status = wuffs_base__make_status(wuffs_base__suspension__short_read); goto suspend; } uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; - uint32_t num_bits_7 = ((uint32_t)(*scratch & 0xFF)); + uint32_t num_bits_5 = ((uint32_t)(*scratch & 0xFF)); *scratch >>= 8; *scratch <<= 8; - *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_7); - if (num_bits_7 == 24) { - t_7 = ((uint32_t)(*scratch >> 32)); + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_5); + if (num_bits_5 == 24) { + t_5 = ((uint32_t)(*scratch >> 32)); break; } - num_bits_7 += 8; - *scratch |= ((uint64_t)(num_bits_7)); + num_bits_5 += 8; + *scratch |= ((uint64_t)(num_bits_5)); } } - v_checksum_want = t_7; + 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)) { status = wuffs_base__make_status(wuffs_png__error__bad_checksum); @@ -38873,13 +38829,30 @@ wuffs_png__decoder__decode_frame( uint32_t v_pass_width = 0; uint32_t v_pass_height = 0; + const uint8_t* iop_a_src = NULL; + const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + if (a_src) { + io0_a_src = a_src->data.ptr; + io1_a_src = io0_a_src + a_src->meta.ri; + iop_a_src = io1_a_src; + io2_a_src = io0_a_src + a_src->meta.wi; + } + uint32_t coro_susp_point = self->private_impl.p_decode_frame[0]; switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; if (self->private_impl.f_call_sequence < 4) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_png__decoder__decode_frame_config(self, NULL, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } if (status.repr) { goto suspend; } @@ -38888,6 +38861,72 @@ wuffs_png__decoder__decode_frame( status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + uint64_t t_0; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_0 = ((uint64_t)(wuffs_base__peek_u32be__no_bounds_check(iop_a_src))); + iop_a_src += 4; + } else { + self->private_data.s_decode_frame[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_frame[0].scratch; + uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0); + if (num_bits_0 == 24) { + t_0 = ((uint64_t)(*scratch >> 32)); + break; + } + num_bits_0 += 8; + *scratch |= ((uint64_t)(num_bits_0)); + } + } + self->private_impl.f_chunk_length = t_0; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + uint32_t t_1; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_frame[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_frame[0].scratch; + uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1; + if (num_bits_1 == 24) { + t_1 = ((uint32_t)(*scratch)); + break; + } + num_bits_1 += 8; + *scratch |= ((uint64_t)(num_bits_1)) << 56; + } + } + self->private_impl.f_chunk_type = t_1; + } + if ( ! self->private_impl.f_ignore_checksum && (self->private_impl.f_chunk_type == 1413563465)) { + 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] = ((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_crc32__ieee_hasher__update_u32(&self->private_data.f_crc32, wuffs_base__make_slice_u8(self->private_impl.f_chunk_type_array, 4)); + } if (self->private_impl.f_zlib_is_dirty) { wuffs_base__ignore_status(wuffs_zlib__decoder__initialize(&self->private_data.f_zlib, sizeof (wuffs_zlib__decoder), WUFFS_VERSION, 0)); if (self->private_impl.f_ignore_checksum) { @@ -38918,8 +38957,14 @@ wuffs_png__decoder__decode_frame( if ((v_pass_width > 0) && (v_pass_height > 0)) { self->private_impl.f_pass_bytes_per_row = wuffs_png__decoder__calculate_bytes_per_row(self, v_pass_width); self->private_impl.f_pass_workbuf_length = (((uint64_t)(v_pass_height)) * (1 + self->private_impl.f_pass_bytes_per_row)); - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); status = wuffs_png__decoder__decode_pass(self, a_src, a_workbuf); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } if (status.repr) { goto suspend; } @@ -38963,6 +39008,10 @@ wuffs_png__decoder__decode_frame( goto exit; exit: + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + if (wuffs_base__status__is_error(&status)) { self->private_impl.magic = WUFFS_BASE__DISABLED; } diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 048a8927..d8801e23 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -200,10 +200,23 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ // IDAT chunk breaks the loop. The only other possible critical chunk is a // PLTE chunk. We verify PLTE checksums here but ignore other checksums. while true { - this.chunk_length = args.src.read_u32be_as_u64?() - this.chunk_type = args.src.read_u32le?() - if (not this.ignore_checksum) and - ((this.chunk_type == 'IDAT'le) or (this.chunk_type == 'PLTE'le)) { + while args.src.length() < 8, + post args.src.length() >= 8, + { + if args.src.is_closed() { + return "#bad chunk" + } + yield? base."$short read" + } endwhile + + 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 { + break + } + args.src.skip_u32_fast!(actual: 8, worst_case: 8) + + if (not this.ignore_checksum) and (this.chunk_type == 'PLTE'le) { this.crc32.reset!() 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 @@ -212,10 +225,6 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ this.crc32.update_u32!(x: this.chunk_type_array[..]) } - if this.chunk_type == 'IDAT'le { - break - } - while true { mark = args.src.mark() status =? this.decode_other_chunk?(src: args.src) @@ -750,6 +759,17 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, return base."@end of data" } + this.chunk_length = args.src.read_u32be_as_u64?() + this.chunk_type = args.src.read_u32le?() + if (not this.ignore_checksum) and (this.chunk_type == 'IDAT'le) { + this.crc32.reset!() + 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.update_u32!(x: this.chunk_type_array[..]) + } + if this.zlib_is_dirty { this.zlib.reset!() if this.ignore_checksum { diff --git a/test/c/std/png.c b/test/c/std/png.c index ce45f49f..0df36986 100644 --- a/test/c/std/png.c +++ b/test/c/std/png.c @@ -625,8 +625,9 @@ test_wuffs_png_decode_metadata_iccp() { } { + // 423 = 0x1A7 is just before the "????IDAT" bytes. uint64_t have = wuffs_base__image_config__first_frame_io_position(&ic); - uint64_t want = 431; + uint64_t want = 423; if (have != want) { RETURN_FAIL("first_frame_io_position: have %" PRIu64 ", want %" PRIu64, have, want); @@ -651,6 +652,65 @@ test_wuffs_png_decode_metadata_iccp() { return NULL; } +const char* // +test_wuffs_png_decode_restart_frame() { + CHECK_FOCUS(__func__); + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = g_src_slice_u8, + }); + CHECK_STRING(read_file(&src, "test/data/pjw-thumbnail.png")); + + wuffs_png__decoder dec; + CHECK_STATUS("initialize", + wuffs_png__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + + wuffs_base__image_config ic = ((wuffs_base__image_config){}); + CHECK_STATUS("decode_image_config", + wuffs_png__decoder__decode_image_config(&dec, &ic, &src)); + // 51 = 0x33 is just before the "????IDAT" bytes. + uint64_t ffio = wuffs_base__image_config__first_frame_io_position(&ic); + if (ffio != 51) { + RETURN_FAIL("first_frame_io_position: have %" PRIu64 ", want 51", ffio); + } + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); + CHECK_STATUS("set_from_slice", wuffs_base__pixel_buffer__set_from_slice( + &pb, &ic.pixcfg, g_pixel_slice_u8)); + + for (int i = 0; i < 2; i++) { + if (i > 0) { + CHECK_STATUS("restart_frame", + wuffs_png__decoder__restart_frame(&dec, 0, 51)); + if (51 <= src.meta.wi) { + src.meta.ri = 51; + } + } + + uint64_t rpos = wuffs_base__io_buffer__reader_position(&src); + if (rpos != 51) { + RETURN_FAIL("reader_position (before) #%d: have %" PRIu64 ", want 51", i, + rpos); + } + + wuffs_base__status status = wuffs_png__decoder__decode_frame( + &dec, &pb, &src, WUFFS_BASE__PIXEL_BLEND__SRC, g_work_slice_u8, NULL); + if (!wuffs_base__status__is_ok(&status)) { + RETURN_FAIL("decode_frame #%d: %s", i, status.repr); + } + + // 196 = 0xC4 is just before the "????IEND" bytes. + rpos = wuffs_base__io_buffer__reader_position(&src); + if (rpos != 196) { + RETURN_FAIL("reader_position (after) #%d: have %" PRIu64 ", want 196", i, + rpos); + } + } + + return NULL; +} + // ---------------- Mimic Tests #ifdef WUFFS_MIMIC @@ -982,6 +1042,7 @@ proc g_tests[] = { test_wuffs_png_decode_interface, test_wuffs_png_decode_metadata_chrm_gama_srgb, test_wuffs_png_decode_metadata_iccp, + test_wuffs_png_decode_restart_frame, #ifdef WUFFS_MIMIC