diff --git a/gen/c/gif.c b/gen/c/gif.c index a2344bd1..60d2318e 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -215,18 +215,20 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, goto cleanup0; } - uint32_t v_bits; - uint32_t v_n_bits; uint32_t v_clear_code; uint32_t v_end_code; + uint32_t v_save_code; uint32_t v_width; + uint32_t v_bits; + uint32_t v_n_bits; uint32_t v_code; - v_bits = 0; - v_n_bits = 0; v_clear_code = (((uint32_t)(1)) << self->f_literal_width); v_end_code = (v_clear_code + 1); + v_save_code = v_end_code; v_width = (self->f_literal_width + 1); + v_bits = 0; + v_n_bits = 0; label_0_continue:; while (1) { while (v_n_bits < v_width) { @@ -244,11 +246,15 @@ label_0_continue:; if (v_code < v_clear_code) { goto label_0_break; } else if (v_code == v_clear_code) { + v_save_code = v_end_code; v_width = (self->f_literal_width + 1); goto label_0_continue; } else if (v_code == v_end_code) { status = puffs_gif_status_ok; goto cleanup0; + } else if (v_code <= v_save_code) { + status = puffs_gif_status_ok; + goto cleanup0; } else { status = puffs_gif_error_bad_gif_image; goto cleanup0; diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 9e65541d..48f56f83 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -13,13 +13,17 @@ pub struct lzw_decoder?( ) pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { - var bits u32 - var n_bits u32 + // These variables don't change over the lifetime of this func. var clear_code u32[4..256] = (1 as u32) << this.literal_width var end_code u32[5..257] = clear_code + 1 + + // These variables do change. + var save_code u32[..4095] = end_code var width u32[..12] = this.literal_width + 1 - // TODO: implement. + // These variables yield src's bits in Least Significant Bits order. + var bits u32 + var n_bits u32 while true, pre n_bits < 8, @@ -40,10 +44,13 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { if code < clear_code { break } else if code == clear_code { + save_code = end_code width = this.literal_width + 1 continue } else if code == end_code { return + } else if code <= save_code { + return } else { return error "bad GIF image" }