Skip to content

Compress an out-of-range btype as a dynamic block, as C does - #212

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
kunpai:btype-out-of-range-dynamic
Nov 20, 2026
Merged

copybara-service[bot] merged 1 commit into
google:masterfrom
kunpai:btype-out-of-range-dynamic

Conversation

@kunpai

@kunpai kunpai commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Compress an out-of-range btype as a dynamic block, as C does

Problem

ZopfliDeflatePart (C) dispatches on btype: 0 is a stored block, 1 is a fixed-tree block, and every other value takes the dynamic (btype-2) path and writes a block. The port's deflate_helper (Rust) matches 0, 1 and 2 and returns for anything else, writing nothing. For example, ZopfliDeflate with btype = 5, final = 1, a non-zero bit pointer of 0, over a 48-byte input:

C Rust
btype 5 writes a dynamic block returns, nothing written

Question

Which behaviour do we want: C's or the Rust port's? Specifically, for a btype outside 0, 1, 2, should ZopfliDeflate/ZopfliDeflatePart compress it as a dynamic block (like the C source), or return without writing anything (like the Rust port)?

This PR implements C's behaviour.

Fix

deflate_helper's btype match sends any value other than 0 or 1 down the dynamic path:

     let block_type = match btype {
         0 => BlockType::Uncompressed,
         1 => BlockType::FixedTree,
-        2 => BlockType::DynamicTree,
-        _ => return,
+        _ => BlockType::DynamicTree,
     };

Replayed on the btype = 5 record (bit pointer 0, 48-byte input), before and after:

ZopfliDeflate C Rust main Rust patched
output a dynamic block nothing the same bytes as C

C and the patched port now produce byte-identical output, and the divergence closes.

Downstream check. Deflating the repository's README with btype = 3:

C Rust main Rust patched
ZopfliDeflate(btype=3) 813 bytes, zlib.decompressobj(-15) inflates back to the file 0 bytes, emits nothing 813 bytes, zlib.decompressobj(-15) inflates back to the file

The patched port emits a valid dynamic block for btype = 3 that round-trips through a standard inflater, where main emits nothing. Its bytes are not identical to C's, because the port picks different Huffman code lengths where several are optimal (#213). With that PR as well, the port's 813 bytes are C's, final bit pointer included. The normal path is also untouched: ZopfliGzipCompress on the repository's README gives the same 831-byte stream from the port before and after this patch.

Notes for review:

  • With this PR the port runs the full dynamic-block search for an out-of-range btype, so a call that returned instantly now does the same work as C and is as slow as C.
  • The two records that first surfaced this (btype = -1 with bit pointers 64 and 216) also carry an out-of-range bit pointer, which is a separate divergence (Write BFINAL with the caller's bit pointer, as C's AddBit does #219). After this PR their btype is handled as C handles it, but they still differ on that second point, so they are not clean closes on their own; the btype = 5 record above isolates this one. With both PRs the bit pointer 64 record closes. The bit pointer 216 one has an empty output too, so C writes before the buffer there (Write the last output byte the way C's AddBit does when the output is empty #220).

C's ZopfliDeflatePart sends every btype other than 0 and 1 down the
dynamic path and writes a block. deflate_helper matched 0, 1 and 2 and
returned for anything else, writing nothing.

@1c3t3a 1c3t3a left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! We want C behavior!

@copybara-service
copybara-service Bot merged commit d3d6758 into google:master Nov 20, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants