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

+12 -3
View File
@@ -17,7 +17,14 @@
package main
// print-deflate-magic-numbers.go prints the std/deflate lcode_magic_numbers
// values based on the tables in RFC 1951 secion 3.2.5.
// and dcode_magic_numbers values based on the tables in RFC 1951 secion 3.2.5.
//
// The dcode base numbers are biased by -1 so that (base_number_minus_1 +
// extra_bits) fits in the range [0, 32767]. This makes a bitwise and with
// 0x7FFF a no-op, in terms of computed value, but proves to the compiler that
// the result is within a certain range. Furthermore, proving that (d + 1) > 0
// is trivial, for d of type u32[..something], compared to proving that d > 0,
// which usually requires a runtime check (an if branch).
//
// Usage: go run print-deflate-magic-numbers.go
@@ -35,13 +42,15 @@ func main() {
func main1() error {
for i := 0; i < 2; i++ {
bias := uint32(0)
if i != 0 {
fmt.Println()
bias = 1 // See the "dcode base numbers" comment above.
}
for j := 0; j < 32; j++ {
x := uint32(0x08000000)
if baseNumbers[i][j] != bad {
x = 0x40000000 | (baseNumbers[i][j] << 8) | (extraBits[i][j] << 4)
if bn := baseNumbers[i][j]; bn != bad {
x = 0x40000000 | ((bn - bias) << 8) | (extraBits[i][j] << 4)
}
fmt.Printf("0x%08X,", x)
if j&7 == 7 {
+10 -9
View File
@@ -210,7 +210,7 @@ outer_loop:
goto outer_loop;
}
if ((table_entry >> 30) != 0) {
// Back-reference; length = base number + extra bits.
// Back-reference; length = base_number + extra_bits.
break;
}
if ((table_entry >> 29) != 0) {
@@ -229,7 +229,7 @@ outer_loop:
table_entry = self->private_impl.f_huffs[0][top + (bits & mask)];
}
// length = base number + extra bits.
// length = base_number + extra_bits.
uint32_t length = (table_entry >> 8) & 0xFFFF;
{
uint32_t n = (table_entry >> 4) & 0x0F;
@@ -263,7 +263,7 @@ outer_loop:
bits >>= n;
n_bits -= n;
if ((table_entry >> 30) != 0) {
// Back-reference; distance = base number + extra bits.
// Back-reference; distance = base_number + extra_bits.
break;
}
if ((table_entry >> 24) != 0x10) {
@@ -277,8 +277,9 @@ outer_loop:
table_entry = self->private_impl.f_huffs[1][top + (bits & mask)];
}
// distance = base number + extra bits.
uint32_t distance = (table_entry >> 8) & 0xFFFF;
// dist_minus_1 = base_number_minus_1 + extra_bits + 1.
// distance = dist_minus_1 + 1.
uint32_t dist_minus_1 = (table_entry >> 8) & 0xFFFF;
{
uint32_t n = (table_entry >> 4) & 0x0F;
if (n) {
@@ -291,19 +292,19 @@ outer_loop:
n_bits += 8;
}
#endif
distance += bits & wuffs_base__width_to_mask_table[n];
dist_minus_1 += bits & wuffs_base__width_to_mask_table[n];
bits >>= n;
n_bits -= n;
}
}
// TODO: look at a sliding window, not just output written so far to dst.
if ((ptrdiff_t)(distance) > (pdst - pdst0)) {
if ((ptrdiff_t)(dist_minus_1 + 1) > (pdst - pdst0)) {
status = WUFFS_DEFLATE__ERROR_BAD_ARGUMENT;
goto end;
}
uint8_t* pback = pdst - distance;
uint8_t* pback = pdst - (dist_minus_1 + 1);
#if defined(WUFFS_DEFLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS)
// Back-copy fast path, copying 8 instead of 1 bytes at a time.
@@ -312,7 +313,7 @@ outer_loop:
// 8*N >= length, i.e. we round length up to a multiple of 8), instead of
// only length bytes, but that's OK, as subsequent iterations will fix up
// the overrun.
if (distance >= 8) {
if (dist_minus_1 + 1 >= 8) {
while (1) {
*((uint64_t*)(pdst)) = *((uint64_t*)(pback));
if (length <= 8) {