From 8134c89786010a19a875e1478fdfa980a0265db5 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 22 Jun 2017 11:25:07 +1000 Subject: [PATCH] Decode the rest of a GIF. Passing an Image Descriptor's block data payload to the LZW decoder is still a TODO. --- cmd/puffs-gen-c/main.go | 10 +++ gen/c/std/gif.c | 171 +++++++++++++++++++++++++++++++++++++-- gen/h/std/gif.h | 9 ++- lang/check/bounds.go | 3 +- lang/check/type.go | 3 +- std/gif/decode_gif.puffs | 103 +++++++++++++++++++++++ 6 files changed, 289 insertions(+), 10 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 65a0001d..8eb6fb5b 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -1033,6 +1033,16 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { g.pkgName, g.perFunc.funk.Receiver().String(g.tm), aPrefix) g.writes("if (status) { goto cleanup0; }\n") + } else if isThisMethod(g.tm, n, "decode_extension") { + g.printf("status = puffs_%s_%s_decode_extension(self, %ssrc);\n", + g.pkgName, g.perFunc.funk.Receiver().String(g.tm), aPrefix) + g.writes("if (status) { goto cleanup0; }\n") + + } else if isThisMethod(g.tm, n, "decode_id") { + g.printf("status = puffs_%s_%s_decode_id(self, %ssrc);\n", + g.pkgName, g.perFunc.funk.Receiver().String(g.tm), aPrefix) + g.writes("if (status) { goto cleanup0; }\n") + } else { // TODO: fix this. // diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 250c6584..f87b232e 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -80,9 +80,12 @@ typedef enum { puffs_gif_status_short_read = -12, puffs_gif_status_short_write = -14, puffs_gif_error_closed_for_writes = -16 + 1, - puffs_gif_error_bad_gif_header = -256 + 1, - puffs_gif_error_lzw_code_is_out_of_range = -258 + 1, - puffs_gif_error_lzw_prefix_chain_is_cyclical = -260 + 1, + puffs_gif_error_bad_gif_block = -256 + 1, + puffs_gif_error_bad_gif_extension_label = -258 + 1, + puffs_gif_error_bad_gif_header = -260 + 1, + puffs_gif_error_todo_unsupported_local_color_table = -262 + 1, + puffs_gif_error_lzw_code_is_out_of_range = -264 + 1, + puffs_gif_error_lzw_prefix_chain_is_cyclical = -266 + 1, } puffs_gif_status; bool puffs_gif_status_is_error(puffs_gif_status s); @@ -186,7 +189,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s) { return s & 1; } -const char* puffs_gif_status_strings[12] = { +const char* puffs_gif_status_strings[15] = { "gif: ok", "gif: bad version", "gif: bad receiver", @@ -196,7 +199,10 @@ const char* puffs_gif_status_strings[12] = { "gif: short read", "gif: short write", "gif: closed for writes", + "gif: bad GIF block", + "gif: bad GIF extension label", "gif: bad GIF header", + "gif: TODO: unsupported Local Color Table", "gif: LZW code is out of range", "gif: LZW prefix chain is cyclical", }; @@ -208,7 +214,7 @@ const char* puffs_gif_status_string(puffs_gif_status s) { return puffs_gif_status_strings[s]; } s -= 119; - if ((9 <= s) && (s < 12)) { + if ((9 <= s) && (s < 15)) { return puffs_gif_status_strings[s]; } } @@ -225,6 +231,12 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, puffs_base_reader1 a_src); +puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self, + puffs_base_reader1 a_src); + +puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, + puffs_base_reader1 a_src); + // ---------------- Constructor and Destructor Implementations // PUFFS_MAGIC is a magic number to check that constructors are called. It's @@ -306,6 +318,8 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, goto cleanup0; } + uint8_t v_c; + status = puffs_gif_decoder_decode_header(self, a_src); if (status) { goto cleanup0; @@ -314,6 +328,32 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, if (status) { goto cleanup0; } + while (true) { + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + goto cleanup0; + } + uint8_t t_0 = a_src.buf->ptr[a_src.buf->ri++]; + v_c = t_0; + if (v_c == 33) { + status = puffs_gif_decoder_decode_extension(self, a_src); + if (status) { + goto cleanup0; + } + } else if (v_c == 44) { + status = puffs_gif_decoder_decode_id(self, a_src); + if (status) { + goto cleanup0; + } + } else if (v_c == 59) { + status = puffs_gif_status_ok; + goto cleanup0; + } else { + status = puffs_gif_error_bad_gif_block; + goto cleanup0; + } + } cleanup0: self->private_impl.status = status; @@ -408,6 +448,127 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, return status; } +puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self, + puffs_base_reader1 a_src) { + puffs_gif_status status = self->private_impl.status; + + uint8_t v_label; + uint32_t v_block_size; + uint32_t v_j; + uint8_t v_temp; + + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_0 = a_src.buf->ptr[a_src.buf->ri++]; + v_label = t_0; + if (v_label == 1) { + } else if (v_label == 249) { + } else if (v_label == 254) { + } else if (v_label == 255) { + } else { + return puffs_gif_error_bad_gif_extension_label; + } + while (true) { + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_1 = a_src.buf->ptr[a_src.buf->ri++]; + v_block_size = ((uint32_t)(t_1)); + if (v_block_size == 0) { + goto label_0_break; + } + v_j = 0; + while (v_j < v_block_size) { + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_2 = a_src.buf->ptr[a_src.buf->ri++]; + v_temp = t_2; + v_temp *= 0; + v_j += 1; + } + } +label_0_break:; + + return status; +} + +puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, + puffs_base_reader1 a_src) { + puffs_gif_status status = self->private_impl.status; + + uint8_t v_c[9]; + uint32_t v_i; + bool v_interlace; + uint8_t v_literal_width; + uint32_t v_block_size; + uint32_t v_j; + uint8_t v_temp; + + for (size_t i = 0; i < 9; i++) { + v_c[i] = 0; + }; + v_i = 0; + while (v_i < 9) { + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_0 = a_src.buf->ptr[a_src.buf->ri++]; + v_c[v_i] = t_0; + v_i += 1; + } + v_interlace = ((v_c[8] & 64) != 0); + if (v_interlace) { + } + if ((v_c[8] & 128) != 0) { + return puffs_gif_error_todo_unsupported_local_color_table; + } + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_1 = a_src.buf->ptr[a_src.buf->ri++]; + v_literal_width = t_1; + v_literal_width *= 0; + while (true) { + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_2 = a_src.buf->ptr[a_src.buf->ri++]; + v_block_size = ((uint32_t)(t_2)); + if (v_block_size == 0) { + goto label_0_break; + } + v_j = 0; + while (v_j < v_block_size) { + if (a_src.buf->ri >= a_src.buf->wi) { + status = a_src.buf->closed ? puffs_gif_error_unexpected_eof + : puffs_gif_status_short_read; + return status; + } + uint8_t t_3 = a_src.buf->ptr[a_src.buf->ri++]; + v_temp = t_3; + v_temp *= 0; + v_j += 1; + } + } +label_0_break:; + + return status; +} + void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, uint32_t a_lw) { if (!self) { diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 0eb2a3bb..61eab19c 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -80,9 +80,12 @@ typedef enum { puffs_gif_status_short_read = -12, puffs_gif_status_short_write = -14, puffs_gif_error_closed_for_writes = -16 + 1, - puffs_gif_error_bad_gif_header = -256 + 1, - puffs_gif_error_lzw_code_is_out_of_range = -258 + 1, - puffs_gif_error_lzw_prefix_chain_is_cyclical = -260 + 1, + puffs_gif_error_bad_gif_block = -256 + 1, + puffs_gif_error_bad_gif_extension_label = -258 + 1, + puffs_gif_error_bad_gif_header = -260 + 1, + puffs_gif_error_todo_unsupported_local_color_table = -262 + 1, + puffs_gif_error_lzw_code_is_out_of_range = -264 + 1, + puffs_gif_error_lzw_prefix_chain_is_cyclical = -266 + 1, } puffs_gif_status; bool puffs_gif_status_is_error(puffs_gif_status s); diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 75900e29..003301b5 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -600,7 +600,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, case t.KeyOpenParen: // TODO: delete this hack that only matches "in.src.read_u8?()" etc. if isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) || - isThisMethod(q.tm, n, "decode_header") || isThisMethod(q.tm, n, "decode_lsd") { + isThisMethod(q.tm, n, "decode_header") || isThisMethod(q.tm, n, "decode_lsd") || + isThisMethod(q.tm, n, "decode_extension") || isThisMethod(q.tm, n, "decode_id") { for _, o := range n.Args() { // TODO: check that the arg range at the caller and the diff --git a/lang/check/type.go b/lang/check/type.go index 2efcf1b0..53954993 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -328,7 +328,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { // n is a function call. // TODO: delete this hack that only matches "in.src.read_u8?()" etc. if isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) || - isThisMethod(q.tm, n, "decode_header") || isThisMethod(q.tm, n, "decode_lsd") { + isThisMethod(q.tm, n, "decode_header") || isThisMethod(q.tm, n, "decode_lsd") || + isThisMethod(q.tm, n, "decode_extension") || isThisMethod(q.tm, n, "decode_id") { if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil { return err diff --git a/std/gif/decode_gif.puffs b/std/gif/decode_gif.puffs index 468fa06c..cf7ace98 100644 --- a/std/gif/decode_gif.puffs +++ b/std/gif/decode_gif.puffs @@ -3,8 +3,12 @@ // The GIF spec is https://www.w3.org/Graphics/GIF/spec-gif89a.txt +pub error "bad GIF block" +pub error "bad GIF extension label" pub error "bad GIF header" +pub error "TODO: unsupported Local Color Table" + pub struct decoder?( width u32[..65535], height u32[..65535], @@ -19,6 +23,21 @@ pub struct decoder?( pub func decoder.decode?(dst writer1, src reader1)() { this.decode_header?(src:in.src) this.decode_lsd?(src:in.src) + while true { + var c u8 = in.src.read_u8?() + if c == 0x21 { // The spec calls 0x21 the "Extension Introducer". + this.decode_extension?(src:in.src) + } else if c == 0x2C { // The spec calls 0x2C the "Image Separator". + // TODO: animated GIFs can have multiple Image Descriptors. + // + // TODO: reset this.lzw if it's not the first ID. + this.decode_id?(src:in.src) + } else if c == 0x3B { // The spec calls 0x3B the "Trailer". + return + } else { + return error "bad GIF block" + } + } } // decode_header reads either "GIF87a" or "GIF89a". @@ -64,3 +83,87 @@ pri func decoder.decode_lsd?(src reader1)() { } } } + +// decode_extension reads an extension. The Extension Introducer byte has +// already been read. +// +// See the spec: +// - section 23 "Graphic Control Extension" on page 15. +// - section 24 "Comment Extension" on page 17. +// - section 25 "Plain Text Extension" on page 18. +// - section 26 "Application Extension" on page 21. +pri func decoder.decode_extension?(src reader1)() { + var label u8 = in.src.read_u8?() + if label == 0x01 { // The spec calls 0x01 the "Plain Text Label". + } else if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label". + } else if label == 0xFE { // The spec calls 0xFE the "Comment Label". + } else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label". + } else { + return error "bad GIF extension label" + } + + // Skip the data blocks. + while true { + var block_size u32[..255] = in.src.read_u8?() as u32 + if block_size == 0 { + break + } + var j u32 + while j < block_size { + assert j < 255 via "a < b: a < c; c <= b"(c:block_size) + // TODO: we shouldn't need a temp variable. + // + // TODO: have an in.src.skip?(n:block_size) method? + var temp u8 = in.src.read_u8?() + temp *= 0 // Avoid "unused variable" C compiler warning. + j += 1 + } + } +} + +// decode_id reads an Image Descriptor. The Image Separator byte has already +// been read. +// +// See the spec section 20 "Image Descriptor" on page 11. +pri func decoder.decode_id?(src reader1)() { + var c[9] u8 + var i u32 + while i < 9 { + c[i] = in.src.read_u8?() + i += 1 + } + + // TODO: use image left/top/width/height. + + // TODO: use interlace. + var interlace bool = (c[8] & 0x40) != 0 + if interlace { // Avoid "unused variable" C compiler warning. + } + + // TODO: read the optional Local Color Table. + if (c[8] & 0x80) != 0 { + return error "TODO: unsupported Local Color Table" + } + + var literal_width u8 = in.src.read_u8?() + // TODO: call this.lzw.set_literal_width. + literal_width *= 0 // Avoid "unused variable" C compiler warning. + + // TODO: pass the data blocks' payload to this.lzw. + while true { + var block_size u32[..255] = in.src.read_u8?() as u32 + if block_size == 0 { + break + } + var j u32 + while j < block_size { + assert j < 255 via "a < b: a < c; c <= b"(c:block_size) + // TODO: we shouldn't need a temp variable. + // + // TODO: have an in.src.skip?(n:block_size) method? + var temp u8 = in.src.read_u8?() + temp *= 0 // Avoid "unused variable" C compiler warning. + j += 1 + } + } +}