- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# deflate.h

    00001 /* deflate.h -- internal compression state
    00002  * Copyright (C) 1995-2002 Jean-loup Gailly
    00003  * For conditions of distribution and use, see copyright notice in zlib.h
    00004  */
    00005
    00006 /* WARNING: this file should *not* be used by applications. It is
    00007    part of the implementation of the compression library and is
    00008    subject to change. Applications should only use zlib.h.
    00009  */
    00010
    00011 /* @(#) $Id: deflate.h,v 1.1 2003/11/09 01:37:56 gabest Exp $ */
    00012
    00013 #ifndef _DEFLATE_H
    00014 #define _DEFLATE_H
    00015
    00016 #include "zutil.h"
    00017
    00018 /* ===========================================================================
    00019  * Internal compression state.
    00020  */
    00021
    00022 #define LENGTH_CODES 29
    00023 /* number of length codes, not counting the special END_BLOCK code */
    00024
    00025 #define LITERALS  256
    00026 /* number of literal bytes 0..255 */
    00027
    00028 #define L_CODES (LITERALS+1+LENGTH_CODES)
    00029 /* number of Literal or Length codes, including the END_BLOCK code */
    00030
    00031 #define D_CODES   30
    00032 /* number of distance codes */
    00033
    00034 #define BL_CODES  19
    00035 /* number of codes used to transfer the bit lengths */
    00036
    00037 #define HEAP_SIZE (2*L_CODES+1)
    00038 /* maximum heap size */
    00039
    00040 #define MAX_BITS 15
    00041 /* All codes must not exceed MAX_BITS bits */
    00042
    00043 #define INIT_STATE    42
    00044 #define BUSY_STATE   113
    00045 #define FINISH_STATE 666
    00046 /* Stream status */
    00047
    00048
    00049 /* Data structure describing a single value and its code string. */
    00050 typedef struct ct_data_s {
    00051     union {
    00052         ush  freq;       /* frequency count */
    00053         ush  code;       /* bit string */
    00054     } fc;
    00055     union {
    00056         ush  dad;        /* father node in Huffman tree */
    00057         ush  len;        /* length of bit string */
    00058     } dl;
    00059 } FAR ct_data;
    00060
    00061 #define Freq fc.freq
    00062 #define Code fc.code
    00063 #define Dad  dl.dad
    00064 #define Len  dl.len
    00065
    00066 typedef struct static_tree_desc_s  static_tree_desc;
    00067
    00068 typedef struct tree_desc_s {
    00069     ct_data *dyn_tree;           /* the dynamic tree */
    00070     int     max_code;            /* largest code with non zero frequency */
    00071     static_tree_desc *stat_desc; /* the corresponding static tree */
    00072 } FAR tree_desc;
    00073
    00074 typedef ush Pos;
    00075 typedef Pos FAR Posf;
    00076 typedef unsigned IPos;
    00077
    00078 /* A Pos is an index in the character window. We use short instead of int to
    00079  * save space in the various tables. IPos is used only for parameter passing.
    00080  */
    00081
    00082 typedef struct internal_state {
    00083     z_streamp strm;      /* pointer back to this zlib stream */
    00084     int   status;        /* as the name implies */
    00085     Bytef *pending_buf;  /* output still pending */
    00086     ulg   pending_buf_size; /* size of pending_buf */
    00087     Bytef *pending_out;  /* next pending byte to output to the stream */
    00088     int   pending;       /* nb of bytes in the pending buffer */
    00089     int   noheader;      /* suppress zlib header and adler32 */
    00090     Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
    00091     Byte  method;        /* STORED (for zip only) or DEFLATED */
    00092     int   last_flush;    /* value of flush param for previous deflate call */
    00093
    00094                 /* used by deflate.c: */
    00095
    00096     uInt  w_size;        /* LZ77 window size (32K by default) */
    00097     uInt  w_bits;        /* log2(w_size)  (8..16) */
    00098     uInt  w_mask;        /* w_size - 1 */
    00099
    00100     Bytef *window;
    00101     /* Sliding window. Input bytes are read into the second half of the window,
    00102      * and move to the first half later to keep a dictionary of at least wSize
    00103      * bytes. With this organization, matches are limited to a distance of
    00104      * wSize-MAX_MATCH bytes, but this ensures that IO is always
    00105      * performed with a length multiple of the block size. Also, it limits
    00106      * the window size to 64K, which is quite useful on MSDOS.
    00107      * To do: use the user input buffer as sliding window.
    00108      */
    00109
    00110     ulg window_size;
    00111     /* Actual size of window: 2*wSize, except when the user input buffer
    00112      * is directly used as sliding window.
    00113      */
    00114
    00115     Posf *prev;
    00116     /* Link to older string with same hash index. To limit the size of this
    00117      * array to 64K, this link is maintained only for the last 32K strings.
    00118      * An index in this array is thus a window index modulo 32K.
    00119      */
    00120
    00121     Posf *head; /* Heads of the hash chains or NIL. */
    00122
    00123     uInt  ins_h;          /* hash index of string to be inserted */
    00124     uInt  hash_size;      /* number of elements in hash table */
    00125     uInt  hash_bits;      /* log2(hash_size) */
    00126     uInt  hash_mask;      /* hash_size-1 */
    00127
    00128     uInt  hash_shift;
    00129     /* Number of bits by which ins_h must be shifted at each input
    00130      * step. It must be such that after MIN_MATCH steps, the oldest
    00131      * byte no longer takes part in the hash key, that is:
    00132      *   hash_shift * MIN_MATCH >= hash_bits
    00133      */
    00134
    00135     long block_start;
    00136     /* Window position at the beginning of the current output block. Gets
    00137      * negative when the window is moved backwards.
    00138      */
    00139
    00140     uInt match_length;           /* length of best match */
    00141     IPos prev_match;             /* previous match */
    00142     int match_available;         /* set if previous match exists */
    00143     uInt strstart;               /* start of string to insert */
    00144     uInt match_start;            /* start of matching string */
    00145     uInt lookahead;              /* number of valid bytes ahead in window */
    00146
    00147     uInt prev_length;
    00148     /* Length of the best match at previous step. Matches not greater than this
    00149      * are discarded. This is used in the lazy match evaluation.
    00150      */
    00151
    00152     uInt max_chain_length;
    00153     /* To speed up deflation, hash chains are never searched beyond this
    00154      * length.  A higher limit improves compression ratio but degrades the
    00155      * speed.
    00156      */
    00157
    00158     uInt max_lazy_match;
    00159     /* Attempt to find a better match only when the current match is strictly
    00160      * smaller than this value. This mechanism is used only for compression
    00161      * levels >= 4.
    00162      */
    00163 #   define max_insert_length  max_lazy_match
    00164     /* Insert new strings in the hash table only if the match length is not
    00165      * greater than this length. This saves time but degrades compression.
    00166      * max_insert_length is used only for compression levels <= 3.
    00167      */
    00168
    00169     int level;    /* compression level (1..9) */
    00170     int strategy; /* favor or force Huffman coding*/
    00171
    00172     uInt good_match;
    00173     /* Use a faster search when the previous match is longer than this */
    00174
    00175     int nice_match; /* Stop searching when current match exceeds this */
    00176
    00177                 /* used by trees.c: */
    00178     /* Didn't use ct_data typedef below to supress compiler warning */
    00179     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
    00180     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
    00181     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
    00182
    00183     struct tree_desc_s l_desc;               /* desc. for literal tree */
    00184     struct tree_desc_s d_desc;               /* desc. for distance tree */
    00185     struct tree_desc_s bl_desc;              /* desc. for bit length tree */
    00186
    00187     ush bl_count[MAX_BITS+1];
    00188     /* number of codes at each bit length for an optimal tree */
    00189
    00190     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
    00191     int heap_len;               /* number of elements in the heap */
    00192     int heap_max;               /* element of largest frequency */
    00193     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
    00194      * The same heap array is used to build all trees.
    00195      */
    00196
    00197     uch depth[2*L_CODES+1];
    00198     /* Depth of each subtree used as tie breaker for trees of equal frequency
    00199      */
    00200
    00201     uchf *l_buf;          /* buffer for literals or lengths */
    00202
    00203     uInt  lit_bufsize;
    00204     /* Size of match buffer for literals/lengths.  There are 4 reasons for
    00205      * limiting lit_bufsize to 64K:
    00206      *   - frequencies can be kept in 16 bit counters
    00207      *   - if compression is not successful for the first block, all input
    00208      *     data is still in the window so we can still emit a stored block even
    00209      *     when input comes from standard input.  (This can also be done for
    00210      *     all blocks if lit_bufsize is not greater than 32K.)
    00211      *   - if compression is not successful for a file smaller than 64K, we can
    00212      *     even emit a stored file instead of a stored block (saving 5 bytes).
    00213      *     This is applicable only for zip (not gzip or zlib).
    00214      *   - creating new Huffman trees less frequently may not provide fast
    00215      *     adaptation to changes in the input data statistics. (Take for
    00216      *     example a binary file with poorly compressible code followed by
    00217      *     a highly compressible string table.) Smaller buffer sizes give
    00218      *     fast adaptation but have of course the overhead of transmitting
    00219      *     trees more frequently.
    00220      *   - I can't count above 4
    00221      */
    00222
    00223     uInt last_lit;      /* running index in l_buf */
    00224
    00225     ushf *d_buf;
    00226     /* Buffer for distances. To simplify the code, d_buf and l_buf have
    00227      * the same number of elements. To use different lengths, an extra flag
    00228      * array would be necessary.
    00229      */
    00230
    00231     ulg opt_len;        /* bit length of current block with optimal trees */
    00232     ulg static_len;     /* bit length of current block with static trees */
    00233     uInt matches;       /* number of string matches in current block */
    00234     int last_eob_len;   /* bit length of EOB code for last block */
    00235
    00236 #ifdef DEBUG
    00237     ulg compressed_len; /* total bit length of compressed file mod 2^32 */
    00238     ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */
    00239 #endif
    00240
    00241     ush bi_buf;
    00242     /* Output buffer. bits are inserted starting at the bottom (least
    00243      * significant bits).
    00244      */
    00245     int bi_valid;
    00246     /* Number of valid bits in bi_buf.  All bits above the last valid bit
    00247      * are always zero.
    00248      */
    00249
    00250 } FAR deflate_state;
    00251
    00252 /* Output a byte on the stream.
    00253  * IN assertion: there is enough room in pending_buf.
    00254  */
    00255 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
    00256
    00257
    00258 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
    00259 /* Minimum amount of lookahead, except at the end of the input file.
    00260  * See deflate.c for comments about the MIN_MATCH+1.
    00261  */
    00262
    00263 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
    00264 /* In order to simplify the code, particularly on 16 bit machines, match
    00265  * distances are limited to MAX_DIST instead of WSIZE.
    00266  */
    00267
    00268         /* in trees.c */
    00269 void _tr_init         OF((deflate_state *s));
    00270 int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
    00271 void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
    00272                           int eof));
    00273 void _tr_align        OF((deflate_state *s));
    00274 void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
    00275                           int eof));
    00276
    00277 #define d_code(dist) \
    00278    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
    00279 /* Mapping from a distance to a distance code. dist is the distance - 1 and
    00280  * must not have side effects. _dist_code[256] and _dist_code[257] are never
    00281  * used.
    00282  */
    00283
    00284 #ifndef DEBUG
    00285 /* Inline versions of _tr_tally for speed: */
    00286
    00287 #if defined(GEN_TREES_H) || !defined(STDC)
    00288   extern uch _length_code[];
    00289   extern uch _dist_code[];
    00290 #else
    00291   extern const uch _length_code[];
    00292   extern const uch _dist_code[];
    00293 #endif
    00294
    00295 # define _tr_tally_lit(s, c, flush) \
    00296   { uch cc = (c); \
    00297     s->d_buf[s->last_lit] = 0; \
    00298     s->l_buf[s->last_lit++] = cc; \
    00299     s->dyn_ltree[cc].Freq++; \
    00300     flush = (s->last_lit == s->lit_bufsize-1); \
    00301    }
    00302 # define _tr_tally_dist(s, distance, length, flush) \
    00303   { uch len = (length); \
    00304     ush dist = (distance); \
    00305     s->d_buf[s->last_lit] = dist; \
    00306     s->l_buf[s->last_lit++] = len; \
    00307     dist--; \
    00308     s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
    00309     s->dyn_dtree[d_code(dist)].Freq++; \
    00310     flush = (s->last_lit == s->lit_bufsize-1); \
    00311   }
    00312 #else
    00313 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
    00314 # define _tr_tally_dist(s, distance, length, flush) \
    00315               flush = _tr_tally(s, distance, length)
    00316 #endif
    00317
    00318 #endif