📄 zip.cpp
字号:
*/
do {
pqremove(tree, n); /* n = node of least frequency */
m = state.ts.heap[SMALLEST]; /* m = node of next least frequency */
state.ts.heap[--state.ts.heap_max] = n; /* keep the nodes sorted by frequency */
state.ts.heap[--state.ts.heap_max] = m;
/* Create a new node father of n and m */
tree[node].fc.freq = (ush)(tree[n].fc.freq + tree[m].fc.freq);
state.ts.depth[node] = (uch) (Max(state.ts.depth[n], state.ts.depth[m]) + 1);
tree[n].dl.dad = tree[m].dl.dad = (ush)node;
/* and insert the new node in the heap */
state.ts.heap[SMALLEST] = node++;
pqdownheap(state,tree, SMALLEST);
} while (state.ts.heap_len >= 2);
state.ts.heap[--state.ts.heap_max] = state.ts.heap[SMALLEST];
/* At this point, the fields freq and dad are set. We can now
* generate the bit lengths.
*/
gen_bitlen(state,(tree_desc *)desc);
/* The field len is now set, we can generate the bit codes */
gen_codes (state,(ct_data *)tree, max_code);
}
/* ===========================================================================
* Scan a literal or distance tree to determine the frequencies of the codes
* in the bit length tree. Updates opt_len to take into account the repeat
* counts. (The contribution of the bit length codes will be added later
* during the construction of bl_tree.)
*/
void scan_tree (TState &state,ct_data *tree, int max_code)
{
int n; /* iterates over all tree elements */
int prevlen = -1; /* last emitted length */
int curlen; /* length of current code */
int nextlen = tree[0].dl.len; /* length of next code */
int count = 0; /* repeat count of the current code */
int max_count = 7; /* max repeat count */
int min_count = 4; /* min repeat count */
if (nextlen == 0) max_count = 138, min_count = 3;
tree[max_code+1].dl.len = (ush)-1; /* guard */
for (n = 0; n <= max_code; n++) {
curlen = nextlen; nextlen = tree[n+1].dl.len;
if (++count < max_count && curlen == nextlen) {
continue;
} else if (count < min_count) {
state.ts.bl_tree[curlen].fc.freq = (ush)(state.ts.bl_tree[curlen].fc.freq + count);
} else if (curlen != 0) {
if (curlen != prevlen) state.ts.bl_tree[curlen].fc.freq++;
state.ts.bl_tree[REP_3_6].fc.freq++;
} else if (count <= 10) {
state.ts.bl_tree[REPZ_3_10].fc.freq++;
} else {
state.ts.bl_tree[REPZ_11_138].fc.freq++;
}
count = 0; prevlen = curlen;
if (nextlen == 0) {
max_count = 138, min_count = 3;
} else if (curlen == nextlen) {
max_count = 6, min_count = 3;
} else {
max_count = 7, min_count = 4;
}
}
}
/* ===========================================================================
* Send a literal or distance tree in compressed form, using the codes in
* bl_tree.
*/
void send_tree (TState &state, ct_data *tree, int max_code)
{
int n; /* iterates over all tree elements */
int prevlen = -1; /* last emitted length */
int curlen; /* length of current code */
int nextlen = tree[0].dl.len; /* length of next code */
int count = 0; /* repeat count of the current code */
int max_count = 7; /* max repeat count */
int min_count = 4; /* min repeat count */
/* tree[max_code+1].dl.len = -1; */ /* guard already set */
if (nextlen == 0) max_count = 138, min_count = 3;
for (n = 0; n <= max_code; n++) {
curlen = nextlen; nextlen = tree[n+1].dl.len;
if (++count < max_count && curlen == nextlen) {
continue;
} else if (count < min_count) {
do { send_code(state, curlen, state.ts.bl_tree); } while (--count != 0);
} else if (curlen != 0) {
if (curlen != prevlen) {
send_code(state, curlen, state.ts.bl_tree); count--;
}
Assert(state,count >= 3 && count <= 6, " 3_6?");
send_code(state,REP_3_6, state.ts.bl_tree); send_bits(state,count-3, 2);
} else if (count <= 10) {
send_code(state,REPZ_3_10, state.ts.bl_tree); send_bits(state,count-3, 3);
} else {
send_code(state,REPZ_11_138, state.ts.bl_tree); send_bits(state,count-11, 7);
}
count = 0; prevlen = curlen;
if (nextlen == 0) {
max_count = 138, min_count = 3;
} else if (curlen == nextlen) {
max_count = 6, min_count = 3;
} else {
max_count = 7, min_count = 4;
}
}
}
/* ===========================================================================
* Construct the Huffman tree for the bit lengths and return the index in
* bl_order of the last bit length code to send.
*/
int build_bl_tree(TState &state)
{
int max_blindex; /* index of last bit length code of non zero freq */
/* Determine the bit length frequencies for literal and distance trees */
scan_tree(state,(ct_data *)state.ts.dyn_ltree, state.ts.l_desc.max_code);
scan_tree(state,(ct_data *)state.ts.dyn_dtree, state.ts.d_desc.max_code);
/* Build the bit length tree: */
build_tree(state,(tree_desc *)(&state.ts.bl_desc));
/* opt_len now includes the length of the tree representations, except
* the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
*/
/* Determine the number of bit length codes to send. The pkzip format
* requires that at least 4 bit length codes be sent. (appnote.txt says
* 3 but the actual value used is 4.)
*/
for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
if (state.ts.bl_tree[bl_order[max_blindex]].dl.len != 0) break;
}
/* Update opt_len to include the bit length tree and counts */
state.ts.opt_len += 3*(max_blindex+1) + 5+5+4;
Trace("\ndyn trees: dyn %ld, stat %ld", state.ts.opt_len, state.ts.static_len);
return max_blindex;
}
/* ===========================================================================
* Send the header for a block using dynamic Huffman trees: the counts, the
* lengths of the bit length codes, the literal tree and the distance tree.
* IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
*/
void send_all_trees(TState &state,int lcodes, int dcodes, int blcodes)
{
int rank; /* index in bl_order */
Assert(state,lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
Assert(state,lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
"too many codes");
Trace("\nbl counts: ");
send_bits(state,lcodes-257, 5);
/* not +255 as stated in appnote.txt 1.93a or -256 in 2.04c */
send_bits(state,dcodes-1, 5);
send_bits(state,blcodes-4, 4); /* not -3 as stated in appnote.txt */
for (rank = 0; rank < blcodes; rank++) {
Trace("\nbl code %2d ", bl_order[rank]);
send_bits(state,state.ts.bl_tree[bl_order[rank]].dl.len, 3);
}
Trace("\nbl tree: sent %ld", state.bs.bits_sent);
send_tree(state,(ct_data *)state.ts.dyn_ltree, lcodes-1); /* send the literal tree */
Trace("\nlit tree: sent %ld", state.bs.bits_sent);
send_tree(state,(ct_data *)state.ts.dyn_dtree, dcodes-1); /* send the distance tree */
Trace("\ndist tree: sent %ld", state.bs.bits_sent);
}
/* ===========================================================================
* Determine the best encoding for the current block: dynamic trees, static
* trees or store, and output the encoded block to the zip file. This function
* returns the total compressed length (in bytes) for the file so far.
*/
ulg flush_block(TState &state,char *buf, ulg stored_len, int eof)
{
ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
int max_blindex; /* index of last bit length code of non zero freq */
state.ts.flag_buf[state.ts.last_flags] = state.ts.flags; /* Save the flags for the last 8 items */
/* Check if the file is ascii or binary */
if (*state.ts.file_type == (ush)UNKNOWN) set_file_type(state);
/* Construct the literal and distance trees */
build_tree(state,(tree_desc *)(&state.ts.l_desc));
Trace("\nlit data: dyn %ld, stat %ld", state.ts.opt_len, state.ts.static_len);
build_tree(state,(tree_desc *)(&state.ts.d_desc));
Trace("\ndist data: dyn %ld, stat %ld", state.ts.opt_len, state.ts.static_len);
/* At this point, opt_len and static_len are the total bit lengths of
* the compressed block data, excluding the tree representations.
*/
/* Build the bit length tree for the above two trees, and get the index
* in bl_order of the last bit length code to send.
*/
max_blindex = build_bl_tree(state);
/* Determine the best encoding. Compute first the block length in bytes */
opt_lenb = (state.ts.opt_len+3+7)>>3;
static_lenb = (state.ts.static_len+3+7)>>3;
state.ts.input_len += stored_len; /* for debugging only */
Trace("\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
opt_lenb, state.ts.opt_len, static_lenb, state.ts.static_len, stored_len,
state.ts.last_lit, state.ts.last_dist);
if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
// Originally, zip allowed the file to be transformed from a compressed
// into a stored file in the case where compression failed, there
// was only one block, and it was allowed to change. I've removed this
// possibility since the code's cleaner if no changes are allowed.
//if (stored_len <= opt_lenb && eof && state.ts.cmpr_bytelen == 0L
// && state.ts.cmpr_len_bits == 0L && state.seekable)
//{ // && state.ts.file_method != NULL
// // Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there:
// Assert(state,buf!=NULL,"block vanished");
// copy_block(state,buf, (unsigned)stored_len, 0); // without header
// state.ts.cmpr_bytelen = stored_len;
// Assert(state,false,"unimplemented *state.ts.file_method = STORE;");
// //*state.ts.file_method = STORE;
//}
//else
if (stored_len+4 <= opt_lenb && buf != (char*)NULL) {
/* 4: two words for the lengths */
/* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
* Otherwise we can't have processed more than WSIZE input bytes since
* the last block flush, because compression would have been
* successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
* transform a block into a stored block.
*/
send_bits(state,(STORED_BLOCK<<1)+eof, 3); /* send block type */
state.ts.cmpr_bytelen += ((state.ts.cmpr_len_bits + 3 + 7) >> 3) + stored_len + 4;
state.ts.cmpr_len_bits = 0L;
copy_block(state,buf, (unsigned)stored_len, 1); /* with header */
}
else if (static_lenb == opt_lenb) {
send_bits(state,(STATIC_TREES<<1)+eof, 3);
compress_block(state,(ct_data *)state.ts.static_ltree, (ct_data *)state.ts.static_dtree);
state.ts.cmpr_len_bits += 3 + state.ts.static_len;
state.ts.cmpr_bytelen += state.ts.cmpr_len_bits >> 3;
state.ts.cmpr_len_bits &= 7L;
}
else {
send_bits(state,(DYN_TREES<<1)+eof, 3);
send_all_trees(state,state.ts.l_desc.max_code+1, state.ts.d_desc.max_code+1, max_blindex+1);
compress_block(state,(ct_data *)state.ts.dyn_ltree, (ct_data *)state.ts.dyn_dtree);
state.ts.cmpr_len_bits += 3 + state.ts.opt_len;
state.ts.cmpr_bytelen += state.ts.cmpr_len_bits >> 3;
state.ts.cmpr_len_bits &= 7L;
}
Assert(state,((state.ts.cmpr_bytelen << 3) + state.ts.cmpr_len_bits) == state.bs.bits_sent, "bad compressed size");
init_block(state);
if (eof) {
// Assert(state,input_len == isize, "bad input size");
bi_windup(state);
state.ts.cmpr_len_bits += 7; /* align on byte boundary */
}
Trace("\n");
return state.ts.cmpr_bytelen + (state.ts.cmpr_len_bits >> 3);
}
/* ===========================================================================
* Save the match info and tally the frequency counts. Return true if
* the current block must be flushed.
*/
int ct_tally (TState &state,int dist, int lc)
{
state.ts.l_buf[state.ts.last_lit++] = (uch)lc;
if (dist == 0) {
/* lc is the unmatched char */
state.ts.dyn_ltree[lc].fc.freq++;
} else {
/* Here, lc is the match length - MIN_MATCH */
dist--; /* dist = match distance - 1 */
Assert(state,(ush)dist < (ush)MAX_DIST &&
(ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
(ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match");
state.ts.dyn_ltree[state.ts.length_code[lc]+LITERALS+1].fc.freq++;
state.ts.dyn_dtree[d_code(dist)].fc.freq++;
state.ts.d_buf[state.ts.last_dist++] = (ush)dist;
state.ts.flags |= state.ts.flag_bit;
}
state.ts.flag_bit <<= 1;
/* Output the flags if they fill a byte: */
if ((state.ts.last_lit & 7) == 0) {
state.ts.flag_buf[state.ts.last_flags++] = state.ts.flags;
state.ts.flags = 0, state.ts.flag_bit = 1;
}
/* Try to guess if it is profitable to stop the current block here */
if (state.level > 2 && (state.ts.last_lit & 0xfff) == 0) {
/* Compute an upper bound for the compressed length */
ulg out_length = (ulg)state.ts.last_lit*8L;
ulg in_length = (ulg)state.ds.strstart-state.ds.block_start;
int dcode;
for (dcode = 0; dcode < D_CODES; dcode++) {
out_length += (ulg)state.ts.dyn_dtree[dcode].fc.freq*(5L+extra_dbits[dcode]);
}
out_length >>= 3;
Trace("\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
state.ts.last_lit, state.ts.last_dist, in_length, out_length,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -