Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
ZDeflate.c
Go to the documentation of this file.
1/* @(#)root/zip:$Id$ */
2/* Author: */
3/*
4
5 Copyright (C) 1990-1993 Mark Adler, Richard B. Wales, Jean-loup Gailly,
6 Kai Uwe Rommel and Igor Mandrichenko.
7 For conditions of distribution and use, see copyright notice in zlib.h
8
9*/
10#include "Bits.h"
11
12/*
13 * deflate.c by Jean-loup Gailly.
14 *
15 * PURPOSE
16 *
17 * Identify new text as repetitions of old text within a fixed-
18 * length sliding window trailing behind the new text.
19 *
20 * DISCUSSION
21 *
22 * The "deflation" process depends on being able to identify portions
23 * of the input text which are identical to earlier input (within a
24 * sliding window trailing behind the input currently being processed).
25 *
26 * The most straightforward technique turns out to be the fastest for
27 * most input files: try all possible matches and select the longest.
28 * The key feature of this algorithm is that insertions into the string
29 * dictionary are very simple and thus fast, and deletions are avoided
30 * completely. Insertions are performed at each input character, whereas
31 * string matches are performed only when the previous match ends. So it
32 * is preferable to spend more time in matches to allow very fast string
33 * insertions and avoid deletions. The matching algorithm for small
34 * strings is inspired from that of Rabin & Karp. A brute force approach
35 * is used to find longer strings when a small match has been found.
36 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
37 * (by Leonid Broukhis).
38 * A previous version of this file used a more sophisticated algorithm
39 * (by Fiala and Greene) which is guaranteed to run in linear amortized
40 * time, but has a larger average cost, uses more memory and is patented.
41 * However the F&G algorithm may be faster for some highly redundant
42 * files if the parameter max_chain_length (described below) is too large.
43 *
44 * ACKNOWLEDGEMENTS
45 *
46 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
47 * I found it in 'freeze' written by Leonid Broukhis.
48 * Thanks to many info-zippers for bug reports and testing.
49 *
50 * REFERENCES
51 *
52 * APPNOTE.TXT documentation file in PKZIP 1.93a distribution.
53 *
54 * A description of the Rabin and Karp algorithm is given in the book
55 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
56 *
57 * Fiala,E.R., and Greene,D.H.
58 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
59 *
60 * INTERFACE
61 *
62 * void lm_init (int pack_level, ush *flags)
63 * Initialize the "longest match" routines for a new file
64 *
65 * ulg deflate (void)
66 * Processes a new input file and return its compressed length. Sets
67 * the compressed length, crc, deflate flags and internal file
68 * attributes.
69 */
70
71/* #include "zip.h" */
72/* #include "ZIP.h" */
73
74/* ===========================================================================
75 * Configuration parameters
76 */
77
78/* Compile with MEDIUM_MEM to reduce the memory requirements or
79 * with SMALL_MEM to use as little memory as possible. Use BIG_MEM if the
80 * entire input file can be held in memory (not possible on 16 bit systems).
81 * Warning: defining these symbols affects HASH_BITS (see below) and thus
82 * affects the compression ratio. The compressed output
83 * is still correct, and might even be smaller in some cases.
84 */
85
86#if BITS_NOT_INCLUDED
87#ifdef SMALL_MEM
88# define HASH_BITS 13 /* Number of bits used to hash strings */
89#endif
90#ifdef MEDIUM_MEM
91# define HASH_BITS 14
92#endif
93#ifndef HASH_BITS
94# define HASH_BITS 15
95/* For portability to 16 bit machines, do not use values above 15. */
96#endif
97#endif
98
99/* #define HASH_SIZE (unsigned)(1<<HASH_BITS) now in Bits.h */
100#define HASH_MASK (HASH_SIZE-1)
101#define WMASK (WSIZE-1)
102/* HASH_SIZE and WSIZE must be powers of two */
103
104#define NIL 0
105/* Tail of hash chains */
106
107#define FAST 4
108#define SLOW 2
109/* speed options for the general purpose bit flag */
110
111#ifndef TOO_FAR
112# define TOO_FAR 4096
113#endif
114/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
115
116#ifdef ATARI_ST
117# undef MSDOS /* avoid the processor specific parts */
118 /* (but the Atari should never define MSDOS anyway ...) */
119#endif
120#if defined(MSDOS) && !defined(NO_ASM) && !defined(ASMV) && !defined(WIN32)
121# define ASMV
122#endif
123#if defined(ASMV) && !defined(MSDOS) && defined(DYN_ALLOC)
124 error: DYN_ALLOC not yet supported in match.s
125#endif
126#if defined(MSDOS) && !defined(__32BIT__)
127# define MAXSEG_64K
128#endif
129
130
131
132/* Values for state->max_lazy_match, good_match and max_chain_length, depending on
133 * the desired pack level (0..9). The values given below have been tuned to
134 * exclude worst case performance for pathological files. Better values may be
135 * found for specific files.
136 */
137
138typedef struct config {
139 ush good_length; /* reduce lazy search above this match length */
140 ush max_lazy; /* do not perform lazy search above this match length */
141 ush nice_length; /* quit search above this match length */
144
145
147/* good lazy nice chain */
148/* 0 */ {0, 0, 0, 0}, /* store only */
149/* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */
150/* 2 */ {4, 5, 16, 8},
151/* 3 */ {4, 6, 32, 32},
152
153/* 4 */ {4, 4, 16, 16}, /* lazy matches */
154/* 5 */ {8, 16, 32, 32},
155/* 6 */ {8, 16, 128, 128},
156/* 7 */ {8, 32, 128, 256},
157/* 8 */ {32, 128, 258, 1024},
158/* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
159
160/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
161 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
162 * meaning.
163 */
164
165#define EQUAL 0
166/* result of memcmp for equal strings */
167
168/* ===========================================================================
169 * Prototypes for local functions.
170 */
171
174
175 int R__longest_match OF((bits_internal_state *state, IPos cur_match));
176#ifdef ASMV
177 void match_init OF((void)); /* asm code initialization */
178#endif
179
180#ifdef DEBUG
181local void check_match OF((IPos start, IPos match, int length));
182#endif
183
184/* ===========================================================================
185 * Update a hash value with the given input byte
186 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
187 * input characters, so that a running hash key can be computed from the
188 * previous key instead of complete recalculation each time.
189 */
190#define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
191
192/* ===========================================================================
193 * Insert string s in the dictionary and set match_head to the previous head
194 * of the hash chain (the most recent string with same hash key). Return
195 * the previous length of the hash chain.
196 * IN assertion: all calls to to INSERT_STRING are made with consecutive
197 * input characters and the first MIN_MATCH bytes of s are valid
198 * (except for the last MIN_MATCH-1 bytes of the input file).
199 */
200#define INSERT_STRING(s, match_head) \
201 (UPDATE_HASH(state->ins_h, state->R__window[(s) + MIN_MATCH-1]), \
202 state->R__prev[(s) & WMASK] = match_head = state->R__head[state->ins_h], \
203 state->R__head[state->ins_h] = (s))
204
205/* ===========================================================================
206 * Initialize the "longest match" routines for a new file
207 *
208 * IN assertion: window_size is > 0 if the input file is already read or
209 * mmap'ed in the window[] array, 0 otherwise. In the first case,
210 * window_size is sufficient to contain the whole input file plus
211 * MIN_LOOKAHEAD bytes (to avoid referencing memory beyond the end
212 * of window[] when looking for matches towards the end).
213 */
214int R__lm_init (bits_internal_state *state, int pack_level, ush *flags)
215 /* int pack_level; 0: store, 1: best speed, 9: best compression */
216 /* ush *flags; general purpose bit flag */
217{
218 register unsigned j;
219
220 if (pack_level < 1 || pack_level > 9) {
221 R__error("bad pack level");
222 return 1;
223 }
224
225 /* Do not slide the window if the whole input is already in memory
226 * (window_size > 0)
227 */
228 state->sliding = 0;
229 if (state->R__window_size == 0L) {
230 state->sliding = 1;
231 state->R__window_size = (ulg)2L*WSIZE;
232 }
233
234 /* Use dynamic allocation if compiler does not like big static arrays: */
235#ifdef DYN_ALLOC
236 if (state->R__window == NULL) {
237 state->R__window = (uch*) fcalloc(WSIZE, 2*sizeof(uch));
238 if (state->R__window == NULL) { R__error("window allocation"); return 1; }
239 }
240 if (state->R__prev == NULL) {
241 state->R__prev = (Pos*) fcalloc(WSIZE, sizeof(Pos));
242 state->R__head = (Pos*) fcalloc(HASH_SIZE, sizeof(Pos));
243 if (state->R__prev == NULL || state->R__head == NULL) {
244 R__error("hash table allocation");
245 return 1;
246 }
247 }
248#endif /* DYN_ALLOC */
249
250 /* Initialize the hash table (avoiding 64K overflow for 16 bit systems).
251 * prev[] will be initialized on the fly.
252 */
253 state->R__head[HASH_SIZE-1] = NIL;
254 memset((char*)state->R__head, NIL, (unsigned)(HASH_SIZE-1)*sizeof(*state->R__head));
255
256 /* Set the default configuration parameters:
257 */
258 state->max_lazy_match = configuration_table[pack_level].max_lazy;
259 state->R__good_match = configuration_table[pack_level].good_length;
260#ifndef FULL_SEARCH
261 state->R__nice_match = configuration_table[pack_level].nice_length;
262#endif
263 state->R__max_chain_length = configuration_table[pack_level].max_chain;
264 if (pack_level == 1) {
265 *flags |= FAST;
266 } else if (pack_level == 9) {
267 *flags |= SLOW;
268 }
269 /* ??? reduce max_chain_length for binary files */
270
271 state->R__strstart = 0;
272 state->R__block_start = 0L;
273#ifdef ASMV
274 match_init(); /* initialize the asm code */
275#endif
276
277 j = WSIZE;
278#ifndef MAXSEG_64K
279 if (sizeof(int) > 2) j <<= 1; /* Can read 64K in one step */
280#endif
281 state->lookahead = R__mem_read(state,(char*)state->R__window, j);
282
283 if (state->lookahead == 0 || state->lookahead == (unsigned)EOF) {
284 state->eofile = 1, state->lookahead = 0;
285 return 0;
286 }
287 state->eofile = 0;
288 /* Make sure that we always have enough state->lookahead. This is important
289 * if input comes from a device such as a tty.
290 */
291 while (state->lookahead < MIN_LOOKAHEAD && !state->eofile) R__fill_window(state);
292
293 state->ins_h = 0;
294 for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(state->ins_h, state->R__window[j]);
295 /* If state->lookahead < MIN_MATCH, state->ins_h is garbage, but this is
296 * not important since only literal bytes will be emitted.
297 */
298 return 0;
299}
300
301/* ===========================================================================
302 * Free the window and hash table
303 */
305{
306#ifdef DYN_ALLOC
307 if (state->R__window != NULL) {
308 fcfree(state->R__window);
309 state->R__window = NULL;
310 }
311 if (state->R__prev != NULL) {
312 fcfree(state->R__prev);
313 fcfree(state->R__head);
314 state->R__prev = state->R__head = NULL;
315 }
316#endif /* DYN_ALLOC */
317}
318
319/* ===========================================================================
320 * Set match_start to the longest match starting at the given string and
321 * return its length. Matches shorter or equal to prev_length are discarded,
322 * in which case the result is equal to prev_length and match_start is
323 * garbage.
324 * IN assertions: cur_match is the head of the hash chain for the current
325 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
326 */
327#ifndef ASMV
328/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
329 * match.s. The code is functionally equivalent, so you can use the C version
330 * if desired. A 68000 version is in amiga/match_68.a -- this could be used
331 * with other 68000 based systems such as Macintosh with a little effort.
332 */
334 /* IPos cur_match; */ /* current match */
335{
336 unsigned chain_length = state->R__max_chain_length; /* max hash chain length */
337 register uch *scan = state->R__window + state->R__strstart; /* current string */
338 register uch *match; /* matched string */
339 register int len; /* length of current match */
340 int best_len = state->R__prev_length; /* best match length so far */
341 IPos limit = state->R__strstart > (IPos)MAX_DIST ? state->R__strstart - (IPos)MAX_DIST : NIL;
342 /* Stop when cur_match becomes <= limit. To simplify the code,
343 * we prevent matches with the string of window index 0.
344 */
345
346/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
347 * It is easy to get rid of this optimization if necessary.
348 */
349#if HASH_BITS < 8 || MAX_MATCH != 258
350 error: Code too clever
351#endif
352
353#ifdef UNALIGNED_OK
354 /* Compare two bytes at a time. Note: this is not always beneficial.
355 * Try with and without -DUNALIGNED_OK to check.
356 */
357 register uch *strend = state->R__window + state->R__strstart + MAX_MATCH - 1;
358 register ush scan_start = *(ush*)scan;
359 register ush scan_end = *(ush*)(scan+best_len-1);
360#else
361 register uch *strend = state->R__window + state->R__strstart + MAX_MATCH;
362 register uch scan_end1 = scan[best_len-1];
363 register uch scan_end = scan[best_len];
364#endif
365
366 /* Do not waste too much time if we already have a good match: */
367 if (state->R__prev_length >= state->R__good_match) {
368 chain_length >>= 2;
369 }
370 Assert(state->R__strstart <= state->R__window_size-MIN_LOOKAHEAD, "insufficient lookahead");
371
372 do {
373 Assert(cur_match < state->R__strstart, "no future");
374 match = state->R__window + cur_match;
375
376 /* Skip to next match if the match length cannot increase
377 * or if the match length is less than 2:
378 */
379#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
380 /* This code assumes sizeof(unsigned short) == 2. Do not use
381 * UNALIGNED_OK if your compiler uses a different size.
382 */
383 if (*(ush*)(match+best_len-1) != scan_end ||
384 *(ush*)match != scan_start) continue;
385
386 /* It is not necessary to compare scan[2] and match[2] since they are
387 * always equal when the other bytes match, given that the hash keys
388 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
389 * strstart+3, +5, ... up to strstart+257. We check for insufficient
390 * lookahead only every 4th comparison; the 128th check will be made
391 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
392 * necessary to put more guard bytes at the end of the window, or
393 * to check more often for insufficient lookahead.
394 */
395 scan++, match++;
396 do {
397 } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
398 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
399 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
400 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
401 scan < strend);
402 /* The funny "do {}" generates better code on most compilers */
403
404 /* Here, scan <= window+state->R__strstart+257 */
405 Assert(scan <= state->R__window+(unsigned)(state->R__window_size-1), "wild scan");
406 if (*scan == *match) scan++;
407
408 len = (MAX_MATCH - 1) - (int)(strend-scan);
409 scan = strend - (MAX_MATCH-1);
410
411#else /* UNALIGNED_OK */
412
413 if (match[best_len] != scan_end ||
414 match[best_len-1] != scan_end1 ||
415 *match != *scan ||
416 *++match != scan[1]) continue;
417
418 /* The check at best_len-1 can be removed because it will be made
419 * again later. (This heuristic is not always a win.)
420 * It is not necessary to compare scan[2] and match[2] since they
421 * are always equal when the other bytes match, given that
422 * the hash keys are equal and that HASH_BITS >= 8.
423 */
424 scan += 2, match++;
425
426 /* We check for insufficient lookahead only every 8th comparison;
427 * the 256th check will be made at strstart+258.
428 */
429 do {
430 } while (*++scan == *++match && *++scan == *++match &&
431 *++scan == *++match && *++scan == *++match &&
432 *++scan == *++match && *++scan == *++match &&
433 *++scan == *++match && *++scan == *++match &&
434 scan < strend);
435
436 len = MAX_MATCH - (int)(strend - scan);
437 scan = strend - MAX_MATCH;
438
439#endif /* UNALIGNED_OK */
440
441 if (len > best_len) {
442 state->R__match_start = cur_match;
443 best_len = len;
444 if (len >= state->R__nice_match) break;
445#ifdef UNALIGNED_OK
446 scan_end = *(ush*)(scan+best_len-1);
447#else
448 scan_end1 = scan[best_len-1];
449 scan_end = scan[best_len];
450#endif
451 }
452 } while ((cur_match = state->R__prev[cur_match & WMASK]) > limit
453 && --chain_length != 0);
454
455 return best_len;
456}
457#endif /* ASMV */
458
459#ifdef DEBUG
460/* ===========================================================================
461 * Check that the match at match_start is indeed a match.
462 */
463local int check_match(IPos start, IPos match, int length)
464{
465 /* check that the match is indeed a match */
466 if (memcmp((char*)state->R__window + match,
467 (char*)state->R__window + start, length) != EQUAL) {
468 fprintf(stderr,
469 " start %d, match %d, length %d\n",
470 start, match, length);
471 R__error("invalid match");
472 return 1;
473 }
474/*
475 if (verbose > 1) {
476 fprintf(stderr,"\\‍[%d,%d]", start-match, length);
477 do { putc(state->R__window[start++], stderr); } while (--length != 0);
478 }
479*/
480 return 0;
481}
482#else
483# define check_match(start, match, length)
484#endif
485
486/* ===========================================================================
487 * Fill the window when the lookahead becomes insufficient.
488 * Updates strstart and lookahead, and sets state->eofile if end of input file.
489 *
490 * IN assertion: state->lookahead < MIN_LOOKAHEAD && strstart + state->lookahead > 0
491 * OUT assertions: at least one byte has been read, or state->eofile is set;
492 * file reads are performed for at least two bytes (required for the
493 * translate_eol option).
494 */
496{
497 register unsigned n, m;
498 unsigned more = (unsigned)(state->R__window_size - (ulg)state->lookahead - (ulg)state->R__strstart);
499 /* Amount of free space at the end of the window. */
500
501 /* If the window is almost full and there is insufficient lookahead,
502 * move the upper half to the lower one to make room in the upper half.
503 */
504 if (more == (unsigned)EOF) {
505 /* Very unlikely, but possible on 16 bit machine if strstart == 0
506 * and state->lookahead == 1 (input done one byte at time)
507 */
508 more--;
509
510 /* For MMAP or BIG_MEM, the whole input file is already in memory
511 * so we must not perform sliding. We must however call file_read
512 * in order to compute the crc, update state->lookahead and possibly set state->eofile.
513 */
514 } else if (state->R__strstart >= WSIZE+MAX_DIST && state->sliding) {
515
516 /* By the IN assertion, the window is not empty so we can't confuse
517 * more == 0 with more == 64K on a 16 bit machine.
518 */
519 memcpy((char*)state->R__window, (char*)state->R__window+WSIZE, (unsigned)WSIZE);
520 state->R__match_start -= WSIZE;
521 state->R__strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
522
523 state->R__block_start -= (long) WSIZE;
524
525 for (n = 0; n < HASH_SIZE; n++) {
526 m = state->R__head[n];
527 state->R__head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
528 }
529 for (n = 0; n < WSIZE; n++) {
530 m = state->R__prev[n];
531 state->R__prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
532 /* If n is not on any hash chain, prev[n] is garbage but
533 * its value will never be used.
534 */
535 }
536 more += WSIZE;
537 //if (verbose) putc('.', stderr);
538 }
539 /* At this point, more >= 2 */
540 if (!state->eofile) {
541 n = R__mem_read(state,(char*)state->R__window+state->R__strstart+state->lookahead, more);
542 if (n == 0 || n == (unsigned)EOF) {
543 state->eofile = 1;
544 } else {
545 state->lookahead += n;
546 }
547 }
548}
549
550/* ===========================================================================
551 * Flush the current block, with given end-of-file flag.
552 * IN assertion: strstart is set to the end of the current match.
553 */
554#define FLUSH_BLOCK(eof) \
555 R__flush_block(state, state->R__block_start >= 0L ? (char*)&state->R__window[(unsigned)state->R__block_start] : \
556 (char*)NULL, (long)state->R__strstart - state->R__block_start, (eof),errorflag)
557
558/* ===========================================================================
559 * Processes a new input file and return its compressed length. This
560 * function does not perform lazy evaluationof matches and inserts
561 * new strings in the dictionary only for unmatched strings or for short
562 * matches. It is used only for the fast compression options.
563 */
565{
566 IPos hash_head; /* head of the hash chain */
567 int flush; /* set if current block must be flushed */
568 unsigned match_length = 0; /* length of best match */
569
570 state->R__prev_length = MIN_MATCH-1;
571 while (state->lookahead != 0) {
572 /* Insert the string window[strstart .. strstart+2] in the
573 * dictionary, and set hash_head to the head of the hash chain:
574 */
575 INSERT_STRING(state->R__strstart, hash_head);
576
577 /* Find the longest match, discarding those <= prev_length.
578 * At this point we have always match_length < MIN_MATCH
579 */
580 if (hash_head != NIL && state->R__strstart - hash_head <= MAX_DIST) {
581 /* To simplify the code, we prevent matches with the string
582 * of window index 0 (in particular we have to avoid a match
583 * of the string with itself at the start of the input file).
584 */
585 match_length = R__longest_match (state, hash_head);
586 /* R__longest_match() sets match_start */
587 if (match_length > state->lookahead) match_length = state->lookahead;
588 }
589 if (match_length >= MIN_MATCH) {
590 check_match(state->R__strstart, state->R__match_start, match_length);
591
592 flush = R__ct_tally(state,state->R__strstart-state->R__match_start, match_length - MIN_MATCH);
593
594 state->lookahead -= match_length;
595
596 /* Insert new strings in the hash table only if the match length
597 * is not too large. This saves time but degrades compression.
598 */
599 if (match_length <= max_insert_length) {
600 match_length--; /* string at strstart already in hash table */
601 do {
602 state->R__strstart++;
603 INSERT_STRING(state->R__strstart, hash_head);
604 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
605 * always MIN_MATCH bytes ahead. If state->lookahead < MIN_MATCH
606 * these bytes are garbage, but it does not matter since
607 * the next lookahead bytes will be emitted as literals.
608 */
609 } while (--match_length != 0);
610 state->R__strstart++;
611 } else {
612 state->R__strstart += match_length;
613 match_length = 0;
614 state->ins_h = state->R__window[state->R__strstart];
615 UPDATE_HASH(state->ins_h, state->R__window[state->R__strstart+1]);
616#if MIN_MATCH != 3
617 Call UPDATE_HASH() MIN_MATCH-3 more times
618#endif
619 }
620 } else {
621 /* No match, output a literal byte */
622 Tracevv((stderr,"%c",state->R__window[state->R__strstart]));
623 flush = R__ct_tally (state, 0, state->R__window[state->R__strstart]);
624 state->lookahead--;
625 state->R__strstart++;
626 }
627 if (flush) FLUSH_BLOCK(0), state->R__block_start = state->R__strstart;
628
629 /* Make sure that we always have enough lookahead, except
630 * at the end of the input file. We need MAX_MATCH bytes
631 * for the next match, plus MIN_MATCH bytes to insert the
632 * string following the next match.
633 */
634 while (state->lookahead < MIN_LOOKAHEAD && !state->eofile) R__fill_window(state);
635
636 }
637 return FLUSH_BLOCK(1); /* eof */
638}
639
640/* ===========================================================================
641 * Same as above, but achieves better compression. We use a lazy
642 * evaluation for matches: a match is finally adopted only if there is
643 * no better match at the next window position.
644 */
645ulg R__Deflate(bits_internal_state *state,int *errorflag)
646{
647 IPos hash_head; /* head of hash chain */
648 IPos R__prev_match; /* previous match */
649 int flush; /* set if current block must be flushed */
650 int match_available = 0; /* set if previous match exists */
651 register unsigned match_length = MIN_MATCH-1; /* length of best match */
652#ifdef DEBUG
653 /* extern ulg R__isize; */ /* byte length of input file, for debug only */
654#endif
655
656 if (gCompressionLevel <= 3) return R__Deflate_fast(state,errorflag); /* optimized for speed */
657
658 /* Process the input block. */
659 while (state->lookahead != 0) {
660
661 /* Insert the string window[strstart .. strstart+2] in the
662 * dictionary, and set hash_head to the head of the hash chain:
663 */
664 INSERT_STRING(state->R__strstart, hash_head);
665
666 /* Find the longest match, discarding those <= prev_length.
667 */
668 state->R__prev_length = match_length, R__prev_match = state->R__match_start;
669 match_length = MIN_MATCH-1;
670
671 if (hash_head != NIL && state->R__prev_length < state->max_lazy_match &&
672 state->R__strstart - hash_head <= MAX_DIST) {
673 /* To simplify the code, we prevent matches with the string
674 * of window index 0 (in particular we have to avoid a match
675 * of the string with itself at the start of the input file).
676 */
677 match_length = R__longest_match (state,hash_head);
678 /* R__longest_match() sets match_start */
679 if (match_length > state->lookahead) match_length = state->lookahead;
680
681 /* Ignore a length 3 match if it is too distant: */
682 if (match_length == MIN_MATCH && state->R__strstart-state->R__match_start > TOO_FAR){
683 /* If prev_match is also MIN_MATCH, match_start is garbage
684 * but we will ignore the current match anyway.
685 */
686 match_length--;
687 }
688 }
689 /* If there was a match at the previous step and the current
690 * match is not better, output the previous match:
691 */
692 if (state->R__prev_length >= MIN_MATCH && match_length <= state->R__prev_length) {
693
694 check_match(state->R__strstart-1, R__prev_match, state->R__prev_length);
695
696 flush = R__ct_tally(state,state->R__strstart-1-R__prev_match, state->R__prev_length - MIN_MATCH);
697
698 /* Insert in hash table all strings up to the end of the match.
699 * strstart-1 and strstart are already inserted.
700 */
701 state->lookahead -= state->R__prev_length-1;
702 state->R__prev_length -= 2;
703 do {
704 state->R__strstart++;
705 INSERT_STRING(state->R__strstart, hash_head);
706 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
707 * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
708 * these bytes are garbage, but it does not matter since the
709 * next lookahead bytes will always be emitted as literals.
710 */
711 } while (--state->R__prev_length != 0);
712 match_available = 0;
713 match_length = MIN_MATCH-1;
714 state->R__strstart++;
715 if (flush) FLUSH_BLOCK(0), state->R__block_start = state->R__strstart;
716
717 } else if (match_available) {
718 /* If there was no match at the previous position, output a
719 * single literal. If there was a match but the current match
720 * is longer, truncate the previous match to a single literal.
721 */
722 Tracevv((stderr,"%c",state->R__window[state->R__strstart-1]));
723 if (R__ct_tally (state, 0, state->R__window[state->R__strstart-1])) {
724 FLUSH_BLOCK(0), state->R__block_start = state->R__strstart;
725 }
726 state->R__strstart++;
727 state->lookahead--;
728 } else {
729 /* There is no previous match to compare with, wait for
730 * the next step to decide.
731 */
732 match_available = 1;
733 state->R__strstart++;
734 state->lookahead--;
735 }
736#ifdef DEBUG
737 Assert (state->R__strstart <= state->R__isize && state->lookahead <= state->R__isize, "a bit too far");
738#endif
739
740 /* Make sure that we always have enough lookahead, except
741 * at the end of the input file. We need MAX_MATCH bytes
742 * for the next match, plus MIN_MATCH bytes to insert the
743 * string following the next match.
744 */
745 while (state->lookahead < MIN_LOOKAHEAD && !state->eofile) R__fill_window(state);
746 }
747 if (match_available) R__ct_tally (state, 0, state->R__window[state->R__strstart-1]);
748
749 return FLUSH_BLOCK(1); /* eof */
750}
751
int R__error(char *msg)
Definition Bits.c:125
int gCompressionLevel
Copyright (C) 1990-1993 Mark Adler, Richard B.
Definition Bits.c:77
#define max_insert_length
Definition Bits.h:160
ush Pos
Definition Bits.h:33
int R__mem_read()
#define HASH_SIZE
Definition Bits.h:28
unsigned IPos
Definition Bits.h:35
start
Definition Rotated.cxx:223
#define fcfree
Definition Tailor.h:128
#define fcalloc(items, size)
Definition Tailor.h:127
#define OF(a)
Definition Tailor.h:87
#define FLUSH_BLOCK(eof)
Definition ZDeflate.c:554
#define check_match(start, match, length)
Definition ZDeflate.c:483
static config configuration_table[10]
Definition ZDeflate.c:146
#define EQUAL
Definition ZDeflate.c:165
#define NIL
Definition ZDeflate.c:104
#define INSERT_STRING(s, match_head)
Definition ZDeflate.c:200
#define SLOW
Definition ZDeflate.c:108
int R__lm_free()
Definition ZDeflate.c:304
#define FAST
Definition ZDeflate.c:107
#define UPDATE_HASH(h, c)
Definition ZDeflate.c:190
int R__longest_match()
static ulg R__Deflate_fast()
static int R__fill_window()
#define TOO_FAR
Definition ZDeflate.c:112
#define WMASK
Definition ZDeflate.c:101
#define local
Definition ZIP.h:55
ulg R__Deflate()
#define Assert(cond, msg)
Definition ZIP.h:84
#define MIN_MATCH
Definition ZIP.h:22
int R__lm_init()
#define MIN_LOOKAHEAD
Definition ZIP.h:34
#define MAX_MATCH
Definition ZIP.h:23
int R__ct_tally()
#define MAX_DIST
Definition ZIP.h:39
#define Tracevv(x)
Definition ZIP.h:87
#define NULL
Definition ZInflate.c:15
unsigned short ush
Definition ZInflate.c:225
#define WSIZE
Definition ZInflate.c:229
unsigned long ulg
Definition ZInflate.c:226
unsigned char uch
Definition ZInflate.c:224
#define Code
Definition ZTrees.c:172
const Int_t n
Definition legend1.C:16
void limit()
Definition limit.C:29
int match(register char *str, register char **list)
Definition main.c:570
unsigned int max_lazy_match
Definition Bits.h:155
unsigned R__max_chain_length
Definition Bits.h:150
uch R__window[2L *((unsigned) 32768)]
Definition Bits.h:94
unsigned lookahead
Definition Bits.h:148
unsigned int R__prev_length
Definition Bits.h:140
unsigned ins_h
Definition Bits.h:131
unsigned R__strstart
Definition Bits.h:145
unsigned R__match_start
Definition Bits.h:146
unsigned R__good_match
Definition Bits.h:166
Pos R__prev[((unsigned) 32768)]
Definition Bits.h:104
long R__block_start
Definition Bits.h:123
Pos R__head[(unsigned)(1<< 15)]
Definition Bits.h:109
ush nice_length
Definition ZDeflate.c:141
ush good_length
Definition ZDeflate.c:139
ush max_chain
Definition ZDeflate.c:142
ush max_lazy
Definition ZDeflate.c:140
TMarker m
Definition textangle.C:8