Annotation of pgp/src/zdeflate.c, revision 1.1.1.1

1.1       root        1: /*
                      2: 
                      3:  Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
                      4:  Kai Uwe Rommel and Igor Mandrichenko.
                      5:  Permission is granted to any individual or institution to use, copy, or
                      6:  redistribute this software so long as all of the original files are included
                      7:  unmodified, that it is not sold for profit, and that this copyright notice
                      8:  is retained.
                      9: 
                     10: */
                     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 is of this algorithm is that insertion and deletions
                     29:  *      from the string dictionary are very simple and thus fast. Insertions
                     30:  *      and deletions are performed at each input character, whereas string
                     31:  *      matches are performed only when the previous match ends. So it is
                     32:  *      preferable to spend more time in matches to allow very fast string
                     33:  *      insertions and deletions. The matching algorithm for small strings
                     34:  *      is inspired from that of Rabin & Karp. A brute force approach is
                     35:  *      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 and uses more memory. However
                     41:  *      the F&G algorithm may be faster for some highly redundant files if
                     42:  *      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 2.0 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: 
                     73: /* ===========================================================================
                     74:  * Configuration parameters
                     75:  */
                     76: 
                     77: /* Compile with MEDIUM_MEM to reduce the memory requirements or
                     78:  * with SMALL_MEM to use as little memory as possible.
                     79:  * Warning: defining these symbols affects MATCH_BUFSIZE and HASH_BITS
                     80:  * (see below) and thus affects the compression ratio. The compressed output
                     81:  * is still correct, and might even be smaller in some cases.
                     82:  */
                     83: 
                     84: #ifdef SMALL_MEM
                     85: #   define HASH_BITS  13  /* Number of bits used to hash strings */
                     86: #else
                     87: #ifdef MEDIUM_MEM
                     88: #   define HASH_BITS  14
                     89: #else
                     90: #   define HASH_BITS  15
                     91:    /* For portability to 16 bit machines, do not use values above 15. */
                     92: #endif
                     93: #endif
                     94: 
                     95: #define HASH_SIZE (unsigned)(1<<HASH_BITS)
                     96: #define HASH_MASK (HASH_SIZE-1)
                     97: #define WMASK     (WSIZE-1)
                     98: /* HASH_SIZE and WSIZE must be powers of two */
                     99: 
                    100: #define NIL 0
                    101: /* Tail of hash chains */
                    102: 
                    103: #define FAST 4
                    104: #define SLOW 2
                    105: /* speed options for the general purpose bit flag */
                    106: 
                    107: #ifndef TOO_FAR
                    108: #  define TOO_FAR 4096
                    109: #endif
                    110: /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
                    111: 
                    112: /* ===========================================================================
                    113:  * Local data used by the "longest match" routines.
                    114:  */
                    115: 
                    116: typedef ush Pos;
                    117: typedef unsigned IPos;
                    118: /* A Pos is an index in the character window. We use short instead of int to
                    119:  * save space in the various tables. IPos is used only for parameter passing.
                    120:  */
                    121: 
                    122: #ifndef DYN_ALLOC
                    123:   uch    far window[2L*WSIZE];
                    124:   /* Sliding window. Input bytes are read into the second half of the window,
                    125:    * and move to the first half later to keep a dictionary of at least WSIZE
                    126:    * bytes. With this organization, matches are limited to a distance of
                    127:    * WSIZE-MAX_MATCH bytes, but this ensures that IO is always
                    128:    * performed with a length multiple of the block size. Also, it limits
                    129:    * the window size to 64K, which is quite useful on MSDOS.
                    130:    * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would
                    131:    * be less efficient since the data would have to be copied WSIZE/BSZ times)
                    132:    */
                    133:   Pos    far prev[WSIZE];
                    134:   /* Link to older string with same hash index. To limit the size of this
                    135:    * array to 64K, this link is maintained only for the last 32K strings.
                    136:    * An index in this array is thus a window index modulo 32K.
                    137:    */
                    138:   Pos    far head[HASH_SIZE];
                    139:   /* Heads of the hash chains or NIL */
                    140: #else
                    141:   uch    far * near window = NULL;
                    142:   Pos    far * near prev   = NULL;
                    143:   Pos    far * near head;
                    144: #endif
                    145: 
                    146: long block_start;
                    147: /* window position at the beginning of the current output block. Gets
                    148:  * negative when the window is moved backwards.
                    149:  */
                    150: 
                    151: local unsigned near ins_h;  /* hash index of string to be inserted */
                    152: 
                    153: #define H_SHIFT  ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
                    154: /* Number of bits by which ins_h and del_h must be shifted at each
                    155:  * input step. It must be such that after MIN_MATCH steps, the oldest
                    156:  * byte no longer takes part in the hash key, that is:
                    157:  *   H_SHIFT * MIN_MATCH >= HASH_BITS
                    158:  */
                    159: 
                    160: unsigned int near prev_length;
                    161: /* Length of the best match at previous step. Matches not greater than this
                    162:  * are discarded. This is used in the lazy match evaluation.
                    163:  */
                    164: 
                    165:       unsigned near strstart;      /* start of string to insert */
                    166:       unsigned near match_start;   /* start of matching string */
                    167: local int      near eofile;        /* flag set at end of input file */
                    168: local unsigned near lookahead;     /* number of valid bytes ahead in window */
                    169: 
                    170: unsigned near max_chain_length;
                    171: /* To speed up deflation, hash chains are never searched beyond this length.
                    172:  * A higher limit improves compression ratio but degrades the speed.
                    173:  */
                    174: 
                    175: local unsigned int max_lazy_match;
                    176: /* Attempt to find a better match only when the current match is strictly
                    177:  * smaller than this value.
                    178:  */
                    179: 
                    180: int near good_match;
                    181: /* Use a faster search when the previous match is longer than this */
                    182: 
                    183: 
                    184: /* Values for max_lazy_match, good_match and max_chain_length, depending on
                    185:  * the desired pack level (0..9). The values given below have been tuned to
                    186:  * exclude worst case performance for pathological files. Better values may be
                    187:  * found for specific files.
                    188:  */
                    189: typedef struct config {
                    190:    int good_length;
                    191:    int max_lazy;
                    192:    unsigned max_chain;
                    193:    uch flag;
                    194: } config;
                    195: 
                    196: local config configuration_table[10] = {
                    197: /*      good lazy chain flag */
                    198: /* 0 */ {0,    0,    0,  0},     /* store only */
                    199: /* 1 */ {4,    4,   16,  FAST},  /* maximum speed  */
                    200: /* 2 */ {6,    8,   16,  0},
                    201: /* 3 */ {8,   16,   32,  0},
                    202: /* 4 */ {8,   32,   64,  0},
                    203: /* 5 */ {8,   64,  128,  0},
                    204: /* 6 */ {8,  128,  256,  0},
                    205: /* 7 */ {8,  128,  512,  0},
                    206: /* 8 */ {32, 258, 1024,  0},
                    207: /* 9 */ {32, 258, 4096,  SLOW}}; /* maximum compression */
                    208: 
                    209: /* Note: the current code requires max_lazy >= MIN_MATCH and max_chain >= 4
                    210:  * but these restrictions can easily be removed at a small cost.
                    211:  */
                    212: 
                    213: #define EQUAL 0
                    214: /* result of memcmp for equal strings */
                    215: 
                    216: /* ===========================================================================
                    217:  *  Prototypes for local functions. Use asm version by default for
                    218:  *  MSDOS but not Unix. However the asm version version is recommended
                    219:  *  for 386 Unix.
                    220:  */
                    221: #ifdef ATARI_ST
                    222: #  undef MSDOS /* avoid the processor specific parts */
                    223: #endif
                    224: #if defined(MSDOS) && !defined(NO_ASM) && !defined(ASM)
                    225: #  define ASM
                    226: #endif
                    227: 
                    228: local void fill_window   OF((void));
                    229:       int  longest_match OF((IPos cur_match));
                    230: #ifdef ASM
                    231:       void match_init OF((void)); /* asm code initialization */
                    232: #endif
                    233: 
                    234: #ifdef DEBUG
                    235: local  void check_match OF((IPos start, IPos match, int length));
                    236: #endif
                    237: 
                    238: #define MIN(a,b) ((a) <= (b) ? (a) : (b))
                    239: /* The arguments must not have side effects. */
                    240: 
                    241: /* ===========================================================================
                    242:  * Update a hash value with the given input byte
                    243:  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
                    244:  *    input characters, so that a running hash key can be computed from the
                    245:  *    previous key instead of complete recalculation each time.
                    246:  */
                    247: #define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
                    248: 
                    249: /* ===========================================================================
                    250:  * Insert string s in the dictionary and set match_head to the previous head
                    251:  * of the hash chain (the most recent string with same hash key). Return
                    252:  * the previous length of the hash chain.
                    253:  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
                    254:  *    input characters and the first MIN_MATCH bytes of s are valid
                    255:  *    (except for the last MIN_MATCH-1 bytes of the input file).
                    256:  */
                    257: #define INSERT_STRING(s, match_head) \
                    258:    (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \
                    259:     prev[(s) & WMASK] = match_head = head[ins_h], \
                    260:     head[ins_h] = (s))
                    261: 
                    262: /* ===========================================================================
                    263:  * Initialize the "longest match" routines for a new file
                    264:  */
                    265: void lm_init (pack_level, flags)
                    266:     int pack_level; /* 0: store, 1: best speed, 9: best compression */
                    267:     ush *flags;     /* general purpose bit flag */
                    268: {
                    269:     register unsigned j;
                    270: 
                    271:     if (pack_level < 1 || pack_level > 9) error("bad pack level");
                    272: 
                    273:     /* Use dynamic allocation if compiler does not like big static arrays: */
                    274: #ifdef DYN_ALLOC
                    275:     if (window == NULL) {
                    276:         window = (uch far*) fcalloc(WSIZE,   2*sizeof(uch));
                    277:         prev   = (Pos far*) fcalloc(WSIZE,     sizeof(Pos));
                    278:         head   = (Pos far*) fcalloc(HASH_SIZE, sizeof(Pos));
                    279: 
                    280:         if (window == NULL || prev == NULL || head == NULL) {
                    281:             err(ZE_MEM, "window allocation");
                    282:         }
                    283:     }
                    284: #endif /* DYN_ALLOC */
                    285: #ifdef ASM
                    286:     match_init(); /* initialize the asm code */
                    287: #endif
                    288:     /* Initialize the hash table. */
                    289:     for (j = 0;  j < HASH_SIZE; j++) head[j] = NIL;
                    290:     /* prev will be initialized on the fly */
                    291: 
                    292:     /* Set the default configuration parameters:
                    293:      */
                    294:     max_lazy_match   = configuration_table[pack_level].max_lazy;
                    295:     good_match       = configuration_table[pack_level].good_length;
                    296:     max_chain_length = configuration_table[pack_level].max_chain;
                    297:     *flags          |= configuration_table[pack_level].flag;
                    298:     /* ??? reduce max_chain_length for binary files */
                    299: 
                    300:     strstart = 0;
                    301:     block_start = 0L;
                    302: 
                    303: #if defined(MSDOS) && !defined(__32BIT__)
                    304:     /* Can't read a 64K block under MSDOS */
                    305:     lookahead = read_buf((char*)window, (unsigned)WSIZE);
                    306: #else
                    307:     lookahead = read_buf((char*)window, 2*WSIZE);
                    308: #endif
                    309:     if (lookahead == 0 || lookahead == (unsigned)EOF) {
                    310:        eofile = 1, lookahead = 0;
                    311:        return;
                    312:     }
                    313:     eofile = 0;
                    314:     /* Make sure that we always have enough lookahead. This is important
                    315:      * if input comes from a device such as a tty.
                    316:      */
                    317:     while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
                    318: 
                    319:     ins_h = 0;
                    320:     for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]);
                    321:     /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
                    322:      * not important since only literal bytes will be emitted.
                    323:      */
                    324: }
                    325: 
                    326: /* ===========================================================================
                    327:  * Set match_start to the longest match starting at the given string and
                    328:  * return its length. Matches shorter or equal to prev_length are discarded,
                    329:  * in which case the result is equal to prev_length and match_start is
                    330:  * garbage.
                    331:  * IN assertions: cur_match is the head of the hash chain for the current
                    332:  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
                    333:  */
                    334: #ifndef ASM
                    335: /* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm. The code
                    336:  * is functionally equivalent, so you can use the C version if desired.
                    337:  */
                    338: int longest_match(cur_match)
                    339:     IPos cur_match;                             /* current match */
                    340: {
                    341:     unsigned chain_length = max_chain_length;   /* max hash chain length */
                    342:     register uch far *scan = window + strstart; /* current string */
                    343:     register uch far *match = scan;             /* matched string */
                    344:     register int len;                           /* length of current match */
                    345:     int best_len = prev_length;                 /* best match length so far */
                    346:     IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
                    347:     /* Stop when cur_match becomes <= limit. To simplify the code,
                    348:      * we prevent matches with the string of window index 0.
                    349:      */
                    350: #ifdef UNALIGNED_OK
                    351:     register ush scan_start = *(ush*)scan;
                    352:     register ush scan_end   = *(ush*)(scan+best_len-1);
                    353: #else
                    354:     register uch scan_start = *scan;
                    355:     register uch scan_end1  = scan[best_len-1];
                    356:     register uch scan_end   = scan[best_len];
                    357: #endif
                    358: 
                    359:     /* Do not waste too much time if we already have a good match: */
                    360:     if (prev_length >= good_match) {
                    361:         chain_length >>= 2;
                    362:     }
                    363: 
                    364:     do {
                    365:         Assert(cur_match < strstart, "no future");
                    366:         match = window + cur_match;
                    367: 
                    368:         /* Skip to next match if the match length cannot increase
                    369:          * or if the match length is less than 2:
                    370:          */
                    371: #if (defined(UNALIGNED_OK) && HASH_BITS >= 8)
                    372:         /* This code assumes sizeof(unsigned short) == 2 and
                    373:          * sizeof(unsigned long) == 4. Do not use UNALIGNED_OK if your
                    374:          * compiler uses different sizes.
                    375:          */
                    376:         if (*(ush*)(match+best_len-1) != scan_end ||
                    377:             *(ush*)match != scan_start) continue;
                    378: 
                    379:         len = MIN_MATCH - 4;
                    380:         /* It is not necessary to compare scan[2] and match[2] since they are
                    381:          * always equal when the other bytes match, given that the hash keys
                    382:          * are equal and that HASH_BITS >= 8.
                    383:          */
                    384:         do {} while ((len+=4) < MAX_MATCH-3 &&
                    385:                      *(ulg*)(scan+len) == *(ulg*)(match+len));
                    386:         /* The funny do {} generates better code for most compilers */
                    387: 
                    388:         if (*(ush*)(scan+len) == *(ush*)(match+len)) len += 2;
                    389:         if (scan[len] == match[len]) len++;
                    390: 
                    391: #else /* UNALIGNED_OK */
                    392:         if (match[best_len] != scan_end ||
                    393:             match[best_len-1] != scan_end1 || *match != scan_start)
                    394:            continue;
                    395:         /* It is not necessary to compare scan[1] and match[1] since they
                    396:          * are always equal when the other bytes match, given that
                    397:          * the hash keys are equal and that h_shift+8 <= HASH_BITS,
                    398:          * that is, when the last byte is entirely included in the hash key.
                    399:          * The condition is equivalent to
                    400:          *       (HASH_BITS+2)/3 + 8 <= HASH_BITS
                    401:          * or: HASH_BITS >= 13
                    402:          * Also, we check for a match at best_len-1 to get rid quickly of
                    403:          * the match with the suffix of the match made at the previous step,
                    404:          * which is known to fail.
                    405:          */
                    406: #if HASH_BITS >= 13
                    407:         len = 1;
                    408: #else
                    409:         len = 0;
                    410: #endif
                    411:         do {} while (++len < MAX_MATCH && scan[len] == match[len]);
                    412: 
                    413: #endif /* UNALIGNED_OK */
                    414: 
                    415:         if (len > best_len) {
                    416:             match_start = cur_match;
                    417:             best_len = len;
                    418:             if (len == MAX_MATCH) break;
                    419: #ifdef UNALIGNED_OK
                    420:             scan_end = *(ush*)(scan+best_len-1);
                    421: #else
                    422:             scan_end1  = scan[best_len-1];
                    423:             scan_end   = scan[best_len];
                    424: #endif
                    425:         }
                    426:     } while (--chain_length != 0 &&
                    427:              (cur_match = prev[cur_match & WMASK]) > limit);
                    428: 
                    429:     return best_len;
                    430: }
                    431: #endif /* NO_ASM */
                    432: 
                    433: #ifdef DEBUG
                    434: /* ===========================================================================
                    435:  * Check that the match at match_start is indeed a match.
                    436:  */
                    437: local void check_match(start, match, length)
                    438:     IPos start, match;
                    439:     int length;
                    440: {
                    441:     /* check that the match is indeed a match */
                    442:     if (memcmp((char*)window + match,
                    443:                 (char*)window + start, length) != EQUAL) {
                    444:         fprintf(stderr,
                    445:             " start %d, match %d, length %d\n",
                    446:             start, match, length);
                    447:         error("invalid match");
                    448:     }
                    449:     if (verbose > 1) {
                    450:         fprintf(stderr,"\\[%d,%d]", start-match, length);
                    451:         do { putc(window[start++], stderr); } while (--length != 0);
                    452:     }
                    453: }
                    454: #else
                    455: #  define check_match(start, match, length)
                    456: #endif
                    457: 
                    458: /* ===========================================================================
                    459:  * Fill the window when the lookahead becomes insufficient.
                    460:  * Updates strstart and lookahead, and sets eofile if end of input file.
                    461:  * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
                    462:  * OUT assertions: at least one byte has been read, or eofile is set;
                    463:  *    file reads are performed for at least two bytes (required for the
                    464:  *    translate_eol option).
                    465:  */
                    466: local void fill_window()
                    467: {
                    468:     register unsigned n, m;
                    469:     unsigned more = (unsigned)((ulg)2*WSIZE - (ulg)lookahead - (ulg)strstart);
                    470:     /* Amount of free space at the end of the window. */
                    471: 
                    472:     /* If the window is full, move the upper half to the lower one to make
                    473:      * room in the upper half.
                    474:      */
                    475:     if (more == (unsigned)EOF) {
                    476:         /* Very unlikely, but possible on 16 bit machine if strstart == 0
                    477:          * and lookahead == 1 (input done one byte at time)
                    478:          */
                    479:         more--;
                    480:     } else if (more <= 1) {
                    481:         /* By the IN assertion, the window is not empty so we can't confuse
                    482:          * more == 0 with more == 64K on a 16 bit machine.
                    483:          */
                    484:         memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
                    485:         match_start -= WSIZE;
                    486:         strstart    -= WSIZE;
                    487:         /* strstart - WSIZE >= WSIZE - 1 - lookahead >= WSIZE - MIN_LOOKAHEAD
                    488:          * so we now have strstart >= MAX_DIST:
                    489:          */
                    490:         Assert (strstart >= MAX_DIST, "window slide too early");
                    491:         block_start -= (long) WSIZE;
                    492: 
                    493:         for (n = 0; n < HASH_SIZE; n++) {
                    494:             m = head[n];
                    495:             head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
                    496:         }
                    497:         for (n = 0; n < WSIZE; n++) {
                    498:             m = prev[n];
                    499:             prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
                    500:             /* If n is not on any hash chain, prev[n] is garbage but
                    501:              * its value will never be used.
                    502:              */
                    503:         }
                    504:         more += WSIZE;
                    505: #ifdef ZIP
                    506:         if (verbose) putc('.', stderr);
                    507: #endif
                    508:     }
                    509:     /* At this point, more >= 2 */
                    510:     n = read_buf((char*)window+strstart+lookahead, more);
                    511:     if (n == 0 || n == (unsigned)EOF) {
                    512:         eofile = 1;
                    513:     } else {
                    514:         lookahead += n;
                    515:     }
                    516: }
                    517: 
                    518: /* ===========================================================================
                    519:  * Flush the current block, with given end-of-file flag.
                    520:  * IN assertion: strstart is set to the end of the current match.
                    521:  */
                    522: #define FLUSH_BLOCK(eof) \
                    523:    flush_block(block_start >= 0L ? (char*)&window[block_start] : (char*)NULL,\
                    524:                (long)strstart - block_start, (eof))
                    525: 
                    526: /* ===========================================================================
                    527:  * Processes a new input file and return its compressed length.
                    528:  */
                    529: #ifdef NO_LAZY
                    530: ulg deflate()
                    531: {
                    532:     IPos hash_head; /* head of the hash chain */
                    533:     int flush;      /* set if current block must be flushed */
                    534:     unsigned match_length = 0;  /* length of best match */
                    535: 
                    536:     prev_length = MIN_MATCH-1;
                    537:     while (lookahead != 0) {
                    538:         /* Insert the string window[strstart .. strstart+2] in the
                    539:          * dictionary, and set hash_head to the head of the hash chain:
                    540:          */
                    541:         INSERT_STRING(strstart, hash_head);
                    542: 
                    543:         /* Find the longest match, discarding those <= prev_length.
                    544:          * At this point we have always match_length < MIN_MATCH
                    545:          */
                    546:         if (hash_head != NIL && strstart - hash_head <= MAX_DIST) {
                    547:             /* To simplify the code, we prevent matches with the string
                    548:              * of window index 0 (in particular we have to avoid a match
                    549:              * of the string with itself at the start of the input file).
                    550:              */
                    551:             match_length = longest_match (hash_head);
                    552:             /* longest_match() sets match_start */
                    553:             if (match_length > lookahead) match_length = lookahead;
                    554:         }
                    555:         if (match_length >= MIN_MATCH) {
                    556:             check_match(strstart, match_start, match_length);
                    557: 
                    558:             flush = ct_tally(strstart-match_start, match_length - MIN_MATCH);
                    559: 
                    560:             lookahead -= match_length;
                    561:             match_length--; /* string at strstart already in hash table */
                    562:             do {
                    563:                 strstart++;
                    564:                 INSERT_STRING(strstart, hash_head);
                    565:                 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
                    566:                  * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
                    567:                  * these bytes are garbage, but it does not matter since the
                    568:                  * next lookahead bytes will always be emitted as literals.
                    569:                  */
                    570:             } while (--match_length != 0);
                    571:         } else {
                    572:             /* No match, output a literal byte */
                    573:             flush = ct_tally (0, window[strstart]);
                    574:             lookahead--;
                    575:         }
                    576:         strstart++; 
                    577:         if (flush) FLUSH_BLOCK(0), block_start = strstart;
                    578: 
                    579:         /* Make sure that we always have enough lookahead, except
                    580:          * at the end of the input file. We need MAX_MATCH bytes
                    581:          * for the next match, plus MIN_MATCH bytes to insert the
                    582:          * string following the next match.
                    583:          */
                    584:         while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
                    585: 
                    586:     }
                    587:     return FLUSH_BLOCK(1); /* eof */
                    588: }
                    589: #else /* LAZY */
                    590: /* ===========================================================================
                    591:  * Same as above, but achieves better compression. We use a lazy
                    592:  * evaluation for matches: a match is finally adopted only if there is
                    593:  * no better match at the next window position.
                    594:  */
                    595: ulg deflate()
                    596: {
                    597:     IPos hash_head;          /* head of hash chain */
                    598:     IPos prev_match;         /* previous match */
                    599:     int flush;               /* set if current block must be flushed */
                    600:     int match_available = 0; /* set if previous match exists */
                    601:     register unsigned match_length = MIN_MATCH-1; /* length of best match */
                    602: #ifdef DEBUG
                    603:     extern ulg isize;        /* byte length of input file, for debug only */
                    604: #endif
                    605: 
                    606:     /* Process the input block. */
                    607:     while (lookahead != 0) {
                    608:         /* Insert the string window[strstart .. strstart+2] in the
                    609:          * dictionary, and set hash_head to the head of the hash chain:
                    610:          */
                    611:         INSERT_STRING(strstart, hash_head);
                    612: 
                    613:         /* Find the longest match, discarding those <= prev_length.
                    614:          */
                    615:         prev_length = match_length, prev_match = match_start;
                    616:         match_length = MIN_MATCH-1;
                    617: 
                    618:         if (hash_head != NIL && prev_length < max_lazy_match &&
                    619:             strstart - hash_head <= MAX_DIST) {
                    620:             /* To simplify the code, we prevent matches with the string
                    621:              * of window index 0 (in particular we have to avoid a match
                    622:              * of the string with itself at the start of the input file).
                    623:              */
                    624:             match_length = longest_match (hash_head);
                    625:             /* longest_match() sets match_start */
                    626:             if (match_length > lookahead) match_length = lookahead;
                    627:             /* Ignore a length 3 match if it is too distant: */
                    628:             if (match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
                    629:                 /* If prev_match is also MIN_MATCH, match_start is garbage
                    630:                  * but we will ignore the current match anyway.
                    631:                  */
                    632:                 match_length--;
                    633:             }
                    634:         }
                    635:         /* If there was a match at the previous step and the current
                    636:          * match is not better, output the previous match:
                    637:          */
                    638:         if (prev_length >= MIN_MATCH && match_length <= prev_length) {
                    639: 
                    640:             check_match(strstart-1, prev_match, prev_length);
                    641: 
                    642:             flush = ct_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
                    643: 
                    644:             /* Insert in hash table all strings up to the end of the match.
                    645:              * strstart-1 and strstart are already inserted.
                    646:              */
                    647:             lookahead -= prev_length-1;
                    648:             prev_length -= 2;
                    649:             do {
                    650:                 strstart++;
                    651:                 INSERT_STRING(strstart, hash_head);
                    652:                 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
                    653:                  * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
                    654:                  * these bytes are garbage, but it does not matter since the
                    655:                  * next lookahead bytes will always be emitted as literals.
                    656:                  */
                    657:             } while (--prev_length != 0);
                    658:             match_available = 0;
                    659:             match_length = MIN_MATCH-1;
                    660: 
                    661:         } else if (match_available) {
                    662:             /* If there was no match at the previous position, output a
                    663:              * single literal. If there was a match but the current match
                    664:              * is longer, truncate the previous match to a single literal.
                    665:              */
                    666:             flush = ct_tally (0, window[strstart-1]);
                    667:             Tracevv((stderr,"%c",window[strstart-1]));
                    668:             lookahead--;
                    669:         } else {
                    670:             /* There is no previous match to compare with, wait for
                    671:              * the next step to decide.
                    672:              */
                    673:             match_available = 1;
                    674:             flush = 0;
                    675:             lookahead--;
                    676:         }
                    677:         if (flush) FLUSH_BLOCK(0), block_start = strstart;
                    678:         strstart++;
                    679:         Assert (strstart <= isize && lookahead <= isize, "a bit too far");
                    680: 
                    681:         /* Make sure that we always have enough lookahead, except
                    682:          * at the end of the input file. We need MAX_MATCH bytes
                    683:          * for the next match, plus MIN_MATCH bytes to insert the
                    684:          * string following the next match.
                    685:          */
                    686:         while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
                    687:     }
                    688:     if (match_available) ct_tally (0, window[strstart-1]);
                    689: 
                    690:     return FLUSH_BLOCK(1); /* eof */
                    691: }
                    692: #endif /* LAZY */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.