From bd6d2ea3a1ac9f2a05c1fa3bbece920f301d372b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 16 Jul 2017 19:25:49 +1000 Subject: [PATCH] Handle decoding a GIF in many short reads. --- cmd/puffs-gen-c/func.go | 4 +- gen/c/std/gif.c | 4 +- test/c/std/gif.c | 95 ++++++++++++++++++++++++++++++----------- 3 files changed, 74 insertions(+), 29 deletions(-) diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index 1299ccbb..ff9dfa51 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -962,8 +962,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { if err := g.writeLoadExprDerivedVars(n); err != nil { return err } - // TODO: be principled with "if (status < 0)" vs "if (status)". - g.writes("if (status < 0) { return status; }\n") + // TODO: be principled with "if (l_lzw_src && etc)". + g.writes("if (l_lzw_src && status) { goto suspend; }\n") } else { // TODO: fix this. diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 19f97f76..9116ff87 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1027,8 +1027,8 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, } b_rend_src = b_rptr_src + len; } - if (status < 0) { - return status; + if (l_lzw_src && status) { + goto suspend; } } label_0_break:; diff --git a/test/c/std/gif.c b/test/c/std/gif.c index a609fdc5..061b32a6 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -273,19 +273,19 @@ cleanup1: cleanup0:; } -void test_puffs_gif_lzw_decode_many_small_inputs() { - proc_funcname = __func__; - test_puffs_gif_lzw_decode("../../testdata/bricks-gray.indexes.giflzw", 14731, - "../../testdata/bricks-gray.indexes", 19200, 100); -} - -void test_puffs_gif_lzw_decode_one_large_input() { +void test_puffs_gif_lzw_decode_few_big_reads() { proc_funcname = __func__; test_puffs_gif_lzw_decode("../../testdata/bricks-gray.indexes.giflzw", 14731, "../../testdata/bricks-gray.indexes", 19200, 1 << 30); } +void test_puffs_gif_lzw_decode_many_small_reads() { + proc_funcname = __func__; + test_puffs_gif_lzw_decode("../../testdata/bricks-gray.indexes.giflzw", 14731, + "../../testdata/bricks-gray.indexes", 19200, 100); +} + void test_puffs_gif_lzw_decode_bricks_dither() { proc_funcname = __func__; test_puffs_gif_lzw_decode("../../testdata/bricks-dither.indexes.giflzw", @@ -375,8 +375,9 @@ const char* puffs_gif_decode(puffs_base_buf1* dst, puffs_base_buf1* src) { void test_puffs_gif_decode(const char* filename, const char* palette_filename, const char* indexes_filename, - puffs_gif_status want) { - puffs_base_buf1 dst = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; + puffs_gif_status want, + uint64_t limit) { + puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; if (!read_file(&src, filename)) { @@ -385,18 +386,53 @@ void test_puffs_gif_decode(const char* filename, puffs_gif_decoder dec; puffs_gif_decoder_constructor(&dec, PUFFS_VERSION, 0); - puffs_base_writer1 dst_writer = {.buf = &dst}; + puffs_base_writer1 got_writer = {.buf = &got}; puffs_base_reader1 src_reader = {.buf = &src}; - puffs_gif_status got = puffs_gif_decoder_decode(&dec, dst_writer, src_reader); - if (got != want) { - FAIL("status: got %d, want %d", got, want); - goto cleanup1; + int num_iters = 0; + while (true) { + num_iters++; + uint64_t lim = limit; + src_reader.limit.ptr_to_len = &lim; + size_t old_ri = src.ri; + puffs_gif_status got = + puffs_gif_decoder_decode(&dec, got_writer, src_reader); + if ((src.ri == src.wi) || puffs_gif_status_is_error(got)) { + if (got != want) { + FAIL("status: got %d, want %d", got, want); + goto cleanup1; + } + break; + } + if (got != PUFFS_GIF_SUSPENSION_SHORT_READ) { + FAIL("status: got %d, want %d", got, PUFFS_GIF_SUSPENSION_SHORT_READ); + goto cleanup1; + } + if (src.ri < old_ri) { + FAIL("read index src.ri went backwards"); + goto cleanup1; + } + if (src.ri == old_ri) { + FAIL("no progress was made"); + goto cleanup1; + } } if (want != PUFFS_GIF_STATUS_OK) { goto cleanup1; } + if (limit < 1000000) { + if (num_iters <= 1) { + FAIL("num_iters: got %d, want > 1", num_iters); + goto cleanup1; + } + } else { + if (num_iters != 1) { + FAIL("num_iters: got %d, want 1", num_iters); + goto cleanup1; + } + } + // TODO: provide a public API for getting the width and height. if (dec.private_impl.f_width != 160) { FAIL("width: got %d, want %d", dec.private_impl.f_width, 160); @@ -422,7 +458,7 @@ void test_puffs_gif_decode(const char* filename, if (!read_file(&ind_want, indexes_filename)) { goto cleanup1; } - if (!buf1s_equal("indexes ", &dst, &ind_want)) { + if (!buf1s_equal("indexes ", &got, &ind_want)) { goto cleanup1; } @@ -431,18 +467,26 @@ cleanup1: cleanup0:; } -void test_puffs_gif_decode_input_is_a_gif() { +void test_puffs_gif_decode_input_is_a_gif_few_big_reads() { proc_funcname = __func__; test_puffs_gif_decode("../../testdata/bricks-dither.gif", "../../testdata/bricks-dither.palette", "../../testdata/bricks-dither.indexes", - PUFFS_GIF_STATUS_OK); + PUFFS_GIF_STATUS_OK, 1 << 30); +} + +void test_puffs_gif_decode_input_is_a_gif_many_small_reads() { + proc_funcname = __func__; + test_puffs_gif_decode("../../testdata/bricks-dither.gif", + "../../testdata/bricks-dither.palette", + "../../testdata/bricks-dither.indexes", + PUFFS_GIF_STATUS_OK, 1000); } void test_puffs_gif_decode_input_is_a_png() { proc_funcname = __func__; test_puffs_gif_decode("../../testdata/bricks-dither.png", "", "", - PUFFS_GIF_ERROR_BAD_GIF_HEADER); + PUFFS_GIF_ERROR_BAD_GIF_HEADER, 1 << 30); } // ---------------- Mimic Tests @@ -609,15 +653,16 @@ proc tests[] = { test_basic_sub_struct_constructor, // // LZW Tests - test_puffs_gif_lzw_decode_many_small_inputs, // - test_puffs_gif_lzw_decode_one_large_input, // - test_puffs_gif_lzw_decode_bricks_dither, // - test_puffs_gif_lzw_decode_bricks_nodither, // - test_puffs_gif_lzw_decode_pi, // + test_puffs_gif_lzw_decode_few_big_reads, // + test_puffs_gif_lzw_decode_many_small_reads, // + test_puffs_gif_lzw_decode_bricks_dither, // + test_puffs_gif_lzw_decode_bricks_nodither, // + test_puffs_gif_lzw_decode_pi, // // GIF Tests - test_puffs_gif_decode_input_is_a_gif, // - test_puffs_gif_decode_input_is_a_png, // + test_puffs_gif_decode_input_is_a_gif_few_big_reads, // + test_puffs_gif_decode_input_is_a_gif_many_small_reads, // + test_puffs_gif_decode_input_is_a_png, // #ifdef PUFFS_MIMIC