Fix std/deflate decode_huffman_slow suspends

Unlike the other decode_huffman_etc! methods, decode_huffman_slow? (with
a ?) can suspend. The (hlen, hdist) and the remainder (length, distance)
pairs might not be stable across a suspension, as the args.dst io_writer
might change its history length. We have to re-calculate those pairs
after a short write.

The test/3pdata/nia-checksums-of-pngsuite.txt for interlaced and
non-interlaced flavors of the same image now match.
This commit is contained in:
Nigel Tao committed 2021-11-20 21:31:40 +11:00
1 parent ede0590ead
commit 49a431f98d
3 files changed
+28 -52

No files matched your search

+9 -24
View File
@@ -7361,7 +7361,6 @@ struct wuffs_deflate__decoder__struct {
uint32_t v_length;
uint32_t v_dist_minus_1;
uint32_t v_hlen;
uint32_t v_hdist;
uint64_t scratch;
} s_decode_huffman_slow[1];
} private_data;
@@ -27937,7 +27936,6 @@ wuffs_deflate__decoder__decode_huffman_slow(
v_length = self->private_data.s_decode_huffman_slow[0].v_length;
v_dist_minus_1 = self->private_data.s_decode_huffman_slow[0].v_dist_minus_1;
v_hlen = self->private_data.s_decode_huffman_slow[0].v_hlen;
v_hdist = self->private_data.s_decode_huffman_slow[0].v_hdist;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
@@ -28134,15 +28132,14 @@ wuffs_deflate__decoder__decode_huffman_slow(
v_bits >>= v_table_entry_n_bits;
v_n_bits -= v_table_entry_n_bits;
}
label__inner__continue:;
while (true) {
if (((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)(iop_a_dst - io0_a_dst))) {
v_hdist = ((uint32_t)((((uint64_t)((v_dist_minus_1 + 1))) - ((uint64_t)(iop_a_dst - io0_a_dst)))));
if (v_length > v_hdist) {
v_length -= v_hdist;
if (v_hdist < v_length) {
v_hlen = v_hdist;
} else {
v_hlen = v_length;
v_length = 0;
}
v_hdist += ((uint32_t)((((uint64_t)(self->private_impl.f_transformed_history_count - (a_dst ? a_dst->meta.pos : 0))) & 4294967295)));
if (self->private_impl.f_history_index < v_hdist) {
@@ -28150,24 +28147,15 @@ wuffs_deflate__decoder__decode_huffman_slow(
goto exit;
}
v_hdist = (self->private_impl.f_history_index - v_hdist);
while (true) {
v_n_copied = wuffs_base__io_writer__limited_copy_u32_from_slice(
&iop_a_dst, io2_a_dst,v_hlen, wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_history, 33025), (v_hdist & 32767)));
if (v_hlen <= v_n_copied) {
v_hlen = 0;
goto label__4__break;
}
if (v_n_copied > 0) {
v_hlen -= v_n_copied;
v_hdist = (((uint32_t)(v_hdist + v_n_copied)) & 32767);
if (v_hdist == 0) {
goto label__4__break;
}
}
v_n_copied = wuffs_base__io_writer__limited_copy_u32_from_slice(
&iop_a_dst, io2_a_dst,v_hlen, wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_history, 33025), (v_hdist & 32767)));
if (v_n_copied < v_hlen) {
v_length -= v_n_copied;
status = wuffs_base__make_status(wuffs_base__suspension__short_write);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
goto label__inner__continue;
}
label__4__break:;
v_length -= v_hlen;
if (v_length == 0) {
goto label__loop__continue;
}
@@ -28175,14 +28163,12 @@ wuffs_deflate__decoder__decode_huffman_slow(
v_n_copied = wuffs_base__io_writer__limited_copy_u32_from_history(
&iop_a_dst, io0_a_dst, io2_a_dst, v_length, (v_dist_minus_1 + 1));
if (v_length <= v_n_copied) {
v_length = 0;
goto label__inner__break;
goto label__loop__continue;
}
v_length -= v_n_copied;
status = wuffs_base__make_status(wuffs_base__suspension__short_write);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(10);
}
label__inner__break:;
}
label__loop__break:;
self->private_impl.f_bits = v_bits;
@@ -28211,7 +28197,6 @@ wuffs_deflate__decoder__decode_huffman_slow(
self->private_data.s_decode_huffman_slow[0].v_length = v_length;
self->private_data.s_decode_huffman_slow[0].v_dist_minus_1 = v_dist_minus_1;
self->private_data.s_decode_huffman_slow[0].v_hlen = v_hlen;
self->private_data.s_decode_huffman_slow[0].v_hdist = v_hdist;
goto exit;
exit:
+16 -25
View File
@@ -205,18 +205,17 @@ pri func decoder.decode_huffman_slow?(dst: base.io_writer, src: base.io_reader)
// Copy from this.history.
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
// Set (hlen, hdist) to be the length-distance pair to copy
// from this.history, and (length, distance) to be the
// remaining length-distance pair to copy from args.dst.
// from this.history.
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
if length > hdist {
assert hdist < length via "a < b: b > a"()
if hdist < length {
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
length -= hdist
hlen = hdist
assert hlen <= length via "a <= b: a == c; c <= b"(c: hdist)
} else {
hlen = length
length = 0
assert hlen <= length via "a <= b: a == b"()
}
hdist ~mod+= ((this.transformed_history_count ~mod- args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
if this.history_index < hdist {
return "#bad distance"
@@ -226,24 +225,17 @@ pri func decoder.decode_huffman_slow?(dst: base.io_writer, src: base.io_reader)
hdist = this.history_index - hdist
// Copy from hdist to the end of this.history.
while true {
n_copied = args.dst.limited_copy_u32_from_slice!(
up_to: hlen, s: this.history[hdist & 0x7FFF ..])
if hlen <= n_copied {
hlen = 0
break
}
if n_copied > 0 {
hlen -= n_copied
hdist = (hdist ~mod+ n_copied) & 0x7FFF
if hdist == 0 {
// Wrap around the this.history ringbuffer.
break
}
}
n_copied = args.dst.limited_copy_u32_from_slice!(
up_to: hlen, s: this.history[hdist & 0x7FFF ..])
if n_copied < hlen {
assert n_copied < length via "a < b: a < c; c <= b"(c: hlen)
assert length > n_copied via "a > b: b < a"()
length -= n_copied
yield? base."$short write"
} endwhile
continue.inner
}
assert length >= hlen via "a >= b: b <= a"()
length -= hlen
if length == 0 {
// No need to copy from args.dst.
continue.loop
@@ -254,8 +246,7 @@ pri func decoder.decode_huffman_slow?(dst: base.io_writer, src: base.io_reader)
n_copied = args.dst.limited_copy_u32_from_history!(
up_to: length, distance: (dist_minus_1 + 1))
if length <= n_copied {
length = 0
break.inner
continue.loop
}
length -= n_copied
yield? base."$short write"
+3 -3
View File
@@ -117,11 +117,11 @@ ad298c62 test/3pdata/pngsuite/s03n3p01.png
2c30132c test/3pdata/pngsuite/s04n3p01.png
5fb20358 test/3pdata/pngsuite/s05i3p02.png
5fb20358 test/3pdata/pngsuite/s05n3p02.png
4b1198df test/3pdata/pngsuite/s06i3p02.png
764e90bd test/3pdata/pngsuite/s06i3p02.png
764e90bd test/3pdata/pngsuite/s06n3p02.png
9eebca92 test/3pdata/pngsuite/s07i3p02.png
9eebca92 test/3pdata/pngsuite/s07n3p02.png
222e5ceb test/3pdata/pngsuite/s08i3p02.png
d57f2e84 test/3pdata/pngsuite/s08i3p02.png
d57f2e84 test/3pdata/pngsuite/s08n3p02.png
7585127f test/3pdata/pngsuite/s09i3p02.png
7585127f test/3pdata/pngsuite/s09n3p02.png
@@ -129,7 +129,7 @@ d57f2e84 test/3pdata/pngsuite/s08n3p02.png
034051a8 test/3pdata/pngsuite/s32n3p04.png
a377dcdd test/3pdata/pngsuite/s33i3p04.png
a377dcdd test/3pdata/pngsuite/s33n3p04.png
88952d26 test/3pdata/pngsuite/s34i3p04.png
5bcd998d test/3pdata/pngsuite/s34i3p04.png
5bcd998d test/3pdata/pngsuite/s34n3p04.png
1cefbbe2 test/3pdata/pngsuite/s35i3p04.png
1cefbbe2 test/3pdata/pngsuite/s35n3p04.png