function zip_ct_init()

in themes/docsy/static/js/deflate.js [840:927]


    function zip_ct_init() {
        var n;	// iterates over tree elements
        var bits;	// bit counter
        var length;	// length value
        var code;	// code value
        var dist;	// distance index

        if (zip_static_dtree[0].dl != 0) return; // ct_init already called

        zip_l_desc.dyn_tree = zip_dyn_ltree;
        zip_l_desc.static_tree = zip_static_ltree;
        zip_l_desc.extra_bits = zip_extra_lbits;
        zip_l_desc.extra_base = zip_LITERALS + 1;
        zip_l_desc.elems = zip_L_CODES;
        zip_l_desc.max_length = zip_MAX_BITS;
        zip_l_desc.max_code = 0;

        zip_d_desc.dyn_tree = zip_dyn_dtree;
        zip_d_desc.static_tree = zip_static_dtree;
        zip_d_desc.extra_bits = zip_extra_dbits;
        zip_d_desc.extra_base = 0;
        zip_d_desc.elems = zip_D_CODES;
        zip_d_desc.max_length = zip_MAX_BITS;
        zip_d_desc.max_code = 0;

        zip_bl_desc.dyn_tree = zip_bl_tree;
        zip_bl_desc.static_tree = null;
        zip_bl_desc.extra_bits = zip_extra_blbits;
        zip_bl_desc.extra_base = 0;
        zip_bl_desc.elems = zip_BL_CODES;
        zip_bl_desc.max_length = zip_MAX_BL_BITS;
        zip_bl_desc.max_code = 0;

        // Initialize the mapping length (0..255) -> length code (0..28)
        length = 0;
        for (code = 0; code < zip_LENGTH_CODES - 1; code++) {
            zip_base_length[code] = length;
            for (n = 0; n < (1 << zip_extra_lbits[code]); n++)
                zip_length_code[length++] = code;
        }
        // Assert (length == 256, "ct_init: length != 256");

        /* Note that the length 255 (match length 258) can be represented
            * in two different ways: code 284 + 5 bits or code 285, so we
            * overwrite length_code[255] to use the best encoding:
            */
        zip_length_code[length - 1] = code;

        /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
        dist = 0;
        for (code = 0; code < 16; code++) {
            zip_base_dist[code] = dist;
            for (n = 0; n < (1 << zip_extra_dbits[code]); n++) {
                zip_dist_code[dist++] = code;
            }
        }
        // Assert (dist == 256, "ct_init: dist != 256");
        dist >>= 7; // from now on, all distances are divided by 128
        for (; code < zip_D_CODES; code++) {
            zip_base_dist[code] = dist << 7;
            for (n = 0; n < (1 << (zip_extra_dbits[code] - 7)); n++)
                zip_dist_code[256 + dist++] = code;
        }
        // Assert (dist == 256, "ct_init: 256+dist != 512");

        // Construct the codes of the static literal tree
        for (bits = 0; bits <= zip_MAX_BITS; bits++)
            zip_bl_count[bits] = 0;
        n = 0;
        while (n <= 143) { zip_static_ltree[n++].dl = 8; zip_bl_count[8]++; }
        while (n <= 255) { zip_static_ltree[n++].dl = 9; zip_bl_count[9]++; }
        while (n <= 279) { zip_static_ltree[n++].dl = 7; zip_bl_count[7]++; }
        while (n <= 287) { zip_static_ltree[n++].dl = 8; zip_bl_count[8]++; }
        /* Codes 286 and 287 do not exist, but we must include them in the
            * tree construction to get a canonical Huffman tree (longest code
            * all ones)
            */
        zip_gen_codes(zip_static_ltree, zip_L_CODES + 1);

        /* The static distance tree is trivial: */
        for (n = 0; n < zip_D_CODES; n++) {
            zip_static_dtree[n].dl = 5;
            zip_static_dtree[n].fc = zip_bi_reverse(n, 5);
        }

        // Initialize the first block of the first file:
        zip_init_block();
    }