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