home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 January / VPR0101A.BIN / OLS / TAR32053 / tar32053.exe / SRC / DEFLATE.C < prev    next >
C/C++ Source or Header  |  1999-05-23  |  36KB  |  942 lines

  1. #ifndef __DEFCONF_H
  2. #include "defconf.h"
  3. #endif
  4. /*
  5.    This file was hacked for kmtar for WIN32
  6.                                     at 1996-05-06.
  7.                                     by tantan SGL00213@niftyserve.or.jp 
  8. */
  9. /* deflate.c -- compress data using the deflation algorithm
  10.  * Copyright (C) 1992-1993 Jean-loup Gailly
  11.  * This is free software; you can redistribute it and/or modify it under the
  12.  * terms of the GNU General Public License, see the file COPYING.
  13.  */
  14.  
  15. /*
  16.  *  PURPOSE
  17.  *
  18.  *      Identify new text as repetitions of old text within a fixed-
  19.  *      length sliding window trailing behind the new text.
  20.  *
  21.  *  DISCUSSION
  22.  *
  23.  *      The "deflation" process depends on being able to identify portions
  24.  *      of the input text which are identical to earlier input (within a
  25.  *      sliding window trailing behind the input currently being processed).
  26.  *
  27.  *      The most straightforward technique turns out to be the fastest for
  28.  *      most input files: try all possible matches and select the longest.
  29.  *      The key feature of this algorithm is that insertions into the string
  30.  *      dictionary are very simple and thus fast, and deletions are avoided
  31.  *      completely. Insertions are performed at each input character, whereas
  32.  *      string matches are performed only when the previous match ends. So it
  33.  *      is preferable to spend more time in matches to allow very fast string
  34.  *      insertions and avoid deletions. The matching algorithm for small
  35.  *      strings is inspired from that of Rabin & Karp. A brute force approach
  36.  *      is used to find longer strings when a small match has been found.
  37.  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  38.  *      (by Leonid Broukhis).
  39.  *         A previous version of this file used a more sophisticated algorithm
  40.  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
  41.  *      time, but has a larger average cost, uses more memory and is patented.
  42.  *      However the F&G algorithm may be faster for some highly redundant
  43.  *      files if the parameter max_chain_length (described below) is too large.
  44.  *
  45.  *  ACKNOWLEDGEMENTS
  46.  *
  47.  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  48.  *      I found it in 'freeze' written by Leonid Broukhis.
  49.  *      Thanks to many info-zippers for bug reports and testing.
  50.  *
  51.  *  REFERENCES
  52.  *
  53.  *      APPNOTE.TXT documentation file in PKZIP 1.93a distribution.
  54.  *
  55.  *      A description of the Rabin and Karp algorithm is given in the book
  56.  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  57.  *
  58.  *      Fiala,E.R., and Greene,D.H.
  59.  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  60.  *
  61.  *  INTERFACE
  62.  *
  63.  *      void lm_init (int pack_level, ush *flags)
  64.  *          Initialize the "longest match" routines for a new file
  65.  *
  66.  *      ulg deflate (void)
  67.  *          Processes a new input file and return its compressed length. Sets
  68.  *          the compressed length, crc, deflate flags and internal file
  69.  *          attributes.
  70.  */
  71.  
  72. #include <stdio.h>
  73.  
  74. #include "tailor.h"
  75.  
  76. #include "gzip.h"
  77. /* escape from gzip.h */
  78. #if 0
  79. #ifndef MAXSEG_64K
  80. #  define tab_prefix prev    /* hash link (see deflate.c) */
  81. #  define head (prev+WSIZE)  /* hash head (see deflate.c) */
  82.    EXTERN(ush, tab_prefix);  /* prefix code (see unlzw.c) */
  83. #else
  84. #  define tab_prefix0 prev
  85. #  define head tab_prefix1
  86.    EXTERN(ush, tab_prefix0); /* prefix for even codes */
  87.    EXTERN(ush, tab_prefix1); /* prefix for odd  codes */
  88. #endif
  89. #endif
  90.  
  91. #include "lzw.h" /* just for consistency checking */
  92. #include "misc.h"
  93.  
  94. #ifdef RCSID
  95. static char rcsid[] = "$Id: deflate.c,v 0.15 1993/06/24 10:53:53 jloup Exp $";
  96. #endif
  97.  
  98. extern int lack_of_data;
  99.  
  100. /* ===========================================================================
  101.  * Configuration parameters
  102.  */
  103.  
  104. /* Compile with MEDIUM_MEM to reduce the memory requirements or
  105.  * with SMALL_MEM to use as little memory as possible. Use BIG_MEM if the
  106.  * entire input file can be held in memory (not possible on 16 bit systems).
  107.  * Warning: defining these symbols affects HASH_BITS (see below) and thus
  108.  * affects the compression ratio. The compressed output
  109.  * is still correct, and might even be smaller in some cases.
  110.  */
  111.  
  112. #ifdef SMALL_MEM
  113. #   define HASH_BITS  13  /* Number of bits used to hash strings */
  114. #endif
  115. #ifdef MEDIUM_MEM
  116. #   define HASH_BITS  14
  117. #endif
  118. #ifndef HASH_BITS
  119. #   define HASH_BITS  15
  120.    /* For portability to 16 bit machines, do not use values above 15. */
  121. #endif
  122.  
  123. /* To save space (see unlzw.c), we overlay prev+head with tab_prefix and
  124.  * window with tab_suffix. Check that we can do this:
  125.  */
  126. #if (WSIZE<<1) > (1<<BITS)
  127.    error: cannot overlay window with tab_suffix and prev with tab_prefix0
  128. #endif
  129. #if HASH_BITS > BITS-1
  130.    error: cannot overlay head with tab_prefix1
  131. #endif
  132.  
  133. #define HASH_SIZE (unsigned)(1<<HASH_BITS)
  134. #define HASH_MASK (HASH_SIZE-1)
  135. #define WMASK     (WSIZE-1)
  136. /* HASH_SIZE and WSIZE must be powers of two */
  137.  
  138. #define NIL 0
  139. /* Tail of hash chains */
  140.  
  141. #define FAST 4
  142. #define SLOW 2
  143. /* speed options for the general purpose bit flag */
  144.  
  145. #ifndef TOO_FAR
  146. #  define TOO_FAR 4096
  147. #endif
  148. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  149.  
  150. /* ===========================================================================
  151.  * Local data used by the "longest match" routines.
  152.  */
  153.  
  154. typedef ush Pos;
  155. typedef unsigned IPos;
  156. /* A Pos is an index in the character window. We use short instead of int to
  157.  * save space in the various tables. IPos is used only for parameter passing.
  158.  */
  159.  
  160. /* DECLARE(uch, window, 2L*WSIZE); */
  161. /* Sliding window. Input bytes are read into the second half of the window,
  162.  * and move to the first half later to keep a dictionary of at least WSIZE
  163.  * bytes. With this organization, matches are limited to a distance of
  164.  * WSIZE-MAX_MATCH bytes, but this ensures that IO is always
  165.  * performed with a length multiple of the block size. Also, it limits
  166.  * the window size to 64K, which is quite useful on MSDOS.
  167.  * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would
  168.  * be less efficient).
  169.  */
  170.  
  171. /* DECLARE(Pos, prev, WSIZE); */
  172. /* Link to older string with same hash index. To limit the size of this
  173.  * array to 64K, this link is maintained only for the last 32K strings.
  174.  * An index in this array is thus a window index modulo 32K.
  175.  */
  176.  
  177. /* DECLARE(Pos, head, 1<<HASH_BITS); */
  178. /* Heads of the hash chains or NIL. */
  179.  
  180. ulg window_size = (ulg)2*WSIZE;
  181. /* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
  182.  * input file length plus MIN_LOOKAHEAD.
  183.  */
  184.  
  185. long block_start;
  186. /* window position at the beginning of the current output block. Gets
  187.  * negative when the window is moved backwards.
  188.  */
  189.  
  190. local unsigned ins_h;  /* hash index of string to be inserted */
  191.  
  192. #define H_SHIFT  ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
  193. /* Number of bits by which ins_h and del_h must be shifted at each
  194.  * input step. It must be such that after MIN_MATCH steps, the oldest
  195.  * byte no longer takes part in the hash key, that is:
  196.  *   H_SHIFT * MIN_MATCH >= HASH_BITS
  197.  */
  198.  
  199. unsigned int near prev_length;
  200. /* Length of the best match at previous step. Matches not greater than this
  201.  * are discarded. This is used in the lazy match evaluation.
  202.  */
  203.  
  204.       unsigned near strstart;      /* start of string to insert */
  205.       unsigned near match_start;   /* start of matching string */
  206. local int           eofile;        /* flag set at end of input file */
  207. local unsigned      lookahead;     /* number of valid bytes ahead in window */
  208.  
  209. unsigned near max_chain_length;
  210. /* To speed up deflation, hash chains are never searched beyond this length.
  211.  * A higher limit improves compression ratio but degrades the speed.
  212.  */
  213.  
  214. local unsigned int max_lazy_match;
  215. /* Attempt to find a better match only when the current match is strictly
  216.  * smaller than this value. This mechanism is used only for compression
  217.  * levels >= 4.
  218.  */
  219. #define max_insert_length  max_lazy_match
  220. /* Insert new strings in the hash table only if the match length
  221.  * is not greater than this length. This saves time but degrades compression.
  222.  * max_insert_length is used only for compression levels <= 3.
  223.  */
  224.  
  225. local int compr_level;
  226. /* compression level (1..9) */
  227.  
  228. unsigned near good_match;
  229. /* Use a faster search when the previous match is longer than this */
  230.  
  231.  
  232. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  233.  * the desired pack level (0..9). The values given below have been tuned to
  234.  * exclude worst case performance for pathological files. Better values may be
  235.  * found for specific files.
  236.  */
  237.  
  238. typedef struct config {
  239.    ush good_length; /* reduce lazy search above this match length */
  240.    ush max_lazy;    /* do not perform lazy search above this match length */
  241.    ush nice_length; /* quit search above this match length */
  242.    ush max_chain;
  243. } config;
  244.  
  245. #ifdef  FULL_SEARCH
  246. # define nice_match MAX_MATCH
  247. #else
  248.   int near nice_match; /* Stop searching when current match exceeds this */
  249. #endif
  250.  
  251. local config configuration_table[10] = {
  252. /*      good lazy nice chain */
  253. /* 0 */ {0,    0,  0,    0},  /* store only */
  254. /* 1 */ {4,    4,  8,    4},  /* maximum speed, no lazy matches */
  255. /* 2 */ {4,    5, 16,    8},
  256. /* 3 */ {4,    6, 32,   32},
  257.  
  258. /* 4 */ {4,    4, 16,   16},  /* lazy matches */
  259. /* 5 */ {8,   16, 32,   32},
  260. /* 6 */ {8,   16, 128, 128},
  261. /* 7 */ {8,   32, 128, 256},
  262. /* 8 */ {32, 128, 258, 1024},
  263. /* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
  264.  
  265. /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  266.  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  267.  * meaning.
  268.  */
  269.  
  270. #define EQUAL 0
  271. /* result of memcmp for equal strings */
  272.  
  273. /* ===========================================================================
  274.  *  Prototypes for local functions.
  275.  */
  276. local void fill_window   OF((void));
  277. local void deflate_fast   OF((void));
  278.  
  279.       int  longest_match OF((IPos cur_match));
  280. #ifdef ASMV
  281.       void match_init OF((void)); /* asm code initialization */
  282. #endif
  283.  
  284. #ifdef DEBUG
  285. local  void check_match OF((IPos start, IPos match, int length));
  286. #endif
  287.  
  288. /* ===========================================================================
  289.  * Update a hash value with the given input byte
  290.  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
  291.  *    input characters, so that a running hash key can be computed from the
  292.  *    previous key instead of complete recalculation each time.
  293.  */
  294. #define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
  295.  
  296. /* ===========================================================================
  297.  * Insert string s in the dictionary and set match_head to the previous head
  298.  * of the hash chain (the most recent string with same hash key). Return
  299.  * the previous length of the hash chain.
  300.  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
  301.  *    input characters and the first MIN_MATCH bytes of s are valid
  302.  *    (except for the last MIN_MATCH-1 bytes of the input file).
  303.  */
  304. #define INSERT_STRING(s, match_head) \
  305.    (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \
  306.     prev[(s) & WMASK] = match_head = head[ins_h], \
  307.     head[ins_h] = (s))
  308.  
  309. /* ===========================================================================
  310.  * Initialize the "longest match" routines for a new file
  311.  */
  312. void lm_init (pack_level, flags)
  313.     int pack_level; /* 0: store, 1: best speed, 9: best compression */
  314.     ush *flags;     /* general purpose bit flag */
  315. {
  316. #ifndef    WIN32
  317.     register unsigned j;
  318. #endif
  319.  
  320.     if (pack_level < 1 || pack_level > 9) fatal("zip","bad pack level");
  321.     compr_level = pack_level;
  322.  
  323.     /* Initialize the hash table. */
  324. #if defined(MAXSEG_64K) && HASH_BITS == 15
  325.     for (j = 0;  j < HASH_SIZE; j++) head[j] = NIL;
  326. #else
  327.     memzero((char*)head, HASH_SIZE*sizeof(*head));
  328. #endif
  329.     /* prev will be initialized on the fly */
  330.  
  331.     /* Set the default configuration parameters:
  332.      */
  333.     max_lazy_match   = configuration_table[pack_level].max_lazy;
  334.     good_match       = configuration_table[pack_level].good_length;
  335. #ifndef FULL_SEARCH
  336.     nice_match       = configuration_table[pack_level].nice_length;
  337. #endif
  338.     max_chain_length = configuration_table[pack_level].max_chain;
  339.     if (pack_level == 1) {
  340.        *flags |= FAST;
  341.     } else if (pack_level == 9) {
  342.        *flags |= SLOW;
  343.     }
  344.     /* ??? reduce max_chain_length for binary files */
  345. }
  346.  
  347. void win_init(void)
  348. {
  349.     register unsigned j;
  350.  
  351.     strstart = 0;
  352.     block_start = 0L;
  353. #ifdef ASMV
  354.     match_init(); /* initialize the asm code */
  355. #endif
  356.  
  357.     lookahead = read_buf((char *)window,
  358. #ifdef    MSC
  359.              (unsigned int)WSIZE);
  360. #else
  361.              sizeof(int) <= 2 ? (unsigned int)WSIZE : (unsigned int)(2*WSIZE));
  362. #endif
  363.  
  364.     if (lookahead == 0 || lookahead == (unsigned)EOF) {
  365.        if (lack_of_data == 0)
  366.           eofile = 1, lookahead = 0;
  367.        return;
  368.     }
  369.     eofile = 0;
  370.     /* Make sure that we always have enough lookahead. This is important
  371.      * if input comes from a device such as a tty.
  372.      */
  373.     while (lookahead < MIN_LOOKAHEAD && !eofile){
  374.         fill_window();
  375.         if (lack_of_data)
  376.             return;
  377.     }
  378.  
  379.     ins_h = 0;
  380.     for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]);
  381.     /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
  382.      * not important since only literal bytes will be emitted.
  383.      */
  384. }
  385.  
  386. /* ===========================================================================
  387.  * Set match_start to the longest match starting at the given string and
  388.  * return its length. Matches shorter or equal to prev_length are discarded,
  389.  * in which case the result is equal to prev_length and match_start is
  390.  * garbage.
  391.  * IN assertions: cur_match is the head of the hash chain for the current
  392.  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  393.  */
  394. #ifndef ASMV
  395. /* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
  396.  * match.s. The code is functionally equivalent, so you can use the C version
  397.  * if desired.
  398.  */
  399. int longest_match(cur_match)
  400.     IPos cur_match;                             /* current match */
  401. {
  402.     unsigned chain_length = max_chain_length;   /* max hash chain length */
  403.     register uch *scan = window + strstart;     /* current string */
  404.     register uch *match;                        /* matched string */
  405.     register int len;                           /* length of current match */
  406.     int best_len = prev_length;                 /* best match length so far */
  407.     IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
  408.     /* Stop when cur_match becomes <= limit. To simplify the code,
  409.      * we prevent matches with the string of window index 0.
  410.      */
  411.  
  412. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  413.  * It is easy to get rid of this optimization if necessary.
  414.  */
  415. #if HASH_BITS < 8 || MAX_MATCH != 258
  416.    error: Code too clever
  417. #endif
  418.  
  419. #ifdef UNALIGNED_OK
  420.     /* Compare two bytes at a time. Note: this is not always beneficial.
  421.      * Try with and without -DUNALIGNED_OK to check.
  422.      */
  423.     register uch *strend = window + strstart + MAX_MATCH - 1;
  424.     register ush scan_start = *(ush*)scan;
  425.     register ush scan_end   = *(ush*)(scan+best_len-1);
  426. #else
  427.     register uch *strend = window + strstart + MAX_MATCH;
  428.     register uch scan_end1  = scan[best_len-1];
  429.     register uch scan_end   = scan[best_len];
  430. #endif
  431.  
  432.     /* Do not waste too much time if we already have a good match: */
  433.     if (prev_length >= good_match) {
  434.         chain_length >>= 2;
  435.     }
  436.     Assert(strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
  437.  
  438.     do {
  439.         Assert(cur_match < strstart, "no future");
  440.         match = window + cur_match;
  441.  
  442.         /* Skip to next match if the match length cannot increase
  443.          * or if the match length is less than 2:
  444.          */
  445. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  446.         /* This code assumes sizeof(unsigned short) == 2. Do not use
  447.          * UNALIGNED_OK if your compiler uses a different size.
  448.          */
  449.         if (*(ush*)(match+best_len-1) != scan_end ||
  450.             *(ush*)match != scan_start) continue;
  451.  
  452.         /* It is not necessary to compare scan[2] and match[2] since they are
  453.          * always equal when the other bytes match, given that the hash keys
  454.          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  455.          * strstart+3, +5, ... up to strstart+257. We check for insufficient
  456.          * lookahead only every 4th comparison; the 128th check will be made
  457.          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  458.          * necessary to put more guard bytes at the end of the window, or
  459.          * to check more often for insufficient lookahead.
  460.          */
  461.         scan++, match++;
  462.         do {
  463.         } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
  464.                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  465.                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  466.                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  467.                  scan < strend);
  468.         /* The funny "do {}" generates better code on most compilers */
  469.  
  470.         /* Here, scan <= window+strstart+257 */
  471.         Assert(scan <= window+(unsigned)(window_size-1), "wild scan");
  472.         if (*scan == *match) scan++;
  473.  
  474.         len = (MAX_MATCH - 1) - (int)(strend-scan);
  475.         scan = strend - (MAX_MATCH-1);
  476.  
  477. #else /* UNALIGNED_OK */
  478.  
  479.         if (match[best_len]   != scan_end  ||
  480.             match[best_len-1] != scan_end1 ||
  481.             *match            != *scan     ||
  482.             *++match          != scan[1])      continue;
  483.  
  484.         /* The check at best_len-1 can be removed because it will be made
  485.          * again later. (This heuristic is not always a win.)
  486.          * It is not necessary to compare scan[2] and match[2] since they
  487.          * are always equal when the other bytes match, given that
  488.          * the hash keys are equal and that HASH_BITS >= 8.
  489.          */
  490.         scan += 2, match++;
  491.  
  492.         /* We check for insufficient lookahead only every 8th comparison;
  493.          * the 256th check will be made at strstart+258.
  494.          */
  495.         do {
  496.         } while (*++scan == *++match && *++scan == *++match &&
  497.                  *++scan == *++match && *++scan == *++match &&
  498.                  *++scan == *++match && *++scan == *++match &&
  499.                  *++scan == *++match && *++scan == *++match &&
  500.                  scan < strend);
  501.  
  502.         len = MAX_MATCH - (int)(strend - scan);
  503.         scan = strend - MAX_MATCH;
  504.  
  505. #endif /* UNALIGNED_OK */
  506.  
  507.         if (len > best_len) {
  508.             match_start = cur_match;
  509.             best_len = len;
  510.             if (len >= nice_match) break;
  511. #ifdef UNALIGNED_OK
  512.             scan_end = *(ush*)(scan+best_len-1);
  513. #else
  514.             scan_end1  = scan[best_len-1];
  515.             scan_end   = scan[best_len];
  516. #endif
  517.         }
  518.     } while ((cur_match = prev[cur_match & WMASK]) > limit
  519.          && --chain_length != 0);
  520.  
  521.     return best_len;
  522. }
  523. #endif /* ASMV */
  524.  
  525. #ifdef DEBUG
  526. /* ===========================================================================
  527.  * Check that the match at match_start is indeed a match.
  528.  */
  529. local void check_match(start, match, length)
  530.     IPos start, match;
  531.     int length;
  532. {
  533.     /* check that the match is indeed a match */
  534.     if (memcmp((char*)window + match,
  535.                 (char*)window + start, length) != EQUAL) {
  536.         fprintf(stderr,
  537.             " start %d, match %d, length %d\n",
  538.             start, match, length);
  539.         fatal("zip","invalid match");
  540.     }
  541.     if (verbose > 1) {
  542.         fprintf(stderr,"\\[%d,%d]", start-match, length);
  543.         do { putc(window[start++], stderr); } while (--length != 0);
  544.     }
  545. }
  546. #else
  547. #  define check_match(start, match, length)
  548. #endif
  549.  
  550. /* ===========================================================================
  551.  * Fill the window when the lookahead becomes insufficient.
  552.  * Updates strstart and lookahead, and sets eofile if end of input file.
  553.  * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
  554.  * OUT assertions: at least one byte has been read, or eofile is set;
  555.  *    file reads are performed for at least two bytes (required for the
  556.  *    translate_eol option).
  557.  */
  558. /* fill_window関数の内部変数*/
  559. static unsigned fill_window_more;
  560. static int fill_window_re_entry_flag=0;
  561.  
  562. local void fill_window()
  563. {
  564.     register unsigned n, m;
  565.     /*static unsigned more;*/
  566.     /*static int re_entry_flag=0;*/
  567.     /* Amount of free space at the end of the window. */
  568.  
  569.      if (fill_window_re_entry_flag)
  570.          goto re_entry;
  571.  
  572.     fill_window_more = (unsigned)(window_size - (ulg)lookahead - (ulg)strstart);
  573.     /* If the window is almost full and there is insufficient lookahead,
  574.      * move the upper half to the lower one to make room in the upper half.
  575.      */
  576.     if (fill_window_more == (unsigned)EOF) {
  577.         /* Very unlikely, but possible on 16 bit machine if strstart == 0
  578.          * and lookahead == 1 (input done one byte at time)
  579.          */
  580.         fill_window_more--;
  581.     } else if (strstart >= WSIZE+MAX_DIST) {
  582.         /* By the IN assertion, the window is not empty so we can't confuse
  583.          * more == 0 with more == 64K on a 16 bit machine.
  584.          */
  585.         Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
  586.  
  587.         memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
  588.         match_start -= WSIZE;
  589.         strstart    -= WSIZE; /* we now have strstart >= MAX_DIST: */
  590.  
  591.         block_start -= (long) WSIZE;
  592.  
  593.         for (n = 0; n < HASH_SIZE; n++) {
  594.             m = head[n];
  595.             head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  596.         }
  597.         for (n = 0; n < WSIZE; n++) {
  598.             m = prev[n];
  599.             prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  600.             /* If n is not on any hash chain, prev[n] is garbage but
  601.              * its value will never be used.
  602.              */
  603.         }
  604.         fill_window_more += WSIZE;
  605.     }
  606. re_entry:
  607.     fill_window_re_entry_flag = 0;
  608.     /* At this point, more >= 2 */
  609.     if (!eofile) {
  610.         n = file_read((char*)window+strstart+lookahead, fill_window_more);
  611.         if (lack_of_data){
  612.            fill_window_re_entry_flag = 1;
  613.            return;
  614.         }
  615.         if (n == 0 || n == (unsigned)EOF) {
  616.             eofile = 1;
  617.         } else {
  618.             lookahead += n;
  619.         }
  620.     }
  621. }
  622.  
  623. /* ===========================================================================
  624.  * Flush the current block, with given end-of-file flag.
  625.  * IN assertion: strstart is set to the end of the current match.
  626.  */
  627. #define FLUSH_BLOCK(eof) \
  628.    flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
  629.                 (char*)NULL, (long)strstart - block_start, (eof))
  630.  
  631. /* ===========================================================================
  632.  * Fiush the inter buffer 
  633.  */
  634. void zip_end_flush(void)
  635. {
  636.     if (lookahead < MIN_LOOKAHEAD  && !eofile)
  637.         fill_window();
  638. }
  639.  
  640.  
  641. /* ===========================================================================
  642.  * Processes a new input file and return its compressed length. This
  643.  * function does not perform lazy evaluationof matches and inserts
  644.  * new strings in the dictionary only for unmatched strings or for short
  645.  * matches. It is used only for the fast compression options.
  646.  */
  647. /* deflate_fast関数の内部変数*/
  648.     static IPos deflate_fast_hash_head; /* head of the hash chain */
  649.     static unsigned deflate_fast_match_length = 0;  /* length of best match */
  650.     static int deflate_fast_flag = 0;
  651. local void deflate_fast()
  652. {
  653.     /*static IPos hash_head;*/ /* head of the hash chain */
  654.     int flush;      /* set if current block must be flushed */
  655.     /*static unsigned match_length = 0;*/  /* length of best match */
  656.     /*static int flag = 0;*/
  657.  
  658.     if (!deflate_fast_flag){
  659.         prev_length = MIN_MATCH-1;
  660.         deflate_fast_flag = 1;
  661.     }
  662. /*    while (lookahead != 0) {*/
  663.         /* Insert the string window[strstart .. strstart+2] in the
  664.          * dictionary, and set hash_head to the head of the hash chain:
  665.          */
  666.         INSERT_STRING(strstart, deflate_fast_hash_head);
  667.  
  668.         /* Find the longest match, discarding those <= prev_length.
  669.          * At this point we have always match_length < MIN_MATCH
  670.          */
  671.         if (deflate_fast_hash_head != NIL && strstart - deflate_fast_hash_head <= MAX_DIST) {
  672.             /* To simplify the code, we prevent matches with the string
  673.              * of window index 0 (in particular we have to avoid a match
  674.              * of the string with itself at the start of the input file).
  675.              */
  676.             deflate_fast_match_length = longest_match (deflate_fast_hash_head);
  677.             /* longest_match() sets match_start */
  678.             if (deflate_fast_match_length > lookahead) deflate_fast_match_length = lookahead;
  679.         }
  680.         if (deflate_fast_match_length >= MIN_MATCH) {
  681.             check_match(strstart, match_start, deflate_fast_match_length);
  682.  
  683.             flush = ct_tally(strstart-match_start, deflate_fast_match_length - MIN_MATCH);
  684.  
  685.             lookahead -= deflate_fast_match_length;
  686.  
  687.         /* Insert new strings in the hash table only if the match length
  688.              * is not too large. This saves time but degrades compression.
  689.              */
  690.             if (deflate_fast_match_length <= max_insert_length) {
  691.                 deflate_fast_match_length--; /* string at strstart already in hash table */
  692.                 do {
  693.                     strstart++;
  694.                     INSERT_STRING(strstart, deflate_fast_hash_head);
  695.                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  696.                      * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  697.                      * these bytes are garbage, but it does not matter since
  698.                      * the next lookahead bytes will be emitted as literals.
  699.                      */
  700.                 } while (--deflate_fast_match_length != 0);
  701.             strstart++; 
  702.             } else {
  703.             strstart += deflate_fast_match_length;
  704.             deflate_fast_match_length = 0;
  705.             ins_h = window[strstart];
  706.             UPDATE_HASH(ins_h, window[strstart+1]);
  707. #if MIN_MATCH != 3
  708.                 Call UPDATE_HASH() MIN_MATCH-3 more times
  709. #endif
  710.             }
  711.         } else {
  712.             /* No match, output a literal byte */
  713.             Tracevv((stderr,"%c",window[strstart]));
  714.             flush = ct_tally (0, window[strstart]);
  715.             lookahead--;
  716.         strstart++; 
  717.         }
  718.         if (flush) FLUSH_BLOCK(0), block_start = strstart;
  719.  
  720.         /* Make sure that we always have enough lookahead, except
  721.          * at the end of the input file. We need MAX_MATCH bytes
  722.          * for the next match, plus MIN_MATCH bytes to insert the
  723.          * string following the next match.
  724.          */
  725. #if 0
  726.         while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  727.  
  728.     }
  729.     return FLUSH_BLOCK(1); /* eof */
  730. #endif
  731. }
  732.  
  733. /* ===========================================================================
  734.  * Same as above, but achieves better compression. We use a lazy
  735.  * evaluation for matches: a match is finally adopted only if there is
  736.  * no better match at the next window position.
  737.  */
  738. static int match_available = 0; /* set if previous match exists */
  739. void deflate_sub(void);
  740.  
  741. ulg deflate()
  742. {
  743.    while(lookahead != 0) {
  744.        while (lookahead < MIN_LOOKAHEAD && !eofile){
  745.         fill_window();
  746.         if (lack_of_data)
  747.            return 0;
  748.        }
  749.       if (compr_level <= 3){
  750.           deflate_fast(); /* optimized for speed */
  751.       }else
  752.           deflate_sub();  /* optimized for space */
  753.     }
  754.     if (compr_level > 3)
  755.       if (match_available) ct_tally (0, window[strstart-1]);
  756.  
  757.     return FLUSH_BLOCK(1); /* eof */
  758. }
  759.  
  760. /* deflate_sub関数の内部変数*/
  761.    static IPos deflate_sub_hash_head=0;          /* head of hash chain */
  762.     static IPos deflate_sub_prev_match=0;         /* previous match */
  763.     static unsigned deflate_sub_match_length = MIN_MATCH-1; /* length of best match */
  764.  
  765. void deflate_sub()
  766. {
  767.     /*static IPos hash_head=0;*/          /* head of hash chain */
  768.     /*static IPos prev_match=0; */        /* previous match */
  769.     int flush=0;               /* set if current block must be flushed */
  770.     /*static unsigned match_length = MIN_MATCH-1;*/ /* length of best match */
  771. #ifdef DEBUG
  772.     extern long isize;        /* byte length of input file, for debug only */
  773. #endif
  774.  
  775.     /* Process the input block. */
  776. /*    while (lookahead != 0) {*/
  777.         /* Insert the string window[strstart .. strstart+2] in the
  778.          * dictionary, and set hash_head to the head of the hash chain:
  779.          */
  780.         INSERT_STRING(strstart, deflate_sub_hash_head);
  781.  
  782.         /* Find the longest match, discarding those <= prev_length.
  783.          */
  784.         prev_length = deflate_sub_match_length, deflate_sub_prev_match = match_start;
  785.         deflate_sub_match_length = MIN_MATCH-1;
  786.  
  787.         if (deflate_sub_hash_head != NIL && prev_length < max_lazy_match &&
  788.             strstart - deflate_sub_hash_head <= MAX_DIST) {
  789.             /* To simplify the code, we prevent matches with the string
  790.              * of window index 0 (in particular we have to avoid a match
  791.              * of the string with itself at the start of the input file).
  792.              */
  793.             deflate_sub_match_length = longest_match (deflate_sub_hash_head);
  794.             /* longest_match() sets match_start */
  795.             if (deflate_sub_match_length > lookahead) deflate_sub_match_length = lookahead;
  796.  
  797.             /* Ignore a length 3 match if it is too distant: */
  798.             if (deflate_sub_match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
  799.                 /* If prev_match is also MIN_MATCH, match_start is garbage
  800.                  * but we will ignore the current match anyway.
  801.                  */
  802.                 deflate_sub_match_length--;
  803.             }
  804.         }
  805.         /* If there was a match at the previous step and the current
  806.          * match is not better, output the previous match:
  807.          */
  808.         if (prev_length >= MIN_MATCH && deflate_sub_match_length <= prev_length) {
  809.  
  810.             check_match(strstart-1, deflate_sub_prev_match, prev_length);
  811.  
  812.             flush = ct_tally(strstart-1-deflate_sub_prev_match, prev_length - MIN_MATCH);
  813.  
  814.             /* Insert in hash table all strings up to the end of the match.
  815.              * strstart-1 and strstart are already inserted.
  816.              */
  817.             lookahead -= prev_length-1;
  818.             prev_length -= 2;
  819.             do {
  820.                 strstart++;
  821.                 INSERT_STRING(strstart, deflate_sub_hash_head);
  822.                 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  823.                  * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  824.                  * these bytes are garbage, but it does not matter since the
  825.                  * next lookahead bytes will always be emitted as literals.
  826.                  */
  827.             } while (--prev_length != 0);
  828.             match_available = 0;
  829.             deflate_sub_match_length = MIN_MATCH-1;
  830.             strstart++;
  831.             if (flush) FLUSH_BLOCK(0), block_start = strstart;
  832.  
  833.         } else if (match_available) {
  834.             /* If there was no match at the previous position, output a
  835.              * single literal. If there was a match but the current match
  836.              * is longer, truncate the previous match to a single literal.
  837.              */
  838.             Tracevv((stderr,"%c",window[strstart-1]));
  839.             if (ct_tally (0, window[strstart-1])) {
  840.                 FLUSH_BLOCK(0), block_start = strstart;
  841.             }
  842.             strstart++;
  843.             lookahead--;
  844.         } else {
  845.             /* There is no previous match to compare with, wait for
  846.              * the next step to decide.
  847.              */
  848.             match_available = 1;
  849.             strstart++;
  850.             lookahead--;
  851.         }
  852.         Assert (strstart <= isize && lookahead <= isize, "a bit too far");
  853.  
  854.         /* Make sure that we always have enough lookahead, except
  855.          * at the end of the input file. We need MAX_MATCH bytes
  856.          * for the next match, plus MIN_MATCH bytes to insert the
  857.          * string following the next match.
  858.          */
  859. #if 0
  860.         while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  861.     }
  862.     if (match_available) ct_tally (0, window[strstart-1]);
  863.  
  864.     return FLUSH_BLOCK(1); /* eof */
  865. #endif
  866.  
  867. }
  868. void deflate_static_init(void)
  869. {
  870.     window_size = (ulg)2*WSIZE;
  871. /* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
  872.  * input file length plus MIN_LOOKAHEAD.
  873.  */
  874.  
  875.     block_start=0;
  876. /* window position at the beginning of the current output block. Gets
  877.  * negative when the window is moved backwards.
  878.  */
  879.  
  880.     ins_h=0;  /* hash index of string to be inserted */
  881.  
  882. /*#define H_SHIFT  ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)*/
  883. /* Number of bits by which ins_h and del_h must be shifted at each
  884.  * input step. It must be such that after MIN_MATCH steps, the oldest
  885.  * byte no longer takes part in the hash key, that is:
  886.  *   H_SHIFT * MIN_MATCH >= HASH_BITS
  887.  */
  888.  
  889.     prev_length=0;
  890. /* Length of the best match at previous step. Matches not greater than this
  891.  * are discarded. This is used in the lazy match evaluation.
  892.  */
  893.  
  894.     strstart=0;      /* start of string to insert */
  895.     match_start=0;   /* start of matching string */
  896.     eofile=0;        /* flag set at end of input file */
  897.     lookahead=0;     /* number of valid bytes ahead in window */
  898.  
  899.     max_chain_length=0;
  900. /* To speed up deflation, hash chains are never searched beyond this length.
  901.  * A higher limit improves compression ratio but degrades the speed.
  902.  */
  903.  
  904.     max_lazy_match=0;
  905. /* Attempt to find a better match only when the current match is strictly
  906.  * smaller than this value. This mechanism is used only for compression
  907.  * levels >= 4.
  908.  */
  909. /* #define max_insert_length  max_lazy_match*/
  910. /* Insert new strings in the hash table only if the match length
  911.  * is not greater than this length. This saves time but degrades compression.
  912.  * max_insert_length is used only for compression levels <= 3.
  913.  */
  914.  
  915.     compr_level=0;
  916. /* compression level (1..9) */
  917.  
  918.     good_match=0;
  919. /* Use a faster search when the previous match is longer than this */
  920. #ifdef  FULL_SEARCH
  921. /* # define nice_match MAX_MATCH*/
  922. #else
  923.     nice_match=0; /* Stop searching when current match exceeds this */
  924. #endif
  925. /* fill_window関数の内部変数*/
  926.     fill_window_more=0;
  927.     fill_window_re_entry_flag=0;
  928.  
  929. /* deflate_fast関数の内部変数*/
  930.     deflate_fast_hash_head=0; /* head of the hash chain */
  931.     deflate_fast_match_length = 0;  /* length of best match */
  932.     deflate_fast_flag = 0;
  933.  
  934.     match_available = 0; /* set if previous match exists */
  935.  
  936. /* deflate_sub関数の内部変数*/
  937.    deflate_sub_hash_head=0;          /* head of hash chain */
  938.    deflate_sub_prev_match=0;         /* previous match */
  939.    deflate_sub_match_length = MIN_MATCH-1; /* length of best match */
  940.  
  941.  
  942. }