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

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