diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index c42dbce8..6c4d6dea 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -798,9 +798,24 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible") case a.KIf: - // TODO: consider suspendible calls. - n := n.If() - for { + // TODO: for writeSuspendibles, make sure that we get order of + // sub-expression evaluation correct. + n, nCloseCurly := n.If(), 1 + for first := true; ; first = false { + if n.Condition().Suspendible() { + if !first { + g.writeb('{') + const maxCloseCurly = 1000 + if nCloseCurly == maxCloseCurly { + return fmt.Errorf("too many nested if's") + } + nCloseCurly++ + } + if err := g.writeSuspendibles(n.Condition(), depth); err != nil { + return err + } + } + g.writes("if (") if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil { return err @@ -826,7 +841,9 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { } g.writes("} else ") } - g.writes("}\n") + for ; nCloseCurly > 0; nCloseCurly-- { + g.writes("}\n") + } return nil case a.KJump: diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 812c68bc..0018e617 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -106,7 +106,6 @@ typedef struct { puffs_gif_status status; uint32_t magic; puffs_gif_lzw_decoder f_lzw; - uint32_t f_temporary_hack; } private_impl; } puffs_gif_decoder; @@ -286,7 +285,19 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, goto cleanup0; } - if (self->private_impl.f_temporary_hack != 71) { + if (a_src->ri >= a_src->wi) { + status = a_src->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + goto cleanup0; + } + uint8_t t_0 = a_src->ptr[a_src->ri++]; + if (a_src->ri >= a_src->wi) { + status = a_src->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + goto cleanup0; + } + uint8_t t_1 = a_src->ptr[a_src->ri++]; + if ((t_0 != 71) || (t_1 != 73)) { status = puffs_gif_error_bad_gif_header; goto cleanup0; } diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 2aeb9528..b1bec60d 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -106,7 +106,6 @@ typedef struct { puffs_gif_status status; uint32_t magic; puffs_gif_lzw_decoder f_lzw; - uint32_t f_temporary_hack; } private_impl; } puffs_gif_decoder; diff --git a/std/gif/decode_gif.puffs b/std/gif/decode_gif.puffs index 83624bbd..6629dbcf 100644 --- a/std/gif/decode_gif.puffs +++ b/std/gif/decode_gif.puffs @@ -5,11 +5,10 @@ pub error "bad GIF header" pub struct decoder?( lzw lzw_decoder, - temporary_hack u32, ) pub func decoder.decode?(dst ptr buf1, src ptr buf1)() { - if this.temporary_hack != 0x47 { + if (in.src.read_u8?() != 0x47) or (in.src.read_u8?() != 0x49) { return error "bad GIF header" } } diff --git a/test/c/std/gif.c b/test/c/std/gif.c index b4018ec5..750240af 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -244,6 +244,42 @@ cleanup1: cleanup0:; } +// ---------------- GIF Tests + +void test_gif_decode_input_is_a_xxx(const char* filename, + puffs_gif_status want) { + puffs_base_buf1 dst = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; + puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; + + if (!read_file(&src, filename)) { + goto cleanup0; + } + + puffs_gif_decoder dec; + puffs_gif_decoder_constructor(&dec, PUFFS_VERSION, 0); + puffs_gif_status got = puffs_gif_decoder_decode(&dec, &dst, &src); + if (got != want) { + FAIL("status: got %d, want %d", got, want); + goto cleanup1; + } + +cleanup1: + puffs_gif_decoder_destructor(&dec); +cleanup0:; +} + +void test_gif_decode_input_is_a_gif() { + test_funcname = __func__; + test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.gif", + puffs_gif_status_ok); +} + +void test_gif_decode_input_is_a_png() { + test_funcname = __func__; + test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.png", + puffs_gif_error_bad_gif_header); +} + // ---------------- Manifest // The empty comments forces clang-format to place one element per line. @@ -260,6 +296,9 @@ test tests[] = { test_sub_struct_constructor, // // LZW Tests test_lzw_decode, // + // GIF Tests + test_gif_decode_input_is_a_gif, // + test_gif_decode_input_is_a_png, // // End NULL, };