Fix deflate decode when the distance is 32768

This commit is contained in:
Nigel Tao committed 2018-03-23 12:49:42 +11:00
1 parent 7de631fb96
commit 4675efdcc5
9 files changed
+115 -76

No files matched your search

+24 -17
View File
@@ -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
}
}