Recognize a GIF "ANIMEXTS1.0" extension

This commit is contained in:
Nigel Tao committed 2018-02-15 14:35:01 +11:00
1 parent fb2430e765
commit f82c33db2f
3 files changed
+51 -33

No files matched your search

+30 -24
View File
@@ -323,6 +323,8 @@ typedef struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint8_t v_c; uint8_t v_c;
uint8_t v_block_size; uint8_t v_block_size;
bool v_not_animexts;
bool v_not_netscape;
uint64_t scratch; uint64_t scratch;
} c_decode_ae[1]; } c_decode_ae[1];
struct { struct {
@@ -764,6 +766,10 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
// ---------------- Private Consts // ---------------- 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] = { static const uint8_t wuffs_gif__netscape2dot0[11] = {
78, 69, 84, 83, 67, 65, 80, 69, 50, 46, 48, 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_c;
uint8_t v_block_size; uint8_t v_block_size;
bool v_not_animexts;
bool v_not_netscape;
uint8_t* b_rptr_src = NULL; uint8_t* b_rptr_src = NULL;
uint8_t* b_rstart_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) { if (coro_susp_point) {
v_c = self->private_impl.c_decode_ae[0].v_c; 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_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) { switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; 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; b_rptr_src += self->private_impl.c_decode_ae[0].scratch;
goto label_0_break; goto label_0_break;
} }
v_not_animexts = 0;
v_not_netscape = 0;
v_block_size = 0; v_block_size = 0;
while (v_block_size < 11) { 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++; uint8_t t_1 = *b_rptr_src++;
v_c = t_1; v_c = t_1;
} }
if (v_c != wuffs_gif__netscape2dot0[v_block_size]) { v_not_animexts =
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); (v_not_animexts || (v_c != wuffs_gif__animexts1dot0[v_block_size]));
self->private_impl.c_decode_ae[0].scratch = v_not_netscape =
((uint32_t)((10 - v_block_size))); (v_not_netscape || (v_c != wuffs_gif__netscape2dot0[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_block_size += 1; 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)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
@@ -1665,9 +1669,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae(
v_block_size = t_2; v_block_size = t_2;
} }
if (v_block_size != 3) { 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)); 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 > if (self->private_impl.c_decode_ae[0].scratch >
b_rend_src - b_rptr_src) { b_rend_src - b_rptr_src) {
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; 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)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
} }
@@ -1686,9 +1690,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae(
v_c = t_3; v_c = t_3;
} }
if (v_c != 1) { 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; 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 > if (self->private_impl.c_decode_ae[0].scratch >
b_rend_src - b_rptr_src) { b_rend_src - b_rptr_src) {
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; goto label_0_break;
} }
{ {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
uint16_t t_5; uint16_t t_5;
if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) { if (WUFFS_BASE__LIKELY(b_rend_src - b_rptr_src >= 2)) {
t_5 = wuffs_base__load_u16le(b_rptr_src); t_5 = wuffs_base__load_u16le(b_rptr_src);
b_rptr_src += 2; b_rptr_src += 2;
} else { } else {
self->private_impl.c_decode_ae[0].scratch = 0; self->private_impl.c_decode_ae[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12);
while (true) { while (true) {
if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) { if (WUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src; goto short_read_src;
@@ -1735,7 +1739,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae(
goto label_0_break; goto label_0_break;
} }
label_0_break:; label_0_break:;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13);
if (a_src.buf) { if (a_src.buf) {
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
a_src.buf->ri += n; 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].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_c = v_c;
self->private_impl.c_decode_ae[0].v_block_size = v_block_size; 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: exit:
if (a_src.buf) { if (a_src.buf) {
+2
View File
@@ -323,6 +323,8 @@ typedef struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint8_t v_c; uint8_t v_c;
uint8_t v_block_size; uint8_t v_block_size;
bool v_not_animexts;
bool v_not_netscape;
uint64_t scratch; uint64_t scratch;
} c_decode_ae[1]; } c_decode_ae[1];
struct { struct {
+19 -9
View File
@@ -40,8 +40,8 @@ pub struct decoder?(
interlace bool, interlace bool,
// Absent a NETSCAPE2.0 extension, the implicit number of animation loops // Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
// is 1. // animation loops is 1.
seen_num_loops bool, seen_num_loops bool,
num_loops u32 = 1, 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. // netscape2dot0 is "NETSCAPE2.0" as bytes.
pri const netscape2dot0[11] u8 = $( pri const netscape2dot0[11] u8 = $(
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30, 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)() { pri func decoder.decode_ae?(src reader1)() {
// This "while true" always executes exactly once, as it ends with a // This "while true" always executes exactly once, as it ends with a
// "break", but using "break"s throughout simplifies the control flow. // "break", but using "break"s throughout simplifies the control flow.
while:loop true { while true {
var c u8 var c u8
var block_size u8 = in.src.read_u8?() var block_size u8 = in.src.read_u8?()
if block_size == 0 { if block_size == 0 {
return return
} }
// Look only for an 11 byte "NETSCAPE2.0" extension, as per // Look only for an 11 byte "ANIMEXTS1.0" or "NETSCAPE2.0" extension,
// http://www.vurdalakov.net/misc/gif/netscape-looping-application-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 { if block_size != 11 {
in.src.skip32?(n:block_size as u32) in.src.skip32?(n:block_size as u32)
break break
} }
var not_animexts bool
var not_netscape bool
block_size = 0 // Re-purpose the block_size variable as a counter. block_size = 0 // Re-purpose the block_size variable as a counter.
while block_size < 11 { while block_size < 11 {
c = in.src.read_u8?() c = in.src.read_u8?()
if c != netscape2dot0[block_size] { not_animexts = not_animexts or (c != animexts1dot0[block_size])
in.src.skip32?(n:(10 - block_size) as u32) not_netscape = not_netscape or (c != netscape2dot0[block_size])
break:loop
}
block_size += 1 block_size += 1
} }
if not_animexts and not_netscape {
break
}
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop // Those 11 bytes should be followed by 0x03, 0x01 and then the loop
// count. // count.