[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.9

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