| cannam@89 | 1 /* deflate.h -- internal compression state | 
| cannam@89 | 2  * Copyright (C) 1995-2012 Jean-loup Gailly | 
| cannam@89 | 3  * For conditions of distribution and use, see copyright notice in zlib.h | 
| cannam@89 | 4  */ | 
| cannam@89 | 5 | 
| cannam@89 | 6 /* WARNING: this file should *not* be used by applications. It is | 
| cannam@89 | 7    part of the implementation of the compression library and is | 
| cannam@89 | 8    subject to change. Applications should only use zlib.h. | 
| cannam@89 | 9  */ | 
| cannam@89 | 10 | 
| cannam@89 | 11 /* @(#) $Id$ */ | 
| cannam@89 | 12 | 
| cannam@89 | 13 #ifndef DEFLATE_H | 
| cannam@89 | 14 #define DEFLATE_H | 
| cannam@89 | 15 | 
| cannam@89 | 16 #include "zutil.h" | 
| cannam@89 | 17 | 
| cannam@89 | 18 /* define NO_GZIP when compiling if you want to disable gzip header and | 
| cannam@89 | 19    trailer creation by deflate().  NO_GZIP would be used to avoid linking in | 
| cannam@89 | 20    the crc code when it is not needed.  For shared libraries, gzip encoding | 
| cannam@89 | 21    should be left enabled. */ | 
| cannam@89 | 22 #ifndef NO_GZIP | 
| cannam@89 | 23 #  define GZIP | 
| cannam@89 | 24 #endif | 
| cannam@89 | 25 | 
| cannam@89 | 26 /* =========================================================================== | 
| cannam@89 | 27  * Internal compression state. | 
| cannam@89 | 28  */ | 
| cannam@89 | 29 | 
| cannam@89 | 30 #define LENGTH_CODES 29 | 
| cannam@89 | 31 /* number of length codes, not counting the special END_BLOCK code */ | 
| cannam@89 | 32 | 
| cannam@89 | 33 #define LITERALS  256 | 
| cannam@89 | 34 /* number of literal bytes 0..255 */ | 
| cannam@89 | 35 | 
| cannam@89 | 36 #define L_CODES (LITERALS+1+LENGTH_CODES) | 
| cannam@89 | 37 /* number of Literal or Length codes, including the END_BLOCK code */ | 
| cannam@89 | 38 | 
| cannam@89 | 39 #define D_CODES   30 | 
| cannam@89 | 40 /* number of distance codes */ | 
| cannam@89 | 41 | 
| cannam@89 | 42 #define BL_CODES  19 | 
| cannam@89 | 43 /* number of codes used to transfer the bit lengths */ | 
| cannam@89 | 44 | 
| cannam@89 | 45 #define HEAP_SIZE (2*L_CODES+1) | 
| cannam@89 | 46 /* maximum heap size */ | 
| cannam@89 | 47 | 
| cannam@89 | 48 #define MAX_BITS 15 | 
| cannam@89 | 49 /* All codes must not exceed MAX_BITS bits */ | 
| cannam@89 | 50 | 
| cannam@89 | 51 #define Buf_size 16 | 
| cannam@89 | 52 /* size of bit buffer in bi_buf */ | 
| cannam@89 | 53 | 
| cannam@89 | 54 #define INIT_STATE    42 | 
| cannam@89 | 55 #define EXTRA_STATE   69 | 
| cannam@89 | 56 #define NAME_STATE    73 | 
| cannam@89 | 57 #define COMMENT_STATE 91 | 
| cannam@89 | 58 #define HCRC_STATE   103 | 
| cannam@89 | 59 #define BUSY_STATE   113 | 
| cannam@89 | 60 #define FINISH_STATE 666 | 
| cannam@89 | 61 /* Stream status */ | 
| cannam@89 | 62 | 
| cannam@89 | 63 | 
| cannam@89 | 64 /* Data structure describing a single value and its code string. */ | 
| cannam@89 | 65 typedef struct ct_data_s { | 
| cannam@89 | 66     union { | 
| cannam@89 | 67         ush  freq;       /* frequency count */ | 
| cannam@89 | 68         ush  code;       /* bit string */ | 
| cannam@89 | 69     } fc; | 
| cannam@89 | 70     union { | 
| cannam@89 | 71         ush  dad;        /* father node in Huffman tree */ | 
| cannam@89 | 72         ush  len;        /* length of bit string */ | 
| cannam@89 | 73     } dl; | 
| cannam@89 | 74 } FAR ct_data; | 
| cannam@89 | 75 | 
| cannam@89 | 76 #define Freq fc.freq | 
| cannam@89 | 77 #define Code fc.code | 
| cannam@89 | 78 #define Dad  dl.dad | 
| cannam@89 | 79 #define Len  dl.len | 
| cannam@89 | 80 | 
| cannam@89 | 81 typedef struct static_tree_desc_s  static_tree_desc; | 
| cannam@89 | 82 | 
| cannam@89 | 83 typedef struct tree_desc_s { | 
| cannam@89 | 84     ct_data *dyn_tree;           /* the dynamic tree */ | 
| cannam@89 | 85     int     max_code;            /* largest code with non zero frequency */ | 
| cannam@89 | 86     static_tree_desc *stat_desc; /* the corresponding static tree */ | 
| cannam@89 | 87 } FAR tree_desc; | 
| cannam@89 | 88 | 
| cannam@89 | 89 typedef ush Pos; | 
| cannam@89 | 90 typedef Pos FAR Posf; | 
| cannam@89 | 91 typedef unsigned IPos; | 
| cannam@89 | 92 | 
| cannam@89 | 93 /* A Pos is an index in the character window. We use short instead of int to | 
| cannam@89 | 94  * save space in the various tables. IPos is used only for parameter passing. | 
| cannam@89 | 95  */ | 
| cannam@89 | 96 | 
| cannam@89 | 97 typedef struct internal_state { | 
| cannam@89 | 98     z_streamp strm;      /* pointer back to this zlib stream */ | 
| cannam@89 | 99     int   status;        /* as the name implies */ | 
| cannam@89 | 100     Bytef *pending_buf;  /* output still pending */ | 
| cannam@89 | 101     ulg   pending_buf_size; /* size of pending_buf */ | 
| cannam@89 | 102     Bytef *pending_out;  /* next pending byte to output to the stream */ | 
| cannam@89 | 103     uInt   pending;      /* nb of bytes in the pending buffer */ | 
| cannam@89 | 104     int   wrap;          /* bit 0 true for zlib, bit 1 true for gzip */ | 
| cannam@89 | 105     gz_headerp  gzhead;  /* gzip header information to write */ | 
| cannam@89 | 106     uInt   gzindex;      /* where in extra, name, or comment */ | 
| cannam@89 | 107     Byte  method;        /* STORED (for zip only) or DEFLATED */ | 
| cannam@89 | 108     int   last_flush;    /* value of flush param for previous deflate call */ | 
| cannam@89 | 109 | 
| cannam@89 | 110                 /* used by deflate.c: */ | 
| cannam@89 | 111 | 
| cannam@89 | 112     uInt  w_size;        /* LZ77 window size (32K by default) */ | 
| cannam@89 | 113     uInt  w_bits;        /* log2(w_size)  (8..16) */ | 
| cannam@89 | 114     uInt  w_mask;        /* w_size - 1 */ | 
| cannam@89 | 115 | 
| cannam@89 | 116     Bytef *window; | 
| cannam@89 | 117     /* Sliding window. Input bytes are read into the second half of the window, | 
| cannam@89 | 118      * and move to the first half later to keep a dictionary of at least wSize | 
| cannam@89 | 119      * bytes. With this organization, matches are limited to a distance of | 
| cannam@89 | 120      * wSize-MAX_MATCH bytes, but this ensures that IO is always | 
| cannam@89 | 121      * performed with a length multiple of the block size. Also, it limits | 
| cannam@89 | 122      * the window size to 64K, which is quite useful on MSDOS. | 
| cannam@89 | 123      * To do: use the user input buffer as sliding window. | 
| cannam@89 | 124      */ | 
| cannam@89 | 125 | 
| cannam@89 | 126     ulg window_size; | 
| cannam@89 | 127     /* Actual size of window: 2*wSize, except when the user input buffer | 
| cannam@89 | 128      * is directly used as sliding window. | 
| cannam@89 | 129      */ | 
| cannam@89 | 130 | 
| cannam@89 | 131     Posf *prev; | 
| cannam@89 | 132     /* Link to older string with same hash index. To limit the size of this | 
| cannam@89 | 133      * array to 64K, this link is maintained only for the last 32K strings. | 
| cannam@89 | 134      * An index in this array is thus a window index modulo 32K. | 
| cannam@89 | 135      */ | 
| cannam@89 | 136 | 
| cannam@89 | 137     Posf *head; /* Heads of the hash chains or NIL. */ | 
| cannam@89 | 138 | 
| cannam@89 | 139     uInt  ins_h;          /* hash index of string to be inserted */ | 
| cannam@89 | 140     uInt  hash_size;      /* number of elements in hash table */ | 
| cannam@89 | 141     uInt  hash_bits;      /* log2(hash_size) */ | 
| cannam@89 | 142     uInt  hash_mask;      /* hash_size-1 */ | 
| cannam@89 | 143 | 
| cannam@89 | 144     uInt  hash_shift; | 
| cannam@89 | 145     /* Number of bits by which ins_h must be shifted at each input | 
| cannam@89 | 146      * step. It must be such that after MIN_MATCH steps, the oldest | 
| cannam@89 | 147      * byte no longer takes part in the hash key, that is: | 
| cannam@89 | 148      *   hash_shift * MIN_MATCH >= hash_bits | 
| cannam@89 | 149      */ | 
| cannam@89 | 150 | 
| cannam@89 | 151     long block_start; | 
| cannam@89 | 152     /* Window position at the beginning of the current output block. Gets | 
| cannam@89 | 153      * negative when the window is moved backwards. | 
| cannam@89 | 154      */ | 
| cannam@89 | 155 | 
| cannam@89 | 156     uInt match_length;           /* length of best match */ | 
| cannam@89 | 157     IPos prev_match;             /* previous match */ | 
| cannam@89 | 158     int match_available;         /* set if previous match exists */ | 
| cannam@89 | 159     uInt strstart;               /* start of string to insert */ | 
| cannam@89 | 160     uInt match_start;            /* start of matching string */ | 
| cannam@89 | 161     uInt lookahead;              /* number of valid bytes ahead in window */ | 
| cannam@89 | 162 | 
| cannam@89 | 163     uInt prev_length; | 
| cannam@89 | 164     /* Length of the best match at previous step. Matches not greater than this | 
| cannam@89 | 165      * are discarded. This is used in the lazy match evaluation. | 
| cannam@89 | 166      */ | 
| cannam@89 | 167 | 
| cannam@89 | 168     uInt max_chain_length; | 
| cannam@89 | 169     /* To speed up deflation, hash chains are never searched beyond this | 
| cannam@89 | 170      * length.  A higher limit improves compression ratio but degrades the | 
| cannam@89 | 171      * speed. | 
| cannam@89 | 172      */ | 
| cannam@89 | 173 | 
| cannam@89 | 174     uInt max_lazy_match; | 
| cannam@89 | 175     /* Attempt to find a better match only when the current match is strictly | 
| cannam@89 | 176      * smaller than this value. This mechanism is used only for compression | 
| cannam@89 | 177      * levels >= 4. | 
| cannam@89 | 178      */ | 
| cannam@89 | 179 #   define max_insert_length  max_lazy_match | 
| cannam@89 | 180     /* Insert new strings in the hash table only if the match length is not | 
| cannam@89 | 181      * greater than this length. This saves time but degrades compression. | 
| cannam@89 | 182      * max_insert_length is used only for compression levels <= 3. | 
| cannam@89 | 183      */ | 
| cannam@89 | 184 | 
| cannam@89 | 185     int level;    /* compression level (1..9) */ | 
| cannam@89 | 186     int strategy; /* favor or force Huffman coding*/ | 
| cannam@89 | 187 | 
| cannam@89 | 188     uInt good_match; | 
| cannam@89 | 189     /* Use a faster search when the previous match is longer than this */ | 
| cannam@89 | 190 | 
| cannam@89 | 191     int nice_match; /* Stop searching when current match exceeds this */ | 
| cannam@89 | 192 | 
| cannam@89 | 193                 /* used by trees.c: */ | 
| cannam@89 | 194     /* Didn't use ct_data typedef below to suppress compiler warning */ | 
| cannam@89 | 195     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */ | 
| cannam@89 | 196     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ | 
| cannam@89 | 197     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */ | 
| cannam@89 | 198 | 
| cannam@89 | 199     struct tree_desc_s l_desc;               /* desc. for literal tree */ | 
| cannam@89 | 200     struct tree_desc_s d_desc;               /* desc. for distance tree */ | 
| cannam@89 | 201     struct tree_desc_s bl_desc;              /* desc. for bit length tree */ | 
| cannam@89 | 202 | 
| cannam@89 | 203     ush bl_count[MAX_BITS+1]; | 
| cannam@89 | 204     /* number of codes at each bit length for an optimal tree */ | 
| cannam@89 | 205 | 
| cannam@89 | 206     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */ | 
| cannam@89 | 207     int heap_len;               /* number of elements in the heap */ | 
| cannam@89 | 208     int heap_max;               /* element of largest frequency */ | 
| cannam@89 | 209     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. | 
| cannam@89 | 210      * The same heap array is used to build all trees. | 
| cannam@89 | 211      */ | 
| cannam@89 | 212 | 
| cannam@89 | 213     uch depth[2*L_CODES+1]; | 
| cannam@89 | 214     /* Depth of each subtree used as tie breaker for trees of equal frequency | 
| cannam@89 | 215      */ | 
| cannam@89 | 216 | 
| cannam@89 | 217     uchf *l_buf;          /* buffer for literals or lengths */ | 
| cannam@89 | 218 | 
| cannam@89 | 219     uInt  lit_bufsize; | 
| cannam@89 | 220     /* Size of match buffer for literals/lengths.  There are 4 reasons for | 
| cannam@89 | 221      * limiting lit_bufsize to 64K: | 
| cannam@89 | 222      *   - frequencies can be kept in 16 bit counters | 
| cannam@89 | 223      *   - if compression is not successful for the first block, all input | 
| cannam@89 | 224      *     data is still in the window so we can still emit a stored block even | 
| cannam@89 | 225      *     when input comes from standard input.  (This can also be done for | 
| cannam@89 | 226      *     all blocks if lit_bufsize is not greater than 32K.) | 
| cannam@89 | 227      *   - if compression is not successful for a file smaller than 64K, we can | 
| cannam@89 | 228      *     even emit a stored file instead of a stored block (saving 5 bytes). | 
| cannam@89 | 229      *     This is applicable only for zip (not gzip or zlib). | 
| cannam@89 | 230      *   - creating new Huffman trees less frequently may not provide fast | 
| cannam@89 | 231      *     adaptation to changes in the input data statistics. (Take for | 
| cannam@89 | 232      *     example a binary file with poorly compressible code followed by | 
| cannam@89 | 233      *     a highly compressible string table.) Smaller buffer sizes give | 
| cannam@89 | 234      *     fast adaptation but have of course the overhead of transmitting | 
| cannam@89 | 235      *     trees more frequently. | 
| cannam@89 | 236      *   - I can't count above 4 | 
| cannam@89 | 237      */ | 
| cannam@89 | 238 | 
| cannam@89 | 239     uInt last_lit;      /* running index in l_buf */ | 
| cannam@89 | 240 | 
| cannam@89 | 241     ushf *d_buf; | 
| cannam@89 | 242     /* Buffer for distances. To simplify the code, d_buf and l_buf have | 
| cannam@89 | 243      * the same number of elements. To use different lengths, an extra flag | 
| cannam@89 | 244      * array would be necessary. | 
| cannam@89 | 245      */ | 
| cannam@89 | 246 | 
| cannam@89 | 247     ulg opt_len;        /* bit length of current block with optimal trees */ | 
| cannam@89 | 248     ulg static_len;     /* bit length of current block with static trees */ | 
| cannam@89 | 249     uInt matches;       /* number of string matches in current block */ | 
| cannam@89 | 250     uInt insert;        /* bytes at end of window left to insert */ | 
| cannam@89 | 251 | 
| cannam@89 | 252 #ifdef DEBUG | 
| cannam@89 | 253     ulg compressed_len; /* total bit length of compressed file mod 2^32 */ | 
| cannam@89 | 254     ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */ | 
| cannam@89 | 255 #endif | 
| cannam@89 | 256 | 
| cannam@89 | 257     ush bi_buf; | 
| cannam@89 | 258     /* Output buffer. bits are inserted starting at the bottom (least | 
| cannam@89 | 259      * significant bits). | 
| cannam@89 | 260      */ | 
| cannam@89 | 261     int bi_valid; | 
| cannam@89 | 262     /* Number of valid bits in bi_buf.  All bits above the last valid bit | 
| cannam@89 | 263      * are always zero. | 
| cannam@89 | 264      */ | 
| cannam@89 | 265 | 
| cannam@89 | 266     ulg high_water; | 
| cannam@89 | 267     /* High water mark offset in window for initialized bytes -- bytes above | 
| cannam@89 | 268      * this are set to zero in order to avoid memory check warnings when | 
| cannam@89 | 269      * longest match routines access bytes past the input.  This is then | 
| cannam@89 | 270      * updated to the new high water mark. | 
| cannam@89 | 271      */ | 
| cannam@89 | 272 | 
| cannam@89 | 273 } FAR deflate_state; | 
| cannam@89 | 274 | 
| cannam@89 | 275 /* Output a byte on the stream. | 
| cannam@89 | 276  * IN assertion: there is enough room in pending_buf. | 
| cannam@89 | 277  */ | 
| cannam@89 | 278 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} | 
| cannam@89 | 279 | 
| cannam@89 | 280 | 
| cannam@89 | 281 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) | 
| cannam@89 | 282 /* Minimum amount of lookahead, except at the end of the input file. | 
| cannam@89 | 283  * See deflate.c for comments about the MIN_MATCH+1. | 
| cannam@89 | 284  */ | 
| cannam@89 | 285 | 
| cannam@89 | 286 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD) | 
| cannam@89 | 287 /* In order to simplify the code, particularly on 16 bit machines, match | 
| cannam@89 | 288  * distances are limited to MAX_DIST instead of WSIZE. | 
| cannam@89 | 289  */ | 
| cannam@89 | 290 | 
| cannam@89 | 291 #define WIN_INIT MAX_MATCH | 
| cannam@89 | 292 /* Number of bytes after end of data in window to initialize in order to avoid | 
| cannam@89 | 293    memory checker errors from longest match routines */ | 
| cannam@89 | 294 | 
| cannam@89 | 295         /* in trees.c */ | 
| cannam@89 | 296 void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); | 
| cannam@89 | 297 int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); | 
| cannam@89 | 298 void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, | 
| cannam@89 | 299                         ulg stored_len, int last)); | 
| cannam@89 | 300 void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); | 
| cannam@89 | 301 void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); | 
| cannam@89 | 302 void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, | 
| cannam@89 | 303                         ulg stored_len, int last)); | 
| cannam@89 | 304 | 
| cannam@89 | 305 #define d_code(dist) \ | 
| cannam@89 | 306    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) | 
| cannam@89 | 307 /* Mapping from a distance to a distance code. dist is the distance - 1 and | 
| cannam@89 | 308  * must not have side effects. _dist_code[256] and _dist_code[257] are never | 
| cannam@89 | 309  * used. | 
| cannam@89 | 310  */ | 
| cannam@89 | 311 | 
| cannam@89 | 312 #ifndef DEBUG | 
| cannam@89 | 313 /* Inline versions of _tr_tally for speed: */ | 
| cannam@89 | 314 | 
| cannam@89 | 315 #if defined(GEN_TREES_H) || !defined(STDC) | 
| cannam@89 | 316   extern uch ZLIB_INTERNAL _length_code[]; | 
| cannam@89 | 317   extern uch ZLIB_INTERNAL _dist_code[]; | 
| cannam@89 | 318 #else | 
| cannam@89 | 319   extern const uch ZLIB_INTERNAL _length_code[]; | 
| cannam@89 | 320   extern const uch ZLIB_INTERNAL _dist_code[]; | 
| cannam@89 | 321 #endif | 
| cannam@89 | 322 | 
| cannam@89 | 323 # define _tr_tally_lit(s, c, flush) \ | 
| cannam@89 | 324   { uch cc = (c); \ | 
| cannam@89 | 325     s->d_buf[s->last_lit] = 0; \ | 
| cannam@89 | 326     s->l_buf[s->last_lit++] = cc; \ | 
| cannam@89 | 327     s->dyn_ltree[cc].Freq++; \ | 
| cannam@89 | 328     flush = (s->last_lit == s->lit_bufsize-1); \ | 
| cannam@89 | 329    } | 
| cannam@89 | 330 # define _tr_tally_dist(s, distance, length, flush) \ | 
| cannam@89 | 331   { uch len = (length); \ | 
| cannam@89 | 332     ush dist = (distance); \ | 
| cannam@89 | 333     s->d_buf[s->last_lit] = dist; \ | 
| cannam@89 | 334     s->l_buf[s->last_lit++] = len; \ | 
| cannam@89 | 335     dist--; \ | 
| cannam@89 | 336     s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ | 
| cannam@89 | 337     s->dyn_dtree[d_code(dist)].Freq++; \ | 
| cannam@89 | 338     flush = (s->last_lit == s->lit_bufsize-1); \ | 
| cannam@89 | 339   } | 
| cannam@89 | 340 #else | 
| cannam@89 | 341 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) | 
| cannam@89 | 342 # define _tr_tally_dist(s, distance, length, flush) \ | 
| cannam@89 | 343               flush = _tr_tally(s, distance, length) | 
| cannam@89 | 344 #endif | 
| cannam@89 | 345 | 
| cannam@89 | 346 #endif /* DEFLATE_H */ |