199767f8919635c4928607450d9e0abb932109ceToomas Soome/* trees.c -- output deflated data using Huffman coding
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 1995-2012 Jean-loup Gailly
199767f8919635c4928607450d9e0abb932109ceToomas Soome * detect_data_type() function provided freely by Cosmin Truta, 2006
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ALGORITHM
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The "deflation" process uses several Huffman trees. The more
199767f8919635c4928607450d9e0abb932109ceToomas Soome * common source values are represented by shorter bit sequences.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Each code tree is stored in a compressed form which is itself
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a Huffman encoding of the lengths of all the code strings (in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ascending order by source values). The actual code strings are
199767f8919635c4928607450d9e0abb932109ceToomas Soome * reconstructed from the lengths in the inflate process, as described
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in the deflate specification.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * REFERENCES
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Storer, James A.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Data Compression: Methods and Theory, pp. 49-50.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Computer Science Press, 1988. ISBN 0-7167-8156-5.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sedgewick, R.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Algorithms, p290.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Addison-Wesley, 1983. ISBN 0-201-06672-6.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* @(#) $Id$ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* #define GEN_TREES_H */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "deflate.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <ctype.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Constants
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAX_BL_BITS 7
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Bit length codes must not exceed MAX_BL_BITS bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define END_BLOCK 256
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* end of block literal code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define REP_3_6 16
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* repeat previous bit length 3-6 times (2 bits of repeat count) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define REPZ_3_10 17
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* repeat a zero length 3-10 times (3 bits of repeat count) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define REPZ_11_138 18
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* repeat a zero length 11-138 times (7 bits of repeat count) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal const int extra_dbits[D_CODES] /* extra bits for each distance code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal const uch bl_order[BL_CODES]
199767f8919635c4928607450d9e0abb932109ceToomas Soome = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* The lengths of the bit length codes are sent in order of decreasing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * probability, to avoid transmitting the lengths for unused bit length codes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Local data. These are initialized only once.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(GEN_TREES_H) || !defined(STDC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* non ANSI compilers may not accept trees.h */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal ct_data static_ltree[L_CODES+2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* The static literal tree. Since the bit lengths are imposed, there is no
199767f8919635c4928607450d9e0abb932109ceToomas Soome * need for the L_CODES extra codes used during heap construction. However
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
199767f8919635c4928607450d9e0abb932109ceToomas Soome * below).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal ct_data static_dtree[D_CODES];
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* The static distance tree. (Actually a trivial tree since all codes use
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 5 bits.)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuch _dist_code[DIST_CODE_LEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Distance codes. The first 256 values correspond to the distances
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3 .. 258, the last 256 values correspond to the top 8 bits of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the 15 bit distances.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuch _length_code[MAX_MATCH-MIN_MATCH+1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* length code for each normalized match length (0 == MIN_MATCH) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal int base_length[LENGTH_CODES];
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* First normalized length for each code (0 = MIN_MATCH) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal int base_dist[D_CODES];
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* First normalized distance for each code (0 = distance of 1) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include "trees.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* GEN_TREES_H */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct static_tree_desc_s {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const ct_data *static_tree; /* static tree or NULL */
199767f8919635c4928607450d9e0abb932109ceToomas Soome const intf *extra_bits; /* extra bits for each code or NULL */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int extra_base; /* base index for extra_bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int elems; /* max number of elements in the tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_length; /* max bit length for the codes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal static_tree_desc static_l_desc =
199767f8919635c4928607450d9e0abb932109ceToomas Soome{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal static_tree_desc static_d_desc =
199767f8919635c4928607450d9e0abb932109ceToomas Soome{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal static_tree_desc static_bl_desc =
199767f8919635c4928607450d9e0abb932109ceToomas Soome{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Local (static) routines in this file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void tr_static_init OF((void));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void init_block OF((deflate_state *s));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void gen_bitlen OF((deflate_state *s, tree_desc *desc));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void build_tree OF((deflate_state *s, tree_desc *desc));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal int build_bl_tree OF((deflate_state *s));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
199767f8919635c4928607450d9e0abb932109ceToomas Soome int blcodes));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void compress_block OF((deflate_state *s, const ct_data *ltree,
199767f8919635c4928607450d9e0abb932109ceToomas Soome const ct_data *dtree));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal int detect_data_type OF((deflate_state *s));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal unsigned bi_reverse OF((unsigned value, int length));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void bi_windup OF((deflate_state *s));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void bi_flush OF((deflate_state *s));
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void copy_block OF((deflate_state *s, charf *buf, unsigned len,
199767f8919635c4928607450d9e0abb932109ceToomas Soome int header));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef GEN_TREES_H
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void gen_trees_header OF((void));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Send a code of the given tree. c and tree must not have side effects */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* DEBUG */
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define send_code(s, c, tree) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, tree[c].Code, tree[c].Len); }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Output a short LSB first on the stream.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: there is enough room in pendingBuf.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define put_short(s, w) { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_byte(s, (uch)((w) & 0xff)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_byte(s, (uch)((ush)(w) >> 8)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Send a value on a given number of bits.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: length <= 16 and value fits in length bits.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void send_bits OF((deflate_state *s, int value, int length));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void send_bits(s, value, length)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int value; /* value to send */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int length; /* number of bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracevv((stderr," l %2d v %4x ", length, value));
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert(length > 0 && length <= 15, "invalid length");
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bits_sent += (ulg)length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If not enough room in bi_buf, use (valid) bits from bi_buf and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
199767f8919635c4928607450d9e0abb932109ceToomas Soome * unused bits in value.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->bi_valid > (int)Buf_size - length) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf |= (ush)value << s->bi_valid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_short(s, s->bi_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid += length - Buf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf |= (ush)value << s->bi_valid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid += length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* !DEBUG */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define send_bits(s, value, length) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome{ int len = length;\
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->bi_valid > (int)Buf_size - len) {\
199767f8919635c4928607450d9e0abb932109ceToomas Soome int val = value;\
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf |= (ush)val << s->bi_valid;\
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_short(s, s->bi_buf);\
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid += len - Buf_size;\
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {\
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf |= (ush)(value) << s->bi_valid;\
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid += len;\
199767f8919635c4928607450d9e0abb932109ceToomas Soome }\
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* DEBUG */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* the arguments must not have side effects */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initialize the various 'constant' tables.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void tr_static_init()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(GEN_TREES_H) || !defined(STDC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome static int static_init_done = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n; /* iterates over tree elements */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bits; /* bit counter */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int length; /* length value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int code; /* code value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int dist; /* distance index */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ush bl_count[MAX_BITS+1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* number of codes at each bit length for an optimal tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (static_init_done) return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* For some embedded targets, global variables are not initialized: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NO_INIT_GLOBAL_POINTERS
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_l_desc.static_tree = static_ltree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_l_desc.extra_bits = extra_lbits;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_d_desc.static_tree = static_dtree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_d_desc.extra_bits = extra_dbits;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_bl_desc.extra_bits = extra_blbits;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialize the mapping length (0..255) -> length code (0..28) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome length = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (code = 0; code < LENGTH_CODES-1; code++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome base_length[code] = length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < (1<<extra_lbits[code]); n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome _length_code[length++] = (uch)code;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (length == 256, "tr_static_init: length != 256");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Note that the length 255 (match length 258) can be represented
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in two different ways: code 284 + 5 bits or code 285, so we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * overwrite length_code[255] to use the best encoding:
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome _length_code[length-1] = (uch)code;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dist = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (code = 0 ; code < 16; code++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome base_dist[code] = dist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < (1<<extra_dbits[code]); n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome _dist_code[dist++] = (uch)code;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (dist == 256, "tr_static_init: dist != 256");
199767f8919635c4928607450d9e0abb932109ceToomas Soome dist >>= 7; /* from now on, all distances are divided by 128 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for ( ; code < D_CODES; code++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome base_dist[code] = dist << 7;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome _dist_code[256 + dist++] = (uch)code;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (dist == 256, "tr_static_init: 256+dist != 512");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Construct the codes of the static literal tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Codes 286 and 287 do not exist, but we must include them in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * tree construction to get a canonical Huffman tree (longest code
199767f8919635c4928607450d9e0abb932109ceToomas Soome * all ones)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The static distance tree is trivial: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < D_CODES; n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_dtree[n].Len = 5;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_dtree[n].Code = bi_reverse((unsigned)n, 5);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_init_done = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifdef GEN_TREES_H
199767f8919635c4928607450d9e0abb932109ceToomas Soome gen_trees_header();
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* defined(GEN_TREES_H) || !defined(STDC) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Genererate the file trees.h describing the static trees.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef GEN_TREES_H
199767f8919635c4928607450d9e0abb932109ceToomas Soome# ifndef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome# endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define SEPARATOR(i, last, width) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((i) == (last)? "\n};\n\n" : \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((i) % (width) == (width)-1 ? ",\n" : ", "))
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid gen_trees_header()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome FILE *header = fopen("trees.h", "w");
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (header != NULL, "Can't open trees.h");
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "/* header created automatically with -DGEN_TREES_H */\n\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < L_CODES+2; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < D_CODES; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < DIST_CODE_LEN; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "%2u%s", _dist_code[i],
199767f8919635c4928607450d9e0abb932109ceToomas Soome SEPARATOR(i, DIST_CODE_LEN-1, 20));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "%2u%s", _length_code[i],
199767f8919635c4928607450d9e0abb932109ceToomas Soome SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < LENGTH_CODES; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "%1u%s", base_length[i],
199767f8919635c4928607450d9e0abb932109ceToomas Soome SEPARATOR(i, LENGTH_CODES-1, 20));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "local const int base_dist[D_CODES] = {\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < D_CODES; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(header, "%5u%s", base_dist[i],
199767f8919635c4928607450d9e0abb932109ceToomas Soome SEPARATOR(i, D_CODES-1, 10));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fclose(header);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* GEN_TREES_H */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initialize the tree data structures for a new zlib stream.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL _tr_init(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome tr_static_init();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->l_desc.dyn_tree = s->dyn_ltree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->l_desc.stat_desc = &static_l_desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->d_desc.dyn_tree = s->dyn_dtree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->d_desc.stat_desc = &static_d_desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_desc.dyn_tree = s->bl_tree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_desc.stat_desc = &static_bl_desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len = 0L;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bits_sent = 0L;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialize the first block of the first file: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome init_block(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initialize a new block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void init_block(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n; /* iterates over tree elements */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialize the trees. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->dyn_ltree[END_BLOCK].Freq = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->opt_len = s->static_len = 0L;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->last_lit = s->matches = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SMALLEST 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Index within the heap array of least frequent node in the Huffman tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remove the smallest element from the heap and recreate the heap with
199767f8919635c4928607450d9e0abb932109ceToomas Soome * one less element. Updates heap and heap_len.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define pqremove(s, tree, top) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome{\
199767f8919635c4928607450d9e0abb932109ceToomas Soome top = s->heap[SMALLEST]; \
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[SMALLEST] = s->heap[s->heap_len--]; \
199767f8919635c4928607450d9e0abb932109ceToomas Soome pqdownheap(s, tree, SMALLEST); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Compares to subtrees, using the tree depth as tie breaker when
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the subtrees have equal frequency. This minimizes the worst case length.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define smaller(tree, n, m, depth) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (tree[n].Freq < tree[m].Freq || \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Restore the heap property by moving down the tree starting at node k,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exchanging a node with the smallest of its two sons if necessary, stopping
199767f8919635c4928607450d9e0abb932109ceToomas Soome * when the heap property is re-established (each father smaller than its
199767f8919635c4928607450d9e0abb932109ceToomas Soome * two sons).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void pqdownheap(s, tree, k)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ct_data *tree; /* the tree to restore */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int k; /* node to move down */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int v = s->heap[k];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int j = k << 1; /* left son of k */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (j <= s->heap_len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set j to the smallest of the two sons: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (j < s->heap_len &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome j++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Exit if v is smaller than both sons */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smaller(tree, v, s->heap[j], s->depth)) break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Exchange v with the smallest son */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[k] = s->heap[j]; k = j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* And continue down the tree, setting j to the left son of k */
199767f8919635c4928607450d9e0abb932109ceToomas Soome j <<= 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[k] = v;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Compute the optimal bit lengths for a tree and update the total bit length
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for the current block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: the fields freq and dad are set, heap[heap_max] and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * above are the tree nodes sorted by increasing frequency.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT assertions: the field len is set to the optimal bit length, the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * array bl_count contains the frequencies for each bit length.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The length opt_len is updated; static_len is also updated if stree is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * not null.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void gen_bitlen(s, desc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree_desc *desc; /* the tree descriptor */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ct_data *tree = desc->dyn_tree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_code = desc->max_code;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const ct_data *stree = desc->stat_desc->static_tree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const intf *extra = desc->stat_desc->extra_bits;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int base = desc->stat_desc->extra_base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_length = desc->stat_desc->max_length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int h; /* heap index */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n, m; /* iterate over the tree elements */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bits; /* bit length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int xbits; /* extra bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ush f; /* frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int overflow = 0; /* number of elements with bit length too large */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* In a first pass, compute the optimal bit lengths (which may
199767f8919635c4928607450d9e0abb932109ceToomas Soome * overflow in the case of the bit length tree).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = s->heap[h];
199767f8919635c4928607450d9e0abb932109ceToomas Soome bits = tree[tree[n].Dad].Len + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bits > max_length) bits = max_length, overflow++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[n].Len = (ush)bits;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We overwrite tree[n].Dad which is no longer needed */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (n > max_code) continue; /* not a leaf node */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_count[bits]++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome xbits = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (n >= base) xbits = extra[n-base];
199767f8919635c4928607450d9e0abb932109ceToomas Soome f = tree[n].Freq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->opt_len += (ulg)f * (bits + xbits);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (overflow == 0) return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Trace((stderr,"\nbit length overflow\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* This happens for example on obj2 and pic of the Calgary corpus */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find the first bit length which could increase: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bits = max_length-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (s->bl_count[bits] == 0) bits--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_count[bits]--; /* move one leaf down the tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_count[max_length]--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The brother of the overflow item also moves one step up,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * but this does not affect bl_count[max_length]
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome overflow -= 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (overflow > 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Now recompute all bit lengths, scanning in increasing frequency.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
199767f8919635c4928607450d9e0abb932109ceToomas Soome * lengths instead of fixing only the wrong ones. This idea is taken
199767f8919635c4928607450d9e0abb932109ceToomas Soome * from 'ar' written by Haruhiko Okumura.)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (bits = max_length; bits != 0; bits--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = s->bl_count[bits];
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (n != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome m = s->heap[--h];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (m > max_code) continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((unsigned) tree[m].Len != (unsigned) bits) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->opt_len += ((long)bits - (long)tree[m].Len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(long)tree[m].Freq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[m].Len = (ush)bits;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome n--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Generate the codes for a given tree and bit counts (which need not be
199767f8919635c4928607450d9e0abb932109ceToomas Soome * optimal).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: the array bl_count contains the bit length statistics for
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the given tree and the field len is set for all tree elements.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT assertion: the field code is set for all tree elements of non
199767f8919635c4928607450d9e0abb932109ceToomas Soome * zero code length.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void gen_codes (tree, max_code, bl_count)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ct_data *tree; /* the tree to decorate */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_code; /* largest code with non zero frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ushf *bl_count; /* number of codes at each bit length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ush next_code[MAX_BITS+1]; /* next code value for each bit length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ush code = 0; /* running code value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bits; /* bit index */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n; /* code index */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The distribution counts are first used to generate the code values
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without bit reversal.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (bits = 1; bits <= MAX_BITS; bits++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome next_code[bits] = code = (code + bl_count[bits-1]) << 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check that the bit counts in bl_count are consistent. The last code
199767f8919635c4928607450d9e0abb932109ceToomas Soome * must be all ones.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "inconsistent bit counts");
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n <= max_code; n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len = tree[n].Len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0) continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Now reverse the bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[n].Code = bi_reverse(next_code[len]++, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
199767f8919635c4928607450d9e0abb932109ceToomas Soome n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Construct one Huffman tree and assigns the code bit strings and lengths.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Update the total bit length for the current block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: the field freq is set for all tree elements.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT assertions: the fields len and code are set to the optimal bit length
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and corresponding code. The length opt_len is updated; static_len is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * also updated if stree is not null. The field max_code is set.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void build_tree(s, desc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree_desc *desc; /* the tree descriptor */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ct_data *tree = desc->dyn_tree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const ct_data *stree = desc->stat_desc->static_tree;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int elems = desc->stat_desc->elems;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n, m; /* iterate over heap elements */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_code = -1; /* largest code with non zero frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int node; /* new node being created */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Construct the initial heap, with least frequent element in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
199767f8919635c4928607450d9e0abb932109ceToomas Soome * heap[0] is not used.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap_len = 0, s->heap_max = HEAP_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < elems; n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tree[n].Freq != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[++(s->heap_len)] = max_code = n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->depth[n] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[n].Len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The pkzip format requires that at least one distance code exists,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and that at least one bit should be sent even if there is only one
199767f8919635c4928607450d9e0abb932109ceToomas Soome * possible code. So to avoid special checks later on we force at least
199767f8919635c4928607450d9e0abb932109ceToomas Soome * two codes of non zero frequency.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (s->heap_len < 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[node].Freq = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->depth[node] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->opt_len--; if (stree) s->static_len -= stree[node].Len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* node is 0 or 1 so it does not have extra bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->max_code = max_code;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * establish sub-heaps of increasing lengths:
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Construct the Huffman tree by repeatedly combining the least two
199767f8919635c4928607450d9e0abb932109ceToomas Soome * frequent nodes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome node = elems; /* next internal node of the tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pqremove(s, tree, n); /* n = node of least frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome m = s->heap[SMALLEST]; /* m = node of next least frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[--(s->heap_max)] = m;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Create a new node father of n and m */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[node].Freq = tree[n].Freq + tree[m].Freq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->depth[n] : s->depth[m]) + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[n].Dad = tree[m].Dad = (ush)node;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DUMP_BL_TREE
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tree == s->bl_tree) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* and insert the new node in the heap */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[SMALLEST] = node++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pqdownheap(s, tree, SMALLEST);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (s->heap_len >= 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->heap[--(s->heap_max)] = s->heap[SMALLEST];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* At this point, the fields freq and dad are set. We can now
199767f8919635c4928607450d9e0abb932109ceToomas Soome * generate the bit lengths.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome gen_bitlen(s, (tree_desc *)desc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The field len is now set, we can generate the bit codes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome gen_codes ((ct_data *)tree, max_code, s->bl_count);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Scan a literal or distance tree to determine the frequencies of the codes
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in the bit length tree.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void scan_tree (s, tree, max_code)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ct_data *tree; /* the tree to be scanned */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_code; /* and its largest code of non zero frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n; /* iterates over all tree elements */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int prevlen = -1; /* last emitted length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int curlen; /* length of current code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nextlen = tree[0].Len; /* length of next code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count = 0; /* repeat count of the current code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_count = 7; /* max repeat count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int min_count = 4; /* min repeat count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nextlen == 0) max_count = 138, min_count = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tree[max_code+1].Len = (ush)0xffff; /* guard */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n <= max_code; n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome curlen = nextlen; nextlen = tree[n+1].Len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (++count < max_count && curlen == nextlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (count < min_count) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_tree[curlen].Freq += count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (curlen != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (curlen != prevlen) s->bl_tree[curlen].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_tree[REP_3_6].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (count <= 10) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_tree[REPZ_3_10].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bl_tree[REPZ_11_138].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = 0; prevlen = curlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nextlen == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_count = 138, min_count = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (curlen == nextlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_count = 6, min_count = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_count = 7, min_count = 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Send a literal or distance tree in compressed form, using the codes in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bl_tree.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void send_tree (s, tree, max_code)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ct_data *tree; /* the tree to be scanned */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_code; /* and its largest code of non zero frequency */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n; /* iterates over all tree elements */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int prevlen = -1; /* last emitted length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int curlen; /* length of current code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nextlen = tree[0].Len; /* length of next code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count = 0; /* repeat count of the current code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_count = 7; /* max repeat count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int min_count = 4; /* min repeat count */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* tree[max_code+1].Len = -1; */ /* guard already set */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nextlen == 0) max_count = 138, min_count = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n <= max_code; n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome curlen = nextlen; nextlen = tree[n+1].Len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (++count < max_count && curlen == nextlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (count < min_count) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (curlen != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (curlen != prevlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, curlen, s->bl_tree); count--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert(count >= 3 && count <= 6, " 3_6?");
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (count <= 10) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = 0; prevlen = curlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nextlen == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_count = 138, min_count = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (curlen == nextlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_count = 6, min_count = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_count = 7, min_count = 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Construct the Huffman tree for the bit lengths and return the index in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bl_order of the last bit length code to send.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal int build_bl_tree(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_blindex; /* index of last bit length code of non zero freq */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Determine the bit length frequencies for literal and distance trees */
199767f8919635c4928607450d9e0abb932109ceToomas Soome scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
199767f8919635c4928607450d9e0abb932109ceToomas Soome scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Build the bit length tree: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome build_tree(s, (tree_desc *)(&(s->bl_desc)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* opt_len now includes the length of the tree representations, except
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Determine the number of bit length codes to send. The pkzip format
199767f8919635c4928607450d9e0abb932109ceToomas Soome * requires that at least 4 bit length codes be sent. (appnote.txt says
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3 but the actual value used is 4.)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Update opt_len to include the bit length tree and counts */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->opt_len += 3*(max_blindex+1) + 5+5+4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->opt_len, s->static_len));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return max_blindex;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Send the header for a block using dynamic Huffman trees: the counts, the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * lengths of the bit length codes, the literal tree and the distance tree.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void send_all_trees(s, lcodes, dcodes, blcodes)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int lcodes, dcodes, blcodes; /* number of codes for each tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rank; /* index in bl_order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "too many codes");
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\nbl counts: "));
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, dcodes-1, 5);
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (rank = 0; rank < blcodes; rank++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Send a stored block
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome charf *buf; /* input block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ulg stored_len; /* length of input block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int last; /* one if this is the last block for a file */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len += (stored_len + 4) << 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL _tr_flush_bits(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi_flush(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Send one empty static block to give enough lookahead for inflate.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This takes 10 bits, of which 7 may remain in the bit buffer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL _tr_align(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, STATIC_TREES<<1, 3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, END_BLOCK, static_ltree);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi_flush(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Determine the best encoding for the current block: dynamic trees, static
199767f8919635c4928607450d9e0abb932109ceToomas Soome * trees or store, and output the encoded block to the zip file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome charf *buf; /* input block, or NULL if too old */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ulg stored_len; /* length of input block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int last; /* one if this is the last block for a file */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int max_blindex = 0; /* index of last bit length code of non zero freq */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Build the Huffman trees unless a stored block is forced */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->level > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check if the file is binary or text */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->strm->data_type == Z_UNKNOWN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->strm->data_type = detect_data_type(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Construct the literal and distance trees */
199767f8919635c4928607450d9e0abb932109ceToomas Soome build_tree(s, (tree_desc *)(&(s->l_desc)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->static_len));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome build_tree(s, (tree_desc *)(&(s->d_desc)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->static_len));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* At this point, opt_len and static_len are the total bit lengths of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the compressed block data, excluding the tree representations.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Build the bit length tree for the above two trees, and get the index
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in bl_order of the last bit length code to send.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_blindex = build_bl_tree(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Determine the best encoding. Compute the block lengths in bytes. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome opt_lenb = (s->opt_len+3+7)>>3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static_lenb = (s->static_len+3+7)>>3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
199767f8919635c4928607450d9e0abb932109ceToomas Soome opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->last_lit));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert(buf != (char*)0, "lost buf");
199767f8919635c4928607450d9e0abb932109ceToomas Soome opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef FORCE_STORED
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf != (char*)0) { /* force stored block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (stored_len+4 <= opt_lenb && buf != (char*)0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* 4: two words for the lengths */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Otherwise we can't have processed more than WSIZE input bytes since
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the last block flush, because compression would have been
199767f8919635c4928607450d9e0abb932109ceToomas Soome * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * transform a block into a stored block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome _tr_stored_block(s, buf, stored_len, last);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef FORCE_STATIC
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (static_lenb >= 0) { /* force static trees */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, (STATIC_TREES<<1)+last, 3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome compress_block(s, (const ct_data *)static_ltree,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (const ct_data *)static_dtree);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len += 3 + s->static_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, (DYN_TREES<<1)+last, 3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome max_blindex+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome compress_block(s, (const ct_data *)s->dyn_ltree,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (const ct_data *)s->dyn_dtree);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len += 3 + s->opt_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (s->compressed_len == s->bits_sent, "bad compressed size");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The above check is made mod 2^32, for files larger than 512 MB
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and uLong implemented on 32 bits.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome init_block(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (last) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi_windup(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len += 7; /* align on byte boundary */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->compressed_len-7*last));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Save the match info and tally the frequency counts. Return true if
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the current block must be flushed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint ZLIB_INTERNAL _tr_tally (s, dist, lc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned dist; /* distance of matched string */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->d_buf[s->last_lit] = (ush)dist;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->l_buf[s->last_lit++] = (uch)lc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dist == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* lc is the unmatched char */
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->dyn_ltree[lc].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->matches++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Here, lc is the match length - MIN_MATCH */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dist--; /* dist = match distance - 1 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert((ush)dist < (ush)MAX_DIST(s) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->dyn_dtree[d_code(dist)].Freq++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TRUNCATE_BLOCK
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to guess if it is profitable to stop the current block here */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Compute an upper bound for the compressed length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ulg out_length = (ulg)s->last_lit*8L;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ulg in_length = (ulg)((long)s->strstart - s->block_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome int dcode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (dcode = 0; dcode < D_CODES; dcode++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome out_length += (ulg)s->dyn_dtree[dcode].Freq *
199767f8919635c4928607450d9e0abb932109ceToomas Soome (5L+extra_dbits[dcode]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome out_length >>= 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->last_lit, in_length, out_length,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 100L - out_length*100L/in_length));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (s->last_lit == s->lit_bufsize-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We avoid equality with lit_bufsize because of wraparound at 64K
199767f8919635c4928607450d9e0abb932109ceToomas Soome * on 16 bit machines and because stored blocks are restricted to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 64K-1 bytes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Send the block data compressed using the given Huffman trees
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void compress_block(s, ltree, dtree)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const ct_data *ltree; /* literal tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome const ct_data *dtree; /* distance tree */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned dist; /* distance of matched string */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int lc; /* match length or unmatched char (if dist == 0) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned lx = 0; /* running index in l_buf */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned code; /* the code to send */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int extra; /* number of extra bits to send */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->last_lit != 0) do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dist = s->d_buf[lx];
199767f8919635c4928607450d9e0abb932109ceToomas Soome lc = s->l_buf[lx++];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dist == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, lc, ltree); /* send a literal byte */
199767f8919635c4928607450d9e0abb932109ceToomas Soome Tracecv(isgraph(lc), (stderr," '%c' ", lc));
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Here, lc is the match length - MIN_MATCH */
199767f8919635c4928607450d9e0abb932109ceToomas Soome code = _length_code[lc];
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, code+LITERALS+1, ltree); /* send the length code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome extra = extra_lbits[code];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (extra != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome lc -= base_length[code];
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, lc, extra); /* send the extra length bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome dist--; /* dist is now the match distance - 1 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome code = d_code(dist);
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert (code < D_CODES, "bad d_code");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, code, dtree); /* send the distance code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome extra = extra_dbits[code];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (extra != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dist -= base_dist[code];
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_bits(s, dist, extra); /* send the extra distance bits */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } /* literal or match pair ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "pendingBuf overflow");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (lx < s->last_lit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_code(s, END_BLOCK, ltree);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check if the data type is TEXT or BINARY, using the following algorithm:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - TEXT if the two conditions below are satisfied:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a) There are no non-portable control characters belonging to the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "black list" (0..6, 14..25, 28..31).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * b) There is at least one printable character belonging to the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - BINARY otherwise.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The following partially-portable control characters form a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "gray list" that is ignored in this detection algorithm:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: the fields Freq of dyn_ltree are set.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal int detect_data_type(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* black_mask is the bit mask of black-listed bytes
199767f8919635c4928607450d9e0abb932109ceToomas Soome * set bits 0..6, 14..25, and 28..31
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 0xf3ffc07f = binary 11110011111111111100000001111111
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long black_mask = 0xf3ffc07fUL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check for non-textual ("black-listed") bytes. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n <= 31; n++, black_mask >>= 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return Z_BINARY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check for textual ("white-listed") bytes. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome || s->dyn_ltree[13].Freq != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return Z_TEXT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 32; n < LITERALS; n++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->dyn_ltree[n].Freq != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return Z_TEXT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* There are no "black-listed" or "white-listed" bytes:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * this stream either is empty or has tolerated ("gray-listed") bytes only.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return Z_BINARY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Reverse the first len bits of a code, using straightforward code (a faster
199767f8919635c4928607450d9e0abb932109ceToomas Soome * method would use a table)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN assertion: 1 <= len <= 15
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal unsigned bi_reverse(code, len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned code; /* the value to invert */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len; /* its bit length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome register unsigned res = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome res |= code & 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome code >>= 1, res <<= 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (--len > 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return res >> 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Flush the bit buffer, keeping at most 7 bits in it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void bi_flush(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->bi_valid == 16) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_short(s, s->bi_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (s->bi_valid >= 8) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_byte(s, (Byte)s->bi_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf >>= 8;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid -= 8;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Flush the bit buffer and align the output on a byte boundary
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void bi_windup(s)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s->bi_valid > 8) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_short(s, s->bi_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (s->bi_valid > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_byte(s, (Byte)s->bi_buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_buf = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bi_valid = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bits_sent = (s->bits_sent+7) & ~7;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy a stored block, storing first the length and its
199767f8919635c4928607450d9e0abb932109ceToomas Soome * one's complement if requested.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomelocal void copy_block(s, buf, len, header)
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflate_state *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome charf *buf; /* the input data */
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned len; /* its length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int header; /* true if block header must be written */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi_windup(s); /* align on byte boundary */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (header) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_short(s, (ush)len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_short(s, (ush)~len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bits_sent += 2*16;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome s->bits_sent += (ulg)len<<3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (len--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_byte(s, *buf++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}