Now the two big ideas meet. Today you Huffman-code both symbol streams, write their code tables into the container, and pack the codes - producing a real DEFLATE-lite compressed block under method 1.
Encode data as a DEFLATE-lite block: two Huffman tables plus the packed literal/length and distance codes.
This lesson is where LZ77 and Huffman finally combine. The steps chain everything
built so far: tokenize the input into literal/length and distance streams,
build a Huffman code table for each stream (frequencies, lengths, canonical
codes - the whole chapter-three pipeline, run twice), then write the container
header with method 0x01, both code tables in the wide format, and finally
the packed codes.
The packing order is the contract the decoder relies on: walk the literal/length
symbols writing each one’s code, and each time you emit a length symbol,
immediately write the code for the corresponding distance from the distance
stream. That interleaving is what lets the decoder recover matches in step. For
ABCABCD the block begins 0x5A 0x5A 0x01 with original length 0x00000007, then
the two tables, then the bits. This is a genuine two-stage compressor - repeats
found by LZSS, then the resulting symbols entropy-coded by Huffman - and the
payoff, decoding it back, is the very next lesson.
// 1. tokenize -> litlen[] and dist[] symbol streams (lesson 26)// 2. build Huffman lengths + canonical codes for EACH stream (chapter 3)// 3. write header (method 0x01, uint32 origLen)// 4. write litlen table, then dist table (wide format, lesson 27)// 5. pack: for each litlen symbol write its code; after a length symbol,// write the matching distance symbol's code