From fc1002cb89df52c0f9e08421bf38d5862444e62d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 17 Nov 2018 21:52:26 +1100 Subject: [PATCH] Implement gif.decoder.workbuf_len Also relax decode_frame's workbuf length check from not-equals to less-than. --- example/gifplayer/gifplayer.c | 11 +++-- fuzz/c/std/gif_fuzzer.c | 2 +- release/c/wuffs-unsupported-snapshot.h | 7 +-- std/gif/decode_gif.wuffs | 7 ++- test/c/std/gif.c | 68 +++++++------------------- 5 files changed, 34 insertions(+), 61 deletions(-) diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index 904a4877..a100a44e 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -223,7 +223,8 @@ size_t print_color_art(wuffs_base__pixel_buffer* pb) { // ---- -const char* try_allocate(wuffs_base__image_config* ic) { +const char* try_allocate(wuffs_gif__decoder* dec, + wuffs_base__image_config* ic) { uint32_t width = wuffs_base__pixel_config__width(&ic->pixcfg); uint32_t height = wuffs_base__pixel_config__height(&ic->pixcfg); uint64_t num_pixels = ((uint64_t)width) * ((uint64_t)height); @@ -249,7 +250,7 @@ const char* try_allocate(wuffs_base__image_config* ic) { } workbuf = wuffs_base__malloc_slice_u8( - malloc, wuffs_base__image_config__workbuf_len(ic).max_incl); + malloc, wuffs_gif__decoder__workbuf_len(dec).max_incl); if (!workbuf.ptr) { return "could not allocate work buffer"; } @@ -267,8 +268,8 @@ const char* try_allocate(wuffs_base__image_config* ic) { return NULL; } -const char* allocate(wuffs_base__image_config* ic) { - const char* z = try_allocate(ic); +const char* allocate(wuffs_gif__decoder* dec, wuffs_base__image_config* ic) { + const char* z = try_allocate(dec, ic); if (z) { free(printbuf.ptr); printbuf = ((wuffs_base__slice_u8){}); @@ -321,7 +322,7 @@ const char* play() { if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) { return "image dimensions are too large"; } - const char* msg = allocate(&ic); + const char* msg = allocate(&dec, &ic); if (msg) { return msg; } diff --git a/fuzz/c/std/gif_fuzzer.c b/fuzz/c/std/gif_fuzzer.c index f87c7b25..7e4e2113 100644 --- a/fuzz/c/std/gif_fuzzer.c +++ b/fuzz/c/std/gif_fuzzer.c @@ -73,7 +73,7 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) { goto exit; } - uint64_t n = wuffs_base__image_config__workbuf_len(&ic).max_incl; + uint64_t n = wuffs_gif__decoder__workbuf_len(&dec).max_incl; if (n > 64 * 1024 * 1024) { // Don't allocate more than 64 MiB. ret = "image too large"; goto exit; diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 6d1a2b04..ce59de62 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -7511,8 +7511,9 @@ wuffs_gif__decoder__workbuf_len(const wuffs_gif__decoder* self) { return ((wuffs_base__range_ii_u64){}); } - return wuffs_base__utility__make_range_ii_u64(&self->private_impl.f_util, 0, - 0); + return wuffs_base__utility__make_range_ii_u64( + &self->private_impl.f_util, ((uint64_t)(self->private_impl.f_width)), + ((uint64_t)(self->private_impl.f_width))); } // -------- func gif.decoder.restart_frame @@ -7776,7 +7777,7 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (((uint64_t)(a_workbuf.len)) != + if (((uint64_t)(a_workbuf.len)) < ((uint64_t)(self->private_impl.f_width))) { status = wuffs_base__error__bad_workbuf_length; goto exit; diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 7cac24ab..8e667830 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -146,6 +146,7 @@ pub func decoder.decode_image_config!??(dst nptr base.image_config, src base.io_ pixsub:0, width:this.width, height:this.height, + // TODO: workbuf_len should be an aspect of the decoder, not the image_config. workbuf_len0:this.width as base.u64, workbuf_len1:this.width as base.u64, first_frame_io_position:this.frame_config_io_position, @@ -179,7 +180,9 @@ pub func decoder.frame_dirty_rect() base.rect_ie_u32 { } pub func decoder.workbuf_len() base.range_ii_u64 { - return this.util.make_range_ii_u64(min_incl:0, max_incl:0) + return this.util.make_range_ii_u64( + min_incl:this.width as base.u64, + max_incl:this.width as base.u64) } pub func decoder.restart_frame!??(index base.u64, io_position base.u64) { @@ -251,7 +254,7 @@ pri func decoder.skip_frame!??(src base.io_reader) { // TODO: honor args.opts. pub func decoder.decode_frame!??(dst ptr base.pixel_buffer, src base.io_reader, workbuf slice base.u8, opts nptr base.decode_frame_options) { - if args.workbuf.length() != (this.width as base.u64) { + if args.workbuf.length() < (this.width as base.u64) { return status "?bad workbuf length" } if this.call_sequence != 2 { diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 3809676b..db4be72d 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -211,15 +211,6 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, return z; } - uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl; - if (workbuf_len > BUFFER_SIZE) { - return "work buffer size is too large"; - } - wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){ - .ptr = global_work_array, - .len = workbuf_len, - }); - while (true) { z = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); if (z == wuffs_base__warning__end_of_data) { @@ -228,7 +219,8 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, return z; } - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); if (z) { return z; } @@ -290,10 +282,6 @@ const char* do_test_wuffs_gif_decode(const char* filename, RETURN_FAIL("height: got %" PRIu32 ", want 120", wuffs_base__pixel_config__height(&ic.pixcfg)); } - if (wuffs_base__image_config__workbuf_len(&ic).max_incl != 160) { - RETURN_FAIL("workbuf_len: got %" PRIu64 ", want 160", - wuffs_base__image_config__workbuf_len(&ic).max_incl); - } if (!wuffs_base__image_config__first_frame_is_opaque(&ic)) { RETURN_FAIL("first_frame_is_opaque: got false, want true"); } @@ -309,10 +297,10 @@ const char* do_test_wuffs_gif_decode(const char* filename, } } - wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){ - .ptr = global_work_array, - .len = 160, - }); + if (wuffs_gif__decoder__workbuf_len(&dec).min_incl != 160) { + RETURN_FAIL("workbuf_len: got %" PRIu64 ", want 160", + wuffs_gif__decoder__workbuf_len(&dec).min_incl); + } int num_iters = 0; while (true) { @@ -349,8 +337,8 @@ const char* do_test_wuffs_gif_decode(const char* filename, } size_t old_ri = src.meta.ri; - const char* z = - wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); + const char* z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); if (!z) { break; @@ -421,7 +409,8 @@ const char* do_test_wuffs_gif_decode(const char* filename, RETURN_FAIL("decode_frame returned \"ok\" but src was exhausted"); } wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); if (z != wuffs_base__warning__end_of_data) { RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", z, wuffs_base__warning__end_of_data); @@ -496,15 +485,6 @@ const char* do_test_wuffs_gif_decode_animated( RETURN_FAIL("decode_image_config: got \"%s\"", z); } - uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl; - if (workbuf_len > BUFFER_SIZE) { - return "work buffer size is too large"; - } - wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){ - .ptr = global_work_array, - .len = workbuf_len, - }); - uint32_t got_num_loops = wuffs_gif__decoder__num_animation_loops(&dec); if (got_num_loops != want_num_loops) { RETURN_FAIL("num_loops: got %" PRIu32 ", want %" PRIu32, got_num_loops, @@ -539,7 +519,8 @@ const char* do_test_wuffs_gif_decode_animated( } } - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); if (z) { RETURN_FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); } @@ -548,7 +529,8 @@ const char* do_test_wuffs_gif_decode_animated( // There should be no more frames, no matter how many times we call // decode_frame. for (i = 0; i < 3; i++) { - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, NULL); + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); if (z != wuffs_base__warning__end_of_data) { RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", z, wuffs_base__warning__end_of_data); @@ -641,15 +623,6 @@ const char* test_wuffs_gif_decode_frame_out_of_bounds() { RETURN_FAIL("set_from_slice: \"%s\"", z); } - uint64_t workbuf_len = wuffs_base__image_config__workbuf_len(&ic).max_incl; - if (workbuf_len > BUFFER_SIZE) { - RETURN_FAIL("work buffer size is too large"); - } - wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){ - .ptr = global_work_array, - .len = workbuf_len, - }); - // See test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt // for the want_frame_config_bounds and want_pixel_indexes source. @@ -703,8 +676,8 @@ const char* test_wuffs_gif_decode_frame_out_of_bounds() { } } - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, - NULL); + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); if (z) { RETURN_FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); } @@ -884,11 +857,6 @@ const char* do_test_wuffs_gif_num_decoded(bool frame_config) { } } - wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){ - .ptr = global_work_array, - .len = 64, - }); - const char* method = frame_config ? "decode_frame_config" : "decode_frame"; bool end_of_data = false; uint64_t want = 0; @@ -909,8 +877,8 @@ const char* do_test_wuffs_gif_num_decoded(bool frame_config) { if (frame_config) { z = wuffs_gif__decoder__decode_frame_config(&dec, NULL, src_reader); } else { - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, workbuf, - NULL); + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + global_work_slice, NULL); } if (!z) {