From e0478b1d917eec1edaac9f6fbae64b70449be21b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 13 May 2018 15:55:24 +1000 Subject: [PATCH] Guard against the LZW literal_width changing --- gen/c/std/gif.c | 9 +++++++-- gen/h/std/gif.h | 1 + std/gif/decode_lzw.wuffs | 5 +++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 4e29b611..d81e0a9d 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1109,6 +1109,7 @@ typedef struct { uint32_t v_end_code; uint32_t v_save_code; uint32_t v_prev_code; + uint32_t v_initial_width; uint32_t v_width; uint32_t v_bits; uint32_t v_n_bits; @@ -3071,6 +3072,7 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode( uint32_t v_end_code; uint32_t v_save_code; uint32_t v_prev_code; + uint32_t v_initial_width; uint32_t v_width; uint32_t v_bits; uint32_t v_n_bits; @@ -3116,6 +3118,7 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode( v_end_code = self->private_impl.c_decode[0].v_end_code; v_save_code = self->private_impl.c_decode[0].v_save_code; v_prev_code = self->private_impl.c_decode[0].v_prev_code; + v_initial_width = self->private_impl.c_decode[0].v_initial_width; v_width = self->private_impl.c_decode[0].v_width; v_bits = self->private_impl.c_decode[0].v_bits; v_n_bits = self->private_impl.c_decode[0].v_n_bits; @@ -3134,7 +3137,8 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode( v_end_code = (v_clear_code + 1); v_save_code = v_end_code; v_prev_code = 0; - v_width = (self->private_impl.f_literal_width + 1); + v_initial_width = (self->private_impl.f_literal_width + 1); + v_width = v_initial_width; v_bits = 0; v_n_bits = 0; label_0_continue:; @@ -3168,7 +3172,7 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode( } else if (v_code == v_clear_code) { v_save_code = v_end_code; v_prev_code = 0; - v_width = (self->private_impl.f_literal_width + 1); + v_width = v_initial_width; goto label_0_continue; } else if (v_code == v_end_code) { status = WUFFS_GIF__STATUS_OK; @@ -3239,6 +3243,7 @@ suspend: self->private_impl.c_decode[0].v_end_code = v_end_code; self->private_impl.c_decode[0].v_save_code = v_save_code; self->private_impl.c_decode[0].v_prev_code = v_prev_code; + self->private_impl.c_decode[0].v_initial_width = v_initial_width; self->private_impl.c_decode[0].v_width = v_width; self->private_impl.c_decode[0].v_bits = v_bits; self->private_impl.c_decode[0].v_n_bits = v_n_bits; diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 601d2d39..f21c2430 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -1109,6 +1109,7 @@ typedef struct { uint32_t v_end_code; uint32_t v_save_code; uint32_t v_prev_code; + uint32_t v_initial_width; uint32_t v_width; uint32_t v_bits; uint32_t v_n_bits; diff --git a/std/gif/decode_lzw.wuffs b/std/gif/decode_lzw.wuffs index 198531c7..704e391a 100644 --- a/std/gif/decode_lzw.wuffs +++ b/std/gif/decode_lzw.wuffs @@ -40,7 +40,8 @@ pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() { // `max`. 4096 means do not save. var save_code base.u32[..4096] = end_code var prev_code base.u32[..4095] - var width base.u32[..12] = this.literal_width + 1 + var initial_width base.u32[..12] = this.literal_width + 1 + var width base.u32[..12] = initial_width // These variables yield src's bits in Least Significant Bits order. var bits base.u32 @@ -73,7 +74,7 @@ pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() { } else if code == clear_code { save_code = end_code prev_code = 0 - width = this.literal_width + 1 + width = initial_width continue } else if code == end_code {