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