mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 00:38:14 +01:00
Fix deflate decode when the distance is 32768
This commit is contained in:
1 parent
7de631fb96
commit
4675efdcc5
9 files changed
+115
-76
No files matched your search
@@ -49,10 +49,10 @@ pri const lcode_magic_numbers[32] u32 = $(
|
||||
)
|
||||
|
||||
pri const dcode_magic_numbers[32] u32 = $(
|
||||
0x40000100, 0x40000200, 0x40000300, 0x40000400, 0x40000510, 0x40000710, 0x40000920, 0x40000D20,
|
||||
0x40001130, 0x40001930, 0x40002140, 0x40003140, 0x40004150, 0x40006150, 0x40008160, 0x4000C160,
|
||||
0x40010170, 0x40018170, 0x40020180, 0x40030180, 0x40040190, 0x40060190, 0x400801A0, 0x400C01A0,
|
||||
0x401001B0, 0x401801B0, 0x402001C0, 0x403001C0, 0x404001D0, 0x406001D0, 0x08000000, 0x08000000,
|
||||
0x40000000, 0x40000100, 0x40000200, 0x40000300, 0x40000410, 0x40000610, 0x40000820, 0x40000C20,
|
||||
0x40001030, 0x40001830, 0x40002040, 0x40003040, 0x40004050, 0x40006050, 0x40008060, 0x4000C060,
|
||||
0x40010070, 0x40018070, 0x40020080, 0x40030080, 0x40040090, 0x40060090, 0x400800A0, 0x400C00A0,
|
||||
0x401000B0, 0x401800B0, 0x402000C0, 0x403000C0, 0x404000D0, 0x406000D0, 0x08000000, 0x08000000,
|
||||
)
|
||||
|
||||
pub struct decoder?(
|
||||
|
||||
@@ -156,7 +156,7 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// length = base number + extra bits.
|
||||
// length = base_number + extra_bits.
|
||||
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
@@ -237,7 +237,7 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
assert in.src.available() >= 2
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base number + extra bits.
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) != 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return error "bad Huffman code"
|
||||
@@ -245,9 +245,13 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// distance = base number + extra bits.
|
||||
// distance = base_number_minus_1 + extra_bits.
|
||||
//
|
||||
// TODO: the upper bound isn't right. 0x8000 is a valid distance.
|
||||
// TODO: rename distance to dist_minus_1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 15-ish lines below correct and
|
||||
// undoing that bias makes proving (distance + 1) > 0 trivial.
|
||||
var distance u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
@@ -267,9 +271,6 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
if distance <= 0 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
var n_copied u32
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
@@ -277,18 +278,20 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre length <= 258,
|
||||
pre distance > 0,
|
||||
pre in.dst.available() >= 258,
|
||||
inv in.dst.is_marked(),
|
||||
{
|
||||
// Copy from this.history.
|
||||
if (distance as u64) > in.dst.since_mark().length() {
|
||||
if ((distance + 1) as u64) > in.dst.since_mark().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 in.dst.
|
||||
var hlen u32[..0x7FFF]
|
||||
var hdist u32[..0x7FFF] = ((distance as u64) - in.dst.since_mark().length()) as u32
|
||||
var hdist u32 =
|
||||
(((distance + 1) as u64) - in.dst.since_mark().length()) as u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
// TODO: this if check should be redundant.
|
||||
@@ -305,24 +308,28 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
}
|
||||
// Re-purpose the hdist variable as the this.history index to
|
||||
// start copying from.
|
||||
hdist = (this.history_index - hdist) & 0x7FFF
|
||||
hdist = this.history_index - hdist
|
||||
|
||||
// Copy from hdist to the end of this.history, and then
|
||||
// possibly wrap around the this.history ringbuffer.
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// in.dst.available() is large enough.
|
||||
//
|
||||
// The "while true { etc; break }" is a redundant version of
|
||||
// "etc", but its presence minimizes the diff between
|
||||
// decode_huffman_fast and decode_huffman_slow.
|
||||
while true,
|
||||
inv length <= 258,
|
||||
inv distance > 0,
|
||||
inv in.dst.available() >= 258,
|
||||
inv in.dst.is_marked(),
|
||||
{
|
||||
// TODO: copy_from_slice32 should probably update the
|
||||
// "in.dst.available() >= 258" fact. It should certainly
|
||||
// invalidate any fact that was "<=" instead of ">=".
|
||||
n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
n_copied = in.dst.copy_from_slice32(
|
||||
s:this.history[hdist & 0x7FFF:], length:hlen)
|
||||
if hlen <= n_copied {
|
||||
break
|
||||
}
|
||||
@@ -336,23 +343,23 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
||||
continue:loop
|
||||
}
|
||||
|
||||
if (distance as u64) > in.dst.since_mark().length() {
|
||||
if ((distance + 1) as u64) > in.dst.since_mark().length() {
|
||||
return error "internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert length <= 258
|
||||
assert distance > 0
|
||||
assert (distance as u64) <= in.dst.since_mark().length()
|
||||
assert ((distance + 1) as u64) <= in.dst.since_mark().length()
|
||||
assert in.dst.available() >= 258
|
||||
assert in.dst.is_marked()
|
||||
|
||||
// We can therefore prove:
|
||||
assert (distance + 1) > 0
|
||||
assert (length as u64) <= 258
|
||||
assert (length as u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258)
|
||||
|
||||
// Copy from in.dst.
|
||||
in.dst.copy_from_history32(distance:distance, length:length)
|
||||
in.dst.copy_from_history32(distance:(distance + 1), length:length)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// length = base number + extra bits.
|
||||
// length = base_number + extra_bits.
|
||||
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
@@ -149,7 +149,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
}
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base number + extra bits.
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) != 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return error "bad Huffman code"
|
||||
@@ -157,9 +157,13 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// distance = base number + extra bits.
|
||||
// distance = base_number_minus_1 + extra_bits.
|
||||
//
|
||||
// TODO: the upper bound isn't right. 0x8000 is a valid distance.
|
||||
// TODO: rename distance to dist_minus_1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 15-ish lines below correct and
|
||||
// undoing that bias makes proving (distance + 1) > 0 trivial.
|
||||
var distance u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
@@ -174,20 +178,20 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
if distance <= 0 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
var n_copied u32
|
||||
while true {
|
||||
// Copy from this.history.
|
||||
if (distance as u64) > in.dst.since_mark().length() {
|
||||
if ((distance + 1) as u64) > in.dst.since_mark().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 in.dst.
|
||||
var hlen u32[..0x7FFF]
|
||||
var hdist u32[..0x7FFF] = ((distance as u64) - in.dst.since_mark().length()) as u32
|
||||
var hdist u32 =
|
||||
(((distance + 1) as u64) - in.dst.since_mark().length()) as u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
@@ -199,11 +203,12 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
}
|
||||
// Re-purpose the hdist variable as the this.history index to
|
||||
// start copying from.
|
||||
hdist = (this.history_index - hdist) & 0x7FFF
|
||||
hdist = this.history_index - hdist
|
||||
|
||||
// Copy from hdist to the end of this.history.
|
||||
while true {
|
||||
n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
n_copied = in.dst.copy_from_slice32(
|
||||
s:this.history[hdist & 0x7FFF:], length:hlen)
|
||||
if hlen <= n_copied {
|
||||
hlen = 0
|
||||
break
|
||||
@@ -222,13 +227,14 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
// Copy from the start of this.history, if we wrapped around.
|
||||
if hlen > 0 {
|
||||
while true {
|
||||
n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
||||
n_copied = in.dst.copy_from_slice32(
|
||||
s:this.history[hdist & 0x7FFF:], length:hlen)
|
||||
if hlen <= n_copied {
|
||||
hlen = 0
|
||||
break
|
||||
}
|
||||
hlen -= n_copied
|
||||
hdist = (hdist + (n_copied & 0x7FFF)) & 0x7FFF
|
||||
hdist = hdist ~+ n_copied
|
||||
// TODO: "closed for writes" instead?
|
||||
yield suspension "short write"
|
||||
}
|
||||
@@ -241,7 +247,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
|
||||
}
|
||||
|
||||
// Copy from in.dst.
|
||||
n_copied = in.dst.copy_from_history32(distance:distance, length:length)
|
||||
n_copied = in.dst.copy_from_history32(distance:(distance + 1), length:length)
|
||||
if length <= n_copied {
|
||||
length = 0
|
||||
break
|
||||
|
||||
Reference in new issue
Block a user