From f86c80e42575a44b9994feb036d86482861d4431 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 8 Jan 2022 13:46:27 +1100 Subject: [PATCH] Fix std/deflate degenerate H-D Huffman table RFC 1951 section 3.2.7. "Compression with dynamic Huffman codes (BTYPE=10)" says, "If only one distance code is used, it is encoded using one bit, not zero bits; in this case there is a single code length of one, with one unused code." I had previously mis-read that excerpt to also require that the sole distance code was the shortest distance (code 0, a distance of 1). Fixes #66 --- release/c/wuffs-unsupported-snapshot.c | 19 +++++---- script/make-artificial.go | 16 +++---- std/deflate/decode_deflate.wuffs | 18 ++++---- .../degenerate-huffman-unused.deflate | Bin 15 -> 33 bytes ...rate-huffman-unused.deflate.commentary.txt | 9 ++-- ...nerate-huffman-unused.deflate.decompressed | 2 +- ...huffman-unused.deflate.make-artificial.txt | 39 +++++++++++++++++- 7 files changed, 75 insertions(+), 28 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 9ce5434e..0875822d 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -27008,14 +27008,17 @@ wuffs_deflate__decoder__init_huff( v_i += 1; } if (v_remaining != 0) { - if ((a_which == 1) && - (v_counts[1] == 1) && - (self->private_data.f_code_lengths[a_n_codes0] == 1) && - ((((uint32_t)(v_counts[0])) + a_n_codes0 + 1) == a_n_codes1)) { - self->private_impl.f_n_huffs_bits[1] = 1; - self->private_data.f_huffs[1][0] = (WUFFS_DEFLATE__DCODE_MAGIC_NUMBERS[0] | 1); - self->private_data.f_huffs[1][1] = (WUFFS_DEFLATE__DCODE_MAGIC_NUMBERS[31] | 1); - return wuffs_base__make_status(NULL); + if ((a_which == 1) && (v_counts[1] == 1) && ((((uint32_t)(v_counts[0])) + a_n_codes0 + 1) == a_n_codes1)) { + v_i = 0; + while (v_i <= 29) { + if (self->private_data.f_code_lengths[(a_n_codes0 + v_i)] == 1) { + self->private_impl.f_n_huffs_bits[1] = 1; + self->private_data.f_huffs[1][0] = (WUFFS_DEFLATE__DCODE_MAGIC_NUMBERS[v_i] | 1); + self->private_data.f_huffs[1][1] = (WUFFS_DEFLATE__DCODE_MAGIC_NUMBERS[31] | 1); + return wuffs_base__make_status(NULL); + } + v_i += 1; + } } return wuffs_base__make_status(wuffs_deflate__error__bad_huffman_code_under_subscribed); } diff --git a/script/make-artificial.go b/script/make-artificial.go index 575d0c08..e0d23372 100644 --- a/script/make-artificial.go +++ b/script/make-artificial.go @@ -587,20 +587,20 @@ func stateDeflateDynamicHuffman(line string) (stateFunc, error) { return stateDeflateDynamicHuffman, nil } else if l, d, ok := deflateParseLenDist(line); ok { - if (l != 3) || (d != 2) { - return nil, fmt.Errorf("TODO: support len/dist pairs for dynamic Huffman blocks") + if (l > 10) || (d > 4) { + return nil, fmt.Errorf("TODO: support len/dist ExtraBits > 0 for dynamic Huffman blocks") } - // len 3 is code 257, with no extra bits. - if s := g.huffmans[2][257]; s != "" { + // len 3 is code 257, len 4 is code 258, etc. All with no ExtraBits. + if s := g.huffmans[2][l+254]; s != "" { deflateGlobalsWriteDynamicHuffmanBits(s) } else { - return nil, fmt.Errorf("no code for literal/length symbol 257") + return nil, fmt.Errorf("no code for literal/length symbol %d", l+254) } - // dist 2 is code 1, with no extra bits. - if s := g.huffmans[3][1]; s != "" { + // dist 1 is code 0, dist 2 is code 1, etc. All with no ExtraBits. + if s := g.huffmans[3][d-1]; s != "" { deflateGlobalsWriteDynamicHuffmanBits(s) } else { - return nil, fmt.Errorf("no code for distance symbol 2") + return nil, fmt.Errorf("no code for distance symbol %d", d-1) } return stateDeflateDynamicHuffman, nil diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index c24a4d5e..947f4f2e 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -628,17 +628,21 @@ pri func decoder.init_huff!(which: base.u32[..= 1], n_codes0: base.u32[..= 288], } endwhile if remaining <> 0 { // As a special case, allow a degenerate H-D Huffman table, with only - // one 1-bit code, for the smallest possible distance. + // one 1-bit code. if (args.which == 1) and (counts[1] == 1) and - (this.code_lengths[args.n_codes0] == 1) and (((counts[0] as base.u32) + args.n_codes0 + 1) == args.n_codes1) { - this.n_huffs_bits[1] = 1 - this.huffs[1][0] = DCODE_MAGIC_NUMBERS[0] | 1 - this.huffs[1][1] = DCODE_MAGIC_NUMBERS[31] | 1 - return ok + i = 0 + while i <= 29 { + if this.code_lengths[args.n_codes0 + i] == 1 { + this.n_huffs_bits[1] = 1 + this.huffs[1][0] = DCODE_MAGIC_NUMBERS[i] | 1 + this.huffs[1][1] = DCODE_MAGIC_NUMBERS[31] | 1 + return ok + } + i += 1 + } endwhile } - return "#bad Huffman code (under-subscribed)" } diff --git a/test/data/artificial-deflate/degenerate-huffman-unused.deflate b/test/data/artificial-deflate/degenerate-huffman-unused.deflate index 4751683b8b7875d7df75022ac7559d455ba06d43..d899dd5485d9282459e48ff847aabb855cee7d2f 100644 GIT binary patch literal 33 pcmZQMpvcI;z|gQ@d)PnoPU*H}VFm_