/* =========================================================================== * 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. */ u32 flush_block(char *buf, u32 stored_len, int eof) //char *buf; /* input block, or NULL if too old */ //u32 stored_len; /* length of input block */ //int eof; /* true if this is the last block for a file */ { u32 opt_lenb, static_lenb; /* opt_len and static_len in bytes */ int max_blindex; /* index of last bit length code of non zero freq */ flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */ /* Construct the literal and distance trees */ build_tree((tree_desc near *)(&l_desc)); build_tree((tree_desc near *)(&d_desc)); /* 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(); /* Determine the best encoding. Compute first the block length in bytes */ opt_lenb = (opt_len+3+7)>>3; static_lenb = (static_len+3+7)>>3; #ifdef _DEBUG input_len += stored_len; /* for debugging only */ #endif if (static_lenb <= opt_lenb) opt_lenb = static_lenb; /* If compression failed and this is the first and last block, * the whole file is transformed into a stored file: */ if (stored_len <= opt_lenb && eof && file_method != NULL && cmpr_bytelen == 0L && cmpr_len_bits == 0L) { /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */ if (buf == NULL) error ("block vanished"); copy_block(buf, (unsigned)stored_len, 0); /* without header */ cmpr_bytelen = stored_len; *file_method = compStore; } 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((STORED_BLOCK<<1)+eof, 3); /* send block type */ cmpr_bytelen += ((cmpr_len_bits + 3 + 7) >> 3) + stored_len + 4; cmpr_len_bits = 0L; copy_block(buf, (unsigned)stored_len, 1); /* with header */ } else if (static_lenb == opt_lenb) {
/* =========================================================================== * 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;
/* =========================================================================== * 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 for the file so far. */ word32 CodeTree::flush_block(byte *buf, word32 stored_len, int eof) { word32 opt_lenb, static_lenb; /* opt_len and static_len in bytes */ int max_blindex; /* index of last bit length code of non zero freq */ flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */ /* Construct the literal and distance trees */ build_tree(&l_desc); // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len)); build_tree(&d_desc); // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, 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(); /* Determine the best encoding. Compute first the block length in bytes */ opt_lenb = (opt_len+3+7)>>3; static_lenb = (static_len+3+7)>>3; input_len += stored_len; /* for debugging only */ // Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ", // opt_lenb, opt_len, static_lenb, static_len, stored_len, // last_lit, last_dist)); if (static_lenb <= opt_lenb) opt_lenb = static_lenb; #ifdef FORCE_METHOD if (level == 2 && buf) /* force stored block */ #else if (stored_len+4 <= opt_lenb && buf) /* 4: two words for the lengths */ #endif { /* 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 block type */ send_bits((STORED_BLOCK<<1)+eof, 3); compressed_len = (compressed_len + 3 + 7) & ~7L; compressed_len += (stored_len + 4) << 3; /* with header */ copy_block(buf, (unsigned)stored_len, 1); } #ifdef FORCE_METHOD else if (level == 3) /* force static trees */ #else else if (static_lenb == opt_lenb) #endif { send_bits((STATIC_TREES<<1)+eof, 3); compress_block(static_ltree,static_dtree); compressed_len += 3 + static_len; } else { send_bits((DYN_TREES<<1)+eof, 3); send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1); compress_block(dyn_ltree,dyn_dtree); compressed_len += 3 + opt_len; } // assert (compressed_len == bits_sent); init_block(); if (eof) { // assert (input_len == isize); bi_windup(); compressed_len += 7; /* align on byte boundary */ } // Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3, // compressed_len-7*eof)); return compressed_len >> 3; }