def decode_huffman_data(bits):
    """Decode the litlen/dist huffman trees for a dynamic huffman block."""
    HLIT = bits.read(5)
    HDIST = bits.read(5)
    HCLEN = bits.read(4)
    assert HLIT + 257 <= 286
    assert HDIST + 1 <= 32
    assert HCLEN + 4 <= 32
    # Read the huffman tree for the huffman tree data.
    codelen_codelens = [0] * len(CODELEN_ALPHABET)
    for i in xrange(HCLEN + 4):
        codelen_codelens[CODELEN_ALPHABET[i]] = bits.read(3)
    h_codelen = HuffmanDecoder(codelen_codelens)
    # Read all the codelengths for litlen and dist trees.
    # By spec, we have to read them as a single sequence.
    codelens = []
    while len(codelens) < HLIT + HDIST + 258:
        codelen = h_codelen.decode(bits)
        if codelen <= 15:
            codelens.append(codelen)
        elif codelen == 16:
            num_copies = bits.read(2) + 3
            codelens += [codelens[-1]] * num_copies
        elif codelen == 17:
            num_zeros = bits.read(3) + 3
            codelens += [0] * num_zeros
        else:
            assert codelen == 18
            num_zeros = bits.read(7) + 11
            codelens += [0] * num_zeros
    # Check that we read precisely the right number of codelens.
    assert len(codelens) == HLIT + HDIST + 258
    # Now we can make a pair of decoders.
    return (
        HuffmanDecoder(codelens[:HLIT + 257]),
        HuffmanDecoder(codelens[HLIT + 257:])
    )
Decode the litlen/dist huffman trees for a dynamic huffman block.
tools/compress_memory_initializer.py