/* =========================================================================== * Generate the codes for a given tree and bit counts (which need not be * optimal). * IN assertion: the array bl_count contains the bit length statistics for * the given tree and the field len is set for all tree elements. * OUT assertion: the field code is set for all tree elements of non * zero code length. */ void gen_codes (ct_data near *tree, int max_code) //ct_data near *tree; /* the tree to decorate */ //int max_code; /* largest code with non zero frequency */ { u16 next_code[MAX_BITS+1]; /* next code value for each bit length */ u16 code = 0; /* running code value */ int bits; /* bit index */ int n; /* code index */ /* The distribution counts are first used to generate the code values * without bit reversal. */ for (bits = 1; bits <= MAX_BITS; bits++) { next_code[bits] = code = (u16)((code + bl_count[bits-1]) << 1); } /* Check that the bit counts in bl_count are consistent. The last code * must be all ones. */ Assert(code + bl_count[MAX_BITS]-1 == (1<< ((u16) MAX_BITS)) - 1);//inconsistent bit counts for (n = 0; n <= max_code; n++) { int len = tree[n].Len; if (len == 0) continue; /* Now reverse the bits */ tree[n].Code = (u16)bi_reverse(next_code[len]++, len); } }
/* =========================================================================== * Generate the codes for a given tree and bit counts (which need not be * optimal). * IN assertion: the array bl_count contains the bit length statistics for * the given tree and the field len is set for all tree elements. * OUT assertion: the field code is set for all tree elements of non * zero code length. */ void gen_codes (TState &state, ct_data *tree, int max_code) { ush next_code[MAX_BITS+1]; /* next code value for each bit length */ ush code = 0; /* running code value */ int bits; /* bit index */ int n; /* code index */ /* The distribution counts are first used to generate the code values * without bit reversal. */ for (bits = 1; bits <= MAX_BITS; bits++) { next_code[bits] = code = (ush)((code + state.ts.bl_count[bits-1]) << 1); } /* Check that the bit counts in bl_count are consistent. The last code * must be all ones. */ Assert(state,code + state.ts.bl_count[MAX_BITS]-1 == (1<< ((ush) MAX_BITS)) - 1, "inconsistent bit counts"); Trace("\ngen_codes: max_code %d ", max_code); for (n = 0; n <= max_code; n++) { int len = tree[n].dl.len; if (len == 0) continue; /* Now reverse the bits */ tree[n].fc.code = (ush)bi_reverse(next_code[len]++, len); //Tracec(tree != state.ts.static_ltree, "\nn %3d %c l %2d c %4x (%x) ", n, (isgraph(n) ? n : ' '), len, tree[n].fc.code, next_code[len]-1); } }
void gen_codes(compression_state_t *s, tree_desc_t *td) { int bits, code = 0, n; int next_code[MAX_BITS+1]; int *bl_count = s->bl_count; tree_element_t *tree = td->tree; for (bits = 1; bits <= MAX_BITS; ++bits) { next_code[bits] = code = (code + bl_count[bits - 1]) << 1; } for (n = 0; n <= td->max_code; ++n) { int len = tree[n].Len; if (len == 0) continue; tree[n].Code = bi_reverse(next_code[len]++, len); } }
static void gen_codes( ct_data* tree, int max_code, unsigned short* bl_count ) { unsigned short next_code[ MAX_BITS + 1 ]; unsigned short code = 0; for( int bits = 1 ; bits <= MAX_BITS ; bits++ ) next_code[ bits ] = code = ( code + bl_count[ bits - 1 ] ) << 1; for( int n = 0 ; n <= max_code ; n++ ) { int len = tree[ n ].Len; if( !len ) continue; tree[ n ].Code = bi_reverse( next_code[ len ]++, len ); } }
/* =========================================================================== * Allocate the match buffer, initialize the various tables and save the * location of the internal file attribute (ascii/binary) and method * (DEFLATE/STORE). */ void ct_init(int* method) //ush *attr; /* pointer to internal file attribute */ //int *method; /* pointer to compression method */ { int n; /* iterates over tree elements */ int bits; /* bit counter */ int length; /* length value */ int code; /* code value */ int dist; /* distance index */ file_method = method; cmpr_bytelen = cmpr_len_bits = 0L; #ifdef _DEBUG input_len = 0L; #endif if (static_dtree[0].Len != 0) return; /* ct_init already called */ /* Initialize the mapping length (0..255) -> length code (0..28) */ length = 0; for (code = 0; code < LENGTH_CODES-1; code++) { base_length[code] = length; for (n = 0; n < (1<<extra_lbits[code]); n++) { length_code[length++] = (u8)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: */ length_code[length-1] = (u8)code; /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ dist = 0; for (code = 0 ; code < 16; code++) { base_dist[code] = dist; for (n = 0; n < (1<<extra_dbits[code]); n++) { dist_code[dist++] = (u8)code; } } Assert(dist == 256);//, "ct_init: dist != 256"); dist >>= 7; /* from now on, all distances are divided by 128 */ for ( ; code < D_CODES; code++) { base_dist[code] = dist << 7; for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { dist_code[256 + dist++] = (u8)code; } } Assert(dist == 256);//, "ct_init: 256+dist != 512"); /* Construct the codes of the static literal tree */ for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; n = 0; while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; while (n <= 287) static_ltree[n++].Len = 8, 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) */ gen_codes((ct_data near *)static_ltree, L_CODES+1); /* The static distance tree is trivial: */ for (n = 0; n < D_CODES; n++) { static_dtree[n].Len = 5; static_dtree[n].Code = (u16)bi_reverse(n, 5); } /* Initialize the first block of the first file: */ init_block(); }