deflate.h revision 17651
117651Speter/* deflate.h -- internal compression state
217651Speter * Copyright (C) 1995-1996 Jean-loup Gailly
317651Speter * For conditions of distribution and use, see copyright notice in zlib.h
417651Speter */
517651Speter
617651Speter/* WARNING: this file should *not* be used by applications. It is
717651Speter   part of the implementation of the compression library and is
817651Speter   subject to change. Applications should only use zlib.h.
917651Speter */
1017651Speter
1117651Speter/* $Id: deflate.h,v 1.10 1996/07/02 12:41:00 me Exp $ */
1217651Speter
1317651Speter#ifndef _DEFLATE_H
1417651Speter#define _DEFLATE_H
1517651Speter
1617651Speter#include "zutil.h"
1717651Speter
1817651Speter/* ===========================================================================
1917651Speter * Internal compression state.
2017651Speter */
2117651Speter
2217651Speter#define LENGTH_CODES 29
2317651Speter/* number of length codes, not counting the special END_BLOCK code */
2417651Speter
2517651Speter#define LITERALS  256
2617651Speter/* number of literal bytes 0..255 */
2717651Speter
2817651Speter#define L_CODES (LITERALS+1+LENGTH_CODES)
2917651Speter/* number of Literal or Length codes, including the END_BLOCK code */
3017651Speter
3117651Speter#define D_CODES   30
3217651Speter/* number of distance codes */
3317651Speter
3417651Speter#define BL_CODES  19
3517651Speter/* number of codes used to transfer the bit lengths */
3617651Speter
3717651Speter#define HEAP_SIZE (2*L_CODES+1)
3817651Speter/* maximum heap size */
3917651Speter
4017651Speter#define MAX_BITS 15
4117651Speter/* All codes must not exceed MAX_BITS bits */
4217651Speter
4317651Speter#define INIT_STATE    42
4417651Speter#define BUSY_STATE   113
4517651Speter#define FINISH_STATE 666
4617651Speter/* Stream status */
4717651Speter
4817651Speter
4917651Speter/* Data structure describing a single value and its code string. */
5017651Spetertypedef struct ct_data_s {
5117651Speter    union {
5217651Speter        ush  freq;       /* frequency count */
5317651Speter        ush  code;       /* bit string */
5417651Speter    } fc;
5517651Speter    union {
5617651Speter        ush  dad;        /* father node in Huffman tree */
5717651Speter        ush  len;        /* length of bit string */
5817651Speter    } dl;
5917651Speter} FAR ct_data;
6017651Speter
6117651Speter#define Freq fc.freq
6217651Speter#define Code fc.code
6317651Speter#define Dad  dl.dad
6417651Speter#define Len  dl.len
6517651Speter
6617651Spetertypedef struct static_tree_desc_s  static_tree_desc;
6717651Speter
6817651Spetertypedef struct tree_desc_s {
6917651Speter    ct_data *dyn_tree;           /* the dynamic tree */
7017651Speter    int     max_code;            /* largest code with non zero frequency */
7117651Speter    static_tree_desc *stat_desc; /* the corresponding static tree */
7217651Speter} FAR tree_desc;
7317651Speter
7417651Spetertypedef ush Pos;
7517651Spetertypedef Pos FAR Posf;
7617651Spetertypedef unsigned IPos;
7717651Speter
7817651Speter/* A Pos is an index in the character window. We use short instead of int to
7917651Speter * save space in the various tables. IPos is used only for parameter passing.
8017651Speter */
8117651Speter
8217651Spetertypedef struct internal_state {
8317651Speter    z_streamp strm;      /* pointer back to this zlib stream */
8417651Speter    int   status;        /* as the name implies */
8517651Speter    Bytef *pending_buf;  /* output still pending */
8617651Speter    Bytef *pending_out;  /* next pending byte to output to the stream */
8717651Speter    int   pending;       /* nb of bytes in the pending buffer */
8817651Speter    int   noheader;      /* suppress zlib header and adler32 */
8917651Speter    Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
9017651Speter    Byte  method;        /* STORED (for zip only) or DEFLATED */
9117651Speter    int   last_flush;    /* value of flush param for previous deflate call */
9217651Speter
9317651Speter                /* used by deflate.c: */
9417651Speter
9517651Speter    uInt  w_size;        /* LZ77 window size (32K by default) */
9617651Speter    uInt  w_bits;        /* log2(w_size)  (8..16) */
9717651Speter    uInt  w_mask;        /* w_size - 1 */
9817651Speter
9917651Speter    Bytef *window;
10017651Speter    /* Sliding window. Input bytes are read into the second half of the window,
10117651Speter     * and move to the first half later to keep a dictionary of at least wSize
10217651Speter     * bytes. With this organization, matches are limited to a distance of
10317651Speter     * wSize-MAX_MATCH bytes, but this ensures that IO is always
10417651Speter     * performed with a length multiple of the block size. Also, it limits
10517651Speter     * the window size to 64K, which is quite useful on MSDOS.
10617651Speter     * To do: use the user input buffer as sliding window.
10717651Speter     */
10817651Speter
10917651Speter    ulg window_size;
11017651Speter    /* Actual size of window: 2*wSize, except when the user input buffer
11117651Speter     * is directly used as sliding window.
11217651Speter     */
11317651Speter
11417651Speter    Posf *prev;
11517651Speter    /* Link to older string with same hash index. To limit the size of this
11617651Speter     * array to 64K, this link is maintained only for the last 32K strings.
11717651Speter     * An index in this array is thus a window index modulo 32K.
11817651Speter     */
11917651Speter
12017651Speter    Posf *head; /* Heads of the hash chains or NIL. */
12117651Speter
12217651Speter    uInt  ins_h;          /* hash index of string to be inserted */
12317651Speter    uInt  hash_size;      /* number of elements in hash table */
12417651Speter    uInt  hash_bits;      /* log2(hash_size) */
12517651Speter    uInt  hash_mask;      /* hash_size-1 */
12617651Speter
12717651Speter    uInt  hash_shift;
12817651Speter    /* Number of bits by which ins_h must be shifted at each input
12917651Speter     * step. It must be such that after MIN_MATCH steps, the oldest
13017651Speter     * byte no longer takes part in the hash key, that is:
13117651Speter     *   hash_shift * MIN_MATCH >= hash_bits
13217651Speter     */
13317651Speter
13417651Speter    long block_start;
13517651Speter    /* Window position at the beginning of the current output block. Gets
13617651Speter     * negative when the window is moved backwards.
13717651Speter     */
13817651Speter
13917651Speter    uInt match_length;           /* length of best match */
14017651Speter    IPos prev_match;             /* previous match */
14117651Speter    int match_available;         /* set if previous match exists */
14217651Speter    uInt strstart;               /* start of string to insert */
14317651Speter    uInt match_start;            /* start of matching string */
14417651Speter    uInt lookahead;              /* number of valid bytes ahead in window */
14517651Speter
14617651Speter    uInt prev_length;
14717651Speter    /* Length of the best match at previous step. Matches not greater than this
14817651Speter     * are discarded. This is used in the lazy match evaluation.
14917651Speter     */
15017651Speter
15117651Speter    uInt max_chain_length;
15217651Speter    /* To speed up deflation, hash chains are never searched beyond this
15317651Speter     * length.  A higher limit improves compression ratio but degrades the
15417651Speter     * speed.
15517651Speter     */
15617651Speter
15717651Speter    uInt max_lazy_match;
15817651Speter    /* Attempt to find a better match only when the current match is strictly
15917651Speter     * smaller than this value. This mechanism is used only for compression
16017651Speter     * levels >= 4.
16117651Speter     */
16217651Speter#   define max_insert_length  max_lazy_match
16317651Speter    /* Insert new strings in the hash table only if the match length is not
16417651Speter     * greater than this length. This saves time but degrades compression.
16517651Speter     * max_insert_length is used only for compression levels <= 3.
16617651Speter     */
16717651Speter
16817651Speter    int level;    /* compression level (1..9) */
16917651Speter    int strategy; /* favor or force Huffman coding*/
17017651Speter
17117651Speter    uInt good_match;
17217651Speter    /* Use a faster search when the previous match is longer than this */
17317651Speter
17417651Speter    int nice_match; /* Stop searching when current match exceeds this */
17517651Speter
17617651Speter                /* used by trees.c: */
17717651Speter    /* Didn't use ct_data typedef below to supress compiler warning */
17817651Speter    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
17917651Speter    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
18017651Speter    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
18117651Speter
18217651Speter    struct tree_desc_s l_desc;               /* desc. for literal tree */
18317651Speter    struct tree_desc_s d_desc;               /* desc. for distance tree */
18417651Speter    struct tree_desc_s bl_desc;              /* desc. for bit length tree */
18517651Speter
18617651Speter    ush bl_count[MAX_BITS+1];
18717651Speter    /* number of codes at each bit length for an optimal tree */
18817651Speter
18917651Speter    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
19017651Speter    int heap_len;               /* number of elements in the heap */
19117651Speter    int heap_max;               /* element of largest frequency */
19217651Speter    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
19317651Speter     * The same heap array is used to build all trees.
19417651Speter     */
19517651Speter
19617651Speter    uch depth[2*L_CODES+1];
19717651Speter    /* Depth of each subtree used as tie breaker for trees of equal frequency
19817651Speter     */
19917651Speter
20017651Speter    uchf *l_buf;          /* buffer for literals or lengths */
20117651Speter
20217651Speter    uInt  lit_bufsize;
20317651Speter    /* Size of match buffer for literals/lengths.  There are 4 reasons for
20417651Speter     * limiting lit_bufsize to 64K:
20517651Speter     *   - frequencies can be kept in 16 bit counters
20617651Speter     *   - if compression is not successful for the first block, all input
20717651Speter     *     data is still in the window so we can still emit a stored block even
20817651Speter     *     when input comes from standard input.  (This can also be done for
20917651Speter     *     all blocks if lit_bufsize is not greater than 32K.)
21017651Speter     *   - if compression is not successful for a file smaller than 64K, we can
21117651Speter     *     even emit a stored file instead of a stored block (saving 5 bytes).
21217651Speter     *     This is applicable only for zip (not gzip or zlib).
21317651Speter     *   - creating new Huffman trees less frequently may not provide fast
21417651Speter     *     adaptation to changes in the input data statistics. (Take for
21517651Speter     *     example a binary file with poorly compressible code followed by
21617651Speter     *     a highly compressible string table.) Smaller buffer sizes give
21717651Speter     *     fast adaptation but have of course the overhead of transmitting
21817651Speter     *     trees more frequently.
21917651Speter     *   - I can't count above 4
22017651Speter     */
22117651Speter
22217651Speter    uInt last_lit;      /* running index in l_buf */
22317651Speter
22417651Speter    ushf *d_buf;
22517651Speter    /* Buffer for distances. To simplify the code, d_buf and l_buf have
22617651Speter     * the same number of elements. To use different lengths, an extra flag
22717651Speter     * array would be necessary.
22817651Speter     */
22917651Speter
23017651Speter    ulg opt_len;        /* bit length of current block with optimal trees */
23117651Speter    ulg static_len;     /* bit length of current block with static trees */
23217651Speter    ulg compressed_len; /* total bit length of compressed file */
23317651Speter    uInt matches;       /* number of string matches in current block */
23417651Speter    int last_eob_len;   /* bit length of EOB code for last block */
23517651Speter
23617651Speter#ifdef DEBUG
23717651Speter    ulg bits_sent;      /* bit length of the compressed data */
23817651Speter#endif
23917651Speter
24017651Speter    ush bi_buf;
24117651Speter    /* Output buffer. bits are inserted starting at the bottom (least
24217651Speter     * significant bits).
24317651Speter     */
24417651Speter    int bi_valid;
24517651Speter    /* Number of valid bits in bi_buf.  All bits above the last valid bit
24617651Speter     * are always zero.
24717651Speter     */
24817651Speter
24917651Speter} FAR deflate_state;
25017651Speter
25117651Speter/* Output a byte on the stream.
25217651Speter * IN assertion: there is enough room in pending_buf.
25317651Speter */
25417651Speter#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
25517651Speter
25617651Speter
25717651Speter#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
25817651Speter/* Minimum amount of lookahead, except at the end of the input file.
25917651Speter * See deflate.c for comments about the MIN_MATCH+1.
26017651Speter */
26117651Speter
26217651Speter#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
26317651Speter/* In order to simplify the code, particularly on 16 bit machines, match
26417651Speter * distances are limited to MAX_DIST instead of WSIZE.
26517651Speter */
26617651Speter
26717651Speter        /* in trees.c */
26817651Spetervoid _tr_init         OF((deflate_state *s));
26917651Speterint  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
27017651Speterulg  _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
27117651Speter			  int eof));
27217651Spetervoid _tr_align        OF((deflate_state *s));
27317651Spetervoid _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
27417651Speter                          int eof));
27517651Speter#endif
276