Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Something must be in the air. I've been working on a gzip/deflate visualizer recently as well: https://jonjohnsonjr.github.io/deflate/

This is very work in progress, but for folks looking for a deeper explanation of how dynamic blocks are encoded, this is my attempt to visualize them.

(This all happens locally with way too much wasm, so attempting to upload a large gzip file will likely crash the tab.)

tl;dr for btype 2 blocks:

3 bit block header.

Three values telling you how many extra (above the minimum number) symbols are in each tree: HLIT, HDIST, and HCLEN.

First, we read (HCLEN + 4) * 3 bits.

These are the bit counts for symbols 0-18 in the code length tree, which gives you the bit patterns for a little mini-language used to compactly encode the literal/length and distance trees. 0-15 are literal bit lengths (0 meaning it's omitted). 16 repeats the previous symbol 3-6 times. 17 and 18 encode short (3-10) and long (11-138) runs of zeroes, which is useful for encoding blocks with sparse alphabets.

These bits counts are in a seemingly strange order that tries to push less-likely bit counts towards the end of the list so it can be truncated.

Knowing all the bit lengths for values in this alphabet allows you to reconstruct a huffman tree (thanks to canonical huffman codes) and decode the bit patterns for these code length codes.

That's followed by a bitstream that you decode to get the bit counts for the literal/length and distance trees. HLIT and HDIST (from earlier) tell you how many of these to expect.

Again, you can reconstruct these trees using just the bit lengths thanks to canonical huffman codes, which gives you the bit patterns for the data bitstream.

Then you just decode the rest of the bitstream (using LZSS) until you hit 256, the end of block (EOB).

If you're not already familiar with deflate, don't be discouraged if none of that made any sense. Bill Bird has an excellent (long) lecture that I recommend to everyone: https://www.youtube.com/watch?v=SJPvNi4HrWQ



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: