117651Speter/* deflate.h -- internal compression state
2237410Sdelphij * Copyright (C) 1995-2012 Jean-loup Gailly
3131380Stjr * 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
11146081Skientzle/* @(#) $Id$ */
1217651Speter
13131380Stjr#ifndef DEFLATE_H
14131380Stjr#define DEFLATE_H
1517651Speter
1617651Speter#include "zutil.h"
1717651Speter
18131380Stjr/* define NO_GZIP when compiling if you want to disable gzip header and
19131380Stjr   trailer creation by deflate().  NO_GZIP would be used to avoid linking in
20131380Stjr   the crc code when it is not needed.  For shared libraries, gzip encoding
21131380Stjr   should be left enabled. */
22131380Stjr#ifndef NO_GZIP
23131380Stjr#  define GZIP
24131380Stjr#endif
25131380Stjr
2617651Speter/* ===========================================================================
2717651Speter * Internal compression state.
2817651Speter */
2917651Speter
3017651Speter#define LENGTH_CODES 29
3117651Speter/* number of length codes, not counting the special END_BLOCK code */
3217651Speter
3317651Speter#define LITERALS  256
3417651Speter/* number of literal bytes 0..255 */
3517651Speter
3617651Speter#define L_CODES (LITERALS+1+LENGTH_CODES)
3717651Speter/* number of Literal or Length codes, including the END_BLOCK code */
3817651Speter
3917651Speter#define D_CODES   30
4017651Speter/* number of distance codes */
4117651Speter
4217651Speter#define BL_CODES  19
4317651Speter/* number of codes used to transfer the bit lengths */
4417651Speter
4517651Speter#define HEAP_SIZE (2*L_CODES+1)
4617651Speter/* maximum heap size */
4717651Speter
4817651Speter#define MAX_BITS 15
4917651Speter/* All codes must not exceed MAX_BITS bits */
5017651Speter
51237410Sdelphij#define Buf_size 16
52237410Sdelphij/* size of bit buffer in bi_buf */
53237410Sdelphij
5417651Speter#define INIT_STATE    42
55157046Sdes#define EXTRA_STATE   69
56157046Sdes#define NAME_STATE    73
57157046Sdes#define COMMENT_STATE 91
58157046Sdes#define HCRC_STATE   103
5917651Speter#define BUSY_STATE   113
6017651Speter#define FINISH_STATE 666
6117651Speter/* Stream status */
6217651Speter
6317651Speter
6417651Speter/* Data structure describing a single value and its code string. */
6517651Spetertypedef struct ct_data_s {
6617651Speter    union {
6717651Speter        ush  freq;       /* frequency count */
6817651Speter        ush  code;       /* bit string */
6917651Speter    } fc;
7017651Speter    union {
7117651Speter        ush  dad;        /* father node in Huffman tree */
7217651Speter        ush  len;        /* length of bit string */
7317651Speter    } dl;
7417651Speter} FAR ct_data;
7517651Speter
7617651Speter#define Freq fc.freq
7717651Speter#define Code fc.code
7817651Speter#define Dad  dl.dad
7917651Speter#define Len  dl.len
8017651Speter
8117651Spetertypedef struct static_tree_desc_s  static_tree_desc;
8217651Speter
8317651Spetertypedef struct tree_desc_s {
8417651Speter    ct_data *dyn_tree;           /* the dynamic tree */
8517651Speter    int     max_code;            /* largest code with non zero frequency */
8617651Speter    static_tree_desc *stat_desc; /* the corresponding static tree */
8717651Speter} FAR tree_desc;
8817651Speter
8917651Spetertypedef ush Pos;
9017651Spetertypedef Pos FAR Posf;
9117651Spetertypedef unsigned IPos;
9217651Speter
9317651Speter/* A Pos is an index in the character window. We use short instead of int to
9417651Speter * save space in the various tables. IPos is used only for parameter passing.
9517651Speter */
9617651Speter
9717651Spetertypedef struct internal_state {
9817651Speter    z_streamp strm;      /* pointer back to this zlib stream */
9917651Speter    int   status;        /* as the name implies */
10017651Speter    Bytef *pending_buf;  /* output still pending */
10133908Ssteve    ulg   pending_buf_size; /* size of pending_buf */
10217651Speter    Bytef *pending_out;  /* next pending byte to output to the stream */
103157046Sdes    uInt   pending;      /* nb of bytes in the pending buffer */
104131380Stjr    int   wrap;          /* bit 0 true for zlib, bit 1 true for gzip */
105157046Sdes    gz_headerp  gzhead;  /* gzip header information to write */
106157046Sdes    uInt   gzindex;      /* where in extra, name, or comment */
107250261Sdelphij    Byte  method;        /* can only be DEFLATED */
10817651Speter    int   last_flush;    /* value of flush param for previous deflate call */
10917651Speter
11017651Speter                /* used by deflate.c: */
11117651Speter
11217651Speter    uInt  w_size;        /* LZ77 window size (32K by default) */
11317651Speter    uInt  w_bits;        /* log2(w_size)  (8..16) */
11417651Speter    uInt  w_mask;        /* w_size - 1 */
11517651Speter
11617651Speter    Bytef *window;
11717651Speter    /* Sliding window. Input bytes are read into the second half of the window,
11817651Speter     * and move to the first half later to keep a dictionary of at least wSize
11917651Speter     * bytes. With this organization, matches are limited to a distance of
12017651Speter     * wSize-MAX_MATCH bytes, but this ensures that IO is always
12117651Speter     * performed with a length multiple of the block size. Also, it limits
12217651Speter     * the window size to 64K, which is quite useful on MSDOS.
12317651Speter     * To do: use the user input buffer as sliding window.
12417651Speter     */
12517651Speter
12617651Speter    ulg window_size;
12717651Speter    /* Actual size of window: 2*wSize, except when the user input buffer
12817651Speter     * is directly used as sliding window.
12917651Speter     */
13017651Speter
13117651Speter    Posf *prev;
13217651Speter    /* Link to older string with same hash index. To limit the size of this
13317651Speter     * array to 64K, this link is maintained only for the last 32K strings.
13417651Speter     * An index in this array is thus a window index modulo 32K.
13517651Speter     */
13617651Speter
13717651Speter    Posf *head; /* Heads of the hash chains or NIL. */
13817651Speter
13917651Speter    uInt  ins_h;          /* hash index of string to be inserted */
14017651Speter    uInt  hash_size;      /* number of elements in hash table */
14117651Speter    uInt  hash_bits;      /* log2(hash_size) */
14217651Speter    uInt  hash_mask;      /* hash_size-1 */
14317651Speter
14417651Speter    uInt  hash_shift;
14517651Speter    /* Number of bits by which ins_h must be shifted at each input
14617651Speter     * step. It must be such that after MIN_MATCH steps, the oldest
14717651Speter     * byte no longer takes part in the hash key, that is:
14817651Speter     *   hash_shift * MIN_MATCH >= hash_bits
14917651Speter     */
15017651Speter
15117651Speter    long block_start;
15217651Speter    /* Window position at the beginning of the current output block. Gets
15317651Speter     * negative when the window is moved backwards.
15417651Speter     */
15517651Speter
15617651Speter    uInt match_length;           /* length of best match */
15717651Speter    IPos prev_match;             /* previous match */
15817651Speter    int match_available;         /* set if previous match exists */
15917651Speter    uInt strstart;               /* start of string to insert */
16017651Speter    uInt match_start;            /* start of matching string */
16117651Speter    uInt lookahead;              /* number of valid bytes ahead in window */
16217651Speter
16317651Speter    uInt prev_length;
16417651Speter    /* Length of the best match at previous step. Matches not greater than this
16517651Speter     * are discarded. This is used in the lazy match evaluation.
16617651Speter     */
16717651Speter
16817651Speter    uInt max_chain_length;
16917651Speter    /* To speed up deflation, hash chains are never searched beyond this
17017651Speter     * length.  A higher limit improves compression ratio but degrades the
17117651Speter     * speed.
17217651Speter     */
17317651Speter
17417651Speter    uInt max_lazy_match;
17517651Speter    /* Attempt to find a better match only when the current match is strictly
17617651Speter     * smaller than this value. This mechanism is used only for compression
17717651Speter     * levels >= 4.
17817651Speter     */
17917651Speter#   define max_insert_length  max_lazy_match
18017651Speter    /* Insert new strings in the hash table only if the match length is not
18117651Speter     * greater than this length. This saves time but degrades compression.
18217651Speter     * max_insert_length is used only for compression levels <= 3.
18317651Speter     */
18417651Speter
18517651Speter    int level;    /* compression level (1..9) */
18617651Speter    int strategy; /* favor or force Huffman coding*/
18717651Speter
18817651Speter    uInt good_match;
18917651Speter    /* Use a faster search when the previous match is longer than this */
19017651Speter
19117651Speter    int nice_match; /* Stop searching when current match exceeds this */
19217651Speter
19317651Speter                /* used by trees.c: */
194237410Sdelphij    /* Didn't use ct_data typedef below to suppress compiler warning */
19517651Speter    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
19617651Speter    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
19717651Speter    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
19817651Speter
19917651Speter    struct tree_desc_s l_desc;               /* desc. for literal tree */
20017651Speter    struct tree_desc_s d_desc;               /* desc. for distance tree */
20117651Speter    struct tree_desc_s bl_desc;              /* desc. for bit length tree */
20217651Speter
20317651Speter    ush bl_count[MAX_BITS+1];
20417651Speter    /* number of codes at each bit length for an optimal tree */
20517651Speter
20617651Speter    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
20717651Speter    int heap_len;               /* number of elements in the heap */
20817651Speter    int heap_max;               /* element of largest frequency */
20917651Speter    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
21017651Speter     * The same heap array is used to build all trees.
21117651Speter     */
21217651Speter
21317651Speter    uch depth[2*L_CODES+1];
21417651Speter    /* Depth of each subtree used as tie breaker for trees of equal frequency
21517651Speter     */
21617651Speter
21717651Speter    uchf *l_buf;          /* buffer for literals or lengths */
21817651Speter
21917651Speter    uInt  lit_bufsize;
22017651Speter    /* Size of match buffer for literals/lengths.  There are 4 reasons for
22117651Speter     * limiting lit_bufsize to 64K:
22217651Speter     *   - frequencies can be kept in 16 bit counters
22317651Speter     *   - if compression is not successful for the first block, all input
22417651Speter     *     data is still in the window so we can still emit a stored block even
22517651Speter     *     when input comes from standard input.  (This can also be done for
22617651Speter     *     all blocks if lit_bufsize is not greater than 32K.)
22717651Speter     *   - if compression is not successful for a file smaller than 64K, we can
22817651Speter     *     even emit a stored file instead of a stored block (saving 5 bytes).
22917651Speter     *     This is applicable only for zip (not gzip or zlib).
23017651Speter     *   - creating new Huffman trees less frequently may not provide fast
23117651Speter     *     adaptation to changes in the input data statistics. (Take for
23217651Speter     *     example a binary file with poorly compressible code followed by
23317651Speter     *     a highly compressible string table.) Smaller buffer sizes give
23417651Speter     *     fast adaptation but have of course the overhead of transmitting
23517651Speter     *     trees more frequently.
23617651Speter     *   - I can't count above 4
23717651Speter     */
23817651Speter
23917651Speter    uInt last_lit;      /* running index in l_buf */
24017651Speter
24117651Speter    ushf *d_buf;
24217651Speter    /* Buffer for distances. To simplify the code, d_buf and l_buf have
24317651Speter     * the same number of elements. To use different lengths, an extra flag
24417651Speter     * array would be necessary.
24517651Speter     */
24617651Speter
24717651Speter    ulg opt_len;        /* bit length of current block with optimal trees */
24817651Speter    ulg static_len;     /* bit length of current block with static trees */
24917651Speter    uInt matches;       /* number of string matches in current block */
250237410Sdelphij    uInt insert;        /* bytes at end of window left to insert */
25117651Speter
25217651Speter#ifdef DEBUG
25342471Speter    ulg compressed_len; /* total bit length of compressed file mod 2^32 */
25442471Speter    ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */
25517651Speter#endif
25617651Speter
25717651Speter    ush bi_buf;
25817651Speter    /* Output buffer. bits are inserted starting at the bottom (least
25917651Speter     * significant bits).
26017651Speter     */
26117651Speter    int bi_valid;
26217651Speter    /* Number of valid bits in bi_buf.  All bits above the last valid bit
26317651Speter     * are always zero.
26417651Speter     */
26517651Speter
266205471Sdelphij    ulg high_water;
267205471Sdelphij    /* High water mark offset in window for initialized bytes -- bytes above
268205471Sdelphij     * this are set to zero in order to avoid memory check warnings when
269205471Sdelphij     * longest match routines access bytes past the input.  This is then
270205471Sdelphij     * updated to the new high water mark.
271205471Sdelphij     */
272205471Sdelphij
27317651Speter} FAR deflate_state;
27417651Speter
27517651Speter/* Output a byte on the stream.
27617651Speter * IN assertion: there is enough room in pending_buf.
27717651Speter */
27817651Speter#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
27917651Speter
28017651Speter
28117651Speter#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
28217651Speter/* Minimum amount of lookahead, except at the end of the input file.
28317651Speter * See deflate.c for comments about the MIN_MATCH+1.
28417651Speter */
28517651Speter
28617651Speter#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
28717651Speter/* In order to simplify the code, particularly on 16 bit machines, match
28817651Speter * distances are limited to MAX_DIST instead of WSIZE.
28917651Speter */
29017651Speter
291205471Sdelphij#define WIN_INIT MAX_MATCH
292205471Sdelphij/* Number of bytes after end of data in window to initialize in order to avoid
293205471Sdelphij   memory checker errors from longest match routines */
294205471Sdelphij
29517651Speter        /* in trees.c */
296206924Sdelphijvoid ZLIB_INTERNAL _tr_init OF((deflate_state *s));
297206924Sdelphijint ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
298206924Sdelphijvoid ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
299206924Sdelphij                        ulg stored_len, int last));
300237410Sdelphijvoid ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
301206924Sdelphijvoid ZLIB_INTERNAL _tr_align OF((deflate_state *s));
302206924Sdelphijvoid ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
303206924Sdelphij                        ulg stored_len, int last));
30433908Ssteve
30533908Ssteve#define d_code(dist) \
30633908Ssteve   ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
30733908Ssteve/* Mapping from a distance to a distance code. dist is the distance - 1 and
30833908Ssteve * must not have side effects. _dist_code[256] and _dist_code[257] are never
30933908Ssteve * used.
31033908Ssteve */
31133908Ssteve
31233908Ssteve#ifndef DEBUG
31333908Ssteve/* Inline versions of _tr_tally for speed: */
31433908Ssteve
31533908Ssteve#if defined(GEN_TREES_H) || !defined(STDC)
316206924Sdelphij  extern uch ZLIB_INTERNAL _length_code[];
317206924Sdelphij  extern uch ZLIB_INTERNAL _dist_code[];
31833908Ssteve#else
319206924Sdelphij  extern const uch ZLIB_INTERNAL _length_code[];
320206924Sdelphij  extern const uch ZLIB_INTERNAL _dist_code[];
32117651Speter#endif
32233908Ssteve
32333908Ssteve# define _tr_tally_lit(s, c, flush) \
32433908Ssteve  { uch cc = (c); \
32533908Ssteve    s->d_buf[s->last_lit] = 0; \
32633908Ssteve    s->l_buf[s->last_lit++] = cc; \
32733908Ssteve    s->dyn_ltree[cc].Freq++; \
32833908Ssteve    flush = (s->last_lit == s->lit_bufsize-1); \
32933908Ssteve   }
33033908Ssteve# define _tr_tally_dist(s, distance, length, flush) \
33133908Ssteve  { uch len = (length); \
33233908Ssteve    ush dist = (distance); \
33333908Ssteve    s->d_buf[s->last_lit] = dist; \
33433908Ssteve    s->l_buf[s->last_lit++] = len; \
33533908Ssteve    dist--; \
33633908Ssteve    s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
33733908Ssteve    s->dyn_dtree[d_code(dist)].Freq++; \
33833908Ssteve    flush = (s->last_lit == s->lit_bufsize-1); \
33933908Ssteve  }
34033908Ssteve#else
34133908Ssteve# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
34233908Ssteve# define _tr_tally_dist(s, distance, length, flush) \
343131380Stjr              flush = _tr_tally(s, distance, length)
34433908Ssteve#endif
34533908Ssteve
346131380Stjr#endif /* DEFLATE_H */
347