Two silent compression footguns: lz4 level 3 and the 65,536th ZIP entry
Neither of these bugs produced an error. One made lz4 compression 17 times slower than it should have been, and the other could write a ZIP whose header says it holds zero files. I fixed both on the same day, and the lz4 one is easy to hit in your own code.
lz4: “Normal” was the slow setting
The whole point of lz4 is speed: it usually compresses less than gzip or zstd, and it’s much faster at both ends. So it stood out when a benchmark against the standard command-line tools showed Purrchiver’s lz4 compression taking 17 times as long as the native tool on a 170 MB data set. The output was only 0.3% smaller.
The cause is in libarchive’s lz4 filter. It takes a compression-level option
from 1 to 9, and level 3 is where it switches encoders:
if (data->compression_level >= 3)
outsize = LZ4_compress_HC(...); /* high-compression encoder */
else
/* ...the fast encoder... */LZ4_compress_HC is a different algorithm with a different trade-off. It
searches much harder for matches, so it’s far slower to compress, and
decompression is still fast. That’s the right choice when you compress once
and decompress many times, but it’s not what people expect when they pick lz4.
Level 1, the fast encoder, is libarchive’s default.
Purrchiver has one compression control for every format: Store, Fast, Normal, and Best, mapped to levels 0, 1, 5, and 9. Level 5 is a sensible middle for gzip, xz, or zstd. For lz4 it’s past the cliff. Anyone who left the setting at Normal got the HC encoder without asking for it.
The fix is a per-format mapping. For lz4, Store, Fast, and Normal all go to level 1, and only Best goes to 9:
func lz4FilterLevel(_ level: Int?) -> Int? {
guard let level else { return nil } // nil: libarchive default (fast)
return level >= 9 ? 9 : 1
}Choosing Best is a deliberate request for the smallest output, so it gets HC.
Everything else gets the encoder people choose lz4 for. After the change,
.tar.lz4 runs about as fast as tar --lz4.
The general lesson: one compression slider doesn’t mean the same thing for every algorithm. Level numbers aren’t a shared scale. Some libraries change algorithms at a threshold, not gradually. If you expose one control for several formats, map it per format and check each one against its own CLI.
ZIP: 65,536 entries became 0
Purrchiver builds ZIPs of many files in parallel. It splits the files into groups, compresses each group as a separate ZIP at the same time, and then merges them: it joins the compressed data and writes one new central directory, plus the end-of-central-directory record (EOCD) that ZIP readers find first.
The classic EOCD stores the entry count in a 16-bit field. The most it can say is 65,535, and 0xFFFF is reserved to mean “check the Zip64 record for the real number”. The merge wrote the count like this:
eocd += le16(totalEntries) // truncates to the low 16 bitsAt 65,536 entries, the low 16 bits are 0. Nothing crashed or threw an error, and the parallel path didn’t fall back to anything. The result was a ZIP whose directory held 65,536 entries and whose EOCD said there were none.
The parallel path already declined inputs near 4 GiB, so it would never need Zip64 offsets. But that check was on bytes, not entries. A folder of 70,000 small files is well under 4 GiB and passed it.
Why “silent” is the right word
Whether a wrong count matters depends on the reader. To see how readers handle
it, I zeroed the count in a small, otherwise valid ZIP. Python’s zipfile,
bsdtar, and ditto all ignored the count and listed every file. Info-ZIP’s
unzip listed them and then failed:
error: expected central file header signature not found (file #4).That combination makes a bug like this hard to catch. Your own tools might read the file fine, and someone else’s tools fail on it.
The fix: decline, and refuse to wrap
There are two parts:
- An entry-count gate. The parallel path now declines anything over 65,000 entries, counting files, folders, and symlinks, since all of them go in the directory. Those archives go to libarchive’s sequential ZIP writer, which writes a proper Zip64 record. 65,000 leaves a margin below the limit.
- A guard in the merge itself. If the count would ever exceed 0xFFFE, the merge throws, and doesn’t write a value that wraps. The gate should make this unreachable, but the merge no longer trusts that.
The regression test creates 65,536 empty files, zips them through the same code path the app uses, and checks two things: the classic EOCD count is the 0xFFFF Zip64 marker, not a wrapped number, and Purrchiver’s reader gets all 65,536 entries back.
The takeaway: any time you write a fixed-width field by hand, check the value against the field’s maximum, and throw if it doesn’t fit. Converting to a narrower type that silently truncates is how a perfectly ordinary folder turns into a damaged archive.