[BACK]Return to zopen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / compress

Annotation of src/usr.bin/compress/zopen.c, Revision 1.5

1.5     ! mickey      1: /*     $OpenBSD: zopen.c,v 1.4 1997/01/19 17:25:14 millert Exp $       */
1.1       deraadt     2: /*     $NetBSD: zopen.c,v 1.5 1995/03/26 09:44:53 glass Exp $  */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1985, 1986, 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Diomidis Spinellis and James A. Woods, derived from original
                     10:  * work by Spencer Thomas and Joseph Orost.
                     11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
                     20:  * 3. All advertising materials mentioning features or use of this software
                     21:  *    must display the following acknowledgement:
                     22:  *     This product includes software developed by the University of
                     23:  *     California, Berkeley and its contributors.
                     24:  * 4. Neither the name of the University nor the names of its contributors
                     25:  *    may be used to endorse or promote products derived from this software
                     26:  *    without specific prior written permission.
                     27:  *
                     28:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     29:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     30:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     31:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     32:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     33:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     34:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     35:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     36:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     37:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     38:  * SUCH DAMAGE.
                     39:  */
                     40:
                     41: #if defined(LIBC_SCCS) && !defined(lint)
                     42: #if 0
                     43: static char sccsid[] = "@(#)zopen.c    8.1 (Berkeley) 6/27/93";
                     44: #else
1.5     ! mickey     45: static char rcsid[] = "$OpenBSD: zopen.c,v 1.4 1997/01/19 17:25:14 millert Exp $";
1.1       deraadt    46: #endif
                     47: #endif /* LIBC_SCCS and not lint */
                     48:
                     49: /*-
                     50:  * fcompress.c - File compression ala IEEE Computer, June 1984.
                     51:  *
                     52:  * Compress authors:
                     53:  *             Spencer W. Thomas       (decvax!utah-cs!thomas)
                     54:  *             Jim McKie               (decvax!mcvax!jim)
                     55:  *             Steve Davies            (decvax!vax135!petsd!peora!srd)
                     56:  *             Ken Turkowski           (decvax!decwrl!turtlevax!ken)
                     57:  *             James A. Woods          (decvax!ihnp4!ames!jaw)
                     58:  *             Joe Orost               (decvax!vax135!petsd!joe)
                     59:  *
                     60:  * Cleaned up and converted to library returning I/O streams by
                     61:  * Diomidis Spinellis <dds@doc.ic.ac.uk>.
                     62:  *
                     63:  * zopen(filename, mode, bits)
                     64:  *     Returns a FILE * that can be used for read or write.  The modes
                     65:  *     supported are only "r" and "w".  Seeking is not allowed.  On
                     66:  *     reading the file is decompressed, on writing it is compressed.
                     67:  *     The output is compatible with compress(1) with 16 bit tables.
                     68:  *     Any file produced by compress(1) can be read.
                     69:  */
                     70:
                     71: #include <sys/param.h>
                     72: #include <sys/stat.h>
                     73:
                     74: #include <ctype.h>
                     75: #include <errno.h>
                     76: #include <signal.h>
                     77: #include <stdio.h>
                     78: #include <stdlib.h>
                     79: #include <string.h>
                     80: #include <unistd.h>
1.5     ! mickey     81: #include <fcntl.h>
        !            82: #include "compress.h"
1.1       deraadt    83:
                     84: #define        BITS            16              /* Default bits. */
                     85: #define        HSIZE           69001           /* 95% occupancy */
                     86:
                     87: /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
                     88: typedef long code_int;
                     89: typedef long count_int;
                     90:
1.5     ! mickey     91: static u_char z_magic[] =
1.1       deraadt    92:        {'\037', '\235'};               /* 1F 9D */
                     93:
                     94: #define        BIT_MASK        0x1f            /* Defines for third byte of header. */
                     95: #define        BLOCK_MASK      0x80
                     96:
                     97: /*
                     98:  * Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
                     99:  * a fourth header byte (for expansion).
                    100:  */
                    101: #define        INIT_BITS 9                     /* Initial number of bits/code. */
                    102:
                    103: #define        MAXCODE(n_bits) ((1 << (n_bits)) - 1)
                    104:
                    105: struct s_zstate {
1.5     ! mickey    106:        int zs_fd;                      /* File stream for I/O */
1.1       deraadt   107:        char zs_mode;                   /* r or w */
                    108:        enum {
                    109:                S_START, S_MIDDLE, S_EOF
                    110:        } zs_state;                     /* State of computation */
                    111:        int zs_n_bits;                  /* Number of bits/code. */
                    112:        int zs_maxbits;                 /* User settable max # bits/code. */
                    113:        code_int zs_maxcode;            /* Maximum code, given n_bits. */
                    114:        code_int zs_maxmaxcode;         /* Should NEVER generate this code. */
                    115:        count_int zs_htab [HSIZE];
                    116:        u_short zs_codetab [HSIZE];
                    117:        code_int zs_hsize;              /* For dynamic table sizing. */
                    118:        code_int zs_free_ent;           /* First unused entry. */
                    119:        /*
                    120:         * Block compression parameters -- after all codes are used up,
                    121:         * and compression rate changes, start over.
                    122:         */
                    123:        int zs_block_compress;
                    124:        int zs_clear_flg;
                    125:        long zs_ratio;
                    126:        count_int zs_checkpoint;
                    127:        int zs_offset;
                    128:        long zs_in_count;               /* Length of input. */
                    129:        long zs_bytes_out;              /* Length of compressed output. */
1.5     ! mickey    130:        long zs_out_count;              /* # of codes output (for debugging).*/
        !           131:        u_char zs_buf[BITS];
1.1       deraadt   132:        union {
                    133:                struct {
                    134:                        long zs_fcode;
                    135:                        code_int zs_ent;
                    136:                        code_int zs_hsize_reg;
                    137:                        int zs_hshift;
                    138:                } w;                    /* Write paramenters */
                    139:                struct {
1.5     ! mickey    140:                        u_char *zs_stackp;
1.1       deraadt   141:                        int zs_finchar;
                    142:                        code_int zs_code, zs_oldcode, zs_incode;
                    143:                        int zs_roffset, zs_size;
                    144:                } r;                    /* Read parameters */
                    145:        } u;
                    146: };
                    147:
                    148: /* Definitions to retain old variable names */
1.5     ! mickey    149: #define zs_fcode       u.w.zs_fcode
        !           150: #define zs_ent         u.w.zs_ent
        !           151: #define zs_hsize_reg   u.w.zs_hsize_reg
        !           152: #define zs_hshift      u.w.zs_hshift
        !           153: #define zs_stackp      u.r.zs_stackp
        !           154: #define zs_finchar     u.r.zs_finchar
        !           155: #define zs_code                u.r.zs_code
        !           156: #define zs_oldcode     u.r.zs_oldcode
        !           157: #define zs_incode      u.r.zs_incode
        !           158: #define zs_roffset     u.r.zs_roffset
        !           159: #define zs_size                u.r.zs_size
1.1       deraadt   160:
                    161: /*
                    162:  * To save much memory, we overlay the table used by compress() with those
                    163:  * used by decompress().  The tab_prefix table is the same size and type as
                    164:  * the codetab.  The tab_suffix table needs 2**BITS characters.  We get this
                    165:  * from the beginning of htab.  The output stack uses the rest of htab, and
                    166:  * contains characters.  There is plenty of room for any possible stack
                    167:  * (stack used to be 8000 characters).
                    168:  */
                    169:
1.5     ! mickey    170: #define        htabof(i)       zs->zs_htab[i]
        !           171: #define        codetabof(i)    zs->zs_codetab[i]
1.1       deraadt   172:
                    173: #define        tab_prefixof(i) codetabof(i)
1.5     ! mickey    174: #define        tab_suffixof(i) ((u_char *)(zs->zs_htab))[i]
        !           175: #define        de_stack        ((u_char *)&tab_suffixof(1 << BITS))
1.1       deraadt   176:
                    177: #define        CHECK_GAP 10000         /* Ratio check interval. */
                    178:
                    179: /*
                    180:  * the next two codes should not be changed lightly, as they must not
                    181:  * lie within the contiguous general code space.
                    182:  */
                    183: #define        FIRST   257             /* First free entry. */
                    184: #define        CLEAR   256             /* Table clear output code. */
                    185:
1.5     ! mickey    186: static int     cl_block __P((register struct s_zstate *));
        !           187: static void    cl_hash __P((register struct s_zstate *, register count_int));
        !           188: static code_int        getcode __P((register struct s_zstate *));
        !           189: static int     output __P((register struct s_zstate *, code_int));
1.1       deraadt   190:
                    191: /*-
                    192:  * Algorithm from "A Technique for High Performance Data Compression",
                    193:  * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
                    194:  *
                    195:  * Algorithm:
                    196:  *     Modified Lempel-Ziv method (LZW).  Basically finds common
                    197:  * substrings and replaces them with a variable size code.  This is
                    198:  * deterministic, and can be done on the fly.  Thus, the decompression
                    199:  * procedure needs no input table, but tracks the way the table was built.
                    200:  */
                    201:
                    202: /*-
                    203:  * compress write
                    204:  *
                    205:  * Algorithm:  use open addressing double hashing (no chaining) on the
                    206:  * prefix code / next character combination.  We do a variant of Knuth's
                    207:  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
                    208:  * secondary probe.  Here, the modular division first probe is gives way
                    209:  * to a faster exclusive-or manipulation.  Also do block compression with
                    210:  * an adaptive reset, whereby the code table is cleared when the compression
                    211:  * ratio decreases, but after the table fills.  The variable-length output
                    212:  * codes are re-sized at this point, and a special CLEAR code is generated
                    213:  * for the decompressor.  Late addition:  construct the table according to
                    214:  * file size for noticeable speed improvement on small files.  Please direct
                    215:  * questions about this implementation to ames!jaw.
                    216:  */
1.5     ! mickey    217: int
1.1       deraadt   218: zwrite(cookie, wbp, num)
                    219:        void *cookie;
                    220:        const char *wbp;
                    221:        int num;
                    222: {
                    223:        register code_int i;
                    224:        register int c, disp;
                    225:        struct s_zstate *zs;
                    226:        const u_char *bp;
                    227:        u_char tmp;
                    228:        int count;
                    229:
                    230:        if (num == 0)
                    231:                return (0);
                    232:
                    233:        zs = cookie;
                    234:        count = num;
                    235:        bp = (u_char *)wbp;
1.5     ! mickey    236:        if (zs->zs_state == S_MIDDLE)
1.1       deraadt   237:                goto middle;
1.5     ! mickey    238:        zs->zs_state = S_MIDDLE;
1.1       deraadt   239:
1.5     ! mickey    240:        zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
        !           241:        if (write(zs->zs_fd, z_magic, sizeof(z_magic)) != sizeof(z_magic))
1.1       deraadt   242:                return (-1);
1.5     ! mickey    243:        tmp = (u_char)(zs->zs_maxbits | zs->zs_block_compress);
        !           244:        if (write(zs->zs_fd, &tmp, sizeof(tmp)) != sizeof(tmp))
1.1       deraadt   245:                return (-1);
                    246:
1.5     ! mickey    247:        zs->zs_offset = 0;
        !           248:        zs->zs_bytes_out = 3;           /* Includes 3-byte header mojo. */
        !           249:        zs->zs_out_count = 0;
        !           250:        zs->zs_clear_flg = 0;
        !           251:        zs->zs_ratio = 0;
        !           252:        zs->zs_in_count = 1;
        !           253:        zs->zs_checkpoint = CHECK_GAP;
        !           254:        zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
        !           255:        zs->zs_free_ent = ((zs->zs_block_compress) ? FIRST : 256);
1.1       deraadt   256:
1.5     ! mickey    257:        zs->zs_ent = *bp++;
1.1       deraadt   258:        --count;
                    259:
1.5     ! mickey    260:        zs->zs_hshift = 0;
        !           261:        for (zs->zs_fcode = (long)zs->zs_hsize; zs->zs_fcode < 65536L;
        !           262:             zs->zs_fcode *= 2L)
        !           263:                zs->zs_hshift++;
        !           264:        zs->zs_hshift = 8 - zs->zs_hshift; /* Set hash code range bound. */
1.1       deraadt   265:
1.5     ! mickey    266:        zs->zs_hsize_reg = zs->zs_hsize;
        !           267:        cl_hash(zs, (count_int)zs->zs_hsize_reg);       /* Clear hash table. */
1.1       deraadt   268:
                    269: middle:        for (i = 0; count--;) {
                    270:                c = *bp++;
1.5     ! mickey    271:                zs->zs_in_count++;
        !           272:                zs->zs_fcode = (long)(((long)c << zs->zs_maxbits) +
        !           273:                                      zs->zs_ent);
        !           274:                i = ((c << zs->zs_hshift) ^ zs->zs_ent); /* Xor hashing. */
1.1       deraadt   275:
1.5     ! mickey    276:                if (htabof(i) == zs->zs_fcode) {
        !           277:                        zs->zs_ent = codetabof(i);
1.1       deraadt   278:                        continue;
                    279:                } else if ((long)htabof(i) < 0) /* Empty slot. */
                    280:                        goto nomatch;
1.5     ! mickey    281:                /* Secondary hash (after G. Knott). */
        !           282:                disp = zs->zs_hsize_reg - i;
1.1       deraadt   283:                if (i == 0)
                    284:                        disp = 1;
                    285: probe:         if ((i -= disp) < 0)
1.5     ! mickey    286:                        i += zs->zs_hsize_reg;
1.1       deraadt   287:
1.5     ! mickey    288:                if (htabof(i) == zs->zs_fcode) {
        !           289:                        zs->zs_ent = codetabof(i);
1.1       deraadt   290:                        continue;
                    291:                }
                    292:                if ((long)htabof(i) >= 0)
                    293:                        goto probe;
1.5     ! mickey    294: nomatch:       if (output(zs, (code_int) zs->zs_ent) == -1)
1.1       deraadt   295:                        return (-1);
1.5     ! mickey    296:                zs->zs_out_count++;
        !           297:                zs->zs_ent = c;
        !           298:                if (zs->zs_free_ent < zs->zs_maxmaxcode) {
        !           299:                        /* code -> hashtable */
        !           300:                        codetabof(i) = zs->zs_free_ent++;
        !           301:                        htabof(i) = zs->zs_fcode;
        !           302:                } else if ((count_int)zs->zs_in_count >=
        !           303:                    zs->zs_checkpoint && zs->zs_block_compress) {
1.1       deraadt   304:                        if (cl_block(zs) == -1)
                    305:                                return (-1);
                    306:                }
                    307:        }
                    308:        return (num);
                    309: }
                    310:
1.5     ! mickey    311: int
1.1       deraadt   312: zclose(cookie)
                    313:        void *cookie;
                    314: {
                    315:        struct s_zstate *zs;
                    316:        int rval;
                    317:
                    318:        zs = cookie;
1.5     ! mickey    319:        if (zs->zs_mode == 'w') {               /* Put out the final code. */
        !           320:                if (output(zs, (code_int) zs->zs_ent) == -1) {
        !           321:                        (void)close(zs->zs_fd);
1.1       deraadt   322:                        free(zs);
                    323:                        return (-1);
                    324:                }
1.5     ! mickey    325:                zs->zs_out_count++;
1.1       deraadt   326:                if (output(zs, (code_int) - 1) == -1) {
1.5     ! mickey    327:                        (void)close(zs->zs_fd);
1.1       deraadt   328:                        free(zs);
                    329:                        return (-1);
                    330:                }
                    331:        }
1.5     ! mickey    332:        rval = close(zs->zs_fd);
1.1       deraadt   333:        free(zs);
                    334:        return (rval);
                    335: }
                    336:
                    337: /*-
                    338:  * Output the given code.
                    339:  * Inputs:
                    340:  *     code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
                    341:  *             that n_bits =< (long)wordsize - 1.
                    342:  * Outputs:
                    343:  *     Outputs code to the file.
                    344:  * Assumptions:
                    345:  *     Chars are 8 bits long.
                    346:  * Algorithm:
                    347:  *     Maintain a BITS character long buffer (so that 8 codes will
                    348:  * fit in it exactly).  Use the VAX insv instruction to insert each
                    349:  * code in turn.  When the buffer fills up empty it and start over.
                    350:  */
                    351:
1.5     ! mickey    352: static u_char lmask[9] =
1.1       deraadt   353:        {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
1.5     ! mickey    354: static u_char rmask[9] =
1.1       deraadt   355:        {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
                    356:
                    357: static int
                    358: output(zs, ocode)
1.5     ! mickey    359:        register struct s_zstate *zs;
1.1       deraadt   360:        code_int ocode;
                    361: {
                    362:        register int bits, r_off;
1.5     ! mickey    363:        register u_char *bp;
1.1       deraadt   364:
1.5     ! mickey    365:        r_off = zs->zs_offset;
        !           366:        bits = zs->zs_n_bits;
        !           367:        bp = zs->zs_buf;
1.1       deraadt   368:        if (ocode >= 0) {
                    369:                /* Get to the first byte. */
                    370:                bp += (r_off >> 3);
                    371:                r_off &= 7;
                    372:                /*
                    373:                 * Since ocode is always >= 8 bits, only need to mask the first
                    374:                 * hunk on the left.
                    375:                 */
1.4       millert   376:                *bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
1.1       deraadt   377:                bp++;
                    378:                bits -= (8 - r_off);
                    379:                ocode >>= 8 - r_off;
1.5     ! mickey    380:                /* Get any 8 bit parts in the middle (<=1 for up to 16 bits) */
1.1       deraadt   381:                if (bits >= 8) {
                    382:                        *bp++ = ocode;
                    383:                        ocode >>= 8;
                    384:                        bits -= 8;
                    385:                }
                    386:                /* Last bits. */
                    387:                if (bits)
                    388:                        *bp = ocode;
1.5     ! mickey    389:                zs->zs_offset += zs->zs_n_bits;
        !           390:                if (zs->zs_offset == (zs->zs_n_bits << 3)) {
        !           391:                        bp = zs->zs_buf;
        !           392:                        bits = zs->zs_n_bits;
        !           393:                        zs->zs_bytes_out += bits;
        !           394:                        if (write(zs->zs_fd, bp, bits) != bits)
1.1       deraadt   395:                                return (-1);
                    396:                        bp += bits;
                    397:                        bits = 0;
1.5     ! mickey    398:                        zs->zs_offset = 0;
1.1       deraadt   399:                }
                    400:                /*
                    401:                 * If the next entry is going to be too big for the ocode size,
                    402:                 * then increase it, if possible.
                    403:                 */
1.5     ! mickey    404:                if (zs->zs_free_ent > zs->zs_maxcode ||
        !           405:                    (zs->zs_clear_flg > 0)) {
1.1       deraadt   406:                       /*
                    407:                        * Write the whole buffer, because the input side won't
1.5     ! mickey    408:                        * discover the size increase until after it has read it
1.1       deraadt   409:                        */
1.5     ! mickey    410:                        if (zs->zs_offset > 0) {
        !           411:                                if (write(zs->zs_fd, zs->zs_buf, zs->zs_n_bits)
        !           412:                                    != zs->zs_n_bits)
1.1       deraadt   413:                                        return (-1);
1.5     ! mickey    414:                                zs->zs_bytes_out += zs->zs_n_bits;
1.1       deraadt   415:                        }
1.5     ! mickey    416:                        zs->zs_offset = 0;
1.1       deraadt   417:
1.5     ! mickey    418:                        if (zs->zs_clear_flg) {
        !           419:                                zs->zs_maxcode =
        !           420:                                        MAXCODE(zs->zs_n_bits = INIT_BITS);
        !           421:                                zs->zs_clear_flg = 0;
1.1       deraadt   422:                        } else {
1.5     ! mickey    423:                                zs->zs_n_bits++;
        !           424:                                if (zs->zs_n_bits == zs->zs_maxbits)
        !           425:                                        zs->zs_maxcode = zs->zs_maxmaxcode;
1.1       deraadt   426:                                else
1.5     ! mickey    427:                                        zs->zs_maxcode =
        !           428:                                                MAXCODE(zs->zs_n_bits);
1.1       deraadt   429:                        }
                    430:                }
                    431:        } else {
                    432:                /* At EOF, write the rest of the buffer. */
1.5     ! mickey    433:                if (zs->zs_offset > 0) {
        !           434:                        zs->zs_offset = (zs->zs_offset + 7) / 8;
        !           435:                        if (write(zs->zs_fd, zs->zs_buf, zs->zs_offset)
        !           436:                            != zs->zs_offset)
1.1       deraadt   437:                                return (-1);
1.5     ! mickey    438:                        zs->zs_bytes_out += zs->zs_offset;
1.1       deraadt   439:                }
1.5     ! mickey    440:                zs->zs_offset = 0;
1.1       deraadt   441:        }
                    442:        return (0);
                    443: }
                    444:
                    445: /*
                    446:  * Decompress read.  This routine adapts to the codes in the file building
                    447:  * the "string" table on-the-fly; requiring no table to be stored in the
                    448:  * compressed file.  The tables used herein are shared with those of the
                    449:  * compress() routine.  See the definitions above.
                    450:  */
1.5     ! mickey    451: int
1.1       deraadt   452: zread(cookie, rbp, num)
                    453:        void *cookie;
                    454:        char *rbp;
                    455:        int num;
                    456: {
                    457:        register u_int count;
                    458:        struct s_zstate *zs;
                    459:        u_char *bp, header[3];
                    460:
                    461:        if (num == 0)
                    462:                return (0);
                    463:
                    464:        zs = cookie;
                    465:        count = num;
                    466:        bp = (u_char *)rbp;
1.5     ! mickey    467:        switch (zs->zs_state) {
1.1       deraadt   468:        case S_START:
1.5     ! mickey    469:                zs->zs_state = S_MIDDLE;
1.1       deraadt   470:                break;
                    471:        case S_MIDDLE:
                    472:                goto middle;
                    473:        case S_EOF:
                    474:                goto eof;
                    475:        }
                    476:
                    477:        /* Check the magic number */
1.5     ! mickey    478:        if (read(zs->zs_fd, header, sizeof(header)) != sizeof(header) ||
        !           479:            memcmp(header, z_magic, sizeof(z_magic)) != 0) {
1.1       deraadt   480:                errno = EFTYPE;
                    481:                return (-1);
                    482:        }
1.5     ! mickey    483:        zs->zs_maxbits = header[2];     /* Set -b from file. */
        !           484:        zs->zs_block_compress = zs->zs_maxbits & BLOCK_MASK;
        !           485:        zs->zs_maxbits &= BIT_MASK;
        !           486:        zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
        !           487:        if (zs->zs_maxbits > BITS) {
1.1       deraadt   488:                errno = EFTYPE;
                    489:                return (-1);
                    490:        }
                    491:        /* As above, initialize the first 256 entries in the table. */
1.5     ! mickey    492:        zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
        !           493:        for (zs->zs_code = 255; zs->zs_code >= 0; zs->zs_code--) {
        !           494:                tab_prefixof(zs->zs_code) = 0;
        !           495:                tab_suffixof(zs->zs_code) = (u_char) zs->zs_code;
1.1       deraadt   496:        }
1.5     ! mickey    497:        zs->zs_free_ent = zs->zs_block_compress ? FIRST : 256;
1.1       deraadt   498:
1.5     ! mickey    499:        zs->zs_finchar = zs->zs_oldcode = getcode(zs);
        !           500:        if (zs->zs_oldcode == -1)       /* EOF already? */
1.1       deraadt   501:                return (0);     /* Get out of here */
                    502:
                    503:        /* First code must be 8 bits = char. */
1.5     ! mickey    504:        *bp++ = (u_char)zs->zs_finchar;
1.1       deraadt   505:        count--;
1.5     ! mickey    506:        zs->zs_stackp = de_stack;
1.1       deraadt   507:
1.5     ! mickey    508:        while ((zs->zs_code = getcode(zs)) > -1) {
1.1       deraadt   509:
1.5     ! mickey    510:                if ((zs->zs_code == CLEAR) && zs->zs_block_compress) {
        !           511:                        for (zs->zs_code = 255; zs->zs_code >= 0;
        !           512:                             zs->zs_code--)
        !           513:                                tab_prefixof(zs->zs_code) = 0;
        !           514:                        zs->zs_clear_flg = 1;
        !           515:                        zs->zs_free_ent = FIRST - 1;
        !           516:                        if ((zs->zs_code = getcode(zs)) == -1)  /* O, untimely death! */
1.1       deraadt   517:                                break;
                    518:                }
1.5     ! mickey    519:                zs->zs_incode = zs->zs_code;
1.1       deraadt   520:
                    521:                /* Special case for KwKwK string. */
1.5     ! mickey    522:                if (zs->zs_code >= zs->zs_free_ent) {
        !           523:                        *zs->zs_stackp++ = zs->zs_finchar;
        !           524:                        zs->zs_code = zs->zs_oldcode;
1.1       deraadt   525:                }
                    526:
                    527:                /* Generate output characters in reverse order. */
1.5     ! mickey    528:                while (zs->zs_code >= 256) {
        !           529:                        *zs->zs_stackp++ = tab_suffixof(zs->zs_code);
        !           530:                        zs->zs_code = tab_prefixof(zs->zs_code);
1.1       deraadt   531:                }
1.5     ! mickey    532:                *zs->zs_stackp++ = zs->zs_finchar = tab_suffixof(zs->zs_code);
1.1       deraadt   533:
                    534:                /* And put them out in forward order.  */
                    535: middle:                do {
                    536:                        if (count-- == 0)
                    537:                                return (num);
1.5     ! mickey    538:                        *bp++ = *--zs->zs_stackp;
        !           539:                } while (zs->zs_stackp > de_stack);
1.1       deraadt   540:
                    541:                /* Generate the new entry. */
1.5     ! mickey    542:                if ((zs->zs_code = zs->zs_free_ent) < zs->zs_maxmaxcode) {
        !           543:                        tab_prefixof(zs->zs_code) = (u_short) zs->zs_oldcode;
        !           544:                        tab_suffixof(zs->zs_code) = zs->zs_finchar;
        !           545:                        zs->zs_free_ent = zs->zs_code + 1;
1.1       deraadt   546:                }
                    547:
                    548:                /* Remember previous code. */
1.5     ! mickey    549:                zs->zs_oldcode = zs->zs_incode;
1.1       deraadt   550:        }
1.5     ! mickey    551:        zs->zs_state = S_EOF;
1.1       deraadt   552: eof:   return (num - count);
                    553: }
                    554:
                    555: /*-
                    556:  * Read one code from the standard input.  If EOF, return -1.
                    557:  * Inputs:
                    558:  *     stdin
                    559:  * Outputs:
                    560:  *     code or -1 is returned.
                    561:  */
                    562: static code_int
                    563: getcode(zs)
1.5     ! mickey    564:        register struct s_zstate *zs;
1.1       deraadt   565: {
                    566:        register code_int gcode;
                    567:        register int r_off, bits;
1.5     ! mickey    568:        register u_char *bp;
1.1       deraadt   569:
1.5     ! mickey    570:        bp = zs->zs_buf;
        !           571:        if (zs->zs_clear_flg > 0 || zs->zs_roffset >= zs->zs_size ||
        !           572:            zs->zs_free_ent > zs->zs_maxcode) {
1.1       deraadt   573:                /*
                    574:                 * If the next entry will be too big for the current gcode
                    575:                 * size, then we must increase the size.  This implies reading
                    576:                 * a new buffer full, too.
                    577:                 */
1.5     ! mickey    578:                if (zs->zs_free_ent > zs->zs_maxcode) {
        !           579:                        zs->zs_n_bits++;
        !           580:                        if (zs->zs_n_bits == zs->zs_maxbits)    /* Won't get any bigger now. */
        !           581:                                zs->zs_maxcode = zs->zs_maxmaxcode;
1.1       deraadt   582:                        else
1.5     ! mickey    583:                                zs->zs_maxcode = MAXCODE(zs->zs_n_bits);
1.1       deraadt   584:                }
1.5     ! mickey    585:                if (zs->zs_clear_flg > 0) {
        !           586:                        zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
        !           587:                        zs->zs_clear_flg = 0;
1.1       deraadt   588:                }
1.5     ! mickey    589:                zs->zs_size = read(zs->zs_fd, zs->zs_buf, zs->zs_n_bits);
        !           590:                if (zs->zs_size <= 0)                   /* End of file. */
1.1       deraadt   591:                        return (-1);
1.5     ! mickey    592:                zs->zs_roffset = 0;
1.1       deraadt   593:                /* Round size down to integral number of codes. */
1.5     ! mickey    594:                zs->zs_size = (zs->zs_size << 3) - (zs->zs_n_bits - 1);
1.1       deraadt   595:        }
1.5     ! mickey    596:        r_off = zs->zs_roffset;
        !           597:        bits = zs->zs_n_bits;
1.1       deraadt   598:
                    599:        /* Get to the first byte. */
                    600:        bp += (r_off >> 3);
                    601:        r_off &= 7;
                    602:
                    603:        /* Get first part (low order bits). */
                    604:        gcode = (*bp++ >> r_off);
                    605:        bits -= (8 - r_off);
                    606:        r_off = 8 - r_off;      /* Now, roffset into gcode word. */
                    607:
                    608:        /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
                    609:        if (bits >= 8) {
                    610:                gcode |= *bp++ << r_off;
                    611:                r_off += 8;
                    612:                bits -= 8;
                    613:        }
                    614:
                    615:        /* High order bits. */
                    616:        gcode |= (*bp & rmask[bits]) << r_off;
1.5     ! mickey    617:        zs->zs_roffset += zs->zs_n_bits;
1.1       deraadt   618:
                    619:        return (gcode);
                    620: }
                    621:
                    622: static int
                    623: cl_block(zs)                   /* Table clear for block compress. */
1.5     ! mickey    624:        register struct s_zstate *zs;
1.1       deraadt   625: {
                    626:        register long rat;
                    627:
1.5     ! mickey    628:        zs->zs_checkpoint = zs->zs_in_count + CHECK_GAP;
1.1       deraadt   629:
1.5     ! mickey    630:        if (zs->zs_in_count > 0x007fffff) {     /* Shift will overflow. */
        !           631:                rat = zs->zs_bytes_out >> 8;
1.1       deraadt   632:                if (rat == 0)           /* Don't divide by zero. */
                    633:                        rat = 0x7fffffff;
                    634:                else
1.5     ! mickey    635:                        rat = zs->zs_in_count / rat;
1.1       deraadt   636:        } else
1.5     ! mickey    637:                rat = (zs->zs_in_count << 8) / zs->zs_bytes_out;        /* 8 fractional bits. */
        !           638:        if (rat > zs->zs_ratio)
        !           639:                zs->zs_ratio = rat;
1.1       deraadt   640:        else {
1.5     ! mickey    641:                zs->zs_ratio = 0;
        !           642:                cl_hash(zs, (count_int) zs->zs_hsize);
        !           643:                zs->zs_free_ent = FIRST;
        !           644:                zs->zs_clear_flg = 1;
1.1       deraadt   645:                if (output(zs, (code_int) CLEAR) == -1)
                    646:                        return (-1);
                    647:        }
                    648:        return (0);
                    649: }
                    650:
                    651: static void
                    652: cl_hash(zs, cl_hsize)                  /* Reset code table. */
1.5     ! mickey    653:        register struct s_zstate *zs;
1.1       deraadt   654:        register count_int cl_hsize;
                    655: {
                    656:        register count_int *htab_p;
                    657:        register long i, m1;
                    658:
                    659:        m1 = -1;
1.5     ! mickey    660:        htab_p = zs->zs_htab + cl_hsize;
1.1       deraadt   661:        i = cl_hsize - 16;
                    662:        do {                    /* Might use Sys V memset(3) here. */
                    663:                *(htab_p - 16) = m1;
                    664:                *(htab_p - 15) = m1;
                    665:                *(htab_p - 14) = m1;
                    666:                *(htab_p - 13) = m1;
                    667:                *(htab_p - 12) = m1;
                    668:                *(htab_p - 11) = m1;
                    669:                *(htab_p - 10) = m1;
                    670:                *(htab_p - 9) = m1;
                    671:                *(htab_p - 8) = m1;
                    672:                *(htab_p - 7) = m1;
                    673:                *(htab_p - 6) = m1;
                    674:                *(htab_p - 5) = m1;
                    675:                *(htab_p - 4) = m1;
                    676:                *(htab_p - 3) = m1;
                    677:                *(htab_p - 2) = m1;
                    678:                *(htab_p - 1) = m1;
                    679:                htab_p -= 16;
                    680:        } while ((i -= 16) >= 0);
                    681:        for (i += 16; i > 0; i--)
                    682:                *--htab_p = m1;
1.3       tholo     683: }
                    684:
                    685: FILE *
1.5     ! mickey    686: zopen(name, mode, bits)
        !           687:        const char *name;
        !           688:        const char *mode;
        !           689:        int bits;
        !           690: {
        !           691:        int fd;
        !           692:        void *cookie;
        !           693:        if ((fd = open(name, (*mode=='r'? O_RDONLY:O_WRONLY|O_CREAT),
        !           694:                       S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1)
        !           695:                return NULL;
        !           696:        if ((cookie = z_open(fd, mode, bits)) == NULL) {
        !           697:                close(fd);
        !           698:                return NULL;
        !           699:        }
        !           700:        return funopen(cookie, (*mode == 'r'?zread:NULL),
        !           701:                       (*mode == 'w'?zwrite:NULL), NULL, zclose);
        !           702: }
        !           703:
        !           704: void *
        !           705: z_open(fd, mode, bits)
1.3       tholo     706:        int fd;
                    707:        const char *mode;
                    708:        int bits;
                    709: {
1.5     ! mickey    710:        register struct s_zstate *zs;
1.3       tholo     711:
1.4       millert   712:        if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
1.3       tholo     713:            bits < 0 || bits > BITS) {
                    714:                errno = EINVAL;
                    715:                return (NULL);
                    716:        }
                    717:
                    718:        if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
                    719:                return (NULL);
                    720:
1.5     ! mickey    721:        /* User settable max # bits/code. */
        !           722:        zs->zs_maxbits = bits ? bits : BITS;
        !           723:        /* Should NEVER generate this code. */
        !           724:        zs->zs_maxmaxcode = 1 << zs->zs_maxbits;
        !           725:        zs->zs_hsize = HSIZE;           /* For dynamic table sizing. */
        !           726:        zs->zs_free_ent = 0;            /* First unused entry. */
        !           727:        zs->zs_block_compress = BLOCK_MASK;
        !           728:        zs->zs_clear_flg = 0;
        !           729:        zs->zs_ratio = 0;
        !           730:        zs->zs_checkpoint = CHECK_GAP;
        !           731:        zs->zs_in_count = 1;            /* Length of input. */
        !           732:        zs->zs_out_count = 0;           /* # of codes output (for debugging).*/
        !           733:        zs->zs_state = S_START;
        !           734:        zs->zs_roffset = 0;
        !           735:        zs->zs_size = 0;
        !           736:        zs->zs_mode = mode[0];
1.3       tholo     737:
1.5     ! mickey    738:        zs->zs_fd = fd;
        !           739:        return zs;
1.1       deraadt   740: }
                    741:
1.5     ! mickey    742: int
        !           743: z_check_header(fd, sb, ofn)
        !           744:        int fd;
        !           745:        struct stat *sb;
        !           746:        const char *ofn;
1.1       deraadt   747: {
1.5     ! mickey    748:        int f;
        !           749:        u_char buf[sizeof(z_magic)];
        !           750:        off_t off = lseek(fd, 0, SEEK_CUR);
1.1       deraadt   751:
1.5     ! mickey    752:        f = (read(fd, buf, sizeof(buf)) == sizeof(buf) &&
        !           753:             !memcmp(buf, z_magic, sizeof(buf)));
1.1       deraadt   754:
1.5     ! mickey    755:        lseek (fd, off, SEEK_SET);
1.1       deraadt   756:
1.5     ! mickey    757:        return f;
1.1       deraadt   758: }