From f82c33db2fae2bccee00d762c284c814ac55e07b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 15 Feb 2018 14:35:01 +1100 Subject: [PATCH] Recognize a GIF "ANIMEXTS1.0" extension --- gen/c/std/gif.c | 54 ++++++++++++++++++++++------------------ gen/h/std/gif.h | 2 ++ std/gif/decode_gif.wuffs | 28 ++++++++++++++------- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 75030ddb..91dffa31 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -323,6 +323,8 @@ typedef struct { uint32_t coro_susp_point; uint8_t v_c; uint8_t v_block_size; + bool v_not_animexts; + bool v_not_netscape; uint64_t scratch; } c_decode_ae[1]; struct { @@ -764,6 +766,10 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) { // ---------------- Private Consts +static const uint8_t wuffs_gif__animexts1dot0[11] = { + 65, 78, 73, 77, 69, 88, 84, 83, 49, 46, 48, +}; + static const uint8_t wuffs_gif__netscape2dot0[11] = { 78, 69, 84, 83, 67, 65, 80, 69, 50, 46, 48, }; @@ -1577,6 +1583,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( uint8_t v_c; uint8_t v_block_size; + bool v_not_animexts; + bool v_not_netscape; uint8_t* b_rptr_src = NULL; uint8_t* b_rstart_src = NULL; @@ -1598,6 +1606,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( if (coro_susp_point) { v_c = self->private_impl.c_decode_ae[0].v_c; v_block_size = self->private_impl.c_decode_ae[0].v_block_size; + v_not_animexts = self->private_impl.c_decode_ae[0].v_not_animexts; + v_not_netscape = self->private_impl.c_decode_ae[0].v_not_netscape; } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; @@ -1629,6 +1639,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( b_rptr_src += self->private_impl.c_decode_ae[0].scratch; goto label_0_break; } + v_not_animexts = 0; + v_not_netscape = 0; v_block_size = 0; while (v_block_size < 11) { { @@ -1639,25 +1651,17 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( uint8_t t_1 = *b_rptr_src++; v_c = t_1; } - if (v_c != wuffs_gif__netscape2dot0[v_block_size]) { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); - self->private_impl.c_decode_ae[0].scratch = - ((uint32_t)((10 - v_block_size))); - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); - if (self->private_impl.c_decode_ae[0].scratch > - b_rend_src - b_rptr_src) { - self->private_impl.c_decode_ae[0].scratch -= - b_rend_src - b_rptr_src; - b_rptr_src = b_rend_src; - goto short_read_src; - } - b_rptr_src += self->private_impl.c_decode_ae[0].scratch; - goto label_0_break; - } + v_not_animexts = + (v_not_animexts || (v_c != wuffs_gif__animexts1dot0[v_block_size])); + v_not_netscape = + (v_not_netscape || (v_c != wuffs_gif__netscape2dot0[v_block_size])); v_block_size += 1; } + if (v_not_animexts && v_not_netscape) { + goto label_0_break; + } { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { goto short_read_src; } @@ -1665,9 +1669,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( v_block_size = t_2; } if (v_block_size != 3) { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); self->private_impl.c_decode_ae[0].scratch = ((uint32_t)(v_block_size)); - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); if (self->private_impl.c_decode_ae[0].scratch > b_rend_src - b_rptr_src) { self->private_impl.c_decode_ae[0].scratch -= b_rend_src - b_rptr_src; @@ -1678,7 +1682,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( goto label_0_break; } { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { goto short_read_src; } @@ -1686,9 +1690,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( v_c = t_3; } if (v_c != 1) { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); self->private_impl.c_decode_ae[0].scratch = 2; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); if (self->private_impl.c_decode_ae[0].scratch > b_rend_src - b_rptr_src) { self->private_impl.c_decode_ae[0].scratch -= b_rend_src - b_rptr_src; @@ -1699,14 +1703,14 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( goto label_0_break; } { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); uint16_t t_5; if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) { t_5 = wuffs_base__load_u16le(b_rptr_src); b_rptr_src += 2; } else { self->private_impl.c_decode_ae[0].scratch = 0; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12); while (true) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { goto short_read_src; @@ -1735,7 +1739,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( goto label_0_break; } label_0_break:; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13); if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n; @@ -1765,6 +1769,8 @@ suspend: self->private_impl.c_decode_ae[0].coro_susp_point = coro_susp_point; self->private_impl.c_decode_ae[0].v_c = v_c; self->private_impl.c_decode_ae[0].v_block_size = v_block_size; + self->private_impl.c_decode_ae[0].v_not_animexts = v_not_animexts; + self->private_impl.c_decode_ae[0].v_not_netscape = v_not_netscape; exit: if (a_src.buf) { diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 67060c4b..fead5f41 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -323,6 +323,8 @@ typedef struct { uint32_t coro_susp_point; uint8_t v_c; uint8_t v_block_size; + bool v_not_animexts; + bool v_not_netscape; uint64_t scratch; } c_decode_ae[1]; struct { diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 925061b5..486e311c 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -40,8 +40,8 @@ pub struct decoder?( interlace bool, - // Absent a NETSCAPE2.0 extension, the implicit number of animation loops - // is 1. + // Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of + // animation loops is 1. seen_num_loops bool, num_loops u32 = 1, @@ -191,6 +191,11 @@ pri func decoder.skip_blocks?(src reader1)() { } } +// animexts1dot0 is "ANIMEXTS1.0" as bytes. +pri const animexts1dot0[11] u8 = $( + 0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30, +) + // netscape2dot0 is "NETSCAPE2.0" as bytes. pri const netscape2dot0[11] u8 = $( 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30, @@ -200,28 +205,33 @@ pri const netscape2dot0[11] u8 = $( pri func decoder.decode_ae?(src reader1)() { // This "while true" always executes exactly once, as it ends with a // "break", but using "break"s throughout simplifies the control flow. - while:loop true { + while true { var c u8 var block_size u8 = in.src.read_u8?() if block_size == 0 { return } - // Look only for an 11 byte "NETSCAPE2.0" extension, as per - // http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension + // Look only for an 11 byte "ANIMEXTS1.0" or "NETSCAPE2.0" extension, + // as per: + // - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension + // - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension if block_size != 11 { in.src.skip32?(n:block_size as u32) break } + var not_animexts bool + var not_netscape bool block_size = 0 // Re-purpose the block_size variable as a counter. while block_size < 11 { c = in.src.read_u8?() - if c != netscape2dot0[block_size] { - in.src.skip32?(n:(10 - block_size) as u32) - break:loop - } + not_animexts = not_animexts or (c != animexts1dot0[block_size]) + not_netscape = not_netscape or (c != netscape2dot0[block_size]) block_size += 1 } + if not_animexts and not_netscape { + break + } // Those 11 bytes should be followed by 0x03, 0x01 and then the loop // count.