aboutsummaryrefslogtreecommitdiffstats
path: root/lib/zlib_deflate
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /lib/zlib_deflate
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'lib/zlib_deflate')
-rw-r--r--lib/zlib_deflate/Makefile11
-rw-r--r--lib/zlib_deflate/deflate.c1268
-rw-r--r--lib/zlib_deflate/deflate_syms.c21
-rw-r--r--lib/zlib_deflate/deftree.c1113
-rw-r--r--lib/zlib_deflate/defutil.h334
5 files changed, 2747 insertions, 0 deletions
diff --git a/lib/zlib_deflate/Makefile b/lib/zlib_deflate/Makefile
new file mode 100644
index 000000000000..86275e3fdcbc
--- /dev/null
+++ b/lib/zlib_deflate/Makefile
@@ -0,0 +1,11 @@
1#
2# This is a modified version of zlib, which does all memory
3# allocation ahead of time.
4#
5# This is the compression code, see zlib_inflate for the
6# decompression code.
7#
8
9obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate.o
10
11zlib_deflate-objs := deflate.o deftree.o deflate_syms.o
diff --git a/lib/zlib_deflate/deflate.c b/lib/zlib_deflate/deflate.c
new file mode 100644
index 000000000000..ad9a1bf4fc63
--- /dev/null
+++ b/lib/zlib_deflate/deflate.c
@@ -0,0 +1,1268 @@
1/* +++ deflate.c */
2/* deflate.c -- compress data using the deflation algorithm
3 * Copyright (C) 1995-1996 Jean-loup Gailly.
4 * For conditions of distribution and use, see copyright notice in zlib.h
5 */
6
7/*
8 * ALGORITHM
9 *
10 * The "deflation" process depends on being able to identify portions
11 * of the input text which are identical to earlier input (within a
12 * sliding window trailing behind the input currently being processed).
13 *
14 * The most straightforward technique turns out to be the fastest for
15 * most input files: try all possible matches and select the longest.
16 * The key feature of this algorithm is that insertions into the string
17 * dictionary are very simple and thus fast, and deletions are avoided
18 * completely. Insertions are performed at each input character, whereas
19 * string matches are performed only when the previous match ends. So it
20 * is preferable to spend more time in matches to allow very fast string
21 * insertions and avoid deletions. The matching algorithm for small
22 * strings is inspired from that of Rabin & Karp. A brute force approach
23 * is used to find longer strings when a small match has been found.
24 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
25 * (by Leonid Broukhis).
26 * A previous version of this file used a more sophisticated algorithm
27 * (by Fiala and Greene) which is guaranteed to run in linear amortized
28 * time, but has a larger average cost, uses more memory and is patented.
29 * However the F&G algorithm may be faster for some highly redundant
30 * files if the parameter max_chain_length (described below) is too large.
31 *
32 * ACKNOWLEDGEMENTS
33 *
34 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
35 * I found it in 'freeze' written by Leonid Broukhis.
36 * Thanks to many people for bug reports and testing.
37 *
38 * REFERENCES
39 *
40 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
41 * Available in ftp://ds.internic.net/rfc/rfc1951.txt
42 *
43 * A description of the Rabin and Karp algorithm is given in the book
44 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
45 *
46 * Fiala,E.R., and Greene,D.H.
47 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
48 *
49 */
50
51#include <linux/module.h>
52#include <linux/zutil.h>
53#include "defutil.h"
54
55
56/* ===========================================================================
57 * Function prototypes.
58 */
59typedef enum {
60 need_more, /* block not completed, need more input or more output */
61 block_done, /* block flush performed */
62 finish_started, /* finish started, need only more output at next deflate */
63 finish_done /* finish done, accept no more input or output */
64} block_state;
65
66typedef block_state (*compress_func) (deflate_state *s, int flush);
67/* Compression function. Returns the block state after the call. */
68
69static void fill_window (deflate_state *s);
70static block_state deflate_stored (deflate_state *s, int flush);
71static block_state deflate_fast (deflate_state *s, int flush);
72static block_state deflate_slow (deflate_state *s, int flush);
73static void lm_init (deflate_state *s);
74static void putShortMSB (deflate_state *s, uInt b);
75static void flush_pending (z_streamp strm);
76static int read_buf (z_streamp strm, Byte *buf, unsigned size);
77static uInt longest_match (deflate_state *s, IPos cur_match);
78
79#ifdef DEBUG_ZLIB
80static void check_match (deflate_state *s, IPos start, IPos match,
81 int length);
82#endif
83
84/* ===========================================================================
85 * Local data
86 */
87
88#define NIL 0
89/* Tail of hash chains */
90
91#ifndef TOO_FAR
92# define TOO_FAR 4096
93#endif
94/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
95
96#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
97/* Minimum amount of lookahead, except at the end of the input file.
98 * See deflate.c for comments about the MIN_MATCH+1.
99 */
100
101/* Values for max_lazy_match, good_match and max_chain_length, depending on
102 * the desired pack level (0..9). The values given below have been tuned to
103 * exclude worst case performance for pathological files. Better values may be
104 * found for specific files.
105 */
106typedef struct config_s {
107 ush good_length; /* reduce lazy search above this match length */
108 ush max_lazy; /* do not perform lazy search above this match length */
109 ush nice_length; /* quit search above this match length */
110 ush max_chain;
111 compress_func func;
112} config;
113
114static const config configuration_table[10] = {
115/* good lazy nice chain */
116/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
117/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
118/* 2 */ {4, 5, 16, 8, deflate_fast},
119/* 3 */ {4, 6, 32, 32, deflate_fast},
120
121/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
122/* 5 */ {8, 16, 32, 32, deflate_slow},
123/* 6 */ {8, 16, 128, 128, deflate_slow},
124/* 7 */ {8, 32, 128, 256, deflate_slow},
125/* 8 */ {32, 128, 258, 1024, deflate_slow},
126/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
127
128/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
129 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
130 * meaning.
131 */
132
133#define EQUAL 0
134/* result of memcmp for equal strings */
135
136/* ===========================================================================
137 * Update a hash value with the given input byte
138 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
139 * input characters, so that a running hash key can be computed from the
140 * previous key instead of complete recalculation each time.
141 */
142#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
143
144
145/* ===========================================================================
146 * Insert string str in the dictionary and set match_head to the previous head
147 * of the hash chain (the most recent string with same hash key). Return
148 * the previous length of the hash chain.
149 * IN assertion: all calls to to INSERT_STRING are made with consecutive
150 * input characters and the first MIN_MATCH bytes of str are valid
151 * (except for the last MIN_MATCH-1 bytes of the input file).
152 */
153#define INSERT_STRING(s, str, match_head) \
154 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
155 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
156 s->head[s->ins_h] = (Pos)(str))
157
158/* ===========================================================================
159 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
160 * prev[] will be initialized on the fly.
161 */
162#define CLEAR_HASH(s) \
163 s->head[s->hash_size-1] = NIL; \
164 memset((char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
165
166/* ========================================================================= */
167int zlib_deflateInit_(
168 z_streamp strm,
169 int level,
170 const char *version,
171 int stream_size
172)
173{
174 return zlib_deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS,
175 DEF_MEM_LEVEL,
176 Z_DEFAULT_STRATEGY, version, stream_size);
177 /* To do: ignore strm->next_in if we use it as window */
178}
179
180/* ========================================================================= */
181int zlib_deflateInit2_(
182 z_streamp strm,
183 int level,
184 int method,
185 int windowBits,
186 int memLevel,
187 int strategy,
188 const char *version,
189 int stream_size
190)
191{
192 deflate_state *s;
193 int noheader = 0;
194 static char* my_version = ZLIB_VERSION;
195 deflate_workspace *mem;
196
197 ush *overlay;
198 /* We overlay pending_buf and d_buf+l_buf. This works since the average
199 * output size for (length,distance) codes is <= 24 bits.
200 */
201
202 if (version == NULL || version[0] != my_version[0] ||
203 stream_size != sizeof(z_stream)) {
204 return Z_VERSION_ERROR;
205 }
206 if (strm == NULL) return Z_STREAM_ERROR;
207
208 strm->msg = NULL;
209
210 if (level == Z_DEFAULT_COMPRESSION) level = 6;
211
212 mem = (deflate_workspace *) strm->workspace;
213
214 if (windowBits < 0) { /* undocumented feature: suppress zlib header */
215 noheader = 1;
216 windowBits = -windowBits;
217 }
218 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
219 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
220 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
221 return Z_STREAM_ERROR;
222 }
223 s = (deflate_state *) &(mem->deflate_memory);
224 strm->state = (struct internal_state *)s;
225 s->strm = strm;
226
227 s->noheader = noheader;
228 s->w_bits = windowBits;
229 s->w_size = 1 << s->w_bits;
230 s->w_mask = s->w_size - 1;
231
232 s->hash_bits = memLevel + 7;
233 s->hash_size = 1 << s->hash_bits;
234 s->hash_mask = s->hash_size - 1;
235 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
236
237 s->window = (Byte *) mem->window_memory;
238 s->prev = (Pos *) mem->prev_memory;
239 s->head = (Pos *) mem->head_memory;
240
241 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
242
243 overlay = (ush *) mem->overlay_memory;
244 s->pending_buf = (uch *) overlay;
245 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
246
247 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
248 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
249
250 s->level = level;
251 s->strategy = strategy;
252 s->method = (Byte)method;
253
254 return zlib_deflateReset(strm);
255}
256
257/* ========================================================================= */
258int zlib_deflateSetDictionary(
259 z_streamp strm,
260 const Byte *dictionary,
261 uInt dictLength
262)
263{
264 deflate_state *s;
265 uInt length = dictLength;
266 uInt n;
267 IPos hash_head = 0;
268
269 if (strm == NULL || strm->state == NULL || dictionary == NULL)
270 return Z_STREAM_ERROR;
271
272 s = (deflate_state *) strm->state;
273 if (s->status != INIT_STATE) return Z_STREAM_ERROR;
274
275 strm->adler = zlib_adler32(strm->adler, dictionary, dictLength);
276
277 if (length < MIN_MATCH) return Z_OK;
278 if (length > MAX_DIST(s)) {
279 length = MAX_DIST(s);
280#ifndef USE_DICT_HEAD
281 dictionary += dictLength - length; /* use the tail of the dictionary */
282#endif
283 }
284 memcpy((char *)s->window, dictionary, length);
285 s->strstart = length;
286 s->block_start = (long)length;
287
288 /* Insert all strings in the hash table (except for the last two bytes).
289 * s->lookahead stays null, so s->ins_h will be recomputed at the next
290 * call of fill_window.
291 */
292 s->ins_h = s->window[0];
293 UPDATE_HASH(s, s->ins_h, s->window[1]);
294 for (n = 0; n <= length - MIN_MATCH; n++) {
295 INSERT_STRING(s, n, hash_head);
296 }
297 if (hash_head) hash_head = 0; /* to make compiler happy */
298 return Z_OK;
299}
300
301/* ========================================================================= */
302int zlib_deflateReset(
303 z_streamp strm
304)
305{
306 deflate_state *s;
307
308 if (strm == NULL || strm->state == NULL)
309 return Z_STREAM_ERROR;
310
311 strm->total_in = strm->total_out = 0;
312 strm->msg = NULL;
313 strm->data_type = Z_UNKNOWN;
314
315 s = (deflate_state *)strm->state;
316 s->pending = 0;
317 s->pending_out = s->pending_buf;
318
319 if (s->noheader < 0) {
320 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
321 }
322 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
323 strm->adler = 1;
324 s->last_flush = Z_NO_FLUSH;
325
326 zlib_tr_init(s);
327 lm_init(s);
328
329 return Z_OK;
330}
331
332/* ========================================================================= */
333int zlib_deflateParams(
334 z_streamp strm,
335 int level,
336 int strategy
337)
338{
339 deflate_state *s;
340 compress_func func;
341 int err = Z_OK;
342
343 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
344 s = (deflate_state *) strm->state;
345
346 if (level == Z_DEFAULT_COMPRESSION) {
347 level = 6;
348 }
349 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
350 return Z_STREAM_ERROR;
351 }
352 func = configuration_table[s->level].func;
353
354 if (func != configuration_table[level].func && strm->total_in != 0) {
355 /* Flush the last buffer: */
356 err = zlib_deflate(strm, Z_PARTIAL_FLUSH);
357 }
358 if (s->level != level) {
359 s->level = level;
360 s->max_lazy_match = configuration_table[level].max_lazy;
361 s->good_match = configuration_table[level].good_length;
362 s->nice_match = configuration_table[level].nice_length;
363 s->max_chain_length = configuration_table[level].max_chain;
364 }
365 s->strategy = strategy;
366 return err;
367}
368
369/* =========================================================================
370 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
371 * IN assertion: the stream state is correct and there is enough room in
372 * pending_buf.
373 */
374static void putShortMSB(
375 deflate_state *s,
376 uInt b
377)
378{
379 put_byte(s, (Byte)(b >> 8));
380 put_byte(s, (Byte)(b & 0xff));
381}
382
383/* =========================================================================
384 * Flush as much pending output as possible. All deflate() output goes
385 * through this function so some applications may wish to modify it
386 * to avoid allocating a large strm->next_out buffer and copying into it.
387 * (See also read_buf()).
388 */
389static void flush_pending(
390 z_streamp strm
391)
392{
393 deflate_state *s = (deflate_state *) strm->state;
394 unsigned len = s->pending;
395
396 if (len > strm->avail_out) len = strm->avail_out;
397 if (len == 0) return;
398
399 if (strm->next_out != NULL) {
400 memcpy(strm->next_out, s->pending_out, len);
401 strm->next_out += len;
402 }
403 s->pending_out += len;
404 strm->total_out += len;
405 strm->avail_out -= len;
406 s->pending -= len;
407 if (s->pending == 0) {
408 s->pending_out = s->pending_buf;
409 }
410}
411
412/* ========================================================================= */
413int zlib_deflate(
414 z_streamp strm,
415 int flush
416)
417{
418 int old_flush; /* value of flush param for previous deflate call */
419 deflate_state *s;
420
421 if (strm == NULL || strm->state == NULL ||
422 flush > Z_FINISH || flush < 0) {
423 return Z_STREAM_ERROR;
424 }
425 s = (deflate_state *) strm->state;
426
427 if ((strm->next_in == NULL && strm->avail_in != 0) ||
428 (s->status == FINISH_STATE && flush != Z_FINISH)) {
429 return Z_STREAM_ERROR;
430 }
431 if (strm->avail_out == 0) return Z_BUF_ERROR;
432
433 s->strm = strm; /* just in case */
434 old_flush = s->last_flush;
435 s->last_flush = flush;
436
437 /* Write the zlib header */
438 if (s->status == INIT_STATE) {
439
440 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
441 uInt level_flags = (s->level-1) >> 1;
442
443 if (level_flags > 3) level_flags = 3;
444 header |= (level_flags << 6);
445 if (s->strstart != 0) header |= PRESET_DICT;
446 header += 31 - (header % 31);
447
448 s->status = BUSY_STATE;
449 putShortMSB(s, header);
450
451 /* Save the adler32 of the preset dictionary: */
452 if (s->strstart != 0) {
453 putShortMSB(s, (uInt)(strm->adler >> 16));
454 putShortMSB(s, (uInt)(strm->adler & 0xffff));
455 }
456 strm->adler = 1L;
457 }
458
459 /* Flush as much pending output as possible */
460 if (s->pending != 0) {
461 flush_pending(strm);
462 if (strm->avail_out == 0) {
463 /* Since avail_out is 0, deflate will be called again with
464 * more output space, but possibly with both pending and
465 * avail_in equal to zero. There won't be anything to do,
466 * but this is not an error situation so make sure we
467 * return OK instead of BUF_ERROR at next call of deflate:
468 */
469 s->last_flush = -1;
470 return Z_OK;
471 }
472
473 /* Make sure there is something to do and avoid duplicate consecutive
474 * flushes. For repeated and useless calls with Z_FINISH, we keep
475 * returning Z_STREAM_END instead of Z_BUFF_ERROR.
476 */
477 } else if (strm->avail_in == 0 && flush <= old_flush &&
478 flush != Z_FINISH) {
479 return Z_BUF_ERROR;
480 }
481
482 /* User must not provide more input after the first FINISH: */
483 if (s->status == FINISH_STATE && strm->avail_in != 0) {
484 return Z_BUF_ERROR;
485 }
486
487 /* Start a new block or continue the current one.
488 */
489 if (strm->avail_in != 0 || s->lookahead != 0 ||
490 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
491 block_state bstate;
492
493 bstate = (*(configuration_table[s->level].func))(s, flush);
494
495 if (bstate == finish_started || bstate == finish_done) {
496 s->status = FINISH_STATE;
497 }
498 if (bstate == need_more || bstate == finish_started) {
499 if (strm->avail_out == 0) {
500 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
501 }
502 return Z_OK;
503 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
504 * of deflate should use the same flush parameter to make sure
505 * that the flush is complete. So we don't have to output an
506 * empty block here, this will be done at next call. This also
507 * ensures that for a very small output buffer, we emit at most
508 * one empty block.
509 */
510 }
511 if (bstate == block_done) {
512 if (flush == Z_PARTIAL_FLUSH) {
513 zlib_tr_align(s);
514 } else if (flush == Z_PACKET_FLUSH) {
515 /* Output just the 3-bit `stored' block type value,
516 but not a zero length. */
517 zlib_tr_stored_type_only(s);
518 } else { /* FULL_FLUSH or SYNC_FLUSH */
519 zlib_tr_stored_block(s, (char*)0, 0L, 0);
520 /* For a full flush, this empty block will be recognized
521 * as a special marker by inflate_sync().
522 */
523 if (flush == Z_FULL_FLUSH) {
524 CLEAR_HASH(s); /* forget history */
525 }
526 }
527 flush_pending(strm);
528 if (strm->avail_out == 0) {
529 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
530 return Z_OK;
531 }
532 }
533 }
534 Assert(strm->avail_out > 0, "bug2");
535
536 if (flush != Z_FINISH) return Z_OK;
537 if (s->noheader) return Z_STREAM_END;
538
539 /* Write the zlib trailer (adler32) */
540 putShortMSB(s, (uInt)(strm->adler >> 16));
541 putShortMSB(s, (uInt)(strm->adler & 0xffff));
542 flush_pending(strm);
543 /* If avail_out is zero, the application will call deflate again
544 * to flush the rest.
545 */
546 s->noheader = -1; /* write the trailer only once! */
547 return s->pending != 0 ? Z_OK : Z_STREAM_END;
548}
549
550/* ========================================================================= */
551int zlib_deflateEnd(
552 z_streamp strm
553)
554{
555 int status;
556 deflate_state *s;
557
558 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
559 s = (deflate_state *) strm->state;
560
561 status = s->status;
562 if (status != INIT_STATE && status != BUSY_STATE &&
563 status != FINISH_STATE) {
564 return Z_STREAM_ERROR;
565 }
566
567 strm->state = NULL;
568
569 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
570}
571
572/* =========================================================================
573 * Copy the source state to the destination state.
574 */
575int zlib_deflateCopy (
576 z_streamp dest,
577 z_streamp source
578)
579{
580#ifdef MAXSEG_64K
581 return Z_STREAM_ERROR;
582#else
583 deflate_state *ds;
584 deflate_state *ss;
585 ush *overlay;
586 deflate_workspace *mem;
587
588
589 if (source == NULL || dest == NULL || source->state == NULL) {
590 return Z_STREAM_ERROR;
591 }
592
593 ss = (deflate_state *) source->state;
594
595 *dest = *source;
596
597 mem = (deflate_workspace *) dest->workspace;
598
599 ds = &(mem->deflate_memory);
600
601 dest->state = (struct internal_state *) ds;
602 *ds = *ss;
603 ds->strm = dest;
604
605 ds->window = (Byte *) mem->window_memory;
606 ds->prev = (Pos *) mem->prev_memory;
607 ds->head = (Pos *) mem->head_memory;
608 overlay = (ush *) mem->overlay_memory;
609 ds->pending_buf = (uch *) overlay;
610
611 memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
612 memcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
613 memcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
614 memcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
615
616 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
617 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
618 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
619
620 ds->l_desc.dyn_tree = ds->dyn_ltree;
621 ds->d_desc.dyn_tree = ds->dyn_dtree;
622 ds->bl_desc.dyn_tree = ds->bl_tree;
623
624 return Z_OK;
625#endif
626}
627
628/* ===========================================================================
629 * Read a new buffer from the current input stream, update the adler32
630 * and total number of bytes read. All deflate() input goes through
631 * this function so some applications may wish to modify it to avoid
632 * allocating a large strm->next_in buffer and copying from it.
633 * (See also flush_pending()).
634 */
635static int read_buf(
636 z_streamp strm,
637 Byte *buf,
638 unsigned size
639)
640{
641 unsigned len = strm->avail_in;
642
643 if (len > size) len = size;
644 if (len == 0) return 0;
645
646 strm->avail_in -= len;
647
648 if (!((deflate_state *)(strm->state))->noheader) {
649 strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
650 }
651 memcpy(buf, strm->next_in, len);
652 strm->next_in += len;
653 strm->total_in += len;
654
655 return (int)len;
656}
657
658/* ===========================================================================
659 * Initialize the "longest match" routines for a new zlib stream
660 */
661static void lm_init(
662 deflate_state *s
663)
664{
665 s->window_size = (ulg)2L*s->w_size;
666
667 CLEAR_HASH(s);
668
669 /* Set the default configuration parameters:
670 */
671 s->max_lazy_match = configuration_table[s->level].max_lazy;
672 s->good_match = configuration_table[s->level].good_length;
673 s->nice_match = configuration_table[s->level].nice_length;
674 s->max_chain_length = configuration_table[s->level].max_chain;
675
676 s->strstart = 0;
677 s->block_start = 0L;
678 s->lookahead = 0;
679 s->match_length = s->prev_length = MIN_MATCH-1;
680 s->match_available = 0;
681 s->ins_h = 0;
682}
683
684/* ===========================================================================
685 * Set match_start to the longest match starting at the given string and
686 * return its length. Matches shorter or equal to prev_length are discarded,
687 * in which case the result is equal to prev_length and match_start is
688 * garbage.
689 * IN assertions: cur_match is the head of the hash chain for the current
690 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
691 * OUT assertion: the match length is not greater than s->lookahead.
692 */
693/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
694 * match.S. The code will be functionally equivalent.
695 */
696static uInt longest_match(
697 deflate_state *s,
698 IPos cur_match /* current match */
699)
700{
701 unsigned chain_length = s->max_chain_length;/* max hash chain length */
702 register Byte *scan = s->window + s->strstart; /* current string */
703 register Byte *match; /* matched string */
704 register int len; /* length of current match */
705 int best_len = s->prev_length; /* best match length so far */
706 int nice_match = s->nice_match; /* stop if match long enough */
707 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
708 s->strstart - (IPos)MAX_DIST(s) : NIL;
709 /* Stop when cur_match becomes <= limit. To simplify the code,
710 * we prevent matches with the string of window index 0.
711 */
712 Pos *prev = s->prev;
713 uInt wmask = s->w_mask;
714
715#ifdef UNALIGNED_OK
716 /* Compare two bytes at a time. Note: this is not always beneficial.
717 * Try with and without -DUNALIGNED_OK to check.
718 */
719 register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
720 register ush scan_start = *(ush*)scan;
721 register ush scan_end = *(ush*)(scan+best_len-1);
722#else
723 register Byte *strend = s->window + s->strstart + MAX_MATCH;
724 register Byte scan_end1 = scan[best_len-1];
725 register Byte scan_end = scan[best_len];
726#endif
727
728 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
729 * It is easy to get rid of this optimization if necessary.
730 */
731 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
732
733 /* Do not waste too much time if we already have a good match: */
734 if (s->prev_length >= s->good_match) {
735 chain_length >>= 2;
736 }
737 /* Do not look for matches beyond the end of the input. This is necessary
738 * to make deflate deterministic.
739 */
740 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
741
742 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
743
744 do {
745 Assert(cur_match < s->strstart, "no future");
746 match = s->window + cur_match;
747
748 /* Skip to next match if the match length cannot increase
749 * or if the match length is less than 2:
750 */
751#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
752 /* This code assumes sizeof(unsigned short) == 2. Do not use
753 * UNALIGNED_OK if your compiler uses a different size.
754 */
755 if (*(ush*)(match+best_len-1) != scan_end ||
756 *(ush*)match != scan_start) continue;
757
758 /* It is not necessary to compare scan[2] and match[2] since they are
759 * always equal when the other bytes match, given that the hash keys
760 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
761 * strstart+3, +5, ... up to strstart+257. We check for insufficient
762 * lookahead only every 4th comparison; the 128th check will be made
763 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
764 * necessary to put more guard bytes at the end of the window, or
765 * to check more often for insufficient lookahead.
766 */
767 Assert(scan[2] == match[2], "scan[2]?");
768 scan++, match++;
769 do {
770 } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
771 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
772 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
773 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
774 scan < strend);
775 /* The funny "do {}" generates better code on most compilers */
776
777 /* Here, scan <= window+strstart+257 */
778 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
779 if (*scan == *match) scan++;
780
781 len = (MAX_MATCH - 1) - (int)(strend-scan);
782 scan = strend - (MAX_MATCH-1);
783
784#else /* UNALIGNED_OK */
785
786 if (match[best_len] != scan_end ||
787 match[best_len-1] != scan_end1 ||
788 *match != *scan ||
789 *++match != scan[1]) continue;
790
791 /* The check at best_len-1 can be removed because it will be made
792 * again later. (This heuristic is not always a win.)
793 * It is not necessary to compare scan[2] and match[2] since they
794 * are always equal when the other bytes match, given that
795 * the hash keys are equal and that HASH_BITS >= 8.
796 */
797 scan += 2, match++;
798 Assert(*scan == *match, "match[2]?");
799
800 /* We check for insufficient lookahead only every 8th comparison;
801 * the 256th check will be made at strstart+258.
802 */
803 do {
804 } while (*++scan == *++match && *++scan == *++match &&
805 *++scan == *++match && *++scan == *++match &&
806 *++scan == *++match && *++scan == *++match &&
807 *++scan == *++match && *++scan == *++match &&
808 scan < strend);
809
810 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
811
812 len = MAX_MATCH - (int)(strend - scan);
813 scan = strend - MAX_MATCH;
814
815#endif /* UNALIGNED_OK */
816
817 if (len > best_len) {
818 s->match_start = cur_match;
819 best_len = len;
820 if (len >= nice_match) break;
821#ifdef UNALIGNED_OK
822 scan_end = *(ush*)(scan+best_len-1);
823#else
824 scan_end1 = scan[best_len-1];
825 scan_end = scan[best_len];
826#endif
827 }
828 } while ((cur_match = prev[cur_match & wmask]) > limit
829 && --chain_length != 0);
830
831 if ((uInt)best_len <= s->lookahead) return best_len;
832 return s->lookahead;
833}
834
835#ifdef DEBUG_ZLIB
836/* ===========================================================================
837 * Check that the match at match_start is indeed a match.
838 */
839static void check_match(
840 deflate_state *s,
841 IPos start,
842 IPos match,
843 int length
844)
845{
846 /* check that the match is indeed a match */
847 if (memcmp((char *)s->window + match,
848 (char *)s->window + start, length) != EQUAL) {
849 fprintf(stderr, " start %u, match %u, length %d\n",
850 start, match, length);
851 do {
852 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
853 } while (--length != 0);
854 z_error("invalid match");
855 }
856 if (z_verbose > 1) {
857 fprintf(stderr,"\\[%d,%d]", start-match, length);
858 do { putc(s->window[start++], stderr); } while (--length != 0);
859 }
860}
861#else
862# define check_match(s, start, match, length)
863#endif
864
865/* ===========================================================================
866 * Fill the window when the lookahead becomes insufficient.
867 * Updates strstart and lookahead.
868 *
869 * IN assertion: lookahead < MIN_LOOKAHEAD
870 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
871 * At least one byte has been read, or avail_in == 0; reads are
872 * performed for at least two bytes (required for the zip translate_eol
873 * option -- not supported here).
874 */
875static void fill_window(
876 deflate_state *s
877)
878{
879 register unsigned n, m;
880 register Pos *p;
881 unsigned more; /* Amount of free space at the end of the window. */
882 uInt wsize = s->w_size;
883
884 do {
885 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
886
887 /* Deal with !@#$% 64K limit: */
888 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
889 more = wsize;
890
891 } else if (more == (unsigned)(-1)) {
892 /* Very unlikely, but possible on 16 bit machine if strstart == 0
893 * and lookahead == 1 (input done one byte at time)
894 */
895 more--;
896
897 /* If the window is almost full and there is insufficient lookahead,
898 * move the upper half to the lower one to make room in the upper half.
899 */
900 } else if (s->strstart >= wsize+MAX_DIST(s)) {
901
902 memcpy((char *)s->window, (char *)s->window+wsize,
903 (unsigned)wsize);
904 s->match_start -= wsize;
905 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
906 s->block_start -= (long) wsize;
907
908 /* Slide the hash table (could be avoided with 32 bit values
909 at the expense of memory usage). We slide even when level == 0
910 to keep the hash table consistent if we switch back to level > 0
911 later. (Using level 0 permanently is not an optimal usage of
912 zlib, so we don't care about this pathological case.)
913 */
914 n = s->hash_size;
915 p = &s->head[n];
916 do {
917 m = *--p;
918 *p = (Pos)(m >= wsize ? m-wsize : NIL);
919 } while (--n);
920
921 n = wsize;
922 p = &s->prev[n];
923 do {
924 m = *--p;
925 *p = (Pos)(m >= wsize ? m-wsize : NIL);
926 /* If n is not on any hash chain, prev[n] is garbage but
927 * its value will never be used.
928 */
929 } while (--n);
930 more += wsize;
931 }
932 if (s->strm->avail_in == 0) return;
933
934 /* If there was no sliding:
935 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
936 * more == window_size - lookahead - strstart
937 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
938 * => more >= window_size - 2*WSIZE + 2
939 * In the BIG_MEM or MMAP case (not yet supported),
940 * window_size == input_size + MIN_LOOKAHEAD &&
941 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
942 * Otherwise, window_size == 2*WSIZE so more >= 2.
943 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
944 */
945 Assert(more >= 2, "more < 2");
946
947 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
948 s->lookahead += n;
949
950 /* Initialize the hash value now that we have some input: */
951 if (s->lookahead >= MIN_MATCH) {
952 s->ins_h = s->window[s->strstart];
953 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
954#if MIN_MATCH != 3
955 Call UPDATE_HASH() MIN_MATCH-3 more times
956#endif
957 }
958 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
959 * but this is not important since only literal bytes will be emitted.
960 */
961
962 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
963}
964
965/* ===========================================================================
966 * Flush the current block, with given end-of-file flag.
967 * IN assertion: strstart is set to the end of the current match.
968 */
969#define FLUSH_BLOCK_ONLY(s, eof) { \
970 zlib_tr_flush_block(s, (s->block_start >= 0L ? \
971 (char *)&s->window[(unsigned)s->block_start] : \
972 NULL), \
973 (ulg)((long)s->strstart - s->block_start), \
974 (eof)); \
975 s->block_start = s->strstart; \
976 flush_pending(s->strm); \
977 Tracev((stderr,"[FLUSH]")); \
978}
979
980/* Same but force premature exit if necessary. */
981#define FLUSH_BLOCK(s, eof) { \
982 FLUSH_BLOCK_ONLY(s, eof); \
983 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
984}
985
986/* ===========================================================================
987 * Copy without compression as much as possible from the input stream, return
988 * the current block state.
989 * This function does not insert new strings in the dictionary since
990 * uncompressible data is probably not useful. This function is used
991 * only for the level=0 compression option.
992 * NOTE: this function should be optimized to avoid extra copying from
993 * window to pending_buf.
994 */
995static block_state deflate_stored(
996 deflate_state *s,
997 int flush
998)
999{
1000 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1001 * to pending_buf_size, and each stored block has a 5 byte header:
1002 */
1003 ulg max_block_size = 0xffff;
1004 ulg max_start;
1005
1006 if (max_block_size > s->pending_buf_size - 5) {
1007 max_block_size = s->pending_buf_size - 5;
1008 }
1009
1010 /* Copy as much as possible from input to output: */
1011 for (;;) {
1012 /* Fill the window as much as possible: */
1013 if (s->lookahead <= 1) {
1014
1015 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1016 s->block_start >= (long)s->w_size, "slide too late");
1017
1018 fill_window(s);
1019 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1020
1021 if (s->lookahead == 0) break; /* flush the current block */
1022 }
1023 Assert(s->block_start >= 0L, "block gone");
1024
1025 s->strstart += s->lookahead;
1026 s->lookahead = 0;
1027
1028 /* Emit a stored block if pending_buf will be full: */
1029 max_start = s->block_start + max_block_size;
1030 if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1031 /* strstart == 0 is possible when wraparound on 16-bit machine */
1032 s->lookahead = (uInt)(s->strstart - max_start);
1033 s->strstart = (uInt)max_start;
1034 FLUSH_BLOCK(s, 0);
1035 }
1036 /* Flush if we may have to slide, otherwise block_start may become
1037 * negative and the data will be gone:
1038 */
1039 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1040 FLUSH_BLOCK(s, 0);
1041 }
1042 }
1043 FLUSH_BLOCK(s, flush == Z_FINISH);
1044 return flush == Z_FINISH ? finish_done : block_done;
1045}
1046
1047/* ===========================================================================
1048 * Compress as much as possible from the input stream, return the current
1049 * block state.
1050 * This function does not perform lazy evaluation of matches and inserts
1051 * new strings in the dictionary only for unmatched strings or for short
1052 * matches. It is used only for the fast compression options.
1053 */
1054static block_state deflate_fast(
1055 deflate_state *s,
1056 int flush
1057)
1058{
1059 IPos hash_head = NIL; /* head of the hash chain */
1060 int bflush; /* set if current block must be flushed */
1061
1062 for (;;) {
1063 /* Make sure that we always have enough lookahead, except
1064 * at the end of the input file. We need MAX_MATCH bytes
1065 * for the next match, plus MIN_MATCH bytes to insert the
1066 * string following the next match.
1067 */
1068 if (s->lookahead < MIN_LOOKAHEAD) {
1069 fill_window(s);
1070 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1071 return need_more;
1072 }
1073 if (s->lookahead == 0) break; /* flush the current block */
1074 }
1075
1076 /* Insert the string window[strstart .. strstart+2] in the
1077 * dictionary, and set hash_head to the head of the hash chain:
1078 */
1079 if (s->lookahead >= MIN_MATCH) {
1080 INSERT_STRING(s, s->strstart, hash_head);
1081 }
1082
1083 /* Find the longest match, discarding those <= prev_length.
1084 * At this point we have always match_length < MIN_MATCH
1085 */
1086 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1087 /* To simplify the code, we prevent matches with the string
1088 * of window index 0 (in particular we have to avoid a match
1089 * of the string with itself at the start of the input file).
1090 */
1091 if (s->strategy != Z_HUFFMAN_ONLY) {
1092 s->match_length = longest_match (s, hash_head);
1093 }
1094 /* longest_match() sets match_start */
1095 }
1096 if (s->match_length >= MIN_MATCH) {
1097 check_match(s, s->strstart, s->match_start, s->match_length);
1098
1099 bflush = zlib_tr_tally(s, s->strstart - s->match_start,
1100 s->match_length - MIN_MATCH);
1101
1102 s->lookahead -= s->match_length;
1103
1104 /* Insert new strings in the hash table only if the match length
1105 * is not too large. This saves time but degrades compression.
1106 */
1107 if (s->match_length <= s->max_insert_length &&
1108 s->lookahead >= MIN_MATCH) {
1109 s->match_length--; /* string at strstart already in hash table */
1110 do {
1111 s->strstart++;
1112 INSERT_STRING(s, s->strstart, hash_head);
1113 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1114 * always MIN_MATCH bytes ahead.
1115 */
1116 } while (--s->match_length != 0);
1117 s->strstart++;
1118 } else {
1119 s->strstart += s->match_length;
1120 s->match_length = 0;
1121 s->ins_h = s->window[s->strstart];
1122 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1123#if MIN_MATCH != 3
1124 Call UPDATE_HASH() MIN_MATCH-3 more times
1125#endif
1126 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1127 * matter since it will be recomputed at next deflate call.
1128 */
1129 }
1130 } else {
1131 /* No match, output a literal byte */
1132 Tracevv((stderr,"%c", s->window[s->strstart]));
1133 bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
1134 s->lookahead--;
1135 s->strstart++;
1136 }
1137 if (bflush) FLUSH_BLOCK(s, 0);
1138 }
1139 FLUSH_BLOCK(s, flush == Z_FINISH);
1140 return flush == Z_FINISH ? finish_done : block_done;
1141}
1142
1143/* ===========================================================================
1144 * Same as above, but achieves better compression. We use a lazy
1145 * evaluation for matches: a match is finally adopted only if there is
1146 * no better match at the next window position.
1147 */
1148static block_state deflate_slow(
1149 deflate_state *s,
1150 int flush
1151)
1152{
1153 IPos hash_head = NIL; /* head of hash chain */
1154 int bflush; /* set if current block must be flushed */
1155
1156 /* Process the input block. */
1157 for (;;) {
1158 /* Make sure that we always have enough lookahead, except
1159 * at the end of the input file. We need MAX_MATCH bytes
1160 * for the next match, plus MIN_MATCH bytes to insert the
1161 * string following the next match.
1162 */
1163 if (s->lookahead < MIN_LOOKAHEAD) {
1164 fill_window(s);
1165 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1166 return need_more;
1167 }
1168 if (s->lookahead == 0) break; /* flush the current block */
1169 }
1170
1171 /* Insert the string window[strstart .. strstart+2] in the
1172 * dictionary, and set hash_head to the head of the hash chain:
1173 */
1174 if (s->lookahead >= MIN_MATCH) {
1175 INSERT_STRING(s, s->strstart, hash_head);
1176 }
1177
1178 /* Find the longest match, discarding those <= prev_length.
1179 */
1180 s->prev_length = s->match_length, s->prev_match = s->match_start;
1181 s->match_length = MIN_MATCH-1;
1182
1183 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1184 s->strstart - hash_head <= MAX_DIST(s)) {
1185 /* To simplify the code, we prevent matches with the string
1186 * of window index 0 (in particular we have to avoid a match
1187 * of the string with itself at the start of the input file).
1188 */
1189 if (s->strategy != Z_HUFFMAN_ONLY) {
1190 s->match_length = longest_match (s, hash_head);
1191 }
1192 /* longest_match() sets match_start */
1193
1194 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1195 (s->match_length == MIN_MATCH &&
1196 s->strstart - s->match_start > TOO_FAR))) {
1197
1198 /* If prev_match is also MIN_MATCH, match_start is garbage
1199 * but we will ignore the current match anyway.
1200 */
1201 s->match_length = MIN_MATCH-1;
1202 }
1203 }
1204 /* If there was a match at the previous step and the current
1205 * match is not better, output the previous match:
1206 */
1207 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1208 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1209 /* Do not insert strings in hash table beyond this. */
1210
1211 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1212
1213 bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match,
1214 s->prev_length - MIN_MATCH);
1215
1216 /* Insert in hash table all strings up to the end of the match.
1217 * strstart-1 and strstart are already inserted. If there is not
1218 * enough lookahead, the last two strings are not inserted in
1219 * the hash table.
1220 */
1221 s->lookahead -= s->prev_length-1;
1222 s->prev_length -= 2;
1223 do {
1224 if (++s->strstart <= max_insert) {
1225 INSERT_STRING(s, s->strstart, hash_head);
1226 }
1227 } while (--s->prev_length != 0);
1228 s->match_available = 0;
1229 s->match_length = MIN_MATCH-1;
1230 s->strstart++;
1231
1232 if (bflush) FLUSH_BLOCK(s, 0);
1233
1234 } else if (s->match_available) {
1235 /* If there was no match at the previous position, output a
1236 * single literal. If there was a match but the current match
1237 * is longer, truncate the previous match to a single literal.
1238 */
1239 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1240 if (zlib_tr_tally (s, 0, s->window[s->strstart-1])) {
1241 FLUSH_BLOCK_ONLY(s, 0);
1242 }
1243 s->strstart++;
1244 s->lookahead--;
1245 if (s->strm->avail_out == 0) return need_more;
1246 } else {
1247 /* There is no previous match to compare with, wait for
1248 * the next step to decide.
1249 */
1250 s->match_available = 1;
1251 s->strstart++;
1252 s->lookahead--;
1253 }
1254 }
1255 Assert (flush != Z_NO_FLUSH, "no flush?");
1256 if (s->match_available) {
1257 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1258 zlib_tr_tally (s, 0, s->window[s->strstart-1]);
1259 s->match_available = 0;
1260 }
1261 FLUSH_BLOCK(s, flush == Z_FINISH);
1262 return flush == Z_FINISH ? finish_done : block_done;
1263}
1264
1265int zlib_deflate_workspacesize(void)
1266{
1267 return sizeof(deflate_workspace);
1268}
diff --git a/lib/zlib_deflate/deflate_syms.c b/lib/zlib_deflate/deflate_syms.c
new file mode 100644
index 000000000000..5985b28c8e30
--- /dev/null
+++ b/lib/zlib_deflate/deflate_syms.c
@@ -0,0 +1,21 @@
1/*
2 * linux/lib/zlib_deflate/deflate_syms.c
3 *
4 * Exported symbols for the deflate functionality.
5 *
6 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10
11#include <linux/zlib.h>
12
13EXPORT_SYMBOL(zlib_deflate_workspacesize);
14EXPORT_SYMBOL(zlib_deflate);
15EXPORT_SYMBOL(zlib_deflateInit_);
16EXPORT_SYMBOL(zlib_deflateInit2_);
17EXPORT_SYMBOL(zlib_deflateEnd);
18EXPORT_SYMBOL(zlib_deflateReset);
19EXPORT_SYMBOL(zlib_deflateCopy);
20EXPORT_SYMBOL(zlib_deflateParams);
21MODULE_LICENSE("GPL");
diff --git a/lib/zlib_deflate/deftree.c b/lib/zlib_deflate/deftree.c
new file mode 100644
index 000000000000..ddf348299f24
--- /dev/null
+++ b/lib/zlib_deflate/deftree.c
@@ -0,0 +1,1113 @@
1/* +++ trees.c */
2/* trees.c -- output deflated data using Huffman coding
3 * Copyright (C) 1995-1996 Jean-loup Gailly
4 * For conditions of distribution and use, see copyright notice in zlib.h
5 */
6
7/*
8 * ALGORITHM
9 *
10 * The "deflation" process uses several Huffman trees. The more
11 * common source values are represented by shorter bit sequences.
12 *
13 * Each code tree is stored in a compressed form which is itself
14 * a Huffman encoding of the lengths of all the code strings (in
15 * ascending order by source values). The actual code strings are
16 * reconstructed from the lengths in the inflate process, as described
17 * in the deflate specification.
18 *
19 * REFERENCES
20 *
21 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
22 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
23 *
24 * Storer, James A.
25 * Data Compression: Methods and Theory, pp. 49-50.
26 * Computer Science Press, 1988. ISBN 0-7167-8156-5.
27 *
28 * Sedgewick, R.
29 * Algorithms, p290.
30 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
31 */
32
33/* From: trees.c,v 1.11 1996/07/24 13:41:06 me Exp $ */
34
35/* #include "deflate.h" */
36
37#include <linux/zutil.h>
38#include "defutil.h"
39
40#ifdef DEBUG_ZLIB
41# include <ctype.h>
42#endif
43
44/* ===========================================================================
45 * Constants
46 */
47
48#define MAX_BL_BITS 7
49/* Bit length codes must not exceed MAX_BL_BITS bits */
50
51#define END_BLOCK 256
52/* end of block literal code */
53
54#define REP_3_6 16
55/* repeat previous bit length 3-6 times (2 bits of repeat count) */
56
57#define REPZ_3_10 17
58/* repeat a zero length 3-10 times (3 bits of repeat count) */
59
60#define REPZ_11_138 18
61/* repeat a zero length 11-138 times (7 bits of repeat count) */
62
63static const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
64 = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
65
66static const int extra_dbits[D_CODES] /* extra bits for each distance code */
67 = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
68
69static const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
70 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
71
72static const uch bl_order[BL_CODES]
73 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
74/* The lengths of the bit length codes are sent in order of decreasing
75 * probability, to avoid transmitting the lengths for unused bit length codes.
76 */
77
78#define Buf_size (8 * 2*sizeof(char))
79/* Number of bits used within bi_buf. (bi_buf might be implemented on
80 * more than 16 bits on some systems.)
81 */
82
83/* ===========================================================================
84 * Local data. These are initialized only once.
85 */
86
87static ct_data static_ltree[L_CODES+2];
88/* The static literal tree. Since the bit lengths are imposed, there is no
89 * need for the L_CODES extra codes used during heap construction. However
90 * The codes 286 and 287 are needed to build a canonical tree (see zlib_tr_init
91 * below).
92 */
93
94static ct_data static_dtree[D_CODES];
95/* The static distance tree. (Actually a trivial tree since all codes use
96 * 5 bits.)
97 */
98
99static uch dist_code[512];
100/* distance codes. The first 256 values correspond to the distances
101 * 3 .. 258, the last 256 values correspond to the top 8 bits of
102 * the 15 bit distances.
103 */
104
105static uch length_code[MAX_MATCH-MIN_MATCH+1];
106/* length code for each normalized match length (0 == MIN_MATCH) */
107
108static int base_length[LENGTH_CODES];
109/* First normalized length for each code (0 = MIN_MATCH) */
110
111static int base_dist[D_CODES];
112/* First normalized distance for each code (0 = distance of 1) */
113
114struct static_tree_desc_s {
115 const ct_data *static_tree; /* static tree or NULL */
116 const int *extra_bits; /* extra bits for each code or NULL */
117 int extra_base; /* base index for extra_bits */
118 int elems; /* max number of elements in the tree */
119 int max_length; /* max bit length for the codes */
120};
121
122static static_tree_desc static_l_desc =
123{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
124
125static static_tree_desc static_d_desc =
126{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
127
128static static_tree_desc static_bl_desc =
129{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
130
131/* ===========================================================================
132 * Local (static) routines in this file.
133 */
134
135static void tr_static_init (void);
136static void init_block (deflate_state *s);
137static void pqdownheap (deflate_state *s, ct_data *tree, int k);
138static void gen_bitlen (deflate_state *s, tree_desc *desc);
139static void gen_codes (ct_data *tree, int max_code, ush *bl_count);
140static void build_tree (deflate_state *s, tree_desc *desc);
141static void scan_tree (deflate_state *s, ct_data *tree, int max_code);
142static void send_tree (deflate_state *s, ct_data *tree, int max_code);
143static int build_bl_tree (deflate_state *s);
144static void send_all_trees (deflate_state *s, int lcodes, int dcodes,
145 int blcodes);
146static void compress_block (deflate_state *s, ct_data *ltree,
147 ct_data *dtree);
148static void set_data_type (deflate_state *s);
149static unsigned bi_reverse (unsigned value, int length);
150static void bi_windup (deflate_state *s);
151static void bi_flush (deflate_state *s);
152static void copy_block (deflate_state *s, char *buf, unsigned len,
153 int header);
154
155#ifndef DEBUG_ZLIB
156# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
157 /* Send a code of the given tree. c and tree must not have side effects */
158
159#else /* DEBUG_ZLIB */
160# define send_code(s, c, tree) \
161 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
162 send_bits(s, tree[c].Code, tree[c].Len); }
163#endif
164
165#define d_code(dist) \
166 ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
167/* Mapping from a distance to a distance code. dist is the distance - 1 and
168 * must not have side effects. dist_code[256] and dist_code[257] are never
169 * used.
170 */
171
172/* ===========================================================================
173 * Send a value on a given number of bits.
174 * IN assertion: length <= 16 and value fits in length bits.
175 */
176#ifdef DEBUG_ZLIB
177static void send_bits (deflate_state *s, int value, int length);
178
179static void send_bits(
180 deflate_state *s,
181 int value, /* value to send */
182 int length /* number of bits */
183)
184{
185 Tracevv((stderr," l %2d v %4x ", length, value));
186 Assert(length > 0 && length <= 15, "invalid length");
187 s->bits_sent += (ulg)length;
188
189 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
190 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
191 * unused bits in value.
192 */
193 if (s->bi_valid > (int)Buf_size - length) {
194 s->bi_buf |= (value << s->bi_valid);
195 put_short(s, s->bi_buf);
196 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
197 s->bi_valid += length - Buf_size;
198 } else {
199 s->bi_buf |= value << s->bi_valid;
200 s->bi_valid += length;
201 }
202}
203#else /* !DEBUG_ZLIB */
204
205#define send_bits(s, value, length) \
206{ int len = length;\
207 if (s->bi_valid > (int)Buf_size - len) {\
208 int val = value;\
209 s->bi_buf |= (val << s->bi_valid);\
210 put_short(s, s->bi_buf);\
211 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
212 s->bi_valid += len - Buf_size;\
213 } else {\
214 s->bi_buf |= (value) << s->bi_valid;\
215 s->bi_valid += len;\
216 }\
217}
218#endif /* DEBUG_ZLIB */
219
220/* ===========================================================================
221 * Initialize the various 'constant' tables. In a multi-threaded environment,
222 * this function may be called by two threads concurrently, but this is
223 * harmless since both invocations do exactly the same thing.
224 */
225static void tr_static_init(void)
226{
227 static int static_init_done;
228 int n; /* iterates over tree elements */
229 int bits; /* bit counter */
230 int length; /* length value */
231 int code; /* code value */
232 int dist; /* distance index */
233 ush bl_count[MAX_BITS+1];
234 /* number of codes at each bit length for an optimal tree */
235
236 if (static_init_done) return;
237
238 /* Initialize the mapping length (0..255) -> length code (0..28) */
239 length = 0;
240 for (code = 0; code < LENGTH_CODES-1; code++) {
241 base_length[code] = length;
242 for (n = 0; n < (1<<extra_lbits[code]); n++) {
243 length_code[length++] = (uch)code;
244 }
245 }
246 Assert (length == 256, "tr_static_init: length != 256");
247 /* Note that the length 255 (match length 258) can be represented
248 * in two different ways: code 284 + 5 bits or code 285, so we
249 * overwrite length_code[255] to use the best encoding:
250 */
251 length_code[length-1] = (uch)code;
252
253 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
254 dist = 0;
255 for (code = 0 ; code < 16; code++) {
256 base_dist[code] = dist;
257 for (n = 0; n < (1<<extra_dbits[code]); n++) {
258 dist_code[dist++] = (uch)code;
259 }
260 }
261 Assert (dist == 256, "tr_static_init: dist != 256");
262 dist >>= 7; /* from now on, all distances are divided by 128 */
263 for ( ; code < D_CODES; code++) {
264 base_dist[code] = dist << 7;
265 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
266 dist_code[256 + dist++] = (uch)code;
267 }
268 }
269 Assert (dist == 256, "tr_static_init: 256+dist != 512");
270
271 /* Construct the codes of the static literal tree */
272 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
273 n = 0;
274 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
275 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
276 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
277 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
278 /* Codes 286 and 287 do not exist, but we must include them in the
279 * tree construction to get a canonical Huffman tree (longest code
280 * all ones)
281 */
282 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
283
284 /* The static distance tree is trivial: */
285 for (n = 0; n < D_CODES; n++) {
286 static_dtree[n].Len = 5;
287 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
288 }
289 static_init_done = 1;
290}
291
292/* ===========================================================================
293 * Initialize the tree data structures for a new zlib stream.
294 */
295void zlib_tr_init(
296 deflate_state *s
297)
298{
299 tr_static_init();
300
301 s->compressed_len = 0L;
302
303 s->l_desc.dyn_tree = s->dyn_ltree;
304 s->l_desc.stat_desc = &static_l_desc;
305
306 s->d_desc.dyn_tree = s->dyn_dtree;
307 s->d_desc.stat_desc = &static_d_desc;
308
309 s->bl_desc.dyn_tree = s->bl_tree;
310 s->bl_desc.stat_desc = &static_bl_desc;
311
312 s->bi_buf = 0;
313 s->bi_valid = 0;
314 s->last_eob_len = 8; /* enough lookahead for inflate */
315#ifdef DEBUG_ZLIB
316 s->bits_sent = 0L;
317#endif
318
319 /* Initialize the first block of the first file: */
320 init_block(s);
321}
322
323/* ===========================================================================
324 * Initialize a new block.
325 */
326static void init_block(
327 deflate_state *s
328)
329{
330 int n; /* iterates over tree elements */
331
332 /* Initialize the trees. */
333 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
334 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
335 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
336
337 s->dyn_ltree[END_BLOCK].Freq = 1;
338 s->opt_len = s->static_len = 0L;
339 s->last_lit = s->matches = 0;
340}
341
342#define SMALLEST 1
343/* Index within the heap array of least frequent node in the Huffman tree */
344
345
346/* ===========================================================================
347 * Remove the smallest element from the heap and recreate the heap with
348 * one less element. Updates heap and heap_len.
349 */
350#define pqremove(s, tree, top) \
351{\
352 top = s->heap[SMALLEST]; \
353 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
354 pqdownheap(s, tree, SMALLEST); \
355}
356
357/* ===========================================================================
358 * Compares to subtrees, using the tree depth as tie breaker when
359 * the subtrees have equal frequency. This minimizes the worst case length.
360 */
361#define smaller(tree, n, m, depth) \
362 (tree[n].Freq < tree[m].Freq || \
363 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
364
365/* ===========================================================================
366 * Restore the heap property by moving down the tree starting at node k,
367 * exchanging a node with the smallest of its two sons if necessary, stopping
368 * when the heap property is re-established (each father smaller than its
369 * two sons).
370 */
371static void pqdownheap(
372 deflate_state *s,
373 ct_data *tree, /* the tree to restore */
374 int k /* node to move down */
375)
376{
377 int v = s->heap[k];
378 int j = k << 1; /* left son of k */
379 while (j <= s->heap_len) {
380 /* Set j to the smallest of the two sons: */
381 if (j < s->heap_len &&
382 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
383 j++;
384 }
385 /* Exit if v is smaller than both sons */
386 if (smaller(tree, v, s->heap[j], s->depth)) break;
387
388 /* Exchange v with the smallest son */
389 s->heap[k] = s->heap[j]; k = j;
390
391 /* And continue down the tree, setting j to the left son of k */
392 j <<= 1;
393 }
394 s->heap[k] = v;
395}
396
397/* ===========================================================================
398 * Compute the optimal bit lengths for a tree and update the total bit length
399 * for the current block.
400 * IN assertion: the fields freq and dad are set, heap[heap_max] and
401 * above are the tree nodes sorted by increasing frequency.
402 * OUT assertions: the field len is set to the optimal bit length, the
403 * array bl_count contains the frequencies for each bit length.
404 * The length opt_len is updated; static_len is also updated if stree is
405 * not null.
406 */
407static void gen_bitlen(
408 deflate_state *s,
409 tree_desc *desc /* the tree descriptor */
410)
411{
412 ct_data *tree = desc->dyn_tree;
413 int max_code = desc->max_code;
414 const ct_data *stree = desc->stat_desc->static_tree;
415 const int *extra = desc->stat_desc->extra_bits;
416 int base = desc->stat_desc->extra_base;
417 int max_length = desc->stat_desc->max_length;
418 int h; /* heap index */
419 int n, m; /* iterate over the tree elements */
420 int bits; /* bit length */
421 int xbits; /* extra bits */
422 ush f; /* frequency */
423 int overflow = 0; /* number of elements with bit length too large */
424
425 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
426
427 /* In a first pass, compute the optimal bit lengths (which may
428 * overflow in the case of the bit length tree).
429 */
430 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
431
432 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
433 n = s->heap[h];
434 bits = tree[tree[n].Dad].Len + 1;
435 if (bits > max_length) bits = max_length, overflow++;
436 tree[n].Len = (ush)bits;
437 /* We overwrite tree[n].Dad which is no longer needed */
438
439 if (n > max_code) continue; /* not a leaf node */
440
441 s->bl_count[bits]++;
442 xbits = 0;
443 if (n >= base) xbits = extra[n-base];
444 f = tree[n].Freq;
445 s->opt_len += (ulg)f * (bits + xbits);
446 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
447 }
448 if (overflow == 0) return;
449
450 Trace((stderr,"\nbit length overflow\n"));
451 /* This happens for example on obj2 and pic of the Calgary corpus */
452
453 /* Find the first bit length which could increase: */
454 do {
455 bits = max_length-1;
456 while (s->bl_count[bits] == 0) bits--;
457 s->bl_count[bits]--; /* move one leaf down the tree */
458 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
459 s->bl_count[max_length]--;
460 /* The brother of the overflow item also moves one step up,
461 * but this does not affect bl_count[max_length]
462 */
463 overflow -= 2;
464 } while (overflow > 0);
465
466 /* Now recompute all bit lengths, scanning in increasing frequency.
467 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
468 * lengths instead of fixing only the wrong ones. This idea is taken
469 * from 'ar' written by Haruhiko Okumura.)
470 */
471 for (bits = max_length; bits != 0; bits--) {
472 n = s->bl_count[bits];
473 while (n != 0) {
474 m = s->heap[--h];
475 if (m > max_code) continue;
476 if (tree[m].Len != (unsigned) bits) {
477 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
478 s->opt_len += ((long)bits - (long)tree[m].Len)
479 *(long)tree[m].Freq;
480 tree[m].Len = (ush)bits;
481 }
482 n--;
483 }
484 }
485}
486
487/* ===========================================================================
488 * Generate the codes for a given tree and bit counts (which need not be
489 * optimal).
490 * IN assertion: the array bl_count contains the bit length statistics for
491 * the given tree and the field len is set for all tree elements.
492 * OUT assertion: the field code is set for all tree elements of non
493 * zero code length.
494 */
495static void gen_codes(
496 ct_data *tree, /* the tree to decorate */
497 int max_code, /* largest code with non zero frequency */
498 ush *bl_count /* number of codes at each bit length */
499)
500{
501 ush next_code[MAX_BITS+1]; /* next code value for each bit length */
502 ush code = 0; /* running code value */
503 int bits; /* bit index */
504 int n; /* code index */
505
506 /* The distribution counts are first used to generate the code values
507 * without bit reversal.
508 */
509 for (bits = 1; bits <= MAX_BITS; bits++) {
510 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
511 }
512 /* Check that the bit counts in bl_count are consistent. The last code
513 * must be all ones.
514 */
515 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
516 "inconsistent bit counts");
517 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
518
519 for (n = 0; n <= max_code; n++) {
520 int len = tree[n].Len;
521 if (len == 0) continue;
522 /* Now reverse the bits */
523 tree[n].Code = bi_reverse(next_code[len]++, len);
524
525 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
526 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
527 }
528}
529
530/* ===========================================================================
531 * Construct one Huffman tree and assigns the code bit strings and lengths.
532 * Update the total bit length for the current block.
533 * IN assertion: the field freq is set for all tree elements.
534 * OUT assertions: the fields len and code are set to the optimal bit length
535 * and corresponding code. The length opt_len is updated; static_len is
536 * also updated if stree is not null. The field max_code is set.
537 */
538static void build_tree(
539 deflate_state *s,
540 tree_desc *desc /* the tree descriptor */
541)
542{
543 ct_data *tree = desc->dyn_tree;
544 const ct_data *stree = desc->stat_desc->static_tree;
545 int elems = desc->stat_desc->elems;
546 int n, m; /* iterate over heap elements */
547 int max_code = -1; /* largest code with non zero frequency */
548 int node; /* new node being created */
549
550 /* Construct the initial heap, with least frequent element in
551 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
552 * heap[0] is not used.
553 */
554 s->heap_len = 0, s->heap_max = HEAP_SIZE;
555
556 for (n = 0; n < elems; n++) {
557 if (tree[n].Freq != 0) {
558 s->heap[++(s->heap_len)] = max_code = n;
559 s->depth[n] = 0;
560 } else {
561 tree[n].Len = 0;
562 }
563 }
564
565 /* The pkzip format requires that at least one distance code exists,
566 * and that at least one bit should be sent even if there is only one
567 * possible code. So to avoid special checks later on we force at least
568 * two codes of non zero frequency.
569 */
570 while (s->heap_len < 2) {
571 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
572 tree[node].Freq = 1;
573 s->depth[node] = 0;
574 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
575 /* node is 0 or 1 so it does not have extra bits */
576 }
577 desc->max_code = max_code;
578
579 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
580 * establish sub-heaps of increasing lengths:
581 */
582 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
583
584 /* Construct the Huffman tree by repeatedly combining the least two
585 * frequent nodes.
586 */
587 node = elems; /* next internal node of the tree */
588 do {
589 pqremove(s, tree, n); /* n = node of least frequency */
590 m = s->heap[SMALLEST]; /* m = node of next least frequency */
591
592 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
593 s->heap[--(s->heap_max)] = m;
594
595 /* Create a new node father of n and m */
596 tree[node].Freq = tree[n].Freq + tree[m].Freq;
597 s->depth[node] = (uch) (max(s->depth[n], s->depth[m]) + 1);
598 tree[n].Dad = tree[m].Dad = (ush)node;
599#ifdef DUMP_BL_TREE
600 if (tree == s->bl_tree) {
601 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
602 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
603 }
604#endif
605 /* and insert the new node in the heap */
606 s->heap[SMALLEST] = node++;
607 pqdownheap(s, tree, SMALLEST);
608
609 } while (s->heap_len >= 2);
610
611 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
612
613 /* At this point, the fields freq and dad are set. We can now
614 * generate the bit lengths.
615 */
616 gen_bitlen(s, (tree_desc *)desc);
617
618 /* The field len is now set, we can generate the bit codes */
619 gen_codes ((ct_data *)tree, max_code, s->bl_count);
620}
621
622/* ===========================================================================
623 * Scan a literal or distance tree to determine the frequencies of the codes
624 * in the bit length tree.
625 */
626static void scan_tree(
627 deflate_state *s,
628 ct_data *tree, /* the tree to be scanned */
629 int max_code /* and its largest code of non zero frequency */
630)
631{
632 int n; /* iterates over all tree elements */
633 int prevlen = -1; /* last emitted length */
634 int curlen; /* length of current code */
635 int nextlen = tree[0].Len; /* length of next code */
636 int count = 0; /* repeat count of the current code */
637 int max_count = 7; /* max repeat count */
638 int min_count = 4; /* min repeat count */
639
640 if (nextlen == 0) max_count = 138, min_count = 3;
641 tree[max_code+1].Len = (ush)0xffff; /* guard */
642
643 for (n = 0; n <= max_code; n++) {
644 curlen = nextlen; nextlen = tree[n+1].Len;
645 if (++count < max_count && curlen == nextlen) {
646 continue;
647 } else if (count < min_count) {
648 s->bl_tree[curlen].Freq += count;
649 } else if (curlen != 0) {
650 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
651 s->bl_tree[REP_3_6].Freq++;
652 } else if (count <= 10) {
653 s->bl_tree[REPZ_3_10].Freq++;
654 } else {
655 s->bl_tree[REPZ_11_138].Freq++;
656 }
657 count = 0; prevlen = curlen;
658 if (nextlen == 0) {
659 max_count = 138, min_count = 3;
660 } else if (curlen == nextlen) {
661 max_count = 6, min_count = 3;
662 } else {
663 max_count = 7, min_count = 4;
664 }
665 }
666}
667
668/* ===========================================================================
669 * Send a literal or distance tree in compressed form, using the codes in
670 * bl_tree.
671 */
672static void send_tree(
673 deflate_state *s,
674 ct_data *tree, /* the tree to be scanned */
675 int max_code /* and its largest code of non zero frequency */
676)
677{
678 int n; /* iterates over all tree elements */
679 int prevlen = -1; /* last emitted length */
680 int curlen; /* length of current code */
681 int nextlen = tree[0].Len; /* length of next code */
682 int count = 0; /* repeat count of the current code */
683 int max_count = 7; /* max repeat count */
684 int min_count = 4; /* min repeat count */
685
686 /* tree[max_code+1].Len = -1; */ /* guard already set */
687 if (nextlen == 0) max_count = 138, min_count = 3;
688
689 for (n = 0; n <= max_code; n++) {
690 curlen = nextlen; nextlen = tree[n+1].Len;
691 if (++count < max_count && curlen == nextlen) {
692 continue;
693 } else if (count < min_count) {
694 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
695
696 } else if (curlen != 0) {
697 if (curlen != prevlen) {
698 send_code(s, curlen, s->bl_tree); count--;
699 }
700 Assert(count >= 3 && count <= 6, " 3_6?");
701 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
702
703 } else if (count <= 10) {
704 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
705
706 } else {
707 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
708 }
709 count = 0; prevlen = curlen;
710 if (nextlen == 0) {
711 max_count = 138, min_count = 3;
712 } else if (curlen == nextlen) {
713 max_count = 6, min_count = 3;
714 } else {
715 max_count = 7, min_count = 4;
716 }
717 }
718}
719
720/* ===========================================================================
721 * Construct the Huffman tree for the bit lengths and return the index in
722 * bl_order of the last bit length code to send.
723 */
724static int build_bl_tree(
725 deflate_state *s
726)
727{
728 int max_blindex; /* index of last bit length code of non zero freq */
729
730 /* Determine the bit length frequencies for literal and distance trees */
731 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
732 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
733
734 /* Build the bit length tree: */
735 build_tree(s, (tree_desc *)(&(s->bl_desc)));
736 /* opt_len now includes the length of the tree representations, except
737 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
738 */
739
740 /* Determine the number of bit length codes to send. The pkzip format
741 * requires that at least 4 bit length codes be sent. (appnote.txt says
742 * 3 but the actual value used is 4.)
743 */
744 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
745 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
746 }
747 /* Update opt_len to include the bit length tree and counts */
748 s->opt_len += 3*(max_blindex+1) + 5+5+4;
749 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
750 s->opt_len, s->static_len));
751
752 return max_blindex;
753}
754
755/* ===========================================================================
756 * Send the header for a block using dynamic Huffman trees: the counts, the
757 * lengths of the bit length codes, the literal tree and the distance tree.
758 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
759 */
760static void send_all_trees(
761 deflate_state *s,
762 int lcodes, /* number of codes for each tree */
763 int dcodes, /* number of codes for each tree */
764 int blcodes /* number of codes for each tree */
765)
766{
767 int rank; /* index in bl_order */
768
769 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
770 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
771 "too many codes");
772 Tracev((stderr, "\nbl counts: "));
773 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
774 send_bits(s, dcodes-1, 5);
775 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
776 for (rank = 0; rank < blcodes; rank++) {
777 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
778 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
779 }
780 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
781
782 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
783 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
784
785 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
786 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
787}
788
789/* ===========================================================================
790 * Send a stored block
791 */
792void zlib_tr_stored_block(
793 deflate_state *s,
794 char *buf, /* input block */
795 ulg stored_len, /* length of input block */
796 int eof /* true if this is the last block for a file */
797)
798{
799 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
800 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
801 s->compressed_len += (stored_len + 4) << 3;
802
803 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
804}
805
806/* Send just the `stored block' type code without any length bytes or data.
807 */
808void zlib_tr_stored_type_only(
809 deflate_state *s
810)
811{
812 send_bits(s, (STORED_BLOCK << 1), 3);
813 bi_windup(s);
814 s->compressed_len = (s->compressed_len + 3) & ~7L;
815}
816
817
818/* ===========================================================================
819 * Send one empty static block to give enough lookahead for inflate.
820 * This takes 10 bits, of which 7 may remain in the bit buffer.
821 * The current inflate code requires 9 bits of lookahead. If the
822 * last two codes for the previous block (real code plus EOB) were coded
823 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
824 * the last real code. In this case we send two empty static blocks instead
825 * of one. (There are no problems if the previous block is stored or fixed.)
826 * To simplify the code, we assume the worst case of last real code encoded
827 * on one bit only.
828 */
829void zlib_tr_align(
830 deflate_state *s
831)
832{
833 send_bits(s, STATIC_TREES<<1, 3);
834 send_code(s, END_BLOCK, static_ltree);
835 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
836 bi_flush(s);
837 /* Of the 10 bits for the empty block, we have already sent
838 * (10 - bi_valid) bits. The lookahead for the last real code (before
839 * the EOB of the previous block) was thus at least one plus the length
840 * of the EOB plus what we have just sent of the empty static block.
841 */
842 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
843 send_bits(s, STATIC_TREES<<1, 3);
844 send_code(s, END_BLOCK, static_ltree);
845 s->compressed_len += 10L;
846 bi_flush(s);
847 }
848 s->last_eob_len = 7;
849}
850
851/* ===========================================================================
852 * Determine the best encoding for the current block: dynamic trees, static
853 * trees or store, and output the encoded block to the zip file. This function
854 * returns the total compressed length for the file so far.
855 */
856ulg zlib_tr_flush_block(
857 deflate_state *s,
858 char *buf, /* input block, or NULL if too old */
859 ulg stored_len, /* length of input block */
860 int eof /* true if this is the last block for a file */
861)
862{
863 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
864 int max_blindex = 0; /* index of last bit length code of non zero freq */
865
866 /* Build the Huffman trees unless a stored block is forced */
867 if (s->level > 0) {
868
869 /* Check if the file is ascii or binary */
870 if (s->data_type == Z_UNKNOWN) set_data_type(s);
871
872 /* Construct the literal and distance trees */
873 build_tree(s, (tree_desc *)(&(s->l_desc)));
874 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
875 s->static_len));
876
877 build_tree(s, (tree_desc *)(&(s->d_desc)));
878 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
879 s->static_len));
880 /* At this point, opt_len and static_len are the total bit lengths of
881 * the compressed block data, excluding the tree representations.
882 */
883
884 /* Build the bit length tree for the above two trees, and get the index
885 * in bl_order of the last bit length code to send.
886 */
887 max_blindex = build_bl_tree(s);
888
889 /* Determine the best encoding. Compute first the block length in bytes*/
890 opt_lenb = (s->opt_len+3+7)>>3;
891 static_lenb = (s->static_len+3+7)>>3;
892
893 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
894 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
895 s->last_lit));
896
897 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
898
899 } else {
900 Assert(buf != (char*)0, "lost buf");
901 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
902 }
903
904 /* If compression failed and this is the first and last block,
905 * and if the .zip file can be seeked (to rewrite the local header),
906 * the whole file is transformed into a stored file:
907 */
908#ifdef STORED_FILE_OK
909# ifdef FORCE_STORED_FILE
910 if (eof && s->compressed_len == 0L) { /* force stored file */
911# else
912 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
913# endif
914 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
915 if (buf == (char*)0) error ("block vanished");
916
917 copy_block(s, buf, (unsigned)stored_len, 0); /* without header */
918 s->compressed_len = stored_len << 3;
919 s->method = STORED;
920 } else
921#endif /* STORED_FILE_OK */
922
923#ifdef FORCE_STORED
924 if (buf != (char*)0) { /* force stored block */
925#else
926 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
927 /* 4: two words for the lengths */
928#endif
929 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
930 * Otherwise we can't have processed more than WSIZE input bytes since
931 * the last block flush, because compression would have been
932 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
933 * transform a block into a stored block.
934 */
935 zlib_tr_stored_block(s, buf, stored_len, eof);
936
937#ifdef FORCE_STATIC
938 } else if (static_lenb >= 0) { /* force static trees */
939#else
940 } else if (static_lenb == opt_lenb) {
941#endif
942 send_bits(s, (STATIC_TREES<<1)+eof, 3);
943 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
944 s->compressed_len += 3 + s->static_len;
945 } else {
946 send_bits(s, (DYN_TREES<<1)+eof, 3);
947 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
948 max_blindex+1);
949 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
950 s->compressed_len += 3 + s->opt_len;
951 }
952 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
953 init_block(s);
954
955 if (eof) {
956 bi_windup(s);
957 s->compressed_len += 7; /* align on byte boundary */
958 }
959 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
960 s->compressed_len-7*eof));
961
962 return s->compressed_len >> 3;
963}
964
965/* ===========================================================================
966 * Save the match info and tally the frequency counts. Return true if
967 * the current block must be flushed.
968 */
969int zlib_tr_tally(
970 deflate_state *s,
971 unsigned dist, /* distance of matched string */
972 unsigned lc /* match length-MIN_MATCH or unmatched char (if dist==0) */
973)
974{
975 s->d_buf[s->last_lit] = (ush)dist;
976 s->l_buf[s->last_lit++] = (uch)lc;
977 if (dist == 0) {
978 /* lc is the unmatched char */
979 s->dyn_ltree[lc].Freq++;
980 } else {
981 s->matches++;
982 /* Here, lc is the match length - MIN_MATCH */
983 dist--; /* dist = match distance - 1 */
984 Assert((ush)dist < (ush)MAX_DIST(s) &&
985 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
986 (ush)d_code(dist) < (ush)D_CODES, "zlib_tr_tally: bad match");
987
988 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
989 s->dyn_dtree[d_code(dist)].Freq++;
990 }
991
992 /* Try to guess if it is profitable to stop the current block here */
993 if ((s->last_lit & 0xfff) == 0 && s->level > 2) {
994 /* Compute an upper bound for the compressed length */
995 ulg out_length = (ulg)s->last_lit*8L;
996 ulg in_length = (ulg)((long)s->strstart - s->block_start);
997 int dcode;
998 for (dcode = 0; dcode < D_CODES; dcode++) {
999 out_length += (ulg)s->dyn_dtree[dcode].Freq *
1000 (5L+extra_dbits[dcode]);
1001 }
1002 out_length >>= 3;
1003 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1004 s->last_lit, in_length, out_length,
1005 100L - out_length*100L/in_length));
1006 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1007 }
1008 return (s->last_lit == s->lit_bufsize-1);
1009 /* We avoid equality with lit_bufsize because of wraparound at 64K
1010 * on 16 bit machines and because stored blocks are restricted to
1011 * 64K-1 bytes.
1012 */
1013}
1014
1015/* ===========================================================================
1016 * Send the block data compressed using the given Huffman trees
1017 */
1018static void compress_block(
1019 deflate_state *s,
1020 ct_data *ltree, /* literal tree */
1021 ct_data *dtree /* distance tree */
1022)
1023{
1024 unsigned dist; /* distance of matched string */
1025 int lc; /* match length or unmatched char (if dist == 0) */
1026 unsigned lx = 0; /* running index in l_buf */
1027 unsigned code; /* the code to send */
1028 int extra; /* number of extra bits to send */
1029
1030 if (s->last_lit != 0) do {
1031 dist = s->d_buf[lx];
1032 lc = s->l_buf[lx++];
1033 if (dist == 0) {
1034 send_code(s, lc, ltree); /* send a literal byte */
1035 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1036 } else {
1037 /* Here, lc is the match length - MIN_MATCH */
1038 code = length_code[lc];
1039 send_code(s, code+LITERALS+1, ltree); /* send the length code */
1040 extra = extra_lbits[code];
1041 if (extra != 0) {
1042 lc -= base_length[code];
1043 send_bits(s, lc, extra); /* send the extra length bits */
1044 }
1045 dist--; /* dist is now the match distance - 1 */
1046 code = d_code(dist);
1047 Assert (code < D_CODES, "bad d_code");
1048
1049 send_code(s, code, dtree); /* send the distance code */
1050 extra = extra_dbits[code];
1051 if (extra != 0) {
1052 dist -= base_dist[code];
1053 send_bits(s, dist, extra); /* send the extra distance bits */
1054 }
1055 } /* literal or match pair ? */
1056
1057 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
1058 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
1059
1060 } while (lx < s->last_lit);
1061
1062 send_code(s, END_BLOCK, ltree);
1063 s->last_eob_len = ltree[END_BLOCK].Len;
1064}
1065
1066/* ===========================================================================
1067 * Set the data type to ASCII or BINARY, using a crude approximation:
1068 * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
1069 * IN assertion: the fields freq of dyn_ltree are set and the total of all
1070 * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
1071 */
1072static void set_data_type(
1073 deflate_state *s
1074)
1075{
1076 int n = 0;
1077 unsigned ascii_freq = 0;
1078 unsigned bin_freq = 0;
1079 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
1080 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
1081 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
1082 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
1083}
1084
1085/* ===========================================================================
1086 * Copy a stored block, storing first the length and its
1087 * one's complement if requested.
1088 */
1089static void copy_block(
1090 deflate_state *s,
1091 char *buf, /* the input data */
1092 unsigned len, /* its length */
1093 int header /* true if block header must be written */
1094)
1095{
1096 bi_windup(s); /* align on byte boundary */
1097 s->last_eob_len = 8; /* enough lookahead for inflate */
1098
1099 if (header) {
1100 put_short(s, (ush)len);
1101 put_short(s, (ush)~len);
1102#ifdef DEBUG_ZLIB
1103 s->bits_sent += 2*16;
1104#endif
1105 }
1106#ifdef DEBUG_ZLIB
1107 s->bits_sent += (ulg)len<<3;
1108#endif
1109 /* bundle up the put_byte(s, *buf++) calls */
1110 memcpy(&s->pending_buf[s->pending], buf, len);
1111 s->pending += len;
1112}
1113
diff --git a/lib/zlib_deflate/defutil.h b/lib/zlib_deflate/defutil.h
new file mode 100644
index 000000000000..d9feaf638608
--- /dev/null
+++ b/lib/zlib_deflate/defutil.h
@@ -0,0 +1,334 @@
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. */
40typedef 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} ct_data;
50
51#define Freq fc.freq
52#define Code fc.code
53#define Dad dl.dad
54#define Len dl.len
55
56typedef struct static_tree_desc_s static_tree_desc;
57
58typedef 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} tree_desc;
63
64typedef ush Pos;
65typedef unsigned IPos;
66
67/* A Pos is an index in the character window. We use short instead of int to
68 * save space in the various tables. IPos is used only for parameter passing.
69 */
70
71typedef struct deflate_state {
72 z_streamp strm; /* pointer back to this zlib stream */
73 int status; /* as the name implies */
74 Byte *pending_buf; /* output still pending */
75 ulg pending_buf_size; /* size of pending_buf */
76 Byte *pending_out; /* next pending byte to output to the stream */
77 int pending; /* nb of bytes in the pending buffer */
78 int noheader; /* suppress zlib header and adler32 */
79 Byte data_type; /* UNKNOWN, BINARY or ASCII */
80 Byte method; /* STORED (for zip only) or DEFLATED */
81 int last_flush; /* value of flush param for previous deflate call */
82
83 /* used by deflate.c: */
84
85 uInt w_size; /* LZ77 window size (32K by default) */
86 uInt w_bits; /* log2(w_size) (8..16) */
87 uInt w_mask; /* w_size - 1 */
88
89 Byte *window;
90 /* Sliding window. Input bytes are read into the second half of the window,
91 * and move to the first half later to keep a dictionary of at least wSize
92 * bytes. With this organization, matches are limited to a distance of
93 * wSize-MAX_MATCH bytes, but this ensures that IO is always
94 * performed with a length multiple of the block size. Also, it limits
95 * the window size to 64K, which is quite useful on MSDOS.
96 * To do: use the user input buffer as sliding window.
97 */
98
99 ulg window_size;
100 /* Actual size of window: 2*wSize, except when the user input buffer
101 * is directly used as sliding window.
102 */
103
104 Pos *prev;
105 /* Link to older string with same hash index. To limit the size of this
106 * array to 64K, this link is maintained only for the last 32K strings.
107 * An index in this array is thus a window index modulo 32K.
108 */
109
110 Pos *head; /* Heads of the hash chains or NIL. */
111
112 uInt ins_h; /* hash index of string to be inserted */
113 uInt hash_size; /* number of elements in hash table */
114 uInt hash_bits; /* log2(hash_size) */
115 uInt hash_mask; /* hash_size-1 */
116
117 uInt hash_shift;
118 /* Number of bits by which ins_h must be shifted at each input
119 * step. It must be such that after MIN_MATCH steps, the oldest
120 * byte no longer takes part in the hash key, that is:
121 * hash_shift * MIN_MATCH >= hash_bits
122 */
123
124 long block_start;
125 /* Window position at the beginning of the current output block. Gets
126 * negative when the window is moved backwards.
127 */
128
129 uInt match_length; /* length of best match */
130 IPos prev_match; /* previous match */
131 int match_available; /* set if previous match exists */
132 uInt strstart; /* start of string to insert */
133 uInt match_start; /* start of matching string */
134 uInt lookahead; /* number of valid bytes ahead in window */
135
136 uInt prev_length;
137 /* Length of the best match at previous step. Matches not greater than this
138 * are discarded. This is used in the lazy match evaluation.
139 */
140
141 uInt max_chain_length;
142 /* To speed up deflation, hash chains are never searched beyond this
143 * length. A higher limit improves compression ratio but degrades the
144 * speed.
145 */
146
147 uInt max_lazy_match;
148 /* Attempt to find a better match only when the current match is strictly
149 * smaller than this value. This mechanism is used only for compression
150 * levels >= 4.
151 */
152# define max_insert_length max_lazy_match
153 /* Insert new strings in the hash table only if the match length is not
154 * greater than this length. This saves time but degrades compression.
155 * max_insert_length is used only for compression levels <= 3.
156 */
157
158 int level; /* compression level (1..9) */
159 int strategy; /* favor or force Huffman coding*/
160
161 uInt good_match;
162 /* Use a faster search when the previous match is longer than this */
163
164 int nice_match; /* Stop searching when current match exceeds this */
165
166 /* used by trees.c: */
167 /* Didn't use ct_data typedef below to supress compiler warning */
168 struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
169 struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
170 struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
171
172 struct tree_desc_s l_desc; /* desc. for literal tree */
173 struct tree_desc_s d_desc; /* desc. for distance tree */
174 struct tree_desc_s bl_desc; /* desc. for bit length tree */
175
176 ush bl_count[MAX_BITS+1];
177 /* number of codes at each bit length for an optimal tree */
178
179 int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
180 int heap_len; /* number of elements in the heap */
181 int heap_max; /* element of largest frequency */
182 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
183 * The same heap array is used to build all trees.
184 */
185
186 uch depth[2*L_CODES+1];
187 /* Depth of each subtree used as tie breaker for trees of equal frequency
188 */
189
190 uch *l_buf; /* buffer for literals or lengths */
191
192 uInt lit_bufsize;
193 /* Size of match buffer for literals/lengths. There are 4 reasons for
194 * limiting lit_bufsize to 64K:
195 * - frequencies can be kept in 16 bit counters
196 * - if compression is not successful for the first block, all input
197 * data is still in the window so we can still emit a stored block even
198 * when input comes from standard input. (This can also be done for
199 * all blocks if lit_bufsize is not greater than 32K.)
200 * - if compression is not successful for a file smaller than 64K, we can
201 * even emit a stored file instead of a stored block (saving 5 bytes).
202 * This is applicable only for zip (not gzip or zlib).
203 * - creating new Huffman trees less frequently may not provide fast
204 * adaptation to changes in the input data statistics. (Take for
205 * example a binary file with poorly compressible code followed by
206 * a highly compressible string table.) Smaller buffer sizes give
207 * fast adaptation but have of course the overhead of transmitting
208 * trees more frequently.
209 * - I can't count above 4
210 */
211
212 uInt last_lit; /* running index in l_buf */
213
214 ush *d_buf;
215 /* Buffer for distances. To simplify the code, d_buf and l_buf have
216 * the same number of elements. To use different lengths, an extra flag
217 * array would be necessary.
218 */
219
220 ulg opt_len; /* bit length of current block with optimal trees */
221 ulg static_len; /* bit length of current block with static trees */
222 ulg compressed_len; /* total bit length of compressed file */
223 uInt matches; /* number of string matches in current block */
224 int last_eob_len; /* bit length of EOB code for last block */
225
226#ifdef DEBUG_ZLIB
227 ulg bits_sent; /* bit length of the compressed data */
228#endif
229
230 ush bi_buf;
231 /* Output buffer. bits are inserted starting at the bottom (least
232 * significant bits).
233 */
234 int bi_valid;
235 /* Number of valid bits in bi_buf. All bits above the last valid bit
236 * are always zero.
237 */
238
239} deflate_state;
240
241typedef struct deflate_workspace {
242 /* State memory for the deflator */
243 deflate_state deflate_memory;
244 Byte window_memory[2 * (1 << MAX_WBITS)];
245 Pos prev_memory[1 << MAX_WBITS];
246 Pos head_memory[1 << (MAX_MEM_LEVEL + 7)];
247 char overlay_memory[(1 << (MAX_MEM_LEVEL + 6)) * (sizeof(ush)+2)];
248} deflate_workspace;
249
250/* Output a byte on the stream.
251 * IN assertion: there is enough room in pending_buf.
252 */
253#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
254
255
256#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
257/* Minimum amount of lookahead, except at the end of the input file.
258 * See deflate.c for comments about the MIN_MATCH+1.
259 */
260
261#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
262/* In order to simplify the code, particularly on 16 bit machines, match
263 * distances are limited to MAX_DIST instead of WSIZE.
264 */
265
266 /* in trees.c */
267void zlib_tr_init (deflate_state *s);
268int zlib_tr_tally (deflate_state *s, unsigned dist, unsigned lc);
269ulg zlib_tr_flush_block (deflate_state *s, char *buf, ulg stored_len,
270 int eof);
271void zlib_tr_align (deflate_state *s);
272void zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len,
273 int eof);
274void zlib_tr_stored_type_only (deflate_state *);
275
276
277/* ===========================================================================
278 * Output a short LSB first on the stream.
279 * IN assertion: there is enough room in pendingBuf.
280 */
281#define put_short(s, w) { \
282 put_byte(s, (uch)((w) & 0xff)); \
283 put_byte(s, (uch)((ush)(w) >> 8)); \
284}
285
286/* ===========================================================================
287 * Reverse the first len bits of a code, using straightforward code (a faster
288 * method would use a table)
289 * IN assertion: 1 <= len <= 15
290 */
291static inline unsigned bi_reverse(unsigned code, /* the value to invert */
292 int len) /* its bit length */
293{
294 register unsigned res = 0;
295 do {
296 res |= code & 1;
297 code >>= 1, res <<= 1;
298 } while (--len > 0);
299 return res >> 1;
300}
301
302/* ===========================================================================
303 * Flush the bit buffer, keeping at most 7 bits in it.
304 */
305static inline void bi_flush(deflate_state *s)
306{
307 if (s->bi_valid == 16) {
308 put_short(s, s->bi_buf);
309 s->bi_buf = 0;
310 s->bi_valid = 0;
311 } else if (s->bi_valid >= 8) {
312 put_byte(s, (Byte)s->bi_buf);
313 s->bi_buf >>= 8;
314 s->bi_valid -= 8;
315 }
316}
317
318/* ===========================================================================
319 * Flush the bit buffer and align the output on a byte boundary
320 */
321static inline void bi_windup(deflate_state *s)
322{
323 if (s->bi_valid > 8) {
324 put_short(s, s->bi_buf);
325 } else if (s->bi_valid > 0) {
326 put_byte(s, (Byte)s->bi_buf);
327 }
328 s->bi_buf = 0;
329 s->bi_valid = 0;
330#ifdef DEBUG_ZLIB
331 s->bits_sent = (s->bits_sent+7) & ~7;
332#endif
333}
334