home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / zlib / deflate.c next >
C/C++ Source or Header  |  2003-07-12  |  28KB  |  845 lines

  1. /* deflate.c -- compress data using the deflation algorithm
  2.  * Copyright (C) 1995-1998 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6.  
  7. #include "deflate.h"
  8.  
  9. const char deflate_copyright[] =
  10.    " deflate 1.1.3 Copyright 1995-1998 Jean-loup Gailly ";
  11. /*
  12.   If you use the zlib library in a product, an acknowledgment is welcome
  13.   in the documentation of your product. If for some reason you cannot
  14.   include such an acknowledgment, I would appreciate that you keep this
  15.   copyright string in the executable of your product.
  16.  */
  17.  
  18. /* ===========================================================================
  19.  *  Function prototypes.
  20.  */
  21. typedef enum {
  22.     need_more,      /* block not completed, need more input or more output */
  23.     block_done,     /* block flush performed */
  24.     finish_started, /* finish started, need only more output at next deflate */
  25.     finish_done     /* finish done, accept no more input or output */
  26. } block_state;
  27.  
  28. typedef block_state (*compress_func) OF((deflate_state *s, int flush));
  29. /* Compression function. Returns the block state after the call. */
  30.  
  31. local void fill_window    OF((deflate_state *s));
  32. local block_state deflate_slow   OF((deflate_state *s, int flush));
  33. local void lm_init        OF((deflate_state *s));
  34. local void putShortMSB    OF((deflate_state *s, uInt b));
  35. local void flush_pending  OF((z_streamp strm));
  36. local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
  37. #ifdef ASMV
  38.       void match_init OF((void)); /* asm code initialization */
  39.       uInt longest_match  OF((deflate_state *s, IPos cur_match));
  40. #else
  41. local uInt longest_match  OF((deflate_state *s, IPos cur_match));
  42. #endif
  43.  
  44. #ifdef DEBUG
  45. local  void check_match OF((deflate_state *s, IPos start, IPos match,
  46.                             int length));
  47. #endif
  48.  
  49. /* ===========================================================================
  50.  * Local data
  51.  */
  52.  
  53. #define NIL 0
  54. /* Tail of hash chains */
  55.  
  56. #ifndef TOO_FAR
  57. #  define TOO_FAR 32767 //stock is 4096, but 32767 enables slightly better compression
  58. #endif
  59. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  60.  
  61. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  62.  
  63. typedef struct config_s {
  64.    ush good_length; /* reduce lazy search above this match length */
  65.    ush max_lazy;    /* do not perform lazy search above this match length */
  66.    ush nice_length; /* quit search above this match length */
  67.    ush max_chain;
  68.    compress_func func;
  69. } config;
  70.  
  71. local const config configuration_table = 
  72. /* 9 */ {32, 258, 258, 16384, deflate_slow}; /* maximum compression */
  73.  
  74.  
  75. #define EQUAL 0
  76. /* result of memcmp for equal strings */
  77.  
  78. struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
  79.  
  80. #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  81.  
  82.  
  83. #ifdef FASTEST
  84. #define INSERT_STRING(s, str, match_head) \
  85.    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  86.     match_head = s->head[s->ins_h], \
  87.     s->head[s->ins_h] = (Pos)(str))
  88. #else
  89. #define INSERT_STRING(s, str, match_head) \
  90.    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  91.     s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
  92.     s->head[s->ins_h] = (Pos)(str))
  93. #endif
  94.  
  95. #define CLEAR_HASH(s) \
  96.     s->head[s->hash_size-1] = NIL; \
  97.     zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  98.  
  99. /* ========================================================================= */
  100. int ZEXPORT deflateInit_(strm, level, version, stream_size)
  101.     z_streamp strm;
  102.     int level;
  103.     const char *version;
  104.     int stream_size;
  105. {
  106.     return deflateInit2_(strm, level, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL,
  107.                          Z_DEFAULT_STRATEGY, version, stream_size);
  108.     /* To do: ignore strm->next_in if we use it as window */
  109. }
  110.  
  111. /* ========================================================================= */
  112. int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
  113.                           version, stream_size)
  114.     z_streamp strm;
  115.     int  level;
  116.     int  method;
  117.     int  windowBits;
  118.     int  memLevel;
  119.     int  strategy;
  120.     const char *version;
  121.     int stream_size;
  122. {
  123.     deflate_state *s;
  124.     int noheader = 0;
  125.  
  126.     ushf *overlay;
  127.     /* We overlay pending_buf and d_buf+l_buf. This works since the average
  128.      * output size for (length,distance) codes is <= 24 bits.
  129.      */
  130.  
  131.     if (stream_size != sizeof(z_stream)) {
  132.       return Z_VERSION_ERROR;
  133.     }
  134.     if (strm == Z_NULL) return Z_STREAM_ERROR;
  135.  
  136.  
  137.     if (windowBits < 0) { /* undocumented feature: suppress zlib header */
  138.         noheader = 1;
  139.         windowBits = -windowBits;
  140.     }
  141.     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  142.         windowBits < 8 || windowBits > MAX_WBITS || level < 0 || level > 9 ||
  143.         strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  144.  
  145.         return Z_STREAM_ERROR;
  146.     }
  147.  
  148.     s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  149.     if (s == Z_NULL) return Z_MEM_ERROR;
  150.     strm->state = (struct internal_state FAR *)s;
  151.     s->strm = strm;
  152.  
  153.     s->noheader = noheader;
  154.     s->w_bits = windowBits;
  155.     s->w_size = 1 << s->w_bits;
  156.     s->w_mask = s->w_size - 1;
  157.  
  158.     s->hash_bits = memLevel + 7;
  159.     s->hash_size = 1 << s->hash_bits;
  160.     s->hash_mask = s->hash_size - 1;
  161.     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  162.  
  163.     s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  164.     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
  165.     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
  166.  
  167.     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  168.  
  169.     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  170.     s->pending_buf = (uchf *) overlay;
  171.     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  172.  
  173.     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  174.         s->pending_buf == Z_NULL) {
  175. //        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
  176.         deflateEnd (strm);
  177.         return Z_MEM_ERROR;
  178.     }
  179.     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  180.     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  181.  
  182.     s->level = level;
  183.     s->strategy = strategy;
  184.     s->method = (Byte)method;
  185.  
  186.     return deflateReset(strm);
  187. }
  188.  
  189. /* ========================================================================= */
  190. int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
  191.     z_streamp strm;
  192.     const Bytef *dictionary;
  193.     uInt  dictLength;
  194. {
  195.     deflate_state *s;
  196.     uInt length = dictLength;
  197.     uInt n;
  198.     IPos hash_head = 0;
  199.  
  200.     if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
  201.         strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
  202.  
  203.     s = strm->state;
  204.     
  205.     if (length < MIN_MATCH) return Z_OK;
  206.     if (length > MAX_DIST(s)) {
  207.       length = MAX_DIST(s);
  208. #ifndef USE_DICT_HEAD
  209.       dictionary += dictLength - length; /* use the tail of the dictionary */
  210. #endif
  211.     }
  212.     zmemcpy(s->window, dictionary, length);
  213.     s->strstart = length;
  214.     s->block_start = (long)length;
  215.  
  216.     s->ins_h = s->window[0];
  217.     UPDATE_HASH(s, s->ins_h, s->window[1]);
  218.     for (n = 0; n <= length - MIN_MATCH; n++) {
  219.       INSERT_STRING(s, n, hash_head);
  220.     }
  221.     if (hash_head) hash_head = 0;  /* to make compiler happy */
  222.     return Z_OK;
  223. }
  224.  
  225. /* ========================================================================= */
  226. int ZEXPORT deflateReset (strm)
  227.     z_streamp strm;
  228. {
  229.     deflate_state *s;
  230.     
  231.     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  232.  
  233.     strm->total_in = strm->total_out = 0;
  234. //    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  235.  
  236.     s = (deflate_state *)strm->state;
  237.     s->pending = 0;
  238.     s->pending_out = s->pending_buf;
  239.  
  240.     if (s->noheader < 0) {
  241.         s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
  242.     }
  243.     s->status = s->noheader ? BUSY_STATE : INIT_STATE;
  244.     s->last_flush = Z_NO_FLUSH;
  245.  
  246.     _tr_init(s);
  247.     lm_init(s);
  248.  
  249.     return Z_OK;
  250. }
  251.  
  252. /* ========================================================================= */
  253. int ZEXPORT deflateParams(strm, level, strategy)
  254.     z_streamp strm;
  255.     int level;
  256.     int strategy;
  257. {
  258.     deflate_state *s;
  259.     compress_func func;
  260.     int err = Z_OK;
  261.  
  262.     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  263.     s = strm->state;
  264.  
  265.     if (level == Z_DEFAULT_COMPRESSION) {
  266.       level = 6;
  267.     }
  268.     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  269.       return Z_STREAM_ERROR;
  270.     }
  271.     func = configuration_table.func;
  272.  
  273.     s->level            = level;
  274.     s->max_lazy_match   = configuration_table.max_lazy;
  275.     s->good_match       = configuration_table.good_length;
  276.     s->nice_match       = configuration_table.nice_length;
  277.     s->max_chain_length = configuration_table.max_chain;
  278.     s->strategy         = strategy;
  279.     return err;
  280. }
  281.  
  282. local void putShortMSB (s, b)
  283.     deflate_state *s;
  284.     uInt b;
  285. {
  286.     put_byte(s, (Byte)(b >> 8));
  287.     put_byte(s, (Byte)(b & 0xff));
  288. }   
  289.  
  290. local void flush_pending(strm)
  291.     z_streamp strm;
  292. {
  293.     unsigned len = strm->state->pending;
  294.  
  295.     if (len > strm->avail_out) len = strm->avail_out;
  296.     if (len == 0) return;
  297.  
  298.     zmemcpy(strm->next_out, strm->state->pending_out, len);
  299.     strm->next_out  += len;
  300.     strm->state->pending_out  += len;
  301.     strm->total_out += len;
  302.     strm->avail_out  -= len;
  303.     strm->state->pending -= len;
  304.     if (strm->state->pending == 0) {
  305.         strm->state->pending_out = strm->state->pending_buf;
  306.     }
  307. }
  308.  
  309. /* ========================================================================= */
  310. int ZEXPORT deflate (strm, flush)
  311.     z_streamp strm;
  312.     int flush;
  313. {
  314.     int old_flush; /* value of flush param for previous deflate call */
  315.     deflate_state *s;
  316.  
  317.     if (strm == Z_NULL || strm->state == Z_NULL ||
  318.         flush > Z_FINISH || flush < 0) {
  319.         return Z_STREAM_ERROR;
  320.     }
  321.     s = strm->state;
  322.  
  323.     if (strm->next_out == Z_NULL ||
  324.         (strm->next_in == Z_NULL && strm->avail_in != 0) ||
  325.         (s->status == FINISH_STATE && flush != Z_FINISH)) {
  326.         ERR_RETURN(strm, Z_STREAM_ERROR);
  327.     }
  328.     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  329.  
  330.     s->strm = strm; /* just in case */
  331.     old_flush = s->last_flush;
  332.     s->last_flush = flush;
  333.  
  334.     /* Write the zlib header */
  335.     if (s->status == INIT_STATE) {
  336.  
  337.         uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  338.         uInt level_flags = (s->level-1) >> 1;
  339.  
  340.         if (level_flags > 3) level_flags = 3;
  341.         header |= (level_flags << 6);
  342.         if (s->strstart != 0) header |= PRESET_DICT;
  343.         header += 31 - (header % 31);
  344.  
  345.         s->status = BUSY_STATE;
  346.         putShortMSB(s, header);
  347.  
  348.         /* Save the adler32 of the preset dictionary: */
  349.         if (s->strstart != 0) {
  350.           //putShortMSB(s, (uInt)(strm->adler >> 16));
  351.           //putShortMSB(s, (uInt)(strm->adler & 0xffff));
  352.         }
  353.       //strm->adler = 1L;
  354.     }
  355.  
  356.     /* Flush as much pending output as possible */
  357.     if (s->pending != 0) {
  358.         flush_pending(strm);
  359.         if (strm->avail_out == 0) {
  360.           s->last_flush = -1;
  361.           return Z_OK;
  362.         }
  363.  
  364.     } else if (strm->avail_in == 0 && flush <= old_flush &&
  365.                flush != Z_FINISH) {
  366.         ERR_RETURN(strm, Z_BUF_ERROR);
  367.     }
  368.  
  369.     /* User must not provide more input after the first FINISH: */
  370.     if (s->status == FINISH_STATE && strm->avail_in != 0) {
  371.         ERR_RETURN(strm, Z_BUF_ERROR);
  372.     }
  373.  
  374.     /* Start a new block or continue the current one.
  375.      */
  376.     if (strm->avail_in != 0 || s->lookahead != 0 ||
  377.         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  378.         block_state bstate;
  379.  
  380.         bstate = (*(configuration_table.func))(s, flush);
  381.  
  382.         if (bstate == finish_started || bstate == finish_done) {
  383.             s->status = FINISH_STATE;
  384.         }
  385.         if (bstate == need_more || bstate == finish_started) {
  386.           if (strm->avail_out == 0) {
  387.             s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  388.           }
  389.           return Z_OK;
  390.         }
  391.         if (bstate == block_done) {
  392.             if (flush == Z_PARTIAL_FLUSH) {
  393.                 _tr_align(s);
  394.             } else { /* FULL_FLUSH or SYNC_FLUSH */
  395.                 _tr_stored_block(s, (char*)0, 0L, 0);
  396.                 /* For a full flush, this empty block will be recognized
  397.                  * as a special marker by inflate_sync().
  398.                  */
  399.                 if (flush == Z_FULL_FLUSH) {
  400.                     CLEAR_HASH(s);             /* forget history */
  401.                 }
  402.             }
  403.             flush_pending(strm);
  404.             if (strm->avail_out == 0) {
  405.               s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  406.               return Z_OK;
  407.             }
  408.         }
  409.     }
  410.     Assert(strm->avail_out > 0, "bug2");
  411.  
  412.     if (flush != Z_FINISH) return Z_OK;
  413.     if (s->noheader) return Z_STREAM_END;
  414.  
  415.     flush_pending(strm);
  416.     s->noheader = -1; /* write the trailer only once! */
  417.     return s->pending != 0 ? Z_OK : Z_STREAM_END;
  418. }
  419.  
  420. /* ========================================================================= */
  421. int ZEXPORT deflateEnd (strm)
  422.     z_streamp strm;
  423. {
  424.     int status;
  425.  
  426.     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  427.  
  428.     status = strm->state->status;
  429.     if (status != INIT_STATE && status != BUSY_STATE &&
  430.         status != FINISH_STATE) {
  431.       return Z_STREAM_ERROR;
  432.     }
  433.  
  434.     /* Deallocate in reverse order of allocations: */
  435.     TRY_FREE(strm, strm->state->pending_buf);
  436.     TRY_FREE(strm, strm->state->head);
  437.     TRY_FREE(strm, strm->state->prev);
  438.     TRY_FREE(strm, strm->state->window);
  439.  
  440.     ZFREE(strm, strm->state);
  441.     strm->state = Z_NULL;
  442.  
  443.     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  444. }
  445.  
  446.  
  447. local int read_buf(strm, buf, size)
  448.     z_streamp strm;
  449.     Bytef *buf;
  450.     unsigned size;
  451. {
  452.     unsigned len = strm->avail_in;
  453.  
  454.     if (len > size) len = size;
  455.     if (len == 0) return 0;
  456.  
  457.     strm->avail_in  -= len;
  458.  
  459.     //if (!strm->state->noheader) {
  460. //        strm->adler = adler32(strm->adler, strm->next_in, len);
  461.   //  }
  462.     zmemcpy(buf, strm->next_in, len);
  463.     strm->next_in  += len;
  464.     strm->total_in += len;
  465.  
  466.     return (int)len;
  467. }
  468.  
  469. /* ===========================================================================
  470.  * Initialize the "longest match" routines for a new zlib stream
  471.  */
  472. local void lm_init (s)
  473.     deflate_state *s;
  474. {
  475.     s->window_size = (ulg)2L*s->w_size;
  476.  
  477.     CLEAR_HASH(s);
  478.  
  479.     /* Set the default configuration parameters:
  480.      */
  481.     s->max_lazy_match   = configuration_table.max_lazy;
  482.     s->good_match       = configuration_table.good_length;
  483.     s->nice_match       = configuration_table.nice_length;
  484.     s->max_chain_length = configuration_table.max_chain;
  485.  
  486.     s->strstart = 0;
  487.     s->block_start = 0L;
  488.     s->lookahead = 0;
  489.     s->match_length = s->prev_length = MIN_MATCH-1;
  490.     s->match_available = 0;
  491.     s->ins_h = 0;
  492. #ifdef ASMV
  493.     match_init(); /* initialize the asm code */
  494. #endif
  495. }
  496.  
  497. #ifndef ASMV
  498. /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  499.  * match.S. The code will be functionally equivalent.
  500.  */
  501. #ifndef FASTEST
  502. local uInt longest_match(s, cur_match)
  503.     deflate_state *s;
  504.     IPos cur_match;                             /* current match */
  505. {
  506.     unsigned chain_length = s->max_chain_length;/* max hash chain length */
  507.     register Bytef *scan = s->window + s->strstart; /* current string */
  508.     register Bytef *match;                       /* matched string */
  509.     register int len;                           /* length of current match */
  510.     int best_len = s->prev_length;              /* best match length so far */
  511.     int nice_match = s->nice_match;             /* stop if match long enough */
  512.     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  513.         s->strstart - (IPos)MAX_DIST(s) : NIL;
  514.     /* Stop when cur_match becomes <= limit. To simplify the code,
  515.      * we prevent matches with the string of window index 0.
  516.      */
  517.     Posf *prev = s->prev;
  518.     uInt wmask = s->w_mask;
  519.  
  520. #ifdef UNALIGNED_OK
  521.     /* Compare two bytes at a time. Note: this is not always beneficial.
  522.      * Try with and without -DUNALIGNED_OK to check.
  523.      */
  524.     register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  525.     register ush scan_start = *(ushf*)scan;
  526.     register ush scan_end   = *(ushf*)(scan+best_len-1);
  527. #else
  528.     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  529.     register Byte scan_end1  = scan[best_len-1];
  530.     register Byte scan_end   = scan[best_len];
  531. #endif
  532.  
  533.     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  534.      * It is easy to get rid of this optimization if necessary.
  535.      */
  536.     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  537.  
  538.     /* Do not waste too much time if we already have a good match: */
  539.     if (s->prev_length >= s->good_match) {
  540.         chain_length >>= 2;
  541.     }
  542.     /* Do not look for matches beyond the end of the input. This is necessary
  543.      * to make deflate deterministic.
  544.      */
  545.     if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  546.  
  547.     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  548.  
  549.     do {
  550.         Assert(cur_match < s->strstart, "no future");
  551.         match = s->window + cur_match;
  552.  
  553.         /* Skip to next match if the match length cannot increase
  554.          * or if the match length is less than 2:
  555.          */
  556. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  557.         /* This code assumes sizeof(unsigned short) == 2. Do not use
  558.          * UNALIGNED_OK if your compiler uses a different size.
  559.          */
  560.         if (*(ushf*)(match+best_len-1) != scan_end ||
  561.             *(ushf*)match != scan_start) continue;
  562.  
  563.         Assert(scan[2] == match[2], "scan[2]?");
  564.         scan++, match++;
  565.         do {
  566.         } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  567.                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  568.                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  569.                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  570.                  scan < strend);
  571.         /* The funny "do {}" generates better code on most compilers */
  572.  
  573.         /* Here, scan <= window+strstart+257 */
  574.         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  575.         if (*scan == *match) scan++;
  576.  
  577.         len = (MAX_MATCH - 1) - (int)(strend-scan);
  578.         scan = strend - (MAX_MATCH-1);
  579.  
  580. #else /* UNALIGNED_OK */
  581.  
  582.         if (match[best_len]   != scan_end  ||
  583.             match[best_len-1] != scan_end1 ||
  584.             *match            != *scan     ||
  585.             *++match          != scan[1])      continue;
  586.  
  587.         scan += 2, match++;
  588.         Assert(*scan == *match, "match[2]?");
  589.         do {
  590.         } while (*++scan == *++match && *++scan == *++match &&
  591.                  *++scan == *++match && *++scan == *++match &&
  592.                  *++scan == *++match && *++scan == *++match &&
  593.                  *++scan == *++match && *++scan == *++match &&
  594.                  scan < strend);
  595.  
  596.         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  597.  
  598.         len = MAX_MATCH - (int)(strend - scan);
  599.         scan = strend - MAX_MATCH;
  600.  
  601. #endif /* UNALIGNED_OK */
  602.  
  603.         if (len > best_len) {
  604.             s->match_start = cur_match;
  605.             best_len = len;
  606.             if (len >= nice_match) break;
  607. #ifdef UNALIGNED_OK
  608.             scan_end = *(ushf*)(scan+best_len-1);
  609. #else
  610.             scan_end1  = scan[best_len-1];
  611.             scan_end   = scan[best_len];
  612. #endif
  613.         }
  614.     } while ((cur_match = prev[cur_match & wmask]) > limit
  615.              && --chain_length != 0);
  616.  
  617.     if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  618.     return s->lookahead;
  619. }
  620.  
  621. #else /* FASTEST */
  622. /* ---------------------------------------------------------------------------
  623.  * Optimized version for level == 1 only
  624.  */
  625. local uInt longest_match(s, cur_match)
  626.     deflate_state *s;
  627.     IPos cur_match;                             /* current match */
  628. {
  629.     register Bytef *scan = s->window + s->strstart; /* current string */
  630.     register Bytef *match;                       /* matched string */
  631.     register int len;                           /* length of current match */
  632.     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  633.  
  634.     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  635.  
  636.     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  637.  
  638.     Assert(cur_match < s->strstart, "no future");
  639.  
  640.     match = s->window + cur_match;
  641.  
  642.     /* Return failure if the match length is less than 2:
  643.      */
  644.     if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  645.     scan += 2, match += 2;
  646.     Assert(*scan == *match, "match[2]?");
  647.  
  648.     do {
  649.     } while (*++scan == *++match && *++scan == *++match &&
  650.        *++scan == *++match && *++scan == *++match &&
  651.        *++scan == *++match && *++scan == *++match &&
  652.        *++scan == *++match && *++scan == *++match &&
  653.        scan < strend);
  654.  
  655.     Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  656.  
  657.     len = MAX_MATCH - (int)(strend - scan);
  658.  
  659.     if (len < MIN_MATCH) return MIN_MATCH - 1;
  660.  
  661.     s->match_start = cur_match;
  662.     return len <= s->lookahead ? len : s->lookahead;
  663. }
  664. #endif /* FASTEST */
  665. #endif /* ASMV */
  666.  
  667. #  define check_match(s, start, match, length)
  668.  
  669. local void fill_window(s)
  670.     deflate_state *s;
  671. {
  672.     register unsigned n, m;
  673.     register Posf *p;
  674.     unsigned more;    /* Amount of free space at the end of the window. */
  675.     uInt wsize = s->w_size;
  676.  
  677.     do {
  678.         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  679.  
  680.         /* Deal with !@#$% 64K limit: */
  681.         if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  682.             more = wsize;
  683.  
  684.         } else if (more == (unsigned)(-1)) {
  685.             /* Very unlikely, but possible on 16 bit machine if strstart == 0
  686.              * and lookahead == 1 (input done one byte at time)
  687.              */
  688.             more--;
  689.  
  690.         /* If the window is almost full and there is insufficient lookahead,
  691.          * move the upper half to the lower one to make room in the upper half.
  692.          */
  693.         } else if (s->strstart >= wsize+MAX_DIST(s)) {
  694.  
  695.             zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
  696.             s->match_start -= wsize;
  697.             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
  698.             s->block_start -= (long) wsize;
  699.  
  700.             n = s->hash_size;
  701.             p = &s->head[n];
  702.             do {
  703.               m = *--p;
  704.               *p = (Pos)(m >= wsize ? m-wsize : NIL);
  705.             } while (--n);
  706.  
  707.             n = wsize;
  708. #ifndef FASTEST
  709.             p = &s->prev[n];
  710.             do {
  711.               m = *--p;
  712.               *p = (Pos)(m >= wsize ? m-wsize : NIL);
  713.               /* If n is not on any hash chain, prev[n] is garbage but
  714.                * its value will never be used.
  715.                */
  716.               } while (--n);
  717. #endif
  718.             more += wsize;
  719.         }
  720.         if (s->strm->avail_in == 0) return;
  721.         Assert(more >= 2, "more < 2");
  722.  
  723.         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  724.         s->lookahead += n;
  725.  
  726.         /* Initialize the hash value now that we have some input: */
  727.         if (s->lookahead >= MIN_MATCH) {
  728.             s->ins_h = s->window[s->strstart];
  729.             UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  730. #if MIN_MATCH != 3
  731.             Call UPDATE_HASH() MIN_MATCH-3 more times
  732. #endif
  733.         }
  734.  
  735.     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  736. }
  737.  
  738. #define FLUSH_BLOCK_ONLY(s, eof) { \
  739.    _tr_flush_block(s, (s->block_start >= 0L ? \
  740.                    (charf *)&s->window[(unsigned)s->block_start] : \
  741.                    (charf *)Z_NULL), \
  742.                    (ulg)((long)s->strstart - s->block_start), \
  743.                    (eof)); \
  744.    s->block_start = s->strstart; \
  745.    flush_pending(s->strm); \
  746.    Tracev((stderr,"[FLUSH]")); \
  747. }
  748.  
  749. /* Same but force premature exit if necessary. */
  750. #define FLUSH_BLOCK(s, eof) { \
  751.    FLUSH_BLOCK_ONLY(s, eof); \
  752.    if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  753. }
  754.  
  755.  
  756.  
  757. local block_state deflate_slow(s, flush)
  758.     deflate_state *s;
  759.     int flush;
  760. {
  761.     IPos hash_head = NIL;    /* head of hash chain */
  762.     int bflush;              /* set if current block must be flushed */
  763.  
  764.     /* Process the input block. */
  765.     for (;;) {
  766.         if (s->lookahead < MIN_LOOKAHEAD) {
  767.             fill_window(s);
  768.             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  769.                 return need_more;
  770.             }
  771.             if (s->lookahead == 0) break; /* flush the current block */
  772.         }
  773.         if (s->lookahead >= MIN_MATCH) {
  774.             INSERT_STRING(s, s->strstart, hash_head);
  775.         }
  776.  
  777.         /* Find the longest match, discarding those <= prev_length.
  778.          */
  779.         s->prev_length = s->match_length, s->prev_match = s->match_start;
  780.         s->match_length = MIN_MATCH-1;
  781.  
  782.         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  783.             s->strstart - hash_head <= MAX_DIST(s)) {
  784.             if (s->strategy != Z_HUFFMAN_ONLY) {
  785.                 s->match_length = longest_match (s, hash_head);
  786.             }
  787.             /* longest_match() sets match_start */
  788.  
  789.             if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
  790.                  (s->match_length == MIN_MATCH &&
  791.                   s->strstart - s->match_start > TOO_FAR))) {
  792.  
  793.                 s->match_length = MIN_MATCH-1;
  794.             }
  795.         }
  796.         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  797.             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  798.             /* Do not insert strings in hash table beyond this. */
  799.  
  800.             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  801.  
  802.             _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  803.               s->prev_length - MIN_MATCH, bflush);
  804.  
  805.             s->lookahead -= s->prev_length-1;
  806.             s->prev_length -= 2;
  807.             do {
  808.                 if (++s->strstart <= max_insert) {
  809.                     INSERT_STRING(s, s->strstart, hash_head);
  810.                 }
  811.             } while (--s->prev_length != 0);
  812.             s->match_available = 0;
  813.             s->match_length = MIN_MATCH-1;
  814.             s->strstart++;
  815.  
  816.             if (bflush) FLUSH_BLOCK(s, 0);
  817.  
  818.         } else if (s->match_available) {
  819.             Tracevv((stderr,"%c", s->window[s->strstart-1]));
  820.             _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  821.             if (bflush) {
  822.                 FLUSH_BLOCK_ONLY(s, 0);
  823.             }
  824.             s->strstart++;
  825.             s->lookahead--;
  826.             if (s->strm->avail_out == 0) return need_more;
  827.         } else {
  828.             /* There is no previous match to compare with, wait for
  829.              * the next step to decide.
  830.              */
  831.             s->match_available = 1;
  832.             s->strstart++;
  833.             s->lookahead--;
  834.         }
  835.     }
  836.     Assert (flush != Z_NO_FLUSH, "no flush?");
  837.     if (s->match_available) {
  838.         Tracevv((stderr,"%c", s->window[s->strstart-1]));
  839.         _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  840.         s->match_available = 0;
  841.     }
  842.     FLUSH_BLOCK(s, flush == Z_FINISH);
  843.     return flush == Z_FINISH ? finish_done : block_done;
  844. }
  845.