Repository navigation
Compress an out-of-range btype as a dynamic block, as C does - #212
Merged
copybara-service[bot] merged 1 commit intoNov 20, 2026
Merged
Conversation
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.
This was referenced Oct 28, 2026
1c3t3a
approved these changes
Nov 20, 2026
1c3t3a
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the PR! We want C behavior!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Compress an out-of-range btype as a dynamic block, as C does
Problem
ZopfliDeflatePart(C) dispatches onbtype: 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'sdeflate_helper(Rust) matches 0, 1 and 2 and returns for anything else, writing nothing. For example,ZopfliDeflatewithbtype = 5,final = 1, a non-zero bit pointer of 0, over a 48-byte input:btype5Question
Which behaviour do we want: C's or the Rust port's? Specifically, for a
btypeoutside 0, 1, 2, shouldZopfliDeflate/ZopfliDeflatePartcompress 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'sbtypematch sends any value other than 0 or 1 down the dynamic path:Replayed on the
btype = 5record (bit pointer 0, 48-byte input), before and after:ZopfliDeflateC and the patched port now produce byte-identical output, and the divergence closes.
Downstream check. Deflating the repository's
READMEwithbtype = 3:ZopfliDeflate(btype=3)zlib.decompressobj(-15)inflates back to the filezlib.decompressobj(-15)inflates back to the fileThe patched port emits a valid dynamic block for
btype = 3that round-trips through a standard inflater, wheremainemits 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:ZopfliGzipCompresson the repository'sREADMEgives the same 831-byte stream from the port before and after this patch.Notes for review:
btype, so a call that returned instantly now does the same work as C and is as slow as C.btype = -1with 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 theirbtypeis handled as C handles it, but they still differ on that second point, so they are not clean closes on their own; thebtype = 5record 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).