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

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