jdk/src/share/native/java/util/zip/zlib-1.2.5/deflate.c
changeset 24710 c019dc4104b2
parent 24709 52f4fc801542
child 24711 796059d86950
equal deleted inserted replaced
24709:52f4fc801542 24710:c019dc4104b2
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Oracle designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Oracle in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21  * or visit www.oracle.com if you need additional information or have any
       
    22  * questions.
       
    23  */
       
    24 
       
    25 /* deflate.c -- compress data using the deflation algorithm
       
    26  * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
       
    27  * For conditions of distribution and use, see copyright notice in zlib.h
       
    28  */
       
    29 
       
    30 /*
       
    31  *  ALGORITHM
       
    32  *
       
    33  *      The "deflation" process depends on being able to identify portions
       
    34  *      of the input text which are identical to earlier input (within a
       
    35  *      sliding window trailing behind the input currently being processed).
       
    36  *
       
    37  *      The most straightforward technique turns out to be the fastest for
       
    38  *      most input files: try all possible matches and select the longest.
       
    39  *      The key feature of this algorithm is that insertions into the string
       
    40  *      dictionary are very simple and thus fast, and deletions are avoided
       
    41  *      completely. Insertions are performed at each input character, whereas
       
    42  *      string matches are performed only when the previous match ends. So it
       
    43  *      is preferable to spend more time in matches to allow very fast string
       
    44  *      insertions and avoid deletions. The matching algorithm for small
       
    45  *      strings is inspired from that of Rabin & Karp. A brute force approach
       
    46  *      is used to find longer strings when a small match has been found.
       
    47  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
       
    48  *      (by Leonid Broukhis).
       
    49  *         A previous version of this file used a more sophisticated algorithm
       
    50  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
       
    51  *      time, but has a larger average cost, uses more memory and is patented.
       
    52  *      However the F&G algorithm may be faster for some highly redundant
       
    53  *      files if the parameter max_chain_length (described below) is too large.
       
    54  *
       
    55  *  ACKNOWLEDGEMENTS
       
    56  *
       
    57  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
       
    58  *      I found it in 'freeze' written by Leonid Broukhis.
       
    59  *      Thanks to many people for bug reports and testing.
       
    60  *
       
    61  *  REFERENCES
       
    62  *
       
    63  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
       
    64  *      Available in http://www.ietf.org/rfc/rfc1951.txt
       
    65  *
       
    66  *      A description of the Rabin and Karp algorithm is given in the book
       
    67  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
       
    68  *
       
    69  *      Fiala,E.R., and Greene,D.H.
       
    70  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
       
    71  *
       
    72  */
       
    73 
       
    74 /* @(#) $Id$ */
       
    75 
       
    76 #include "deflate.h"
       
    77 
       
    78 const char deflate_copyright[] =
       
    79    " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
       
    80 /*
       
    81   If you use the zlib library in a product, an acknowledgment is welcome
       
    82   in the documentation of your product. If for some reason you cannot
       
    83   include such an acknowledgment, I would appreciate that you keep this
       
    84   copyright string in the executable of your product.
       
    85  */
       
    86 
       
    87 /* ===========================================================================
       
    88  *  Function prototypes.
       
    89  */
       
    90 typedef enum {
       
    91     need_more,      /* block not completed, need more input or more output */
       
    92     block_done,     /* block flush performed */
       
    93     finish_started, /* finish started, need only more output at next deflate */
       
    94     finish_done     /* finish done, accept no more input or output */
       
    95 } block_state;
       
    96 
       
    97 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
       
    98 /* Compression function. Returns the block state after the call. */
       
    99 
       
   100 local void fill_window    OF((deflate_state *s));
       
   101 local block_state deflate_stored OF((deflate_state *s, int flush));
       
   102 local block_state deflate_fast   OF((deflate_state *s, int flush));
       
   103 #ifndef FASTEST
       
   104 local block_state deflate_slow   OF((deflate_state *s, int flush));
       
   105 #endif
       
   106 local block_state deflate_rle    OF((deflate_state *s, int flush));
       
   107 local block_state deflate_huff   OF((deflate_state *s, int flush));
       
   108 local void lm_init        OF((deflate_state *s));
       
   109 local void putShortMSB    OF((deflate_state *s, uInt b));
       
   110 local void flush_pending  OF((z_streamp strm));
       
   111 local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
       
   112 #ifdef ASMV
       
   113       void match_init OF((void)); /* asm code initialization */
       
   114       uInt longest_match  OF((deflate_state *s, IPos cur_match));
       
   115 #else
       
   116 local uInt longest_match  OF((deflate_state *s, IPos cur_match));
       
   117 #endif
       
   118 
       
   119 #ifdef DEBUG
       
   120 local  void check_match OF((deflate_state *s, IPos start, IPos match,
       
   121                             int length));
       
   122 #endif
       
   123 
       
   124 /* ===========================================================================
       
   125  * Local data
       
   126  */
       
   127 
       
   128 #define NIL 0
       
   129 /* Tail of hash chains */
       
   130 
       
   131 #ifndef TOO_FAR
       
   132 #  define TOO_FAR 4096
       
   133 #endif
       
   134 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
       
   135 
       
   136 /* Values for max_lazy_match, good_match and max_chain_length, depending on
       
   137  * the desired pack level (0..9). The values given below have been tuned to
       
   138  * exclude worst case performance for pathological files. Better values may be
       
   139  * found for specific files.
       
   140  */
       
   141 typedef struct config_s {
       
   142    ush good_length; /* reduce lazy search above this match length */
       
   143    ush max_lazy;    /* do not perform lazy search above this match length */
       
   144    ush nice_length; /* quit search above this match length */
       
   145    ush max_chain;
       
   146    compress_func func;
       
   147 } config;
       
   148 
       
   149 #ifdef FASTEST
       
   150 local const config configuration_table[2] = {
       
   151 /*      good lazy nice chain */
       
   152 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
       
   153 /* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
       
   154 #else
       
   155 local const config configuration_table[10] = {
       
   156 /*      good lazy nice chain */
       
   157 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
       
   158 /* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
       
   159 /* 2 */ {4,    5, 16,    8, deflate_fast},
       
   160 /* 3 */ {4,    6, 32,   32, deflate_fast},
       
   161 
       
   162 /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
       
   163 /* 5 */ {8,   16, 32,   32, deflate_slow},
       
   164 /* 6 */ {8,   16, 128, 128, deflate_slow},
       
   165 /* 7 */ {8,   32, 128, 256, deflate_slow},
       
   166 /* 8 */ {32, 128, 258, 1024, deflate_slow},
       
   167 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
       
   168 #endif
       
   169 
       
   170 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
       
   171  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
       
   172  * meaning.
       
   173  */
       
   174 
       
   175 #define EQUAL 0
       
   176 /* result of memcmp for equal strings */
       
   177 
       
   178 #ifndef NO_DUMMY_DECL
       
   179 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
       
   180 #endif
       
   181 
       
   182 /* ===========================================================================
       
   183  * Update a hash value with the given input byte
       
   184  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
       
   185  *    input characters, so that a running hash key can be computed from the
       
   186  *    previous key instead of complete recalculation each time.
       
   187  */
       
   188 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
       
   189 
       
   190 
       
   191 /* ===========================================================================
       
   192  * Insert string str in the dictionary and set match_head to the previous head
       
   193  * of the hash chain (the most recent string with same hash key). Return
       
   194  * the previous length of the hash chain.
       
   195  * If this file is compiled with -DFASTEST, the compression level is forced
       
   196  * to 1, and no hash chains are maintained.
       
   197  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
       
   198  *    input characters and the first MIN_MATCH bytes of str are valid
       
   199  *    (except for the last MIN_MATCH-1 bytes of the input file).
       
   200  */
       
   201 #ifdef FASTEST
       
   202 #define INSERT_STRING(s, str, match_head) \
       
   203    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
       
   204     match_head = s->head[s->ins_h], \
       
   205     s->head[s->ins_h] = (Pos)(str))
       
   206 #else
       
   207 #define INSERT_STRING(s, str, match_head) \
       
   208    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
       
   209     match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
       
   210     s->head[s->ins_h] = (Pos)(str))
       
   211 #endif
       
   212 
       
   213 /* ===========================================================================
       
   214  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
       
   215  * prev[] will be initialized on the fly.
       
   216  */
       
   217 #define CLEAR_HASH(s) \
       
   218     s->head[s->hash_size-1] = NIL; \
       
   219     zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
       
   220 
       
   221 /* ========================================================================= */
       
   222 int ZEXPORT deflateInit_(strm, level, version, stream_size)
       
   223     z_streamp strm;
       
   224     int level;
       
   225     const char *version;
       
   226     int stream_size;
       
   227 {
       
   228     return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
       
   229                          Z_DEFAULT_STRATEGY, version, stream_size);
       
   230     /* To do: ignore strm->next_in if we use it as window */
       
   231 }
       
   232 
       
   233 /* ========================================================================= */
       
   234 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
       
   235                   version, stream_size)
       
   236     z_streamp strm;
       
   237     int  level;
       
   238     int  method;
       
   239     int  windowBits;
       
   240     int  memLevel;
       
   241     int  strategy;
       
   242     const char *version;
       
   243     int stream_size;
       
   244 {
       
   245     deflate_state *s;
       
   246     int wrap = 1;
       
   247     static const char my_version[] = ZLIB_VERSION;
       
   248 
       
   249     ushf *overlay;
       
   250     /* We overlay pending_buf and d_buf+l_buf. This works since the average
       
   251      * output size for (length,distance) codes is <= 24 bits.
       
   252      */
       
   253 
       
   254     if (version == Z_NULL || version[0] != my_version[0] ||
       
   255         stream_size != sizeof(z_stream)) {
       
   256         return Z_VERSION_ERROR;
       
   257     }
       
   258     if (strm == Z_NULL) return Z_STREAM_ERROR;
       
   259 
       
   260     strm->msg = Z_NULL;
       
   261     if (strm->zalloc == (alloc_func)0) {
       
   262         strm->zalloc = zcalloc;
       
   263         strm->opaque = (voidpf)0;
       
   264     }
       
   265     if (strm->zfree == (free_func)0) strm->zfree = zcfree;
       
   266 
       
   267 #ifdef FASTEST
       
   268     if (level != 0) level = 1;
       
   269 #else
       
   270     if (level == Z_DEFAULT_COMPRESSION) level = 6;
       
   271 #endif
       
   272 
       
   273     if (windowBits < 0) { /* suppress zlib wrapper */
       
   274         wrap = 0;
       
   275         windowBits = -windowBits;
       
   276     }
       
   277 #ifdef GZIP
       
   278     else if (windowBits > 15) {
       
   279         wrap = 2;       /* write gzip wrapper instead */
       
   280         windowBits -= 16;
       
   281     }
       
   282 #endif
       
   283     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
       
   284         windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
       
   285         strategy < 0 || strategy > Z_FIXED) {
       
   286         return Z_STREAM_ERROR;
       
   287     }
       
   288     if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
       
   289     s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
       
   290     if (s == Z_NULL) return Z_MEM_ERROR;
       
   291     strm->state = (struct internal_state FAR *)s;
       
   292     s->strm = strm;
       
   293 
       
   294     s->wrap = wrap;
       
   295     s->gzhead = Z_NULL;
       
   296     s->w_bits = windowBits;
       
   297     s->w_size = 1 << s->w_bits;
       
   298     s->w_mask = s->w_size - 1;
       
   299 
       
   300     s->hash_bits = memLevel + 7;
       
   301     s->hash_size = 1 << s->hash_bits;
       
   302     s->hash_mask = s->hash_size - 1;
       
   303     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
       
   304 
       
   305     s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
       
   306     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
       
   307     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
       
   308 
       
   309     s->high_water = 0;      /* nothing written to s->window yet */
       
   310 
       
   311     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
       
   312 
       
   313     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
       
   314     s->pending_buf = (uchf *) overlay;
       
   315     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
       
   316 
       
   317     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
       
   318         s->pending_buf == Z_NULL) {
       
   319         s->status = FINISH_STATE;
       
   320         strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
       
   321         deflateEnd (strm);
       
   322         return Z_MEM_ERROR;
       
   323     }
       
   324     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
       
   325     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
       
   326 
       
   327     s->level = level;
       
   328     s->strategy = strategy;
       
   329     s->method = (Byte)method;
       
   330 
       
   331     return deflateReset(strm);
       
   332 }
       
   333 
       
   334 /* ========================================================================= */
       
   335 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
       
   336     z_streamp strm;
       
   337     const Bytef *dictionary;
       
   338     uInt  dictLength;
       
   339 {
       
   340     deflate_state *s;
       
   341     uInt length = dictLength;
       
   342     uInt n;
       
   343     IPos hash_head = 0;
       
   344 
       
   345     if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
       
   346         strm->state->wrap == 2 ||
       
   347         (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
       
   348         return Z_STREAM_ERROR;
       
   349 
       
   350     s = strm->state;
       
   351     if (s->wrap)
       
   352         strm->adler = adler32(strm->adler, dictionary, dictLength);
       
   353 
       
   354     if (length < MIN_MATCH) return Z_OK;
       
   355     if (length > s->w_size) {
       
   356         length = s->w_size;
       
   357         dictionary += dictLength - length; /* use the tail of the dictionary */
       
   358     }
       
   359     zmemcpy(s->window, dictionary, length);
       
   360     s->strstart = length;
       
   361     s->block_start = (long)length;
       
   362 
       
   363     /* Insert all strings in the hash table (except for the last two bytes).
       
   364      * s->lookahead stays null, so s->ins_h will be recomputed at the next
       
   365      * call of fill_window.
       
   366      */
       
   367     s->ins_h = s->window[0];
       
   368     UPDATE_HASH(s, s->ins_h, s->window[1]);
       
   369     for (n = 0; n <= length - MIN_MATCH; n++) {
       
   370         INSERT_STRING(s, n, hash_head);
       
   371     }
       
   372     if (hash_head) hash_head = 0;  /* to make compiler happy */
       
   373     return Z_OK;
       
   374 }
       
   375 
       
   376 /* ========================================================================= */
       
   377 int ZEXPORT deflateReset (strm)
       
   378     z_streamp strm;
       
   379 {
       
   380     deflate_state *s;
       
   381 
       
   382     if (strm == Z_NULL || strm->state == Z_NULL ||
       
   383         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
       
   384         return Z_STREAM_ERROR;
       
   385     }
       
   386 
       
   387     strm->total_in = strm->total_out = 0;
       
   388     strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
       
   389     strm->data_type = Z_UNKNOWN;
       
   390 
       
   391     s = (deflate_state *)strm->state;
       
   392     s->pending = 0;
       
   393     s->pending_out = s->pending_buf;
       
   394 
       
   395     if (s->wrap < 0) {
       
   396         s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
       
   397     }
       
   398     s->status = s->wrap ? INIT_STATE : BUSY_STATE;
       
   399     strm->adler =
       
   400 #ifdef GZIP
       
   401         s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
       
   402 #endif
       
   403         adler32(0L, Z_NULL, 0);
       
   404     s->last_flush = Z_NO_FLUSH;
       
   405 
       
   406     _tr_init(s);
       
   407     lm_init(s);
       
   408 
       
   409     return Z_OK;
       
   410 }
       
   411 
       
   412 /* ========================================================================= */
       
   413 int ZEXPORT deflateSetHeader (strm, head)
       
   414     z_streamp strm;
       
   415     gz_headerp head;
       
   416 {
       
   417     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   418     if (strm->state->wrap != 2) return Z_STREAM_ERROR;
       
   419     strm->state->gzhead = head;
       
   420     return Z_OK;
       
   421 }
       
   422 
       
   423 /* ========================================================================= */
       
   424 int ZEXPORT deflatePrime (strm, bits, value)
       
   425     z_streamp strm;
       
   426     int bits;
       
   427     int value;
       
   428 {
       
   429     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   430     strm->state->bi_valid = bits;
       
   431     strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
       
   432     return Z_OK;
       
   433 }
       
   434 
       
   435 /* ========================================================================= */
       
   436 int ZEXPORT deflateParams(strm, level, strategy)
       
   437     z_streamp strm;
       
   438     int level;
       
   439     int strategy;
       
   440 {
       
   441     deflate_state *s;
       
   442     compress_func func;
       
   443     int err = Z_OK;
       
   444 
       
   445     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   446     s = strm->state;
       
   447 
       
   448 #ifdef FASTEST
       
   449     if (level != 0) level = 1;
       
   450 #else
       
   451     if (level == Z_DEFAULT_COMPRESSION) level = 6;
       
   452 #endif
       
   453     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
       
   454         return Z_STREAM_ERROR;
       
   455     }
       
   456     func = configuration_table[s->level].func;
       
   457 
       
   458     if ((strategy != s->strategy || func != configuration_table[level].func) &&
       
   459         strm->total_in != 0) {
       
   460         /* Flush the last buffer: */
       
   461         err = deflate(strm, Z_BLOCK);
       
   462     }
       
   463     if (s->level != level) {
       
   464         s->level = level;
       
   465         s->max_lazy_match   = configuration_table[level].max_lazy;
       
   466         s->good_match       = configuration_table[level].good_length;
       
   467         s->nice_match       = configuration_table[level].nice_length;
       
   468         s->max_chain_length = configuration_table[level].max_chain;
       
   469     }
       
   470     s->strategy = strategy;
       
   471     return err;
       
   472 }
       
   473 
       
   474 /* ========================================================================= */
       
   475 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
       
   476     z_streamp strm;
       
   477     int good_length;
       
   478     int max_lazy;
       
   479     int nice_length;
       
   480     int max_chain;
       
   481 {
       
   482     deflate_state *s;
       
   483 
       
   484     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   485     s = strm->state;
       
   486     s->good_match = good_length;
       
   487     s->max_lazy_match = max_lazy;
       
   488     s->nice_match = nice_length;
       
   489     s->max_chain_length = max_chain;
       
   490     return Z_OK;
       
   491 }
       
   492 
       
   493 /* =========================================================================
       
   494  * For the default windowBits of 15 and memLevel of 8, this function returns
       
   495  * a close to exact, as well as small, upper bound on the compressed size.
       
   496  * They are coded as constants here for a reason--if the #define's are
       
   497  * changed, then this function needs to be changed as well.  The return
       
   498  * value for 15 and 8 only works for those exact settings.
       
   499  *
       
   500  * For any setting other than those defaults for windowBits and memLevel,
       
   501  * the value returned is a conservative worst case for the maximum expansion
       
   502  * resulting from using fixed blocks instead of stored blocks, which deflate
       
   503  * can emit on compressed data for some combinations of the parameters.
       
   504  *
       
   505  * This function could be more sophisticated to provide closer upper bounds for
       
   506  * every combination of windowBits and memLevel.  But even the conservative
       
   507  * upper bound of about 14% expansion does not seem onerous for output buffer
       
   508  * allocation.
       
   509  */
       
   510 uLong ZEXPORT deflateBound(strm, sourceLen)
       
   511     z_streamp strm;
       
   512     uLong sourceLen;
       
   513 {
       
   514     deflate_state *s;
       
   515     uLong complen, wraplen;
       
   516     Bytef *str;
       
   517 
       
   518     /* conservative upper bound for compressed data */
       
   519     complen = sourceLen +
       
   520               ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
       
   521 
       
   522     /* if can't get parameters, return conservative bound plus zlib wrapper */
       
   523     if (strm == Z_NULL || strm->state == Z_NULL)
       
   524         return complen + 6;
       
   525 
       
   526     /* compute wrapper length */
       
   527     s = strm->state;
       
   528     switch (s->wrap) {
       
   529     case 0:                                 /* raw deflate */
       
   530         wraplen = 0;
       
   531         break;
       
   532     case 1:                                 /* zlib wrapper */
       
   533         wraplen = 6 + (s->strstart ? 4 : 0);
       
   534         break;
       
   535     case 2:                                 /* gzip wrapper */
       
   536         wraplen = 18;
       
   537         if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
       
   538             if (s->gzhead->extra != Z_NULL)
       
   539                 wraplen += 2 + s->gzhead->extra_len;
       
   540             str = s->gzhead->name;
       
   541             if (str != Z_NULL)
       
   542                 do {
       
   543                     wraplen++;
       
   544                 } while (*str++);
       
   545             str = s->gzhead->comment;
       
   546             if (str != Z_NULL)
       
   547                 do {
       
   548                     wraplen++;
       
   549                 } while (*str++);
       
   550             if (s->gzhead->hcrc)
       
   551                 wraplen += 2;
       
   552         }
       
   553         break;
       
   554     default:                                /* for compiler happiness */
       
   555         wraplen = 6;
       
   556     }
       
   557 
       
   558     /* if not default parameters, return conservative bound */
       
   559     if (s->w_bits != 15 || s->hash_bits != 8 + 7)
       
   560         return complen + wraplen;
       
   561 
       
   562     /* default settings: return tight bound for that case */
       
   563     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
       
   564            (sourceLen >> 25) + 13 - 6 + wraplen;
       
   565 }
       
   566 
       
   567 /* =========================================================================
       
   568  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
       
   569  * IN assertion: the stream state is correct and there is enough room in
       
   570  * pending_buf.
       
   571  */
       
   572 local void putShortMSB (s, b)
       
   573     deflate_state *s;
       
   574     uInt b;
       
   575 {
       
   576     put_byte(s, (Byte)(b >> 8));
       
   577     put_byte(s, (Byte)(b & 0xff));
       
   578 }
       
   579 
       
   580 /* =========================================================================
       
   581  * Flush as much pending output as possible. All deflate() output goes
       
   582  * through this function so some applications may wish to modify it
       
   583  * to avoid allocating a large strm->next_out buffer and copying into it.
       
   584  * (See also read_buf()).
       
   585  */
       
   586 local void flush_pending(strm)
       
   587     z_streamp strm;
       
   588 {
       
   589     unsigned len = strm->state->pending;
       
   590 
       
   591     if (len > strm->avail_out) len = strm->avail_out;
       
   592     if (len == 0) return;
       
   593 
       
   594     zmemcpy(strm->next_out, strm->state->pending_out, len);
       
   595     strm->next_out  += len;
       
   596     strm->state->pending_out  += len;
       
   597     strm->total_out += len;
       
   598     strm->avail_out  -= len;
       
   599     strm->state->pending -= len;
       
   600     if (strm->state->pending == 0) {
       
   601         strm->state->pending_out = strm->state->pending_buf;
       
   602     }
       
   603 }
       
   604 
       
   605 /* ========================================================================= */
       
   606 int ZEXPORT deflate (strm, flush)
       
   607     z_streamp strm;
       
   608     int flush;
       
   609 {
       
   610     int old_flush; /* value of flush param for previous deflate call */
       
   611     deflate_state *s;
       
   612 
       
   613     if (strm == Z_NULL || strm->state == Z_NULL ||
       
   614         flush > Z_BLOCK || flush < 0) {
       
   615         return Z_STREAM_ERROR;
       
   616     }
       
   617     s = strm->state;
       
   618 
       
   619     if (strm->next_out == Z_NULL ||
       
   620         (strm->next_in == Z_NULL && strm->avail_in != 0) ||
       
   621         (s->status == FINISH_STATE && flush != Z_FINISH)) {
       
   622         ERR_RETURN(strm, Z_STREAM_ERROR);
       
   623     }
       
   624     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
       
   625 
       
   626     s->strm = strm; /* just in case */
       
   627     old_flush = s->last_flush;
       
   628     s->last_flush = flush;
       
   629 
       
   630     /* Write the header */
       
   631     if (s->status == INIT_STATE) {
       
   632 #ifdef GZIP
       
   633         if (s->wrap == 2) {
       
   634             strm->adler = crc32(0L, Z_NULL, 0);
       
   635             put_byte(s, 31);
       
   636             put_byte(s, 139);
       
   637             put_byte(s, 8);
       
   638             if (s->gzhead == Z_NULL) {
       
   639                 put_byte(s, 0);
       
   640                 put_byte(s, 0);
       
   641                 put_byte(s, 0);
       
   642                 put_byte(s, 0);
       
   643                 put_byte(s, 0);
       
   644                 put_byte(s, s->level == 9 ? 2 :
       
   645                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
       
   646                              4 : 0));
       
   647                 put_byte(s, OS_CODE);
       
   648                 s->status = BUSY_STATE;
       
   649             }
       
   650             else {
       
   651                 put_byte(s, (s->gzhead->text ? 1 : 0) +
       
   652                             (s->gzhead->hcrc ? 2 : 0) +
       
   653                             (s->gzhead->extra == Z_NULL ? 0 : 4) +
       
   654                             (s->gzhead->name == Z_NULL ? 0 : 8) +
       
   655                             (s->gzhead->comment == Z_NULL ? 0 : 16)
       
   656                         );
       
   657                 put_byte(s, (Byte)(s->gzhead->time & 0xff));
       
   658                 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
       
   659                 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
       
   660                 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
       
   661                 put_byte(s, s->level == 9 ? 2 :
       
   662                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
       
   663                              4 : 0));
       
   664                 put_byte(s, s->gzhead->os & 0xff);
       
   665                 if (s->gzhead->extra != Z_NULL) {
       
   666                     put_byte(s, s->gzhead->extra_len & 0xff);
       
   667                     put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
       
   668                 }
       
   669                 if (s->gzhead->hcrc)
       
   670                     strm->adler = crc32(strm->adler, s->pending_buf,
       
   671                                         s->pending);
       
   672                 s->gzindex = 0;
       
   673                 s->status = EXTRA_STATE;
       
   674             }
       
   675         }
       
   676         else
       
   677 #endif
       
   678         {
       
   679             uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
       
   680             uInt level_flags;
       
   681 
       
   682             if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
       
   683                 level_flags = 0;
       
   684             else if (s->level < 6)
       
   685                 level_flags = 1;
       
   686             else if (s->level == 6)
       
   687                 level_flags = 2;
       
   688             else
       
   689                 level_flags = 3;
       
   690             header |= (level_flags << 6);
       
   691             if (s->strstart != 0) header |= PRESET_DICT;
       
   692             header += 31 - (header % 31);
       
   693 
       
   694             s->status = BUSY_STATE;
       
   695             putShortMSB(s, header);
       
   696 
       
   697             /* Save the adler32 of the preset dictionary: */
       
   698             if (s->strstart != 0) {
       
   699                 putShortMSB(s, (uInt)(strm->adler >> 16));
       
   700                 putShortMSB(s, (uInt)(strm->adler & 0xffff));
       
   701             }
       
   702             strm->adler = adler32(0L, Z_NULL, 0);
       
   703         }
       
   704     }
       
   705 #ifdef GZIP
       
   706     if (s->status == EXTRA_STATE) {
       
   707         if (s->gzhead->extra != Z_NULL) {
       
   708             uInt beg = s->pending;  /* start of bytes to update crc */
       
   709 
       
   710             while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
       
   711                 if (s->pending == s->pending_buf_size) {
       
   712                     if (s->gzhead->hcrc && s->pending > beg)
       
   713                         strm->adler = crc32(strm->adler, s->pending_buf + beg,
       
   714                                             s->pending - beg);
       
   715                     flush_pending(strm);
       
   716                     beg = s->pending;
       
   717                     if (s->pending == s->pending_buf_size)
       
   718                         break;
       
   719                 }
       
   720                 put_byte(s, s->gzhead->extra[s->gzindex]);
       
   721                 s->gzindex++;
       
   722             }
       
   723             if (s->gzhead->hcrc && s->pending > beg)
       
   724                 strm->adler = crc32(strm->adler, s->pending_buf + beg,
       
   725                                     s->pending - beg);
       
   726             if (s->gzindex == s->gzhead->extra_len) {
       
   727                 s->gzindex = 0;
       
   728                 s->status = NAME_STATE;
       
   729             }
       
   730         }
       
   731         else
       
   732             s->status = NAME_STATE;
       
   733     }
       
   734     if (s->status == NAME_STATE) {
       
   735         if (s->gzhead->name != Z_NULL) {
       
   736             uInt beg = s->pending;  /* start of bytes to update crc */
       
   737             int val;
       
   738 
       
   739             do {
       
   740                 if (s->pending == s->pending_buf_size) {
       
   741                     if (s->gzhead->hcrc && s->pending > beg)
       
   742                         strm->adler = crc32(strm->adler, s->pending_buf + beg,
       
   743                                             s->pending - beg);
       
   744                     flush_pending(strm);
       
   745                     beg = s->pending;
       
   746                     if (s->pending == s->pending_buf_size) {
       
   747                         val = 1;
       
   748                         break;
       
   749                     }
       
   750                 }
       
   751                 val = s->gzhead->name[s->gzindex++];
       
   752                 put_byte(s, val);
       
   753             } while (val != 0);
       
   754             if (s->gzhead->hcrc && s->pending > beg)
       
   755                 strm->adler = crc32(strm->adler, s->pending_buf + beg,
       
   756                                     s->pending - beg);
       
   757             if (val == 0) {
       
   758                 s->gzindex = 0;
       
   759                 s->status = COMMENT_STATE;
       
   760             }
       
   761         }
       
   762         else
       
   763             s->status = COMMENT_STATE;
       
   764     }
       
   765     if (s->status == COMMENT_STATE) {
       
   766         if (s->gzhead->comment != Z_NULL) {
       
   767             uInt beg = s->pending;  /* start of bytes to update crc */
       
   768             int val;
       
   769 
       
   770             do {
       
   771                 if (s->pending == s->pending_buf_size) {
       
   772                     if (s->gzhead->hcrc && s->pending > beg)
       
   773                         strm->adler = crc32(strm->adler, s->pending_buf + beg,
       
   774                                             s->pending - beg);
       
   775                     flush_pending(strm);
       
   776                     beg = s->pending;
       
   777                     if (s->pending == s->pending_buf_size) {
       
   778                         val = 1;
       
   779                         break;
       
   780                     }
       
   781                 }
       
   782                 val = s->gzhead->comment[s->gzindex++];
       
   783                 put_byte(s, val);
       
   784             } while (val != 0);
       
   785             if (s->gzhead->hcrc && s->pending > beg)
       
   786                 strm->adler = crc32(strm->adler, s->pending_buf + beg,
       
   787                                     s->pending - beg);
       
   788             if (val == 0)
       
   789                 s->status = HCRC_STATE;
       
   790         }
       
   791         else
       
   792             s->status = HCRC_STATE;
       
   793     }
       
   794     if (s->status == HCRC_STATE) {
       
   795         if (s->gzhead->hcrc) {
       
   796             if (s->pending + 2 > s->pending_buf_size)
       
   797                 flush_pending(strm);
       
   798             if (s->pending + 2 <= s->pending_buf_size) {
       
   799                 put_byte(s, (Byte)(strm->adler & 0xff));
       
   800                 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
       
   801                 strm->adler = crc32(0L, Z_NULL, 0);
       
   802                 s->status = BUSY_STATE;
       
   803             }
       
   804         }
       
   805         else
       
   806             s->status = BUSY_STATE;
       
   807     }
       
   808 #endif
       
   809 
       
   810     /* Flush as much pending output as possible */
       
   811     if (s->pending != 0) {
       
   812         flush_pending(strm);
       
   813         if (strm->avail_out == 0) {
       
   814             /* Since avail_out is 0, deflate will be called again with
       
   815              * more output space, but possibly with both pending and
       
   816              * avail_in equal to zero. There won't be anything to do,
       
   817              * but this is not an error situation so make sure we
       
   818              * return OK instead of BUF_ERROR at next call of deflate:
       
   819              */
       
   820             s->last_flush = -1;
       
   821             return Z_OK;
       
   822         }
       
   823 
       
   824     /* Make sure there is something to do and avoid duplicate consecutive
       
   825      * flushes. For repeated and useless calls with Z_FINISH, we keep
       
   826      * returning Z_STREAM_END instead of Z_BUF_ERROR.
       
   827      */
       
   828     } else if (strm->avail_in == 0 && flush <= old_flush &&
       
   829                flush != Z_FINISH) {
       
   830         ERR_RETURN(strm, Z_BUF_ERROR);
       
   831     }
       
   832 
       
   833     /* User must not provide more input after the first FINISH: */
       
   834     if (s->status == FINISH_STATE && strm->avail_in != 0) {
       
   835         ERR_RETURN(strm, Z_BUF_ERROR);
       
   836     }
       
   837 
       
   838     /* Start a new block or continue the current one.
       
   839      */
       
   840     if (strm->avail_in != 0 || s->lookahead != 0 ||
       
   841         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
       
   842         block_state bstate;
       
   843 
       
   844         bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
       
   845                     (s->strategy == Z_RLE ? deflate_rle(s, flush) :
       
   846                         (*(configuration_table[s->level].func))(s, flush));
       
   847 
       
   848         if (bstate == finish_started || bstate == finish_done) {
       
   849             s->status = FINISH_STATE;
       
   850         }
       
   851         if (bstate == need_more || bstate == finish_started) {
       
   852             if (strm->avail_out == 0) {
       
   853                 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
       
   854             }
       
   855             return Z_OK;
       
   856             /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
       
   857              * of deflate should use the same flush parameter to make sure
       
   858              * that the flush is complete. So we don't have to output an
       
   859              * empty block here, this will be done at next call. This also
       
   860              * ensures that for a very small output buffer, we emit at most
       
   861              * one empty block.
       
   862              */
       
   863         }
       
   864         if (bstate == block_done) {
       
   865             if (flush == Z_PARTIAL_FLUSH) {
       
   866                 _tr_align(s);
       
   867             } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
       
   868                 _tr_stored_block(s, (char*)0, 0L, 0);
       
   869                 /* For a full flush, this empty block will be recognized
       
   870                  * as a special marker by inflate_sync().
       
   871                  */
       
   872                 if (flush == Z_FULL_FLUSH) {
       
   873                     CLEAR_HASH(s);             /* forget history */
       
   874                     if (s->lookahead == 0) {
       
   875                         s->strstart = 0;
       
   876                         s->block_start = 0L;
       
   877                     }
       
   878                 }
       
   879             }
       
   880             flush_pending(strm);
       
   881             if (strm->avail_out == 0) {
       
   882               s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
       
   883               return Z_OK;
       
   884             }
       
   885         }
       
   886     }
       
   887     Assert(strm->avail_out > 0, "bug2");
       
   888 
       
   889     if (flush != Z_FINISH) return Z_OK;
       
   890     if (s->wrap <= 0) return Z_STREAM_END;
       
   891 
       
   892     /* Write the trailer */
       
   893 #ifdef GZIP
       
   894     if (s->wrap == 2) {
       
   895         put_byte(s, (Byte)(strm->adler & 0xff));
       
   896         put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
       
   897         put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
       
   898         put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
       
   899         put_byte(s, (Byte)(strm->total_in & 0xff));
       
   900         put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
       
   901         put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
       
   902         put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
       
   903     }
       
   904     else
       
   905 #endif
       
   906     {
       
   907         putShortMSB(s, (uInt)(strm->adler >> 16));
       
   908         putShortMSB(s, (uInt)(strm->adler & 0xffff));
       
   909     }
       
   910     flush_pending(strm);
       
   911     /* If avail_out is zero, the application will call deflate again
       
   912      * to flush the rest.
       
   913      */
       
   914     if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
       
   915     return s->pending != 0 ? Z_OK : Z_STREAM_END;
       
   916 }
       
   917 
       
   918 /* ========================================================================= */
       
   919 int ZEXPORT deflateEnd (strm)
       
   920     z_streamp strm;
       
   921 {
       
   922     int status;
       
   923 
       
   924     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   925 
       
   926     status = strm->state->status;
       
   927     if (status != INIT_STATE &&
       
   928         status != EXTRA_STATE &&
       
   929         status != NAME_STATE &&
       
   930         status != COMMENT_STATE &&
       
   931         status != HCRC_STATE &&
       
   932         status != BUSY_STATE &&
       
   933         status != FINISH_STATE) {
       
   934       return Z_STREAM_ERROR;
       
   935     }
       
   936 
       
   937     /* Deallocate in reverse order of allocations: */
       
   938     TRY_FREE(strm, strm->state->pending_buf);
       
   939     TRY_FREE(strm, strm->state->head);
       
   940     TRY_FREE(strm, strm->state->prev);
       
   941     TRY_FREE(strm, strm->state->window);
       
   942 
       
   943     ZFREE(strm, strm->state);
       
   944     strm->state = Z_NULL;
       
   945 
       
   946     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
       
   947 }
       
   948 
       
   949 /* =========================================================================
       
   950  * Copy the source state to the destination state.
       
   951  * To simplify the source, this is not supported for 16-bit MSDOS (which
       
   952  * doesn't have enough memory anyway to duplicate compression states).
       
   953  */
       
   954 int ZEXPORT deflateCopy (dest, source)
       
   955     z_streamp dest;
       
   956     z_streamp source;
       
   957 {
       
   958 #ifdef MAXSEG_64K
       
   959     return Z_STREAM_ERROR;
       
   960 #else
       
   961     deflate_state *ds;
       
   962     deflate_state *ss;
       
   963     ushf *overlay;
       
   964 
       
   965 
       
   966     if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
       
   967         return Z_STREAM_ERROR;
       
   968     }
       
   969 
       
   970     ss = source->state;
       
   971 
       
   972     zmemcpy(dest, source, sizeof(z_stream));
       
   973 
       
   974     ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
       
   975     if (ds == Z_NULL) return Z_MEM_ERROR;
       
   976     dest->state = (struct internal_state FAR *) ds;
       
   977     zmemcpy(ds, ss, sizeof(deflate_state));
       
   978     ds->strm = dest;
       
   979 
       
   980     ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
       
   981     ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
       
   982     ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
       
   983     overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
       
   984     ds->pending_buf = (uchf *) overlay;
       
   985 
       
   986     if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
       
   987         ds->pending_buf == Z_NULL) {
       
   988         deflateEnd (dest);
       
   989         return Z_MEM_ERROR;
       
   990     }
       
   991     /* following zmemcpy do not work for 16-bit MSDOS */
       
   992     zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
       
   993     zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
       
   994     zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
       
   995     zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
       
   996 
       
   997     ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
       
   998     ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
       
   999     ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
       
  1000 
       
  1001     ds->l_desc.dyn_tree = ds->dyn_ltree;
       
  1002     ds->d_desc.dyn_tree = ds->dyn_dtree;
       
  1003     ds->bl_desc.dyn_tree = ds->bl_tree;
       
  1004 
       
  1005     return Z_OK;
       
  1006 #endif /* MAXSEG_64K */
       
  1007 }
       
  1008 
       
  1009 /* ===========================================================================
       
  1010  * Read a new buffer from the current input stream, update the adler32
       
  1011  * and total number of bytes read.  All deflate() input goes through
       
  1012  * this function so some applications may wish to modify it to avoid
       
  1013  * allocating a large strm->next_in buffer and copying from it.
       
  1014  * (See also flush_pending()).
       
  1015  */
       
  1016 local int read_buf(strm, buf, size)
       
  1017     z_streamp strm;
       
  1018     Bytef *buf;
       
  1019     unsigned size;
       
  1020 {
       
  1021     unsigned len = strm->avail_in;
       
  1022 
       
  1023     if (len > size) len = size;
       
  1024     if (len == 0) return 0;
       
  1025 
       
  1026     strm->avail_in  -= len;
       
  1027 
       
  1028     if (strm->state->wrap == 1) {
       
  1029         strm->adler = adler32(strm->adler, strm->next_in, len);
       
  1030     }
       
  1031 #ifdef GZIP
       
  1032     else if (strm->state->wrap == 2) {
       
  1033         strm->adler = crc32(strm->adler, strm->next_in, len);
       
  1034     }
       
  1035 #endif
       
  1036     zmemcpy(buf, strm->next_in, len);
       
  1037     strm->next_in  += len;
       
  1038     strm->total_in += len;
       
  1039 
       
  1040     return (int)len;
       
  1041 }
       
  1042 
       
  1043 /* ===========================================================================
       
  1044  * Initialize the "longest match" routines for a new zlib stream
       
  1045  */
       
  1046 local void lm_init (s)
       
  1047     deflate_state *s;
       
  1048 {
       
  1049     s->window_size = (ulg)2L*s->w_size;
       
  1050 
       
  1051     CLEAR_HASH(s);
       
  1052 
       
  1053     /* Set the default configuration parameters:
       
  1054      */
       
  1055     s->max_lazy_match   = configuration_table[s->level].max_lazy;
       
  1056     s->good_match       = configuration_table[s->level].good_length;
       
  1057     s->nice_match       = configuration_table[s->level].nice_length;
       
  1058     s->max_chain_length = configuration_table[s->level].max_chain;
       
  1059 
       
  1060     s->strstart = 0;
       
  1061     s->block_start = 0L;
       
  1062     s->lookahead = 0;
       
  1063     s->match_length = s->prev_length = MIN_MATCH-1;
       
  1064     s->match_available = 0;
       
  1065     s->ins_h = 0;
       
  1066 #ifndef FASTEST
       
  1067 #ifdef ASMV
       
  1068     match_init(); /* initialize the asm code */
       
  1069 #endif
       
  1070 #endif
       
  1071 }
       
  1072 
       
  1073 #ifndef FASTEST
       
  1074 /* ===========================================================================
       
  1075  * Set match_start to the longest match starting at the given string and
       
  1076  * return its length. Matches shorter or equal to prev_length are discarded,
       
  1077  * in which case the result is equal to prev_length and match_start is
       
  1078  * garbage.
       
  1079  * IN assertions: cur_match is the head of the hash chain for the current
       
  1080  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
       
  1081  * OUT assertion: the match length is not greater than s->lookahead.
       
  1082  */
       
  1083 #ifndef ASMV
       
  1084 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
       
  1085  * match.S. The code will be functionally equivalent.
       
  1086  */
       
  1087 local uInt longest_match(s, cur_match)
       
  1088     deflate_state *s;
       
  1089     IPos cur_match;                             /* current match */
       
  1090 {
       
  1091     unsigned chain_length = s->max_chain_length;/* max hash chain length */
       
  1092     register Bytef *scan = s->window + s->strstart; /* current string */
       
  1093     register Bytef *match;                       /* matched string */
       
  1094     register int len;                           /* length of current match */
       
  1095     int best_len = s->prev_length;              /* best match length so far */
       
  1096     int nice_match = s->nice_match;             /* stop if match long enough */
       
  1097     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
       
  1098         s->strstart - (IPos)MAX_DIST(s) : NIL;
       
  1099     /* Stop when cur_match becomes <= limit. To simplify the code,
       
  1100      * we prevent matches with the string of window index 0.
       
  1101      */
       
  1102     Posf *prev = s->prev;
       
  1103     uInt wmask = s->w_mask;
       
  1104 
       
  1105 #ifdef UNALIGNED_OK
       
  1106     /* Compare two bytes at a time. Note: this is not always beneficial.
       
  1107      * Try with and without -DUNALIGNED_OK to check.
       
  1108      */
       
  1109     register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
       
  1110     register ush scan_start = *(ushf*)scan;
       
  1111     register ush scan_end   = *(ushf*)(scan+best_len-1);
       
  1112 #else
       
  1113     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
       
  1114     register Byte scan_end1  = scan[best_len-1];
       
  1115     register Byte scan_end   = scan[best_len];
       
  1116 #endif
       
  1117 
       
  1118     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
       
  1119      * It is easy to get rid of this optimization if necessary.
       
  1120      */
       
  1121     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
       
  1122 
       
  1123     /* Do not waste too much time if we already have a good match: */
       
  1124     if (s->prev_length >= s->good_match) {
       
  1125         chain_length >>= 2;
       
  1126     }
       
  1127     /* Do not look for matches beyond the end of the input. This is necessary
       
  1128      * to make deflate deterministic.
       
  1129      */
       
  1130     if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
       
  1131 
       
  1132     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
       
  1133 
       
  1134     do {
       
  1135         Assert(cur_match < s->strstart, "no future");
       
  1136         match = s->window + cur_match;
       
  1137 
       
  1138         /* Skip to next match if the match length cannot increase
       
  1139          * or if the match length is less than 2.  Note that the checks below
       
  1140          * for insufficient lookahead only occur occasionally for performance
       
  1141          * reasons.  Therefore uninitialized memory will be accessed, and
       
  1142          * conditional jumps will be made that depend on those values.
       
  1143          * However the length of the match is limited to the lookahead, so
       
  1144          * the output of deflate is not affected by the uninitialized values.
       
  1145          */
       
  1146 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
       
  1147         /* This code assumes sizeof(unsigned short) == 2. Do not use
       
  1148          * UNALIGNED_OK if your compiler uses a different size.
       
  1149          */
       
  1150         if (*(ushf*)(match+best_len-1) != scan_end ||
       
  1151             *(ushf*)match != scan_start) continue;
       
  1152 
       
  1153         /* It is not necessary to compare scan[2] and match[2] since they are
       
  1154          * always equal when the other bytes match, given that the hash keys
       
  1155          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
       
  1156          * strstart+3, +5, ... up to strstart+257. We check for insufficient
       
  1157          * lookahead only every 4th comparison; the 128th check will be made
       
  1158          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
       
  1159          * necessary to put more guard bytes at the end of the window, or
       
  1160          * to check more often for insufficient lookahead.
       
  1161          */
       
  1162         Assert(scan[2] == match[2], "scan[2]?");
       
  1163         scan++, match++;
       
  1164         do {
       
  1165         } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1166                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1167                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1168                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1169                  scan < strend);
       
  1170         /* The funny "do {}" generates better code on most compilers */
       
  1171 
       
  1172         /* Here, scan <= window+strstart+257 */
       
  1173         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
       
  1174         if (*scan == *match) scan++;
       
  1175 
       
  1176         len = (MAX_MATCH - 1) - (int)(strend-scan);
       
  1177         scan = strend - (MAX_MATCH-1);
       
  1178 
       
  1179 #else /* UNALIGNED_OK */
       
  1180 
       
  1181         if (match[best_len]   != scan_end  ||
       
  1182             match[best_len-1] != scan_end1 ||
       
  1183             *match            != *scan     ||
       
  1184             *++match          != scan[1])      continue;
       
  1185 
       
  1186         /* The check at best_len-1 can be removed because it will be made
       
  1187          * again later. (This heuristic is not always a win.)
       
  1188          * It is not necessary to compare scan[2] and match[2] since they
       
  1189          * are always equal when the other bytes match, given that
       
  1190          * the hash keys are equal and that HASH_BITS >= 8.
       
  1191          */
       
  1192         scan += 2, match++;
       
  1193         Assert(*scan == *match, "match[2]?");
       
  1194 
       
  1195         /* We check for insufficient lookahead only every 8th comparison;
       
  1196          * the 256th check will be made at strstart+258.
       
  1197          */
       
  1198         do {
       
  1199         } while (*++scan == *++match && *++scan == *++match &&
       
  1200                  *++scan == *++match && *++scan == *++match &&
       
  1201                  *++scan == *++match && *++scan == *++match &&
       
  1202                  *++scan == *++match && *++scan == *++match &&
       
  1203                  scan < strend);
       
  1204 
       
  1205         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
       
  1206 
       
  1207         len = MAX_MATCH - (int)(strend - scan);
       
  1208         scan = strend - MAX_MATCH;
       
  1209 
       
  1210 #endif /* UNALIGNED_OK */
       
  1211 
       
  1212         if (len > best_len) {
       
  1213             s->match_start = cur_match;
       
  1214             best_len = len;
       
  1215             if (len >= nice_match) break;
       
  1216 #ifdef UNALIGNED_OK
       
  1217             scan_end = *(ushf*)(scan+best_len-1);
       
  1218 #else
       
  1219             scan_end1  = scan[best_len-1];
       
  1220             scan_end   = scan[best_len];
       
  1221 #endif
       
  1222         }
       
  1223     } while ((cur_match = prev[cur_match & wmask]) > limit
       
  1224              && --chain_length != 0);
       
  1225 
       
  1226     if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
       
  1227     return s->lookahead;
       
  1228 }
       
  1229 #endif /* ASMV */
       
  1230 
       
  1231 #else /* FASTEST */
       
  1232 
       
  1233 /* ---------------------------------------------------------------------------
       
  1234  * Optimized version for FASTEST only
       
  1235  */
       
  1236 local uInt longest_match(s, cur_match)
       
  1237     deflate_state *s;
       
  1238     IPos cur_match;                             /* current match */
       
  1239 {
       
  1240     register Bytef *scan = s->window + s->strstart; /* current string */
       
  1241     register Bytef *match;                       /* matched string */
       
  1242     register int len;                           /* length of current match */
       
  1243     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
       
  1244 
       
  1245     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
       
  1246      * It is easy to get rid of this optimization if necessary.
       
  1247      */
       
  1248     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
       
  1249 
       
  1250     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
       
  1251 
       
  1252     Assert(cur_match < s->strstart, "no future");
       
  1253 
       
  1254     match = s->window + cur_match;
       
  1255 
       
  1256     /* Return failure if the match length is less than 2:
       
  1257      */
       
  1258     if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
       
  1259 
       
  1260     /* The check at best_len-1 can be removed because it will be made
       
  1261      * again later. (This heuristic is not always a win.)
       
  1262      * It is not necessary to compare scan[2] and match[2] since they
       
  1263      * are always equal when the other bytes match, given that
       
  1264      * the hash keys are equal and that HASH_BITS >= 8.
       
  1265      */
       
  1266     scan += 2, match += 2;
       
  1267     Assert(*scan == *match, "match[2]?");
       
  1268 
       
  1269     /* We check for insufficient lookahead only every 8th comparison;
       
  1270      * the 256th check will be made at strstart+258.
       
  1271      */
       
  1272     do {
       
  1273     } while (*++scan == *++match && *++scan == *++match &&
       
  1274              *++scan == *++match && *++scan == *++match &&
       
  1275              *++scan == *++match && *++scan == *++match &&
       
  1276              *++scan == *++match && *++scan == *++match &&
       
  1277              scan < strend);
       
  1278 
       
  1279     Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
       
  1280 
       
  1281     len = MAX_MATCH - (int)(strend - scan);
       
  1282 
       
  1283     if (len < MIN_MATCH) return MIN_MATCH - 1;
       
  1284 
       
  1285     s->match_start = cur_match;
       
  1286     return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
       
  1287 }
       
  1288 
       
  1289 #endif /* FASTEST */
       
  1290 
       
  1291 #ifdef DEBUG
       
  1292 /* ===========================================================================
       
  1293  * Check that the match at match_start is indeed a match.
       
  1294  */
       
  1295 local void check_match(s, start, match, length)
       
  1296     deflate_state *s;
       
  1297     IPos start, match;
       
  1298     int length;
       
  1299 {
       
  1300     /* check that the match is indeed a match */
       
  1301     if (zmemcmp(s->window + match,
       
  1302                 s->window + start, length) != EQUAL) {
       
  1303         fprintf(stderr, " start %u, match %u, length %d\n",
       
  1304                 start, match, length);
       
  1305         do {
       
  1306             fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
       
  1307         } while (--length != 0);
       
  1308         z_error("invalid match");
       
  1309     }
       
  1310     if (z_verbose > 1) {
       
  1311         fprintf(stderr,"\\[%d,%d]", start-match, length);
       
  1312         do { putc(s->window[start++], stderr); } while (--length != 0);
       
  1313     }
       
  1314 }
       
  1315 #else
       
  1316 #  define check_match(s, start, match, length)
       
  1317 #endif /* DEBUG */
       
  1318 
       
  1319 /* ===========================================================================
       
  1320  * Fill the window when the lookahead becomes insufficient.
       
  1321  * Updates strstart and lookahead.
       
  1322  *
       
  1323  * IN assertion: lookahead < MIN_LOOKAHEAD
       
  1324  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
       
  1325  *    At least one byte has been read, or avail_in == 0; reads are
       
  1326  *    performed for at least two bytes (required for the zip translate_eol
       
  1327  *    option -- not supported here).
       
  1328  */
       
  1329 local void fill_window(s)
       
  1330     deflate_state *s;
       
  1331 {
       
  1332     register unsigned n, m;
       
  1333     register Posf *p;
       
  1334     unsigned more;    /* Amount of free space at the end of the window. */
       
  1335     uInt wsize = s->w_size;
       
  1336 
       
  1337     do {
       
  1338         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
       
  1339 
       
  1340         /* Deal with !@#$% 64K limit: */
       
  1341         if (sizeof(int) <= 2) {
       
  1342             if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
       
  1343                 more = wsize;
       
  1344 
       
  1345             } else if (more == (unsigned)(-1)) {
       
  1346                 /* Very unlikely, but possible on 16 bit machine if
       
  1347                  * strstart == 0 && lookahead == 1 (input done a byte at time)
       
  1348                  */
       
  1349                 more--;
       
  1350             }
       
  1351         }
       
  1352 
       
  1353         /* If the window is almost full and there is insufficient lookahead,
       
  1354          * move the upper half to the lower one to make room in the upper half.
       
  1355          */
       
  1356         if (s->strstart >= wsize+MAX_DIST(s)) {
       
  1357 
       
  1358             zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
       
  1359             s->match_start -= wsize;
       
  1360             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
       
  1361             s->block_start -= (long) wsize;
       
  1362 
       
  1363             /* Slide the hash table (could be avoided with 32 bit values
       
  1364                at the expense of memory usage). We slide even when level == 0
       
  1365                to keep the hash table consistent if we switch back to level > 0
       
  1366                later. (Using level 0 permanently is not an optimal usage of
       
  1367                zlib, so we don't care about this pathological case.)
       
  1368              */
       
  1369             n = s->hash_size;
       
  1370             p = &s->head[n];
       
  1371             do {
       
  1372                 m = *--p;
       
  1373                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
       
  1374             } while (--n);
       
  1375 
       
  1376             n = wsize;
       
  1377 #ifndef FASTEST
       
  1378             p = &s->prev[n];
       
  1379             do {
       
  1380                 m = *--p;
       
  1381                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
       
  1382                 /* If n is not on any hash chain, prev[n] is garbage but
       
  1383                  * its value will never be used.
       
  1384                  */
       
  1385             } while (--n);
       
  1386 #endif
       
  1387             more += wsize;
       
  1388         }
       
  1389         if (s->strm->avail_in == 0) return;
       
  1390 
       
  1391         /* If there was no sliding:
       
  1392          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
       
  1393          *    more == window_size - lookahead - strstart
       
  1394          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
       
  1395          * => more >= window_size - 2*WSIZE + 2
       
  1396          * In the BIG_MEM or MMAP case (not yet supported),
       
  1397          *   window_size == input_size + MIN_LOOKAHEAD  &&
       
  1398          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
       
  1399          * Otherwise, window_size == 2*WSIZE so more >= 2.
       
  1400          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
       
  1401          */
       
  1402         Assert(more >= 2, "more < 2");
       
  1403 
       
  1404         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
       
  1405         s->lookahead += n;
       
  1406 
       
  1407         /* Initialize the hash value now that we have some input: */
       
  1408         if (s->lookahead >= MIN_MATCH) {
       
  1409             s->ins_h = s->window[s->strstart];
       
  1410             UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
       
  1411 #if MIN_MATCH != 3
       
  1412             Call UPDATE_HASH() MIN_MATCH-3 more times
       
  1413 #endif
       
  1414         }
       
  1415         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
       
  1416          * but this is not important since only literal bytes will be emitted.
       
  1417          */
       
  1418 
       
  1419     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
       
  1420 
       
  1421     /* If the WIN_INIT bytes after the end of the current data have never been
       
  1422      * written, then zero those bytes in order to avoid memory check reports of
       
  1423      * the use of uninitialized (or uninitialised as Julian writes) bytes by
       
  1424      * the longest match routines.  Update the high water mark for the next
       
  1425      * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
       
  1426      * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
       
  1427      */
       
  1428     if (s->high_water < s->window_size) {
       
  1429         ulg curr = s->strstart + (ulg)(s->lookahead);
       
  1430         ulg init;
       
  1431 
       
  1432         if (s->high_water < curr) {
       
  1433             /* Previous high water mark below current data -- zero WIN_INIT
       
  1434              * bytes or up to end of window, whichever is less.
       
  1435              */
       
  1436             init = s->window_size - curr;
       
  1437             if (init > WIN_INIT)
       
  1438                 init = WIN_INIT;
       
  1439             zmemzero(s->window + curr, (unsigned)init);
       
  1440             s->high_water = curr + init;
       
  1441         }
       
  1442         else if (s->high_water < (ulg)curr + WIN_INIT) {
       
  1443             /* High water mark at or above current data, but below current data
       
  1444              * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
       
  1445              * to end of window, whichever is less.
       
  1446              */
       
  1447             init = (ulg)curr + WIN_INIT - s->high_water;
       
  1448             if (init > s->window_size - s->high_water)
       
  1449                 init = s->window_size - s->high_water;
       
  1450             zmemzero(s->window + s->high_water, (unsigned)init);
       
  1451             s->high_water += init;
       
  1452         }
       
  1453     }
       
  1454 }
       
  1455 
       
  1456 /* ===========================================================================
       
  1457  * Flush the current block, with given end-of-file flag.
       
  1458  * IN assertion: strstart is set to the end of the current match.
       
  1459  */
       
  1460 #define FLUSH_BLOCK_ONLY(s, last) { \
       
  1461    _tr_flush_block(s, (s->block_start >= 0L ? \
       
  1462                    (charf *)&s->window[(unsigned)s->block_start] : \
       
  1463                    (charf *)Z_NULL), \
       
  1464                 (ulg)((long)s->strstart - s->block_start), \
       
  1465                 (last)); \
       
  1466    s->block_start = s->strstart; \
       
  1467    flush_pending(s->strm); \
       
  1468    Tracev((stderr,"[FLUSH]")); \
       
  1469 }
       
  1470 
       
  1471 /* Same but force premature exit if necessary. */
       
  1472 #define FLUSH_BLOCK(s, last) { \
       
  1473    FLUSH_BLOCK_ONLY(s, last); \
       
  1474    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
       
  1475 }
       
  1476 
       
  1477 /* ===========================================================================
       
  1478  * Copy without compression as much as possible from the input stream, return
       
  1479  * the current block state.
       
  1480  * This function does not insert new strings in the dictionary since
       
  1481  * uncompressible data is probably not useful. This function is used
       
  1482  * only for the level=0 compression option.
       
  1483  * NOTE: this function should be optimized to avoid extra copying from
       
  1484  * window to pending_buf.
       
  1485  */
       
  1486 local block_state deflate_stored(s, flush)
       
  1487     deflate_state *s;
       
  1488     int flush;
       
  1489 {
       
  1490     /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
       
  1491      * to pending_buf_size, and each stored block has a 5 byte header:
       
  1492      */
       
  1493     ulg max_block_size = 0xffff;
       
  1494     ulg max_start;
       
  1495 
       
  1496     if (max_block_size > s->pending_buf_size - 5) {
       
  1497         max_block_size = s->pending_buf_size - 5;
       
  1498     }
       
  1499 
       
  1500     /* Copy as much as possible from input to output: */
       
  1501     for (;;) {
       
  1502         /* Fill the window as much as possible: */
       
  1503         if (s->lookahead <= 1) {
       
  1504 
       
  1505             Assert(s->strstart < s->w_size+MAX_DIST(s) ||
       
  1506                    s->block_start >= (long)s->w_size, "slide too late");
       
  1507 
       
  1508             fill_window(s);
       
  1509             if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
       
  1510 
       
  1511             if (s->lookahead == 0) break; /* flush the current block */
       
  1512         }
       
  1513         Assert(s->block_start >= 0L, "block gone");
       
  1514 
       
  1515         s->strstart += s->lookahead;
       
  1516         s->lookahead = 0;
       
  1517 
       
  1518         /* Emit a stored block if pending_buf will be full: */
       
  1519         max_start = s->block_start + max_block_size;
       
  1520         if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
       
  1521             /* strstart == 0 is possible when wraparound on 16-bit machine */
       
  1522             s->lookahead = (uInt)(s->strstart - max_start);
       
  1523             s->strstart = (uInt)max_start;
       
  1524             FLUSH_BLOCK(s, 0);
       
  1525         }
       
  1526         /* Flush if we may have to slide, otherwise block_start may become
       
  1527          * negative and the data will be gone:
       
  1528          */
       
  1529         if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
       
  1530             FLUSH_BLOCK(s, 0);
       
  1531         }
       
  1532     }
       
  1533     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1534     return flush == Z_FINISH ? finish_done : block_done;
       
  1535 }
       
  1536 
       
  1537 /* ===========================================================================
       
  1538  * Compress as much as possible from the input stream, return the current
       
  1539  * block state.
       
  1540  * This function does not perform lazy evaluation of matches and inserts
       
  1541  * new strings in the dictionary only for unmatched strings or for short
       
  1542  * matches. It is used only for the fast compression options.
       
  1543  */
       
  1544 local block_state deflate_fast(s, flush)
       
  1545     deflate_state *s;
       
  1546     int flush;
       
  1547 {
       
  1548     IPos hash_head;       /* head of the hash chain */
       
  1549     int bflush;           /* set if current block must be flushed */
       
  1550 
       
  1551     for (;;) {
       
  1552         /* Make sure that we always have enough lookahead, except
       
  1553          * at the end of the input file. We need MAX_MATCH bytes
       
  1554          * for the next match, plus MIN_MATCH bytes to insert the
       
  1555          * string following the next match.
       
  1556          */
       
  1557         if (s->lookahead < MIN_LOOKAHEAD) {
       
  1558             fill_window(s);
       
  1559             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
       
  1560                 return need_more;
       
  1561             }
       
  1562             if (s->lookahead == 0) break; /* flush the current block */
       
  1563         }
       
  1564 
       
  1565         /* Insert the string window[strstart .. strstart+2] in the
       
  1566          * dictionary, and set hash_head to the head of the hash chain:
       
  1567          */
       
  1568         hash_head = NIL;
       
  1569         if (s->lookahead >= MIN_MATCH) {
       
  1570             INSERT_STRING(s, s->strstart, hash_head);
       
  1571         }
       
  1572 
       
  1573         /* Find the longest match, discarding those <= prev_length.
       
  1574          * At this point we have always match_length < MIN_MATCH
       
  1575          */
       
  1576         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
       
  1577             /* To simplify the code, we prevent matches with the string
       
  1578              * of window index 0 (in particular we have to avoid a match
       
  1579              * of the string with itself at the start of the input file).
       
  1580              */
       
  1581             s->match_length = longest_match (s, hash_head);
       
  1582             /* longest_match() sets match_start */
       
  1583         }
       
  1584         if (s->match_length >= MIN_MATCH) {
       
  1585             check_match(s, s->strstart, s->match_start, s->match_length);
       
  1586 
       
  1587             _tr_tally_dist(s, s->strstart - s->match_start,
       
  1588                            s->match_length - MIN_MATCH, bflush);
       
  1589 
       
  1590             s->lookahead -= s->match_length;
       
  1591 
       
  1592             /* Insert new strings in the hash table only if the match length
       
  1593              * is not too large. This saves time but degrades compression.
       
  1594              */
       
  1595 #ifndef FASTEST
       
  1596             if (s->match_length <= s->max_insert_length &&
       
  1597                 s->lookahead >= MIN_MATCH) {
       
  1598                 s->match_length--; /* string at strstart already in table */
       
  1599                 do {
       
  1600                     s->strstart++;
       
  1601                     INSERT_STRING(s, s->strstart, hash_head);
       
  1602                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
       
  1603                      * always MIN_MATCH bytes ahead.
       
  1604                      */
       
  1605                 } while (--s->match_length != 0);
       
  1606                 s->strstart++;
       
  1607             } else
       
  1608 #endif
       
  1609             {
       
  1610                 s->strstart += s->match_length;
       
  1611                 s->match_length = 0;
       
  1612                 s->ins_h = s->window[s->strstart];
       
  1613                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
       
  1614 #if MIN_MATCH != 3
       
  1615                 Call UPDATE_HASH() MIN_MATCH-3 more times
       
  1616 #endif
       
  1617                 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
       
  1618                  * matter since it will be recomputed at next deflate call.
       
  1619                  */
       
  1620             }
       
  1621         } else {
       
  1622             /* No match, output a literal byte */
       
  1623             Tracevv((stderr,"%c", s->window[s->strstart]));
       
  1624             _tr_tally_lit (s, s->window[s->strstart], bflush);
       
  1625             s->lookahead--;
       
  1626             s->strstart++;
       
  1627         }
       
  1628         if (bflush) FLUSH_BLOCK(s, 0);
       
  1629     }
       
  1630     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1631     return flush == Z_FINISH ? finish_done : block_done;
       
  1632 }
       
  1633 
       
  1634 #ifndef FASTEST
       
  1635 /* ===========================================================================
       
  1636  * Same as above, but achieves better compression. We use a lazy
       
  1637  * evaluation for matches: a match is finally adopted only if there is
       
  1638  * no better match at the next window position.
       
  1639  */
       
  1640 local block_state deflate_slow(s, flush)
       
  1641     deflate_state *s;
       
  1642     int flush;
       
  1643 {
       
  1644     IPos hash_head;          /* head of hash chain */
       
  1645     int bflush;              /* set if current block must be flushed */
       
  1646 
       
  1647     /* Process the input block. */
       
  1648     for (;;) {
       
  1649         /* Make sure that we always have enough lookahead, except
       
  1650          * at the end of the input file. We need MAX_MATCH bytes
       
  1651          * for the next match, plus MIN_MATCH bytes to insert the
       
  1652          * string following the next match.
       
  1653          */
       
  1654         if (s->lookahead < MIN_LOOKAHEAD) {
       
  1655             fill_window(s);
       
  1656             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
       
  1657                 return need_more;
       
  1658             }
       
  1659             if (s->lookahead == 0) break; /* flush the current block */
       
  1660         }
       
  1661 
       
  1662         /* Insert the string window[strstart .. strstart+2] in the
       
  1663          * dictionary, and set hash_head to the head of the hash chain:
       
  1664          */
       
  1665         hash_head = NIL;
       
  1666         if (s->lookahead >= MIN_MATCH) {
       
  1667             INSERT_STRING(s, s->strstart, hash_head);
       
  1668         }
       
  1669 
       
  1670         /* Find the longest match, discarding those <= prev_length.
       
  1671          */
       
  1672         s->prev_length = s->match_length, s->prev_match = s->match_start;
       
  1673         s->match_length = MIN_MATCH-1;
       
  1674 
       
  1675         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
       
  1676             s->strstart - hash_head <= MAX_DIST(s)) {
       
  1677             /* To simplify the code, we prevent matches with the string
       
  1678              * of window index 0 (in particular we have to avoid a match
       
  1679              * of the string with itself at the start of the input file).
       
  1680              */
       
  1681             s->match_length = longest_match (s, hash_head);
       
  1682             /* longest_match() sets match_start */
       
  1683 
       
  1684             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
       
  1685 #if TOO_FAR <= 32767
       
  1686                 || (s->match_length == MIN_MATCH &&
       
  1687                     s->strstart - s->match_start > TOO_FAR)
       
  1688 #endif
       
  1689                 )) {
       
  1690 
       
  1691                 /* If prev_match is also MIN_MATCH, match_start is garbage
       
  1692                  * but we will ignore the current match anyway.
       
  1693                  */
       
  1694                 s->match_length = MIN_MATCH-1;
       
  1695             }
       
  1696         }
       
  1697         /* If there was a match at the previous step and the current
       
  1698          * match is not better, output the previous match:
       
  1699          */
       
  1700         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
       
  1701             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
       
  1702             /* Do not insert strings in hash table beyond this. */
       
  1703 
       
  1704             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
       
  1705 
       
  1706             _tr_tally_dist(s, s->strstart -1 - s->prev_match,
       
  1707                            s->prev_length - MIN_MATCH, bflush);
       
  1708 
       
  1709             /* Insert in hash table all strings up to the end of the match.
       
  1710              * strstart-1 and strstart are already inserted. If there is not
       
  1711              * enough lookahead, the last two strings are not inserted in
       
  1712              * the hash table.
       
  1713              */
       
  1714             s->lookahead -= s->prev_length-1;
       
  1715             s->prev_length -= 2;
       
  1716             do {
       
  1717                 if (++s->strstart <= max_insert) {
       
  1718                     INSERT_STRING(s, s->strstart, hash_head);
       
  1719                 }
       
  1720             } while (--s->prev_length != 0);
       
  1721             s->match_available = 0;
       
  1722             s->match_length = MIN_MATCH-1;
       
  1723             s->strstart++;
       
  1724 
       
  1725             if (bflush) FLUSH_BLOCK(s, 0);
       
  1726 
       
  1727         } else if (s->match_available) {
       
  1728             /* If there was no match at the previous position, output a
       
  1729              * single literal. If there was a match but the current match
       
  1730              * is longer, truncate the previous match to a single literal.
       
  1731              */
       
  1732             Tracevv((stderr,"%c", s->window[s->strstart-1]));
       
  1733             _tr_tally_lit(s, s->window[s->strstart-1], bflush);
       
  1734             if (bflush) {
       
  1735                 FLUSH_BLOCK_ONLY(s, 0);
       
  1736             }
       
  1737             s->strstart++;
       
  1738             s->lookahead--;
       
  1739             if (s->strm->avail_out == 0) return need_more;
       
  1740         } else {
       
  1741             /* There is no previous match to compare with, wait for
       
  1742              * the next step to decide.
       
  1743              */
       
  1744             s->match_available = 1;
       
  1745             s->strstart++;
       
  1746             s->lookahead--;
       
  1747         }
       
  1748     }
       
  1749     Assert (flush != Z_NO_FLUSH, "no flush?");
       
  1750     if (s->match_available) {
       
  1751         Tracevv((stderr,"%c", s->window[s->strstart-1]));
       
  1752         _tr_tally_lit(s, s->window[s->strstart-1], bflush);
       
  1753         s->match_available = 0;
       
  1754     }
       
  1755     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1756     return flush == Z_FINISH ? finish_done : block_done;
       
  1757 }
       
  1758 #endif /* FASTEST */
       
  1759 
       
  1760 /* ===========================================================================
       
  1761  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
       
  1762  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
       
  1763  * deflate switches away from Z_RLE.)
       
  1764  */
       
  1765 local block_state deflate_rle(s, flush)
       
  1766     deflate_state *s;
       
  1767     int flush;
       
  1768 {
       
  1769     int bflush;             /* set if current block must be flushed */
       
  1770     uInt prev;              /* byte at distance one to match */
       
  1771     Bytef *scan, *strend;   /* scan goes up to strend for length of run */
       
  1772 
       
  1773     for (;;) {
       
  1774         /* Make sure that we always have enough lookahead, except
       
  1775          * at the end of the input file. We need MAX_MATCH bytes
       
  1776          * for the longest encodable run.
       
  1777          */
       
  1778         if (s->lookahead < MAX_MATCH) {
       
  1779             fill_window(s);
       
  1780             if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
       
  1781                 return need_more;
       
  1782             }
       
  1783             if (s->lookahead == 0) break; /* flush the current block */
       
  1784         }
       
  1785 
       
  1786         /* See how many times the previous byte repeats */
       
  1787         s->match_length = 0;
       
  1788         if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
       
  1789             scan = s->window + s->strstart - 1;
       
  1790             prev = *scan;
       
  1791             if (prev == *++scan && prev == *++scan && prev == *++scan) {
       
  1792                 strend = s->window + s->strstart + MAX_MATCH;
       
  1793                 do {
       
  1794                 } while (prev == *++scan && prev == *++scan &&
       
  1795                          prev == *++scan && prev == *++scan &&
       
  1796                          prev == *++scan && prev == *++scan &&
       
  1797                          prev == *++scan && prev == *++scan &&
       
  1798                          scan < strend);
       
  1799                 s->match_length = MAX_MATCH - (int)(strend - scan);
       
  1800                 if (s->match_length > s->lookahead)
       
  1801                     s->match_length = s->lookahead;
       
  1802             }
       
  1803         }
       
  1804 
       
  1805         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
       
  1806         if (s->match_length >= MIN_MATCH) {
       
  1807             check_match(s, s->strstart, s->strstart - 1, s->match_length);
       
  1808 
       
  1809             _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
       
  1810 
       
  1811             s->lookahead -= s->match_length;
       
  1812             s->strstart += s->match_length;
       
  1813             s->match_length = 0;
       
  1814         } else {
       
  1815             /* No match, output a literal byte */
       
  1816             Tracevv((stderr,"%c", s->window[s->strstart]));
       
  1817             _tr_tally_lit (s, s->window[s->strstart], bflush);
       
  1818             s->lookahead--;
       
  1819             s->strstart++;
       
  1820         }
       
  1821         if (bflush) FLUSH_BLOCK(s, 0);
       
  1822     }
       
  1823     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1824     return flush == Z_FINISH ? finish_done : block_done;
       
  1825 }
       
  1826 
       
  1827 /* ===========================================================================
       
  1828  * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
       
  1829  * (It will be regenerated if this run of deflate switches away from Huffman.)
       
  1830  */
       
  1831 local block_state deflate_huff(s, flush)
       
  1832     deflate_state *s;
       
  1833     int flush;
       
  1834 {
       
  1835     int bflush;             /* set if current block must be flushed */
       
  1836 
       
  1837     for (;;) {
       
  1838         /* Make sure that we have a literal to write. */
       
  1839         if (s->lookahead == 0) {
       
  1840             fill_window(s);
       
  1841             if (s->lookahead == 0) {
       
  1842                 if (flush == Z_NO_FLUSH)
       
  1843                     return need_more;
       
  1844                 break;      /* flush the current block */
       
  1845             }
       
  1846         }
       
  1847 
       
  1848         /* Output a literal byte */
       
  1849         s->match_length = 0;
       
  1850         Tracevv((stderr,"%c", s->window[s->strstart]));
       
  1851         _tr_tally_lit (s, s->window[s->strstart], bflush);
       
  1852         s->lookahead--;
       
  1853         s->strstart++;
       
  1854         if (bflush) FLUSH_BLOCK(s, 0);
       
  1855     }
       
  1856     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1857     return flush == Z_FINISH ? finish_done : block_done;
       
  1858 }