deflate.c
上传用户:sesekoo
上传日期:2020-07-18
资源大小:21543k
文件大小:64k
源码类别:

界面编程

开发平台:

Visual C++

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