gzip 1.2.4
authorJean-loup Gailly <jloup@chorus.fr>
Fri, 13 Aug 1993 12:35:31 +0000 (12:35 +0000)
committerJean-loup Gailly <jloup@chorus.fr>
Fri, 13 Aug 1993 12:35:31 +0000 (12:35 +0000)
deflate.c [new file with mode: 0644]
lzw.c [new file with mode: 0644]
unlzh.c [new file with mode: 0644]
unpack.c [new file with mode: 0644]
unzip.c [new file with mode: 0644]
util.c [new file with mode: 0644]
zip.c [new file with mode: 0644]

diff --git a/deflate.c b/deflate.c
new file mode 100644 (file)
index 0000000..d818248
--- /dev/null
+++ b/deflate.c
@@ -0,0 +1,763 @@
+/* deflate.c -- compress data using the deflation algorithm
+ * Copyright (C) 1992-1993 Jean-loup Gailly
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License, see the file COPYING.
+ */
+
+/*
+ *  PURPOSE
+ *
+ *      Identify new text as repetitions of old text within a fixed-
+ *      length sliding window trailing behind the new text.
+ *
+ *  DISCUSSION
+ *
+ *      The "deflation" process depends on being able to identify portions
+ *      of the input text which are identical to earlier input (within a
+ *      sliding window trailing behind the input currently being processed).
+ *
+ *      The most straightforward technique turns out to be the fastest for
+ *      most input files: try all possible matches and select the longest.
+ *      The key feature of this algorithm is that insertions into the string
+ *      dictionary are very simple and thus fast, and deletions are avoided
+ *      completely. Insertions are performed at each input character, whereas
+ *      string matches are performed only when the previous match ends. So it
+ *      is preferable to spend more time in matches to allow very fast string
+ *      insertions and avoid deletions. The matching algorithm for small
+ *      strings is inspired from that of Rabin & Karp. A brute force approach
+ *      is used to find longer strings when a small match has been found.
+ *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
+ *      (by Leonid Broukhis).
+ *         A previous version of this file used a more sophisticated algorithm
+ *      (by Fiala and Greene) which is guaranteed to run in linear amortized
+ *      time, but has a larger average cost, uses more memory and is patented.
+ *      However the F&G algorithm may be faster for some highly redundant
+ *      files if the parameter max_chain_length (described below) is too large.
+ *
+ *  ACKNOWLEDGEMENTS
+ *
+ *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
+ *      I found it in 'freeze' written by Leonid Broukhis.
+ *      Thanks to many info-zippers for bug reports and testing.
+ *
+ *  REFERENCES
+ *
+ *      APPNOTE.TXT documentation file in PKZIP 1.93a distribution.
+ *
+ *      A description of the Rabin and Karp algorithm is given in the book
+ *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
+ *
+ *      Fiala,E.R., and Greene,D.H.
+ *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
+ *
+ *  INTERFACE
+ *
+ *      void lm_init (int pack_level, ush *flags)
+ *          Initialize the "longest match" routines for a new file
+ *
+ *      ulg deflate (void)
+ *          Processes a new input file and return its compressed length. Sets
+ *          the compressed length, crc, deflate flags and internal file
+ *          attributes.
+ */
+
+#include <stdio.h>
+
+#include "tailor.h"
+#include "gzip.h"
+#include "lzw.h" /* just for consistency checking */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+/* ===========================================================================
+ * Configuration parameters
+ */
+
+/* Compile with MEDIUM_MEM to reduce the memory requirements or
+ * with SMALL_MEM to use as little memory as possible. Use BIG_MEM if the
+ * entire input file can be held in memory (not possible on 16 bit systems).
+ * Warning: defining these symbols affects HASH_BITS (see below) and thus
+ * affects the compression ratio. The compressed output
+ * is still correct, and might even be smaller in some cases.
+ */
+
+#ifdef SMALL_MEM
+#   define HASH_BITS  13  /* Number of bits used to hash strings */
+#endif
+#ifdef MEDIUM_MEM
+#   define HASH_BITS  14
+#endif
+#ifndef HASH_BITS
+#   define HASH_BITS  15
+   /* For portability to 16 bit machines, do not use values above 15. */
+#endif
+
+/* To save space (see unlzw.c), we overlay prev+head with tab_prefix and
+ * window with tab_suffix. Check that we can do this:
+ */
+#if (WSIZE<<1) > (1<<BITS)
+   error: cannot overlay window with tab_suffix and prev with tab_prefix0
+#endif
+#if HASH_BITS > BITS-1
+   error: cannot overlay head with tab_prefix1
+#endif
+
+#define HASH_SIZE (unsigned)(1<<HASH_BITS)
+#define HASH_MASK (HASH_SIZE-1)
+#define WMASK     (WSIZE-1)
+/* HASH_SIZE and WSIZE must be powers of two */
+
+#define NIL 0
+/* Tail of hash chains */
+
+#define FAST 4
+#define SLOW 2
+/* speed options for the general purpose bit flag */
+
+#ifndef TOO_FAR
+#  define TOO_FAR 4096
+#endif
+/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
+
+/* ===========================================================================
+ * Local data used by the "longest match" routines.
+ */
+
+typedef ush Pos;
+typedef unsigned IPos;
+/* A Pos is an index in the character window. We use short instead of int to
+ * save space in the various tables. IPos is used only for parameter passing.
+ */
+
+/* DECLARE(uch, window, 2L*WSIZE); */
+/* Sliding window. Input bytes are read into the second half of the window,
+ * and move to the first half later to keep a dictionary of at least WSIZE
+ * bytes. With this organization, matches are limited to a distance of
+ * WSIZE-MAX_MATCH bytes, but this ensures that IO is always
+ * performed with a length multiple of the block size. Also, it limits
+ * the window size to 64K, which is quite useful on MSDOS.
+ * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would
+ * be less efficient).
+ */
+
+/* DECLARE(Pos, prev, WSIZE); */
+/* Link to older string with same hash index. To limit the size of this
+ * array to 64K, this link is maintained only for the last 32K strings.
+ * An index in this array is thus a window index modulo 32K.
+ */
+
+/* DECLARE(Pos, head, 1<<HASH_BITS); */
+/* Heads of the hash chains or NIL. */
+
+ulg window_size = (ulg)2*WSIZE;
+/* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
+ * input file length plus MIN_LOOKAHEAD.
+ */
+
+long block_start;
+/* window position at the beginning of the current output block. Gets
+ * negative when the window is moved backwards.
+ */
+
+local unsigned ins_h;  /* hash index of string to be inserted */
+
+#define H_SHIFT  ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
+/* Number of bits by which ins_h and del_h must be shifted at each
+ * input step. It must be such that after MIN_MATCH steps, the oldest
+ * byte no longer takes part in the hash key, that is:
+ *   H_SHIFT * MIN_MATCH >= HASH_BITS
+ */
+
+unsigned int near prev_length;
+/* Length of the best match at previous step. Matches not greater than this
+ * are discarded. This is used in the lazy match evaluation.
+ */
+
+      unsigned near strstart;      /* start of string to insert */
+      unsigned near match_start;   /* start of matching string */
+local int           eofile;        /* flag set at end of input file */
+local unsigned      lookahead;     /* number of valid bytes ahead in window */
+
+unsigned near max_chain_length;
+/* To speed up deflation, hash chains are never searched beyond this length.
+ * A higher limit improves compression ratio but degrades the speed.
+ */
+
+local unsigned int max_lazy_match;
+/* Attempt to find a better match only when the current match is strictly
+ * smaller than this value. This mechanism is used only for compression
+ * levels >= 4.
+ */
+#define max_insert_length  max_lazy_match
+/* Insert new strings in the hash table only if the match length
+ * is not greater than this length. This saves time but degrades compression.
+ * max_insert_length is used only for compression levels <= 3.
+ */
+
+local int compr_level;
+/* compression level (1..9) */
+
+unsigned near good_match;
+/* Use a faster search when the previous match is longer than this */
+
+
+/* Values for max_lazy_match, good_match and max_chain_length, depending on
+ * the desired pack level (0..9). The values given below have been tuned to
+ * exclude worst case performance for pathological files. Better values may be
+ * found for specific files.
+ */
+
+typedef struct config {
+   ush good_length; /* reduce lazy search above this match length */
+   ush max_lazy;    /* do not perform lazy search above this match length */
+   ush nice_length; /* quit search above this match length */
+   ush max_chain;
+} config;
+
+#ifdef  FULL_SEARCH
+# define nice_match MAX_MATCH
+#else
+  int near nice_match; /* Stop searching when current match exceeds this */
+#endif
+
+local config configuration_table[10] = {
+/*      good lazy nice chain */
+/* 0 */ {0,    0,  0,    0},  /* store only */
+/* 1 */ {4,    4,  8,    4},  /* maximum speed, no lazy matches */
+/* 2 */ {4,    5, 16,    8},
+/* 3 */ {4,    6, 32,   32},
+
+/* 4 */ {4,    4, 16,   16},  /* lazy matches */
+/* 5 */ {8,   16, 32,   32},
+/* 6 */ {8,   16, 128, 128},
+/* 7 */ {8,   32, 128, 256},
+/* 8 */ {32, 128, 258, 1024},
+/* 9 */ {32, 258, 258, 4096}}; /* maximum compression */
+
+/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
+ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
+ * meaning.
+ */
+
+#define EQUAL 0
+/* result of memcmp for equal strings */
+
+/* ===========================================================================
+ *  Prototypes for local functions.
+ */
+local void fill_window   OF((void));
+local ulg deflate_fast   OF((void));
+
+      int  longest_match OF((IPos cur_match));
+#ifdef ASMV
+      void match_init OF((void)); /* asm code initialization */
+#endif
+
+#ifdef DEBUG
+local  void check_match OF((IPos start, IPos match, int length));
+#endif
+
+/* ===========================================================================
+ * Update a hash value with the given input byte
+ * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
+ *    input characters, so that a running hash key can be computed from the
+ *    previous key instead of complete recalculation each time.
+ */
+#define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
+
+/* ===========================================================================
+ * Insert string s in the dictionary and set match_head to the previous head
+ * of the hash chain (the most recent string with same hash key). Return
+ * the previous length of the hash chain.
+ * IN  assertion: all calls to to INSERT_STRING are made with consecutive
+ *    input characters and the first MIN_MATCH bytes of s are valid
+ *    (except for the last MIN_MATCH-1 bytes of the input file).
+ */
+#define INSERT_STRING(s, match_head) \
+   (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \
+    prev[(s) & WMASK] = match_head = head[ins_h], \
+    head[ins_h] = (s))
+
+/* ===========================================================================
+ * Initialize the "longest match" routines for a new file
+ */
+void lm_init (pack_level, flags)
+    int pack_level; /* 0: store, 1: best speed, 9: best compression */
+    ush *flags;     /* general purpose bit flag */
+{
+    register unsigned j;
+
+    if (pack_level < 1 || pack_level > 9) error("bad pack level");
+    compr_level = pack_level;
+
+    /* Initialize the hash table. */
+#if defined(MAXSEG_64K) && HASH_BITS == 15
+    for (j = 0;  j < HASH_SIZE; j++) head[j] = NIL;
+#else
+    memzero((char*)head, HASH_SIZE*sizeof(*head));
+#endif
+    /* prev will be initialized on the fly */
+
+    /* Set the default configuration parameters:
+     */
+    max_lazy_match   = configuration_table[pack_level].max_lazy;
+    good_match       = configuration_table[pack_level].good_length;
+#ifndef FULL_SEARCH
+    nice_match       = configuration_table[pack_level].nice_length;
+#endif
+    max_chain_length = configuration_table[pack_level].max_chain;
+    if (pack_level == 1) {
+       *flags |= FAST;
+    } else if (pack_level == 9) {
+       *flags |= SLOW;
+    }
+    /* ??? reduce max_chain_length for binary files */
+
+    strstart = 0;
+    block_start = 0L;
+#ifdef ASMV
+    match_init(); /* initialize the asm code */
+#endif
+
+    lookahead = read_buf((char*)window,
+                        sizeof(int) <= 2 ? (unsigned)WSIZE : 2*WSIZE);
+
+    if (lookahead == 0 || lookahead == (unsigned)EOF) {
+       eofile = 1, lookahead = 0;
+       return;
+    }
+    eofile = 0;
+    /* Make sure that we always have enough lookahead. This is important
+     * if input comes from a device such as a tty.
+     */
+    while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
+
+    ins_h = 0;
+    for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]);
+    /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
+     * not important since only literal bytes will be emitted.
+     */
+}
+
+/* ===========================================================================
+ * Set match_start to the longest match starting at the given string and
+ * return its length. Matches shorter or equal to prev_length are discarded,
+ * in which case the result is equal to prev_length and match_start is
+ * garbage.
+ * IN assertions: cur_match is the head of the hash chain for the current
+ *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
+ */
+#ifndef ASMV
+/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
+ * match.s. The code is functionally equivalent, so you can use the C version
+ * if desired.
+ */
+int longest_match(cur_match)
+    IPos cur_match;                             /* current match */
+{
+    unsigned chain_length = max_chain_length;   /* max hash chain length */
+    register uch *scan = window + strstart;     /* current string */
+    register uch *match;                        /* matched string */
+    register int len;                           /* length of current match */
+    int best_len = prev_length;                 /* best match length so far */
+    IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
+    /* Stop when cur_match becomes <= limit. To simplify the code,
+     * we prevent matches with the string of window index 0.
+     */
+
+/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+ * It is easy to get rid of this optimization if necessary.
+ */
+#if HASH_BITS < 8 || MAX_MATCH != 258
+   error: Code too clever
+#endif
+
+#ifdef UNALIGNED_OK
+    /* Compare two bytes at a time. Note: this is not always beneficial.
+     * Try with and without -DUNALIGNED_OK to check.
+     */
+    register uch *strend = window + strstart + MAX_MATCH - 1;
+    register ush scan_start = *(ush*)scan;
+    register ush scan_end   = *(ush*)(scan+best_len-1);
+#else
+    register uch *strend = window + strstart + MAX_MATCH;
+    register uch scan_end1  = scan[best_len-1];
+    register uch scan_end   = scan[best_len];
+#endif
+
+    /* Do not waste too much time if we already have a good match: */
+    if (prev_length >= good_match) {
+        chain_length >>= 2;
+    }
+    Assert(strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");
+
+    do {
+        Assert(cur_match < strstart, "no future");
+        match = window + cur_match;
+
+        /* Skip to next match if the match length cannot increase
+         * or if the match length is less than 2:
+         */
+#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
+        /* This code assumes sizeof(unsigned short) == 2. Do not use
+         * UNALIGNED_OK if your compiler uses a different size.
+         */
+        if (*(ush*)(match+best_len-1) != scan_end ||
+            *(ush*)match != scan_start) continue;
+
+        /* It is not necessary to compare scan[2] and match[2] since they are
+         * always equal when the other bytes match, given that the hash keys
+         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
+         * strstart+3, +5, ... up to strstart+257. We check for insufficient
+         * lookahead only every 4th comparison; the 128th check will be made
+         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
+         * necessary to put more guard bytes at the end of the window, or
+         * to check more often for insufficient lookahead.
+         */
+        scan++, match++;
+        do {
+        } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
+                 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
+                 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
+                 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
+                 scan < strend);
+        /* The funny "do {}" generates better code on most compilers */
+
+        /* Here, scan <= window+strstart+257 */
+        Assert(scan <= window+(unsigned)(window_size-1), "wild scan");
+        if (*scan == *match) scan++;
+
+        len = (MAX_MATCH - 1) - (int)(strend-scan);
+        scan = strend - (MAX_MATCH-1);
+
+#else /* UNALIGNED_OK */
+
+        if (match[best_len]   != scan_end  ||
+            match[best_len-1] != scan_end1 ||
+            *match            != *scan     ||
+            *++match          != scan[1])      continue;
+
+        /* The check at best_len-1 can be removed because it will be made
+         * again later. (This heuristic is not always a win.)
+         * It is not necessary to compare scan[2] and match[2] since they
+         * are always equal when the other bytes match, given that
+         * the hash keys are equal and that HASH_BITS >= 8.
+         */
+        scan += 2, match++;
+
+        /* We check for insufficient lookahead only every 8th comparison;
+         * the 256th check will be made at strstart+258.
+         */
+        do {
+        } while (*++scan == *++match && *++scan == *++match &&
+                 *++scan == *++match && *++scan == *++match &&
+                 *++scan == *++match && *++scan == *++match &&
+                 *++scan == *++match && *++scan == *++match &&
+                 scan < strend);
+
+        len = MAX_MATCH - (int)(strend - scan);
+        scan = strend - MAX_MATCH;
+
+#endif /* UNALIGNED_OK */
+
+        if (len > best_len) {
+            match_start = cur_match;
+            best_len = len;
+            if (len >= nice_match) break;
+#ifdef UNALIGNED_OK
+            scan_end = *(ush*)(scan+best_len-1);
+#else
+            scan_end1  = scan[best_len-1];
+            scan_end   = scan[best_len];
+#endif
+        }
+    } while ((cur_match = prev[cur_match & WMASK]) > limit
+            && --chain_length != 0);
+
+    return best_len;
+}
+#endif /* ASMV */
+
+#ifdef DEBUG
+/* ===========================================================================
+ * Check that the match at match_start is indeed a match.
+ */
+local void check_match(start, match, length)
+    IPos start, match;
+    int length;
+{
+    /* check that the match is indeed a match */
+    if (memcmp((char*)window + match,
+                (char*)window + start, length) != EQUAL) {
+        fprintf(stderr,
+            " start %d, match %d, length %d\n",
+            start, match, length);
+        error("invalid match");
+    }
+    if (verbose > 1) {
+        fprintf(stderr,"\\[%d,%d]", start-match, length);
+        do { putc(window[start++], stderr); } while (--length != 0);
+    }
+}
+#else
+#  define check_match(start, match, length)
+#endif
+
+/* ===========================================================================
+ * Fill the window when the lookahead becomes insufficient.
+ * Updates strstart and lookahead, and sets eofile if end of input file.
+ * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
+ * OUT assertions: at least one byte has been read, or eofile is set;
+ *    file reads are performed for at least two bytes (required for the
+ *    translate_eol option).
+ */
+local void fill_window()
+{
+    register unsigned n, m;
+    unsigned more = (unsigned)(window_size - (ulg)lookahead - (ulg)strstart);
+    /* Amount of free space at the end of the window. */
+
+    /* If the window is almost full and there is insufficient lookahead,
+     * move the upper half to the lower one to make room in the upper half.
+     */
+    if (more == (unsigned)EOF) {
+        /* Very unlikely, but possible on 16 bit machine if strstart == 0
+         * and lookahead == 1 (input done one byte at time)
+         */
+        more--;
+    } else if (strstart >= WSIZE+MAX_DIST) {
+        /* By the IN assertion, the window is not empty so we can't confuse
+         * more == 0 with more == 64K on a 16 bit machine.
+         */
+        Assert(window_size == (ulg)2*WSIZE, "no sliding with BIG_MEM");
+
+        memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
+        match_start -= WSIZE;
+        strstart    -= WSIZE; /* we now have strstart >= MAX_DIST: */
+
+        block_start -= (long) WSIZE;
+
+        for (n = 0; n < HASH_SIZE; n++) {
+            m = head[n];
+            head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
+        }
+        for (n = 0; n < WSIZE; n++) {
+            m = prev[n];
+            prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
+            /* If n is not on any hash chain, prev[n] is garbage but
+             * its value will never be used.
+             */
+        }
+        more += WSIZE;
+    }
+    /* At this point, more >= 2 */
+    if (!eofile) {
+        n = read_buf((char*)window+strstart+lookahead, more);
+        if (n == 0 || n == (unsigned)EOF) {
+            eofile = 1;
+        } else {
+            lookahead += n;
+        }
+    }
+}
+
+/* ===========================================================================
+ * Flush the current block, with given end-of-file flag.
+ * IN assertion: strstart is set to the end of the current match.
+ */
+#define FLUSH_BLOCK(eof) \
+   flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
+                (char*)NULL, (long)strstart - block_start, (eof))
+
+/* ===========================================================================
+ * Processes a new input file and return its compressed length. This
+ * function does not perform lazy evaluationof matches and inserts
+ * new strings in the dictionary only for unmatched strings or for short
+ * matches. It is used only for the fast compression options.
+ */
+local ulg deflate_fast()
+{
+    IPos hash_head; /* head of the hash chain */
+    int flush;      /* set if current block must be flushed */
+    unsigned match_length = 0;  /* length of best match */
+
+    prev_length = MIN_MATCH-1;
+    while (lookahead != 0) {
+        /* Insert the string window[strstart .. strstart+2] in the
+         * dictionary, and set hash_head to the head of the hash chain:
+         */
+        INSERT_STRING(strstart, hash_head);
+
+        /* Find the longest match, discarding those <= prev_length.
+         * At this point we have always match_length < MIN_MATCH
+         */
+        if (hash_head != NIL && strstart - hash_head <= MAX_DIST) {
+            /* To simplify the code, we prevent matches with the string
+             * of window index 0 (in particular we have to avoid a match
+             * of the string with itself at the start of the input file).
+             */
+            match_length = longest_match (hash_head);
+            /* longest_match() sets match_start */
+            if (match_length > lookahead) match_length = lookahead;
+        }
+        if (match_length >= MIN_MATCH) {
+            check_match(strstart, match_start, match_length);
+
+            flush = ct_tally(strstart-match_start, match_length - MIN_MATCH);
+
+            lookahead -= match_length;
+
+           /* Insert new strings in the hash table only if the match length
+             * is not too large. This saves time but degrades compression.
+             */
+            if (match_length <= max_insert_length) {
+                match_length--; /* string at strstart already in hash table */
+                do {
+                    strstart++;
+                    INSERT_STRING(strstart, hash_head);
+                    /* strstart never exceeds WSIZE-MAX_MATCH, so there are
+                     * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
+                     * these bytes are garbage, but it does not matter since
+                     * the next lookahead bytes will be emitted as literals.
+                     */
+                } while (--match_length != 0);
+               strstart++; 
+            } else {
+               strstart += match_length;
+               match_length = 0;
+               ins_h = window[strstart];
+               UPDATE_HASH(ins_h, window[strstart+1]);
+#if MIN_MATCH != 3
+                Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+            }
+        } else {
+            /* No match, output a literal byte */
+            Tracevv((stderr,"%c",window[strstart]));
+            flush = ct_tally (0, window[strstart]);
+            lookahead--;
+           strstart++; 
+        }
+        if (flush) FLUSH_BLOCK(0), block_start = strstart;
+
+        /* Make sure that we always have enough lookahead, except
+         * at the end of the input file. We need MAX_MATCH bytes
+         * for the next match, plus MIN_MATCH bytes to insert the
+         * string following the next match.
+         */
+        while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
+
+    }
+    return FLUSH_BLOCK(1); /* eof */
+}
+
+/* ===========================================================================
+ * Same as above, but achieves better compression. We use a lazy
+ * evaluation for matches: a match is finally adopted only if there is
+ * no better match at the next window position.
+ */
+ulg deflate()
+{
+    IPos hash_head;          /* head of hash chain */
+    IPos prev_match;         /* previous match */
+    int flush;               /* set if current block must be flushed */
+    int match_available = 0; /* set if previous match exists */
+    register unsigned match_length = MIN_MATCH-1; /* length of best match */
+#ifdef DEBUG
+    extern long isize;        /* byte length of input file, for debug only */
+#endif
+
+    if (compr_level <= 3) return deflate_fast(); /* optimized for speed */
+
+    /* Process the input block. */
+    while (lookahead != 0) {
+        /* Insert the string window[strstart .. strstart+2] in the
+         * dictionary, and set hash_head to the head of the hash chain:
+         */
+        INSERT_STRING(strstart, hash_head);
+
+        /* Find the longest match, discarding those <= prev_length.
+         */
+        prev_length = match_length, prev_match = match_start;
+        match_length = MIN_MATCH-1;
+
+        if (hash_head != NIL && prev_length < max_lazy_match &&
+            strstart - hash_head <= MAX_DIST) {
+            /* To simplify the code, we prevent matches with the string
+             * of window index 0 (in particular we have to avoid a match
+             * of the string with itself at the start of the input file).
+             */
+            match_length = longest_match (hash_head);
+            /* longest_match() sets match_start */
+            if (match_length > lookahead) match_length = lookahead;
+
+            /* Ignore a length 3 match if it is too distant: */
+            if (match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
+                /* If prev_match is also MIN_MATCH, match_start is garbage
+                 * but we will ignore the current match anyway.
+                 */
+                match_length--;
+            }
+        }
+        /* If there was a match at the previous step and the current
+         * match is not better, output the previous match:
+         */
+        if (prev_length >= MIN_MATCH && match_length <= prev_length) {
+
+            check_match(strstart-1, prev_match, prev_length);
+
+            flush = ct_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
+
+            /* Insert in hash table all strings up to the end of the match.
+             * strstart-1 and strstart are already inserted.
+             */
+            lookahead -= prev_length-1;
+            prev_length -= 2;
+            do {
+                strstart++;
+                INSERT_STRING(strstart, hash_head);
+                /* strstart never exceeds WSIZE-MAX_MATCH, so there are
+                 * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
+                 * these bytes are garbage, but it does not matter since the
+                 * next lookahead bytes will always be emitted as literals.
+                 */
+            } while (--prev_length != 0);
+            match_available = 0;
+            match_length = MIN_MATCH-1;
+            strstart++;
+            if (flush) FLUSH_BLOCK(0), block_start = strstart;
+
+        } else if (match_available) {
+            /* If there was no match at the previous position, output a
+             * single literal. If there was a match but the current match
+             * is longer, truncate the previous match to a single literal.
+             */
+            Tracevv((stderr,"%c",window[strstart-1]));
+            if (ct_tally (0, window[strstart-1])) {
+                FLUSH_BLOCK(0), block_start = strstart;
+            }
+            strstart++;
+            lookahead--;
+        } else {
+            /* There is no previous match to compare with, wait for
+             * the next step to decide.
+             */
+            match_available = 1;
+            strstart++;
+            lookahead--;
+        }
+        Assert (strstart <= isize && lookahead <= isize, "a bit too far");
+
+        /* Make sure that we always have enough lookahead, except
+         * at the end of the input file. We need MAX_MATCH bytes
+         * for the next match, plus MIN_MATCH bytes to insert the
+         * string following the next match.
+         */
+        while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
+    }
+    if (match_available) ct_tally (0, window[strstart-1]);
+
+    return FLUSH_BLOCK(1); /* eof */
+}
diff --git a/lzw.c b/lzw.c
new file mode 100644 (file)
index 0000000..eb67d46
--- /dev/null
+++ b/lzw.c
@@ -0,0 +1,26 @@
+/* lzw.c -- compress files in LZW format.
+ * This is a dummy version avoiding patent problems.
+ */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+#include "tailor.h"
+#include "gzip.h"
+#include "lzw.h"
+
+static int msg_done = 0;
+
+/* Compress in to out with lzw method. */
+int lzw(in, out)
+    int in, out;
+{
+    if (msg_done) return ERROR;
+    msg_done = 1;
+    fprintf(stderr,"output in compress .Z format not supported\n");
+    if (in != out) { /* avoid warnings on unused variables */
+        exit_code = ERROR;
+    }
+    return ERROR;
+}
diff --git a/unlzh.c b/unlzh.c
new file mode 100644 (file)
index 0000000..f639911
--- /dev/null
+++ b/unlzh.c
@@ -0,0 +1,401 @@
+/* unlzh.c -- decompress files in SCO compress -H (LZH) format.
+ * The code in this file is directly derived from the public domain 'ar002'
+ * written by Haruhiko Okumura.
+ */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+#include <stdio.h>
+
+#include "tailor.h"
+#include "gzip.h"
+#include "lzw.h" /* just for consistency checking */
+
+/* decode.c */
+
+local unsigned  decode  OF((unsigned count, uch buffer[]));
+local void decode_start OF((void));
+
+/* huf.c */
+local void huf_decode_start OF((void));
+local unsigned decode_c     OF((void));
+local unsigned decode_p     OF((void));
+local void read_pt_len      OF((int nn, int nbit, int i_special));
+local void read_c_len       OF((void));
+
+/* io.c */
+local void fillbuf      OF((int n));
+local unsigned getbits  OF((int n));
+local void init_getbits OF((void));
+
+/* maketbl.c */
+
+local void make_table OF((int nchar, uch bitlen[],
+                         int tablebits, ush table[]));
+
+
+#define DICBIT    13    /* 12(-lh4-) or 13(-lh5-) */
+#define DICSIZ ((unsigned) 1 << DICBIT)
+
+#ifndef CHAR_BIT
+#  define CHAR_BIT 8
+#endif
+
+#ifndef UCHAR_MAX
+#  define UCHAR_MAX 255
+#endif
+
+#define BITBUFSIZ (CHAR_BIT * 2 * sizeof(char))
+/* Do not use CHAR_BIT * sizeof(bitbuf), does not work on machines
+ * for which short is not on 16 bits (Cray).
+ */
+
+/* encode.c and decode.c */
+
+#define MAXMATCH 256    /* formerly F (not more than UCHAR_MAX + 1) */
+#define THRESHOLD  3    /* choose optimal value */
+
+/* huf.c */
+
+#define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
+       /* alphabet = {0, 1, 2, ..., NC - 1} */
+#define CBIT 9  /* $\lfloor \log_2 NC \rfloor + 1$ */
+#define CODE_BIT  16  /* codeword length */
+
+#define NP (DICBIT + 1)
+#define NT (CODE_BIT + 3)
+#define PBIT 4  /* smallest integer such that (1U << PBIT) > NP */
+#define TBIT 5  /* smallest integer such that (1U << TBIT) > NT */
+#if NT > NP
+# define NPT NT
+#else
+# define NPT NP
+#endif
+
+/* local ush left[2 * NC - 1]; */
+/* local ush right[2 * NC - 1]; */
+#define left  prev
+#define right head
+#if NC > (1<<(BITS-2))
+    error cannot overlay left+right and prev
+#endif
+
+/* local uch c_len[NC]; */
+#define c_len outbuf
+#if NC > OUTBUFSIZ
+    error cannot overlay c_len and outbuf
+#endif
+
+local uch pt_len[NPT];
+local unsigned blocksize;
+local ush pt_table[256];
+
+/* local ush c_table[4096]; */
+#define c_table d_buf
+#if (DIST_BUFSIZE-1) < 4095
+    error cannot overlay c_table and d_buf
+#endif
+
+/***********************************************************
+        io.c -- input/output
+***********************************************************/
+
+local ush       bitbuf;
+local unsigned  subbitbuf;
+local int       bitcount;
+
+local void fillbuf(n)  /* Shift bitbuf n bits left, read n bits */
+    int n;
+{
+    bitbuf <<= n;
+    while (n > bitcount) {
+       bitbuf |= subbitbuf << (n -= bitcount);
+       subbitbuf = (unsigned)try_byte();
+       if ((int)subbitbuf == EOF) subbitbuf = 0;
+       bitcount = CHAR_BIT;
+    }
+    bitbuf |= subbitbuf >> (bitcount -= n);
+}
+
+local unsigned getbits(n)
+    int n;
+{
+    unsigned x;
+
+    x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
+    return x;
+}
+
+local void init_getbits()
+{
+    bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
+    fillbuf(BITBUFSIZ);
+}
+
+/***********************************************************
+       maketbl.c -- make table for decoding
+***********************************************************/
+
+local void make_table(nchar, bitlen, tablebits, table)
+    int nchar;
+    uch bitlen[];
+    int tablebits;
+    ush table[];
+{
+    ush count[17], weight[17], start[18], *p;
+    unsigned i, k, len, ch, jutbits, avail, nextcode, mask;
+
+    for (i = 1; i <= 16; i++) count[i] = 0;
+    for (i = 0; i < (unsigned)nchar; i++) count[bitlen[i]]++;
+
+    start[1] = 0;
+    for (i = 1; i <= 16; i++)
+       start[i + 1] = start[i] + (count[i] << (16 - i));
+    if ((start[17] & 0xffff) != 0)
+       error("Bad table\n");
+
+    jutbits = 16 - tablebits;
+    for (i = 1; i <= (unsigned)tablebits; i++) {
+       start[i] >>= jutbits;
+       weight[i] = (unsigned) 1 << (tablebits - i);
+    }
+    while (i <= 16) {
+       weight[i] = (unsigned) 1 << (16 - i);
+       i++;
+    }
+
+    i = start[tablebits + 1] >> jutbits;
+    if (i != 0) {
+       k = 1 << tablebits;
+       while (i != k) table[i++] = 0;
+    }
+
+    avail = nchar;
+    mask = (unsigned) 1 << (15 - tablebits);
+    for (ch = 0; ch < (unsigned)nchar; ch++) {
+       if ((len = bitlen[ch]) == 0) continue;
+       nextcode = start[len] + weight[len];
+       if (len <= (unsigned)tablebits) {
+           for (i = start[len]; i < nextcode; i++) table[i] = ch;
+       } else {
+           k = start[len];
+           p = &table[k >> jutbits];
+           i = len - tablebits;
+           while (i != 0) {
+               if (*p == 0) {
+                   right[avail] = left[avail] = 0;
+                   *p = avail++;
+               }
+               if (k & mask) p = &right[*p];
+               else          p = &left[*p];
+               k <<= 1;  i--;
+           }
+           *p = ch;
+       }
+       start[len] = nextcode;
+    }
+}
+
+/***********************************************************
+        huf.c -- static Huffman
+***********************************************************/
+
+local void read_pt_len(nn, nbit, i_special)
+    int nn;
+    int nbit;
+    int i_special;
+{
+    int i, c, n;
+    unsigned mask;
+
+    n = getbits(nbit);
+    if (n == 0) {
+       c = getbits(nbit);
+       for (i = 0; i < nn; i++) pt_len[i] = 0;
+       for (i = 0; i < 256; i++) pt_table[i] = c;
+    } else {
+       i = 0;
+       while (i < n) {
+           c = bitbuf >> (BITBUFSIZ - 3);
+           if (c == 7) {
+               mask = (unsigned) 1 << (BITBUFSIZ - 1 - 3);
+               while (mask & bitbuf) {  mask >>= 1;  c++;  }
+           }
+           fillbuf((c < 7) ? 3 : c - 3);
+           pt_len[i++] = c;
+           if (i == i_special) {
+               c = getbits(2);
+               while (--c >= 0) pt_len[i++] = 0;
+           }
+       }
+       while (i < nn) pt_len[i++] = 0;
+       make_table(nn, pt_len, 8, pt_table);
+    }
+}
+
+local void read_c_len()
+{
+    int i, c, n;
+    unsigned mask;
+
+    n = getbits(CBIT);
+    if (n == 0) {
+       c = getbits(CBIT);
+       for (i = 0; i < NC; i++) c_len[i] = 0;
+       for (i = 0; i < 4096; i++) c_table[i] = c;
+    } else {
+       i = 0;
+       while (i < n) {
+           c = pt_table[bitbuf >> (BITBUFSIZ - 8)];
+           if (c >= NT) {
+               mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
+               do {
+                   if (bitbuf & mask) c = right[c];
+                   else               c = left [c];
+                   mask >>= 1;
+               } while (c >= NT);
+           }
+           fillbuf((int) pt_len[c]);
+           if (c <= 2) {
+               if      (c == 0) c = 1;
+               else if (c == 1) c = getbits(4) + 3;
+               else             c = getbits(CBIT) + 20;
+               while (--c >= 0) c_len[i++] = 0;
+           } else c_len[i++] = c - 2;
+       }
+       while (i < NC) c_len[i++] = 0;
+       make_table(NC, c_len, 12, c_table);
+    }
+}
+
+local unsigned decode_c()
+{
+    unsigned j, mask;
+
+    if (blocksize == 0) {
+       blocksize = getbits(16);
+       if (blocksize == 0) {
+           return NC; /* end of file */
+       }
+       read_pt_len(NT, TBIT, 3);
+       read_c_len();
+       read_pt_len(NP, PBIT, -1);
+    }
+    blocksize--;
+    j = c_table[bitbuf >> (BITBUFSIZ - 12)];
+    if (j >= NC) {
+       mask = (unsigned) 1 << (BITBUFSIZ - 1 - 12);
+       do {
+           if (bitbuf & mask) j = right[j];
+           else               j = left [j];
+           mask >>= 1;
+       } while (j >= NC);
+    }
+    fillbuf((int) c_len[j]);
+    return j;
+}
+
+local unsigned decode_p()
+{
+    unsigned j, mask;
+
+    j = pt_table[bitbuf >> (BITBUFSIZ - 8)];
+    if (j >= NP) {
+       mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
+       do {
+           if (bitbuf & mask) j = right[j];
+           else               j = left [j];
+           mask >>= 1;
+       } while (j >= NP);
+    }
+    fillbuf((int) pt_len[j]);
+    if (j != 0) j = ((unsigned) 1 << (j - 1)) + getbits((int) (j - 1));
+    return j;
+}
+
+local void huf_decode_start()
+{
+    init_getbits();  blocksize = 0;
+}
+
+/***********************************************************
+        decode.c
+***********************************************************/
+
+local int j;    /* remaining bytes to copy */
+local int done; /* set at end of input */
+
+local void decode_start()
+{
+    huf_decode_start();
+    j = 0;
+    done = 0;
+}
+
+/* Decode the input and return the number of decoded bytes put in buffer
+ */
+local unsigned decode(count, buffer)
+    unsigned count;
+    uch buffer[];
+    /* The calling function must keep the number of
+       bytes to be processed.  This function decodes
+       either 'count' bytes or 'DICSIZ' bytes, whichever
+       is smaller, into the array 'buffer[]' of size
+       'DICSIZ' or more.
+       Call decode_start() once for each new file
+       before calling this function.
+     */
+{
+    local unsigned i;
+    unsigned r, c;
+
+    r = 0;
+    while (--j >= 0) {
+       buffer[r] = buffer[i];
+       i = (i + 1) & (DICSIZ - 1);
+       if (++r == count) return r;
+    }
+    for ( ; ; ) {
+       c = decode_c();
+       if (c == NC) {
+           done = 1;
+           return r;
+       }
+       if (c <= UCHAR_MAX) {
+           buffer[r] = c;
+           if (++r == count) return r;
+       } else {
+           j = c - (UCHAR_MAX + 1 - THRESHOLD);
+           i = (r - decode_p() - 1) & (DICSIZ - 1);
+           while (--j >= 0) {
+               buffer[r] = buffer[i];
+               i = (i + 1) & (DICSIZ - 1);
+               if (++r == count) return r;
+           }
+       }
+    }
+}
+
+
+/* ===========================================================================
+ * Unlzh in to out. Return OK or ERROR.
+ */
+int unlzh(in, out)
+    int in;
+    int out;
+{
+    unsigned n;
+    ifd = in;
+    ofd = out;
+
+    decode_start();
+    while (!done) {
+       n = decode((unsigned) DICSIZ, window);
+       if (!test && n > 0) {
+           write_buf(out, (char*)window, n);
+       }
+    }
+    return OK;
+}
diff --git a/unpack.c b/unpack.c
new file mode 100644 (file)
index 0000000..a9bc18e
--- /dev/null
+++ b/unpack.c
@@ -0,0 +1,239 @@
+/* unpack.c -- decompress files in pack format.
+ * Copyright (C) 1992-1993 Jean-loup Gailly
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License, see the file COPYING.
+ */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+#include "tailor.h"
+#include "gzip.h"
+#include "crypt.h"
+
+#define MIN(a,b) ((a) <= (b) ? (a) : (b))
+/* The arguments must not have side effects. */
+
+#define MAX_BITLEN 25
+/* Maximum length of Huffman codes. (Minor modifications to the code
+ * would be needed to support 32 bits codes, but pack never generates
+ * more than 24 bits anyway.)
+ */
+
+#define LITERALS 256
+/* Number of literals, excluding the End of Block (EOB) code */
+
+#define MAX_PEEK 12
+/* Maximum number of 'peek' bits used to optimize traversal of the
+ * Huffman tree.
+ */
+
+local ulg orig_len;       /* original uncompressed length */
+local int max_len;        /* maximum bit length of Huffman codes */
+
+local uch literal[LITERALS];
+/* The literal bytes present in the Huffman tree. The EOB code is not
+ * represented.
+ */
+
+local int lit_base[MAX_BITLEN+1];
+/* All literals of a given bit length are contiguous in literal[] and
+ * have contiguous codes. literal[code+lit_base[len]] is the literal
+ * for a code of len bits.
+ */
+
+local int leaves [MAX_BITLEN+1]; /* Number of leaves for each bit length */
+local int parents[MAX_BITLEN+1]; /* Number of parents for each bit length */
+
+local int peek_bits; /* Number of peek bits currently used */
+
+/* local uch prefix_len[1 << MAX_PEEK]; */
+#define prefix_len outbuf
+/* For each bit pattern b of peek_bits bits, prefix_len[b] is the length
+ * of the Huffman code starting with a prefix of b (upper bits), or 0
+ * if all codes of prefix b have more than peek_bits bits. It is not
+ * necessary to have a huge table (large MAX_PEEK) because most of the
+ * codes encountered in the input stream are short codes (by construction).
+ * So for most codes a single lookup will be necessary.
+ */
+#if (1<<MAX_PEEK) > OUTBUFSIZ
+    error cannot overlay prefix_len and outbuf
+#endif
+
+local ulg bitbuf;
+/* Bits are added on the low part of bitbuf and read from the high part. */
+
+local int valid;                  /* number of valid bits in bitbuf */
+/* all bits above the last valid bit are always zero */
+
+/* Set code to the next 'bits' input bits without skipping them. code
+ * must be the name of a simple variable and bits must not have side effects.
+ * IN assertions: bits <= 25 (so that we still have room for an extra byte
+ * when valid is only 24), and mask = (1<<bits)-1.
+ */
+#define look_bits(code,bits,mask) \
+{ \
+  while (valid < (bits)) bitbuf = (bitbuf<<8) | (ulg)get_byte(), valid += 8; \
+  code = (bitbuf >> (valid-(bits))) & (mask); \
+}
+
+/* Skip the given number of bits (after having peeked at them): */
+#define skip_bits(bits)  (valid -= (bits))
+
+#define clear_bitbuf() (valid = 0, bitbuf = 0)
+
+/* Local functions */
+
+local void read_tree  OF((void));
+local void build_tree OF((void));
+
+/* ===========================================================================
+ * Read the Huffman tree.
+ */
+local void read_tree()
+{
+    int len;  /* bit length */
+    int base; /* base offset for a sequence of leaves */
+    int n;
+
+    /* Read the original input size, MSB first */
+    orig_len = 0;
+    for (n = 1; n <= 4; n++) orig_len = (orig_len << 8) | (ulg)get_byte();
+
+    max_len = (int)get_byte(); /* maximum bit length of Huffman codes */
+    if (max_len > MAX_BITLEN) {
+       error("invalid compressed data -- Huffman code > 32 bits");
+    }
+
+    /* Get the number of leaves at each bit length */
+    n = 0;
+    for (len = 1; len <= max_len; len++) {
+       leaves[len] = (int)get_byte();
+       n += leaves[len];
+    }
+    if (n > LITERALS) {
+       error("too many leaves in Huffman tree");
+    }
+    Trace((stderr, "orig_len %ld, max_len %d, leaves %d\n",
+          orig_len, max_len, n));
+    /* There are at least 2 and at most 256 leaves of length max_len.
+     * (Pack arbitrarily rejects empty files and files consisting of
+     * a single byte even repeated.) To fit the last leaf count in a
+     * byte, it is offset by 2. However, the last literal is the EOB
+     * code, and is not transmitted explicitly in the tree, so we must
+     * adjust here by one only.
+     */
+    leaves[max_len]++;
+
+    /* Now read the leaves themselves */
+    base = 0;
+    for (len = 1; len <= max_len; len++) {
+       /* Remember where the literals of this length start in literal[] : */
+       lit_base[len] = base;
+       /* And read the literals: */
+       for (n = leaves[len]; n > 0; n--) {
+           literal[base++] = (uch)get_byte();
+       }
+    }
+    leaves[max_len]++; /* Now include the EOB code in the Huffman tree */
+}
+
+/* ===========================================================================
+ * Build the Huffman tree and the prefix table.
+ */
+local void build_tree()
+{
+    int nodes = 0; /* number of nodes (parents+leaves) at current bit length */
+    int len;       /* current bit length */
+    uch *prefixp;  /* pointer in prefix_len */
+
+    for (len = max_len; len >= 1; len--) {
+       /* The number of parent nodes at this level is half the total
+        * number of nodes at parent level:
+        */
+       nodes >>= 1;
+       parents[len] = nodes;
+       /* Update lit_base by the appropriate bias to skip the parent nodes
+        * (which are not represented in the literal array):
+        */
+       lit_base[len] -= nodes;
+       /* Restore nodes to be parents+leaves: */
+       nodes += leaves[len];
+    }
+    /* Construct the prefix table, from shortest leaves to longest ones.
+     * The shortest code is all ones, so we start at the end of the table.
+     */
+    peek_bits = MIN(max_len, MAX_PEEK);
+    prefixp = &prefix_len[1<<peek_bits];
+    for (len = 1; len <= peek_bits; len++) {
+       int prefixes = leaves[len] << (peek_bits-len); /* may be 0 */
+       while (prefixes--) *--prefixp = (uch)len;
+    }
+    /* The length of all other codes is unknown: */
+    while (prefixp > prefix_len) *--prefixp = 0;
+}
+
+/* ===========================================================================
+ * Unpack in to out.  This routine does not support the old pack format
+ * with magic header \037\037.
+ *
+ * IN assertions: the buffer inbuf contains already the beginning of
+ *   the compressed data, from offsets inptr to insize-1 included.
+ *   The magic header has already been checked. The output buffer is cleared.
+ */
+int unpack(in, out)
+    int in, out;            /* input and output file descriptors */
+{
+    int len;                /* Bit length of current code */
+    unsigned eob;           /* End Of Block code */
+    register unsigned peek; /* lookahead bits */
+    unsigned peek_mask;     /* Mask for peek_bits bits */
+
+    ifd = in;
+    ofd = out;
+
+    read_tree();     /* Read the Huffman tree */
+    build_tree();    /* Build the prefix table */
+    clear_bitbuf();  /* Initialize bit input */
+    peek_mask = (1<<peek_bits)-1;
+
+    /* The eob code is the largest code among all leaves of maximal length: */
+    eob = leaves[max_len]-1;
+    Trace((stderr, "eob %d %x\n", max_len, eob));
+
+    /* Decode the input data: */
+    for (;;) {
+       /* Since eob is the longest code and not shorter than max_len,
+         * we can peek at max_len bits without having the risk of reading
+         * beyond the end of file.
+        */
+       look_bits(peek, peek_bits, peek_mask);
+       len = prefix_len[peek];
+       if (len > 0) {
+           peek >>= peek_bits - len; /* discard the extra bits */
+       } else {
+           /* Code of more than peek_bits bits, we must traverse the tree */
+           ulg mask = peek_mask;
+           len = peek_bits;
+           do {
+                len++, mask = (mask<<1)+1;
+               look_bits(peek, len, mask);
+           } while (peek < (unsigned)parents[len]);
+           /* loop as long as peek is a parent node */
+       }
+       /* At this point, peek is the next complete code, of len bits */
+       if (peek == eob && len == max_len) break; /* end of file? */
+       put_ubyte(literal[peek+lit_base[len]]);
+       Tracev((stderr,"%02d %04x %c\n", len, peek,
+               literal[peek+lit_base[len]]));
+       skip_bits(len);
+    } /* for (;;) */
+
+    flush_window();
+    Trace((stderr, "bytes_out %ld\n", bytes_out));
+    if (orig_len != (ulg)bytes_out) {
+       error("invalid compressed data--length error");
+    }
+    return OK;
+}
diff --git a/unzip.c b/unzip.c
new file mode 100644 (file)
index 0000000..cd3d50e
--- /dev/null
+++ b/unzip.c
@@ -0,0 +1,199 @@
+/* unzip.c -- decompress files in gzip or pkzip format.
+ * Copyright (C) 1992-1993 Jean-loup Gailly
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License, see the file COPYING.
+ *
+ * The code in this file is derived from the file funzip.c written
+ * and put in the public domain by Mark Adler.
+ */
+
+/*
+   This version can extract files in gzip or pkzip format.
+   For the latter, only the first entry is extracted, and it has to be
+   either deflated or stored.
+ */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+#include "tailor.h"
+#include "gzip.h"
+#include "crypt.h"
+
+/* PKZIP header definitions */
+#define LOCSIG 0x04034b50L      /* four-byte lead-in (lsb first) */
+#define LOCFLG 6                /* offset of bit flag */
+#define  CRPFLG 1               /*  bit for encrypted entry */
+#define  EXTFLG 8               /*  bit for extended local header */
+#define LOCHOW 8                /* offset of compression method */
+#define LOCTIM 10               /* file mod time (for decryption) */
+#define LOCCRC 14               /* offset of crc */
+#define LOCSIZ 18               /* offset of compressed size */
+#define LOCLEN 22               /* offset of uncompressed length */
+#define LOCFIL 26               /* offset of file name field length */
+#define LOCEXT 28               /* offset of extra field length */
+#define LOCHDR 30               /* size of local header, including sig */
+#define EXTHDR 16               /* size of extended local header, inc sig */
+
+
+/* Globals */
+
+int decrypt;        /* flag to turn on decryption */
+char *key;          /* not used--needed to link crypt.c */
+int pkzip = 0;      /* set for a pkzip file */
+int ext_header = 0; /* set if extended local header */
+
+/* ===========================================================================
+ * Check zip file and advance inptr to the start of the compressed data.
+ * Get ofname from the local header if necessary.
+ */
+int check_zipfile(in)
+    int in;   /* input file descriptors */
+{
+    uch *h = inbuf + inptr; /* first local header */
+
+    ifd = in;
+
+    /* Check validity of local header, and skip name and extra fields */
+    inptr += LOCHDR + SH(h + LOCFIL) + SH(h + LOCEXT);
+
+    if (inptr > insize || LG(h) != LOCSIG) {
+       fprintf(stderr, "\n%s: %s: not a valid zip file\n",
+               progname, ifname);
+       exit_code = ERROR;
+       return ERROR;
+    }
+    method = h[LOCHOW];
+    if (method != STORED && method != DEFLATED) {
+       fprintf(stderr,
+               "\n%s: %s: first entry not deflated or stored -- use unzip\n",
+               progname, ifname);
+       exit_code = ERROR;
+       return ERROR;
+    }
+
+    /* If entry encrypted, decrypt and validate encryption header */
+    if ((decrypt = h[LOCFLG] & CRPFLG) != 0) {
+       fprintf(stderr, "\n%s: %s: encrypted file -- use unzip\n",
+               progname, ifname);
+       exit_code = ERROR;
+       return ERROR;
+    }
+
+    /* Save flags for unzip() */
+    ext_header = (h[LOCFLG] & EXTFLG) != 0;
+    pkzip = 1;
+
+    /* Get ofname and time stamp from local header (to be done) */
+    return OK;
+}
+
+/* ===========================================================================
+ * Unzip in to out.  This routine works on both gzip and pkzip files.
+ *
+ * IN assertions: the buffer inbuf contains already the beginning of
+ *   the compressed data, from offsets inptr to insize-1 included.
+ *   The magic header has already been checked. The output buffer is cleared.
+ */
+int unzip(in, out)
+    int in, out;   /* input and output file descriptors */
+{
+    ulg orig_crc = 0;       /* original crc */
+    ulg orig_len = 0;       /* original uncompressed length */
+    int n;
+    uch buf[EXTHDR];        /* extended local header */
+
+    ifd = in;
+    ofd = out;
+
+    updcrc(NULL, 0);           /* initialize crc */
+
+    if (pkzip && !ext_header) {  /* crc and length at the end otherwise */
+       orig_crc = LG(inbuf + LOCCRC);
+       orig_len = LG(inbuf + LOCLEN);
+    }
+
+    /* Decompress */
+    if (method == DEFLATED)  {
+
+       int res = inflate();
+
+       if (res == 3) {
+           error("out of memory");
+       } else if (res != 0) {
+           error("invalid compressed data--format violated");
+       }
+
+    } else if (pkzip && method == STORED) {
+
+       register ulg n = LG(inbuf + LOCLEN);
+
+       if (n != LG(inbuf + LOCSIZ) - (decrypt ? RAND_HEAD_LEN : 0)) {
+
+           fprintf(stderr, "len %ld, siz %ld\n", n, LG(inbuf + LOCSIZ));
+           error("invalid compressed data--length mismatch");
+       }
+       while (n--) {
+           uch c = (uch)get_byte();
+#ifdef CRYPT
+           if (decrypt) zdecode(c);
+#endif
+           put_ubyte(c);
+       }
+       flush_window();
+    } else {
+       error("internal error, invalid method");
+    }
+
+    /* Get the crc and original length */
+    if (!pkzip) {
+        /* crc32  (see algorithm.doc)
+        * uncompressed input size modulo 2^32
+         */
+       for (n = 0; n < 8; n++) {
+           buf[n] = (uch)get_byte(); /* may cause an error if EOF */
+       }
+       orig_crc = LG(buf);
+       orig_len = LG(buf+4);
+
+    } else if (ext_header) {  /* If extended header, check it */
+       /* signature - 4bytes: 0x50 0x4b 0x07 0x08
+        * CRC-32 value
+         * compressed size 4-bytes
+         * uncompressed size 4-bytes
+        */
+       for (n = 0; n < EXTHDR; n++) {
+           buf[n] = (uch)get_byte(); /* may cause an error if EOF */
+       }
+       orig_crc = LG(buf+4);
+       orig_len = LG(buf+12);
+    }
+
+    /* Validate decompression */
+    if (orig_crc != updcrc(outbuf, 0)) {
+       error("invalid compressed data--crc error");
+    }
+    if (orig_len != (ulg)bytes_out) {
+       error("invalid compressed data--length error");
+    }
+
+    /* Check if there are more entries in a pkzip file */
+    if (pkzip && inptr + 4 < insize && LG(inbuf+inptr) == LOCSIG) {
+       if (to_stdout) {
+           WARN((stderr,
+                 "%s: %s has more than one entry--rest ignored\n",
+                 progname, ifname));
+       } else {
+           /* Don't destroy the input zip file */
+           fprintf(stderr,
+                   "%s: %s has more than one entry -- unchanged\n",
+                   progname, ifname);
+           exit_code = ERROR;
+           ext_header = pkzip = 0;
+           return ERROR;
+       }
+    }
+    ext_header = pkzip = 0; /* for next file */
+    return OK;
+}
diff --git a/util.c b/util.c
new file mode 100644 (file)
index 0000000..123f845
--- /dev/null
+++ b/util.c
@@ -0,0 +1,462 @@
+/* util.c -- utility functions for gzip support
+ * Copyright (C) 1992-1993 Jean-loup Gailly
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License, see the file COPYING.
+ */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <sys/types.h>
+
+#include "tailor.h"
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+#endif
+#ifndef NO_FCNTL_H
+#  include <fcntl.h>
+#endif
+
+#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
+#  include <stdlib.h>
+#else
+   extern int errno;
+#endif
+
+#include "gzip.h"
+#include "crypt.h"
+
+extern ulg crc_32_tab[];   /* crc table, defined below */
+
+/* ===========================================================================
+ * Copy input to output unchanged: zcat == cat with --force.
+ * IN assertion: insize bytes have already been read in inbuf.
+ */
+int copy(in, out)
+    int in, out;   /* input and output file descriptors */
+{
+    errno = 0;
+    while (insize != 0 && (int)insize != EOF) {
+       write_buf(out, (char*)inbuf, insize);
+       bytes_out += insize;
+       insize = read(in, (char*)inbuf, INBUFSIZ);
+    }
+    if ((int)insize == EOF && errno != 0) {
+       read_error();
+    }
+    bytes_in = bytes_out;
+    return OK;
+}
+
+/* ===========================================================================
+ * Run a set of bytes through the crc shift register.  If s is a NULL
+ * pointer, then initialize the crc shift register contents instead.
+ * Return the current crc in either case.
+ */
+ulg updcrc(s, n)
+    uch *s;                 /* pointer to bytes to pump through */
+    unsigned n;             /* number of bytes in s[] */
+{
+    register ulg c;         /* temporary variable */
+
+    static ulg crc = (ulg)0xffffffffL; /* shift register contents */
+
+    if (s == NULL) {
+       c = 0xffffffffL;
+    } else {
+       c = crc;
+        if (n) do {
+            c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
+        } while (--n);
+    }
+    crc = c;
+    return c ^ 0xffffffffL;       /* (instead of ~c for 64-bit machines) */
+}
+
+/* ===========================================================================
+ * Clear input and output buffers
+ */
+void clear_bufs()
+{
+    outcnt = 0;
+    insize = inptr = 0;
+    bytes_in = bytes_out = 0L;
+}
+
+/* ===========================================================================
+ * Fill the input buffer. This is called only when the buffer is empty.
+ */
+int fill_inbuf(eof_ok)
+    int eof_ok;          /* set if EOF acceptable as a result */
+{
+    int len;
+
+    /* Read as much as possible */
+    insize = 0;
+    errno = 0;
+    do {
+       len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize);
+        if (len == 0 || len == EOF) break;
+       insize += len;
+    } while (insize < INBUFSIZ);
+
+    if (insize == 0) {
+       if (eof_ok) return EOF;
+       read_error();
+    }
+    bytes_in += (ulg)insize;
+    inptr = 1;
+    return inbuf[0];
+}
+
+/* ===========================================================================
+ * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
+ * (used for the compressed data only)
+ */
+void flush_outbuf()
+{
+    if (outcnt == 0) return;
+
+    write_buf(ofd, (char *)outbuf, outcnt);
+    bytes_out += (ulg)outcnt;
+    outcnt = 0;
+}
+
+/* ===========================================================================
+ * Write the output window window[0..outcnt-1] and update crc and bytes_out.
+ * (Used for the decompressed data only.)
+ */
+void flush_window()
+{
+    if (outcnt == 0) return;
+    updcrc(window, outcnt);
+
+    if (!test) {
+       write_buf(ofd, (char *)window, outcnt);
+    }
+    bytes_out += (ulg)outcnt;
+    outcnt = 0;
+}
+
+/* ===========================================================================
+ * Does the same as write(), but also handles partial pipe writes and checks
+ * for error return.
+ */
+void write_buf(fd, buf, cnt)
+    int       fd;
+    voidp     buf;
+    unsigned  cnt;
+{
+    unsigned  n;
+
+    while ((n = write(fd, buf, cnt)) != cnt) {
+       if (n == (unsigned)(-1)) {
+           write_error();
+       }
+       cnt -= n;
+       buf = (voidp)((char*)buf+n);
+    }
+}
+
+/* ========================================================================
+ * Put string s in lower case, return s.
+ */
+char *strlwr(s)
+    char *s;
+{
+    char *t;
+    for (t = s; *t; t++) *t = tolow(*t);
+    return s;
+}
+
+/* ========================================================================
+ * Return the base name of a file (remove any directory prefix and
+ * any version suffix). For systems with file names that are not
+ * case sensitive, force the base name to lower case.
+ */
+char *basename(fname)
+    char *fname;
+{
+    char *p;
+
+    if ((p = strrchr(fname, PATH_SEP))  != NULL) fname = p+1;
+#ifdef PATH_SEP2
+    if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
+#endif
+#ifdef PATH_SEP3
+    if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
+#endif
+#ifdef SUFFIX_SEP
+    if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
+#endif
+    if (casemap('A') == 'a') strlwr(fname);
+    return fname;
+}
+
+/* ========================================================================
+ * Make a file name legal for file systems not allowing file names with
+ * multiple dots or starting with a dot (such as MSDOS), by changing
+ * all dots except the last one into underlines.  A target dependent
+ * function can be used instead of this simple function by defining the macro
+ * MAKE_LEGAL_NAME in tailor.h and providing the function in a target
+ * dependent module.
+ */
+void make_simple_name(name)
+    char *name;
+{
+    char *p = strrchr(name, '.');
+    if (p == NULL) return;
+    if (p == name) p++;
+    do {
+        if (*--p == '.') *p = '_';
+    } while (p != name);
+}
+
+
+#if defined(NO_STRING_H) && !defined(STDC_HEADERS)
+
+/* Provide missing strspn and strcspn functions. */
+
+#  ifndef __STDC__
+#    define const
+#  endif
+
+int strspn  OF((const char *s, const char *accept));
+int strcspn OF((const char *s, const char *reject));
+
+/* ========================================================================
+ * Return the length of the maximum initial segment
+ * of s which contains only characters in accept.
+ */
+int strspn(s, accept)
+    const char *s;
+    const char *accept;
+{
+    register const char *p;
+    register const char *a;
+    register int count = 0;
+
+    for (p = s; *p != '\0'; ++p) {
+       for (a = accept; *a != '\0'; ++a) {
+           if (*p == *a) break;
+       }
+       if (*a == '\0') return count;
+       ++count;
+    }
+    return count;
+}
+
+/* ========================================================================
+ * Return the length of the maximum inital segment of s
+ * which contains no characters from reject.
+ */
+int strcspn(s, reject)
+    const char *s;
+    const char *reject;
+{
+    register int count = 0;
+
+    while (*s != '\0') {
+       if (strchr(reject, *s++) != NULL) return count;
+       ++count;
+    }
+    return count;
+}
+
+#endif /* NO_STRING_H */
+
+/* ========================================================================
+ * Add an environment variable (if any) before argv, and update argc.
+ * Return the expanded environment variable to be freed later, or NULL 
+ * if no options were added to argv.
+ */
+#define SEPARATOR      " \t"   /* separators in env variable */
+
+char *add_envopt(argcp, argvp, env)
+    int *argcp;          /* pointer to argc */
+    char ***argvp;       /* pointer to argv */
+    char *env;           /* name of environment variable */
+{
+    char *p;             /* running pointer through env variable */
+    char **oargv;        /* runs through old argv array */
+    char **nargv;        /* runs through new argv array */
+    int         oargc = *argcp; /* old argc */
+    int  nargc = 0;      /* number of arguments in env variable */
+
+    env = (char*)getenv(env);
+    if (env == NULL) return NULL;
+
+    p = (char*)xmalloc(strlen(env)+1);
+    env = strcpy(p, env);                    /* keep env variable intact */
+
+    for (p = env; *p; nargc++ ) {            /* move through env */
+       p += strspn(p, SEPARATOR);           /* skip leading separators */
+       if (*p == '\0') break;
+
+       p += strcspn(p, SEPARATOR);          /* find end of word */
+       if (*p) *p++ = '\0';                 /* mark it */
+    }
+    if (nargc == 0) {
+       free(env);
+       return NULL;
+    }
+    *argcp += nargc;
+    /* Allocate the new argv array, with an extra element just in case
+     * the original arg list did not end with a NULL.
+     */
+    nargv = (char**)calloc(*argcp+1, sizeof(char *));
+    if (nargv == NULL) error("out of memory");
+    oargv  = *argvp;
+    *argvp = nargv;
+
+    /* Copy the program name first */
+    if (oargc-- < 0) error("argc<=0");
+    *(nargv++) = *(oargv++);
+
+    /* Then copy the environment args */
+    for (p = env; nargc > 0; nargc--) {
+       p += strspn(p, SEPARATOR);           /* skip separators */
+       *(nargv++) = p;                      /* store start */
+       while (*p++) ;                       /* skip over word */
+    }
+
+    /* Finally copy the old args and add a NULL (usual convention) */
+    while (oargc--) *(nargv++) = *(oargv++);
+    *nargv = NULL;
+    return env;
+}
+
+/* ========================================================================
+ * Error handlers.
+ */
+void error(m)
+    char *m;
+{
+    fprintf(stderr, "\n%s: %s: %s\n", progname, ifname, m);
+    abort_gzip();
+}
+
+void warn(a, b)
+    char *a, *b;            /* message strings juxtaposed in output */
+{
+    WARN((stderr, "%s: %s: warning: %s%s\n", progname, ifname, a, b));
+}
+
+void read_error()
+{
+    fprintf(stderr, "\n%s: ", progname);
+    if (errno != 0) {
+       perror(ifname);
+    } else {
+       fprintf(stderr, "%s: unexpected end of file\n", ifname);
+    }
+    abort_gzip();
+}
+
+void write_error()
+{
+    fprintf(stderr, "\n%s: ", progname);
+    perror(ofname);
+    abort_gzip();
+}
+
+/* ========================================================================
+ * Display compression ratio on the given stream on 6 characters.
+ */
+void display_ratio(num, den, file)
+    long num;
+    long den;
+    FILE *file;
+{
+    long ratio;  /* 1000 times the compression ratio */
+
+    if (den == 0) {
+       ratio = 0; /* no compression */
+    } else if (den < 2147483L) { /* (2**31 -1)/1000 */
+       ratio = 1000L*num/den;
+    } else {
+       ratio = num/(den/1000L);
+    }
+    if (ratio < 0) {
+       putc('-', file);
+       ratio = -ratio;
+    } else {
+       putc(' ', file);
+    }
+    fprintf(file, "%2ld.%1ld%%", ratio / 10L, ratio % 10L);
+}
+
+
+/* ========================================================================
+ * Semi-safe malloc -- never returns NULL.
+ */
+voidp xmalloc (size)
+    unsigned size;
+{
+    voidp cp = (voidp)malloc (size);
+
+    if (cp == NULL) error("out of memory");
+    return cp;
+}
+
+/* ========================================================================
+ * Table of CRC-32's of all single-byte values (made by makecrc.c)
+ */
+ulg crc_32_tab[] = {
+  0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
+  0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
+  0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
+  0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
+  0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
+  0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
+  0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
+  0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
+  0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
+  0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
+  0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
+  0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
+  0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
+  0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
+  0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
+  0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
+  0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
+  0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
+  0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
+  0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
+  0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
+  0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
+  0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
+  0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
+  0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
+  0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
+  0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
+  0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
+  0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
+  0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
+  0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
+  0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
+  0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
+  0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
+  0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
+  0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
+  0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
+  0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
+  0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
+  0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
+  0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
+  0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
+  0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
+  0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
+  0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
+  0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
+  0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
+  0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
+  0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
+  0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
+  0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
+  0x2d02ef8dL
+};
diff --git a/zip.c b/zip.c
new file mode 100644 (file)
index 0000000..145116d
--- /dev/null
+++ b/zip.c
@@ -0,0 +1,117 @@
+/* zip.c -- compress files to the gzip or pkzip format
+ * Copyright (C) 1992-1993 Jean-loup Gailly
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License, see the file COPYING.
+ */
+
+#ifdef RCSID
+static char rcsid[] = "$Id$";
+#endif
+
+#include <ctype.h>
+#include <sys/types.h>
+
+#include "tailor.h"
+#include "gzip.h"
+#include "crypt.h"
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+#endif
+#ifndef NO_FCNTL_H
+#  include <fcntl.h>
+#endif
+
+local ulg crc;       /* crc on uncompressed file data */
+long header_bytes;   /* number of bytes in gzip header */
+
+/* ===========================================================================
+ * Deflate in to out.
+ * IN assertions: the input and output buffers are cleared.
+ *   The variables time_stamp and save_orig_name are initialized.
+ */
+int zip(in, out)
+    int in, out;            /* input and output file descriptors */
+{
+    uch  flags = 0;         /* general purpose bit flags */
+    ush  attr = 0;          /* ascii/binary flag */
+    ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
+
+    ifd = in;
+    ofd = out;
+    outcnt = 0;
+
+    /* Write the header to the gzip file. See algorithm.doc for the format */
+
+    method = DEFLATED;
+    put_byte(GZIP_MAGIC[0]); /* magic header */
+    put_byte(GZIP_MAGIC[1]);
+    put_byte(DEFLATED);      /* compression method */
+
+    if (save_orig_name) {
+       flags |= ORIG_NAME;
+    }
+    put_byte(flags);         /* general flags */
+    put_long(time_stamp);
+
+    /* Write deflated file to zip file */
+    crc = updcrc(0, 0);
+
+    bi_init(out);
+    ct_init(&attr, &method);
+    lm_init(level, &deflate_flags);
+
+    put_byte((uch)deflate_flags); /* extra flags */
+    put_byte(OS_CODE);            /* OS identifier */
+
+    if (save_orig_name) {
+       char *p = basename(ifname); /* Don't save the directory part. */
+       do {
+           put_char(*p);
+       } while (*p++);
+    }
+    header_bytes = (long)outcnt;
+
+    (void)deflate();
+
+#if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
+  /* Check input size (but not in VMS -- variable record lengths mess it up)
+   * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
+   */
+    if (ifile_size != -1L && isize != (ulg)ifile_size) {
+       Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
+       fprintf(stderr, "%s: %s: file size changed while zipping\n",
+               progname, ifname);
+    }
+#endif
+
+    /* Write the crc and uncompressed size */
+    put_long(crc);
+    put_long(isize);
+    header_bytes += 2*sizeof(long);
+
+    flush_outbuf();
+    return OK;
+}
+
+
+/* ===========================================================================
+ * Read a new buffer from the current input file, perform end-of-line
+ * translation, and update the crc and input file size.
+ * IN assertion: size >= 2 (for end-of-line translation)
+ */
+int file_read(buf, size)
+    char *buf;
+    unsigned size;
+{
+    unsigned len;
+
+    Assert(insize == 0, "inbuf not empty");
+
+    len = read(ifd, buf, size);
+    if (len == (unsigned)(-1) || len == 0) return (int)len;
+
+    crc = updcrc((uch*)buf, len);
+    isize += (ulg)len;
+    return (int)len;
+}