1*adbc1403Sandvar /* $NetBSD: zopen.c,v 1.16 2022/03/23 11:08:28 andvar Exp $ */
27025c9c2Sglass
33b93c56bScgd /*-
43b93c56bScgd * Copyright (c) 1985, 1986, 1992, 1993
53b93c56bScgd * The Regents of the University of California. All rights reserved.
63b93c56bScgd *
73b93c56bScgd * This code is derived from software contributed to Berkeley by
83b93c56bScgd * Diomidis Spinellis and James A. Woods, derived from original
93b93c56bScgd * work by Spencer Thomas and Joseph Orost.
103b93c56bScgd *
113b93c56bScgd * Redistribution and use in source and binary forms, with or without
123b93c56bScgd * modification, are permitted provided that the following conditions
133b93c56bScgd * are met:
143b93c56bScgd * 1. Redistributions of source code must retain the above copyright
153b93c56bScgd * notice, this list of conditions and the following disclaimer.
163b93c56bScgd * 2. Redistributions in binary form must reproduce the above copyright
173b93c56bScgd * notice, this list of conditions and the following disclaimer in the
183b93c56bScgd * documentation and/or other materials provided with the distribution.
1989aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
203b93c56bScgd * may be used to endorse or promote products derived from this software
213b93c56bScgd * without specific prior written permission.
223b93c56bScgd *
233b93c56bScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
243b93c56bScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
253b93c56bScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
263b93c56bScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
273b93c56bScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
283b93c56bScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
293b93c56bScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
303b93c56bScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
313b93c56bScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
323b93c56bScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
333b93c56bScgd * SUCH DAMAGE.
343b93c56bScgd */
353b93c56bScgd
363b93c56bScgd #if defined(LIBC_SCCS) && !defined(lint)
377025c9c2Sglass #if 0
387025c9c2Sglass static char sccsid[] = "@(#)zopen.c 8.1 (Berkeley) 6/27/93";
397025c9c2Sglass #else
40*adbc1403Sandvar static char rcsid[] = "$NetBSD: zopen.c,v 1.16 2022/03/23 11:08:28 andvar Exp $";
417025c9c2Sglass #endif
423b93c56bScgd #endif /* LIBC_SCCS and not lint */
433b93c56bScgd
443b93c56bScgd /*-
453b93c56bScgd * fcompress.c - File compression ala IEEE Computer, June 1984.
463b93c56bScgd *
473b93c56bScgd * Compress authors:
483b93c56bScgd * Spencer W. Thomas (decvax!utah-cs!thomas)
493b93c56bScgd * Jim McKie (decvax!mcvax!jim)
503b93c56bScgd * Steve Davies (decvax!vax135!petsd!peora!srd)
513b93c56bScgd * Ken Turkowski (decvax!decwrl!turtlevax!ken)
523b93c56bScgd * James A. Woods (decvax!ihnp4!ames!jaw)
533b93c56bScgd * Joe Orost (decvax!vax135!petsd!joe)
543b93c56bScgd *
553b93c56bScgd * Cleaned up and converted to library returning I/O streams by
563b93c56bScgd * Diomidis Spinellis <dds@doc.ic.ac.uk>.
573b93c56bScgd *
583b93c56bScgd * zopen(filename, mode, bits)
593b93c56bScgd * Returns a FILE * that can be used for read or write. The modes
603b93c56bScgd * supported are only "r" and "w". Seeking is not allowed. On
613b93c56bScgd * reading the file is decompressed, on writing it is compressed.
623b93c56bScgd * The output is compatible with compress(1) with 16 bit tables.
633b93c56bScgd * Any file produced by compress(1) can be read.
643b93c56bScgd */
653b93c56bScgd
663b93c56bScgd #include <sys/param.h>
673b93c56bScgd #include <sys/stat.h>
683b93c56bScgd
693b93c56bScgd #include <errno.h>
703b93c56bScgd #include <signal.h>
713b93c56bScgd #include <stdio.h>
723b93c56bScgd #include <stdlib.h>
733b93c56bScgd #include <string.h>
743b93c56bScgd #include <unistd.h>
753b93c56bScgd
763b93c56bScgd #define BITS 16 /* Default bits. */
773b93c56bScgd #define HSIZE 69001 /* 95% occupancy */
783b93c56bScgd
793b93c56bScgd /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
803b93c56bScgd typedef long code_int;
813b93c56bScgd typedef long count_int;
823b93c56bScgd
833b93c56bScgd typedef u_char char_type;
843b93c56bScgd static char_type magic_header[] =
853b93c56bScgd {'\037', '\235'}; /* 1F 9D */
863b93c56bScgd
873b93c56bScgd #define BIT_MASK 0x1f /* Defines for third byte of header. */
883b93c56bScgd #define BLOCK_MASK 0x80
893b93c56bScgd
903b93c56bScgd /*
913b93c56bScgd * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
923b93c56bScgd * a fourth header byte (for expansion).
933b93c56bScgd */
943b93c56bScgd #define INIT_BITS 9 /* Initial number of bits/code. */
953b93c56bScgd
963b93c56bScgd #define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
973b93c56bScgd
983b93c56bScgd struct s_zstate {
993b93c56bScgd FILE *zs_fp; /* File stream for I/O */
1003b93c56bScgd char zs_mode; /* r or w */
1013b93c56bScgd enum {
1023b93c56bScgd S_START, S_MIDDLE, S_EOF
1033b93c56bScgd } zs_state; /* State of computation */
1043b93c56bScgd int zs_n_bits; /* Number of bits/code. */
1053b93c56bScgd int zs_maxbits; /* User settable max # bits/code. */
1063b93c56bScgd code_int zs_maxcode; /* Maximum code, given n_bits. */
1073b93c56bScgd code_int zs_maxmaxcode; /* Should NEVER generate this code. */
1083b93c56bScgd count_int zs_htab [HSIZE];
1093b93c56bScgd u_short zs_codetab [HSIZE];
1103b93c56bScgd code_int zs_hsize; /* For dynamic table sizing. */
1113b93c56bScgd code_int zs_free_ent; /* First unused entry. */
1123b93c56bScgd /*
1133b93c56bScgd * Block compression parameters -- after all codes are used up,
1143b93c56bScgd * and compression rate changes, start over.
1153b93c56bScgd */
1163b93c56bScgd int zs_block_compress;
1173b93c56bScgd int zs_clear_flg;
1183b93c56bScgd long zs_ratio;
1193b93c56bScgd count_int zs_checkpoint;
1203b93c56bScgd int zs_offset;
1213b93c56bScgd long zs_in_count; /* Length of input. */
1223b93c56bScgd long zs_bytes_out; /* Length of compressed output. */
1233b93c56bScgd long zs_out_count; /* # of codes output (for debugging). */
1243b93c56bScgd char_type zs_buf[BITS];
1253b93c56bScgd union {
1263b93c56bScgd struct {
1273b93c56bScgd long zs_fcode;
1283b93c56bScgd code_int zs_ent;
1293b93c56bScgd code_int zs_hsize_reg;
1303b93c56bScgd int zs_hshift;
131*adbc1403Sandvar } w; /* Write parameters */
1323b93c56bScgd struct {
1333b93c56bScgd char_type *zs_stackp;
1343b93c56bScgd int zs_finchar;
1353b93c56bScgd code_int zs_code, zs_oldcode, zs_incode;
1363b93c56bScgd int zs_roffset, zs_size;
1373b93c56bScgd char_type zs_gbuf[BITS];
1383b93c56bScgd } r; /* Read parameters */
1393b93c56bScgd } u;
1403b93c56bScgd };
1413b93c56bScgd
1423b93c56bScgd /* Definitions to retain old variable names */
1433b93c56bScgd #define fp zs->zs_fp
1443b93c56bScgd #define zmode zs->zs_mode
1453b93c56bScgd #define state zs->zs_state
1463b93c56bScgd #define n_bits zs->zs_n_bits
1473b93c56bScgd #define maxbits zs->zs_maxbits
1483b93c56bScgd #define maxcode zs->zs_maxcode
1493b93c56bScgd #define maxmaxcode zs->zs_maxmaxcode
1503b93c56bScgd #define htab zs->zs_htab
1513b93c56bScgd #define codetab zs->zs_codetab
1523b93c56bScgd #define hsize zs->zs_hsize
1533b93c56bScgd #define free_ent zs->zs_free_ent
1543b93c56bScgd #define block_compress zs->zs_block_compress
1553b93c56bScgd #define clear_flg zs->zs_clear_flg
1563b93c56bScgd #define ratio zs->zs_ratio
1573b93c56bScgd #define checkpoint zs->zs_checkpoint
1583b93c56bScgd #define offset zs->zs_offset
1593b93c56bScgd #define in_count zs->zs_in_count
1603b93c56bScgd #define bytes_out zs->zs_bytes_out
1613b93c56bScgd #define out_count zs->zs_out_count
1623b93c56bScgd #define buf zs->zs_buf
1633b93c56bScgd #define fcode zs->u.w.zs_fcode
1643b93c56bScgd #define hsize_reg zs->u.w.zs_hsize_reg
1653b93c56bScgd #define ent zs->u.w.zs_ent
1663b93c56bScgd #define hshift zs->u.w.zs_hshift
1673b93c56bScgd #define stackp zs->u.r.zs_stackp
1683b93c56bScgd #define finchar zs->u.r.zs_finchar
1693b93c56bScgd #define code zs->u.r.zs_code
1703b93c56bScgd #define oldcode zs->u.r.zs_oldcode
1713b93c56bScgd #define incode zs->u.r.zs_incode
1723b93c56bScgd #define roffset zs->u.r.zs_roffset
1733b93c56bScgd #define size zs->u.r.zs_size
1743b93c56bScgd #define gbuf zs->u.r.zs_gbuf
1753b93c56bScgd
1763b93c56bScgd /*
1773b93c56bScgd * To save much memory, we overlay the table used by compress() with those
1783b93c56bScgd * used by decompress(). The tab_prefix table is the same size and type as
1793b93c56bScgd * the codetab. The tab_suffix table needs 2**BITS characters. We get this
1803b93c56bScgd * from the beginning of htab. The output stack uses the rest of htab, and
1813b93c56bScgd * contains characters. There is plenty of room for any possible stack
1823b93c56bScgd * (stack used to be 8000 characters).
1833b93c56bScgd */
1843b93c56bScgd
1853b93c56bScgd #define htabof(i) htab[i]
1863b93c56bScgd #define codetabof(i) codetab[i]
1873b93c56bScgd
1883b93c56bScgd #define tab_prefixof(i) codetabof(i)
1893b93c56bScgd #define tab_suffixof(i) ((char_type *)(htab))[i]
1903b93c56bScgd #define de_stack ((char_type *)&tab_suffixof(1 << BITS))
1913b93c56bScgd
1923b93c56bScgd #define CHECK_GAP 10000 /* Ratio check interval. */
1933b93c56bScgd
1943b93c56bScgd /*
1953b93c56bScgd * the next two codes should not be changed lightly, as they must not
1963b93c56bScgd * lie within the contiguous general code space.
1973b93c56bScgd */
1983b93c56bScgd #define FIRST 257 /* First free entry. */
1993b93c56bScgd #define CLEAR 256 /* Table clear output code. */
2003b93c56bScgd
201774f88b1Swiz static int cl_block(struct s_zstate *);
202774f88b1Swiz static code_int getcode(struct s_zstate *);
203774f88b1Swiz static int output(struct s_zstate *, code_int);
204774f88b1Swiz static int zclose(void *);
205774f88b1Swiz FILE *zopen(const char *, const char *, int);
206774f88b1Swiz static int zread(void *, char *, int);
207774f88b1Swiz static int zwrite(void *, const char *, int);
2083b93c56bScgd
2093b93c56bScgd /*-
2103b93c56bScgd * Algorithm from "A Technique for High Performance Data Compression",
2113b93c56bScgd * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
2123b93c56bScgd *
2133b93c56bScgd * Algorithm:
2143b93c56bScgd * Modified Lempel-Ziv method (LZW). Basically finds common
2153b93c56bScgd * substrings and replaces them with a variable size code. This is
2163b93c56bScgd * deterministic, and can be done on the fly. Thus, the decompression
2173b93c56bScgd * procedure needs no input table, but tracks the way the table was built.
2183b93c56bScgd */
2193b93c56bScgd
2203b93c56bScgd /*-
2213b93c56bScgd * compress write
2223b93c56bScgd *
2233b93c56bScgd * Algorithm: use open addressing double hashing (no chaining) on the
2243b93c56bScgd * prefix code / next character combination. We do a variant of Knuth's
2253b93c56bScgd * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
2263b93c56bScgd * secondary probe. Here, the modular division first probe is gives way
2273b93c56bScgd * to a faster exclusive-or manipulation. Also do block compression with
2283b93c56bScgd * an adaptive reset, whereby the code table is cleared when the compression
2293b93c56bScgd * ratio decreases, but after the table fills. The variable-length output
2303b93c56bScgd * codes are re-sized at this point, and a special CLEAR code is generated
2313b93c56bScgd * for the decompressor. Late addition: construct the table according to
2323b93c56bScgd * file size for noticeable speed improvement on small files. Please direct
2333b93c56bScgd * questions about this implementation to ames!jaw.
2343b93c56bScgd */
2353b93c56bScgd static int
zwrite(void * cookie,const char * wbp,int num)236774f88b1Swiz zwrite(void *cookie, const char *wbp, int num)
2373b93c56bScgd {
23887f4122cSlukem code_int i;
23987f4122cSlukem int c, disp;
2403b93c56bScgd struct s_zstate *zs;
2413b93c56bScgd const u_char *bp;
2423b93c56bScgd u_char tmp;
2433b93c56bScgd int count;
2443b93c56bScgd
2453b93c56bScgd if (num == 0)
2463b93c56bScgd return (0);
2473b93c56bScgd
2483b93c56bScgd zs = cookie;
2493b93c56bScgd count = num;
25036869194Slukem bp = (const u_char *)wbp;
2513b93c56bScgd if (state == S_MIDDLE)
2523b93c56bScgd goto middle;
2533b93c56bScgd state = S_MIDDLE;
2543b93c56bScgd
25597e6ca09Sandrew maxmaxcode = 1L << maxbits;
2563b93c56bScgd if (fwrite(magic_header,
2573b93c56bScgd sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
2583b93c56bScgd return (-1);
25997e6ca09Sandrew tmp = (u_char)(maxbits | block_compress);
2603b93c56bScgd if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
2613b93c56bScgd return (-1);
2623b93c56bScgd
2633b93c56bScgd offset = 0;
2643b93c56bScgd bytes_out = 3; /* Includes 3-byte header mojo. */
2653b93c56bScgd out_count = 0;
2663b93c56bScgd clear_flg = 0;
2673b93c56bScgd ratio = 0;
2683b93c56bScgd in_count = 1;
2693b93c56bScgd checkpoint = CHECK_GAP;
2703b93c56bScgd maxcode = MAXCODE(n_bits = INIT_BITS);
2713b93c56bScgd free_ent = ((block_compress) ? FIRST : 256);
2723b93c56bScgd
2733b93c56bScgd ent = *bp++;
2743b93c56bScgd --count;
2753b93c56bScgd
2763b93c56bScgd hshift = 0;
2773b93c56bScgd for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
2783b93c56bScgd hshift++;
2793b93c56bScgd hshift = 8 - hshift; /* Set hash code range bound. */
2803b93c56bScgd
2813b93c56bScgd hsize_reg = hsize;
282199b0f67Sjoerg memset(htab, 0xff, hsize_reg * sizeof(count_int));
2833b93c56bScgd
2844e2cb195Sjoerg middle: while (count--) {
2853b93c56bScgd c = *bp++;
2863b93c56bScgd in_count++;
2873b93c56bScgd fcode = (long)(((long)c << maxbits) + ent);
2883b93c56bScgd i = ((c << hshift) ^ ent); /* Xor hashing. */
2893b93c56bScgd
2903b93c56bScgd if (htabof(i) == fcode) {
2913b93c56bScgd ent = codetabof(i);
2923b93c56bScgd continue;
2933b93c56bScgd } else if ((long)htabof(i) < 0) /* Empty slot. */
2943b93c56bScgd goto nomatch;
2953b93c56bScgd disp = hsize_reg - i; /* Secondary hash (after G. Knott). */
2963b93c56bScgd if (i == 0)
2973b93c56bScgd disp = 1;
2983b93c56bScgd probe: if ((i -= disp) < 0)
2993b93c56bScgd i += hsize_reg;
3003b93c56bScgd
3013b93c56bScgd if (htabof(i) == fcode) {
3023b93c56bScgd ent = codetabof(i);
3033b93c56bScgd continue;
3043b93c56bScgd }
3053b93c56bScgd if ((long)htabof(i) >= 0)
3063b93c56bScgd goto probe;
3073b93c56bScgd nomatch: if (output(zs, (code_int) ent) == -1)
3083b93c56bScgd return (-1);
3093b93c56bScgd out_count++;
3103b93c56bScgd ent = c;
3113b93c56bScgd if (free_ent < maxmaxcode) {
3123b93c56bScgd codetabof(i) = free_ent++; /* code -> hashtable */
3133b93c56bScgd htabof(i) = fcode;
3143b93c56bScgd } else if ((count_int)in_count >=
3153b93c56bScgd checkpoint && block_compress) {
3163b93c56bScgd if (cl_block(zs) == -1)
3173b93c56bScgd return (-1);
3183b93c56bScgd }
3193b93c56bScgd }
3203b93c56bScgd return (num);
3213b93c56bScgd }
3223b93c56bScgd
3233b93c56bScgd static int
zclose(void * cookie)324774f88b1Swiz zclose(void *cookie)
3253b93c56bScgd {
3263b93c56bScgd struct s_zstate *zs;
3273b93c56bScgd int rval;
3283b93c56bScgd
3293b93c56bScgd zs = cookie;
3303b93c56bScgd if (zmode == 'w') { /* Put out the final code. */
3313b93c56bScgd if (output(zs, (code_int) ent) == -1) {
3323b93c56bScgd (void)fclose(fp);
3333b93c56bScgd free(zs);
3343b93c56bScgd return (-1);
3353b93c56bScgd }
3363b93c56bScgd out_count++;
3373b93c56bScgd if (output(zs, (code_int) - 1) == -1) {
3383b93c56bScgd (void)fclose(fp);
3393b93c56bScgd free(zs);
3403b93c56bScgd return (-1);
3413b93c56bScgd }
3423b93c56bScgd }
3433b93c56bScgd rval = fclose(fp) == EOF ? -1 : 0;
3443b93c56bScgd free(zs);
3453b93c56bScgd return (rval);
3463b93c56bScgd }
3473b93c56bScgd
3483b93c56bScgd /*-
3493b93c56bScgd * Output the given code.
3503b93c56bScgd * Inputs:
3513b93c56bScgd * code: A n_bits-bit integer. If == -1, then EOF. This assumes
3523b93c56bScgd * that n_bits =< (long)wordsize - 1.
3533b93c56bScgd * Outputs:
3543b93c56bScgd * Outputs code to the file.
3553b93c56bScgd * Assumptions:
3563b93c56bScgd * Chars are 8 bits long.
3573b93c56bScgd * Algorithm:
3583b93c56bScgd * Maintain a BITS character long buffer (so that 8 codes will
3593b93c56bScgd * fit in it exactly). Use the VAX insv instruction to insert each
3603b93c56bScgd * code in turn. When the buffer fills up empty it and start over.
3613b93c56bScgd */
3623b93c56bScgd
3633b93c56bScgd static char_type lmask[9] =
3643b93c56bScgd {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
3653b93c56bScgd static char_type rmask[9] =
3663b93c56bScgd {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
3673b93c56bScgd
3683b93c56bScgd static int
output(struct s_zstate * zs,code_int ocode)369774f88b1Swiz output(struct s_zstate *zs, code_int ocode)
3703b93c56bScgd {
37187f4122cSlukem int bits, r_off;
37287f4122cSlukem char_type *bp;
3733b93c56bScgd
3743b93c56bScgd r_off = offset;
3753b93c56bScgd bits = n_bits;
3763b93c56bScgd bp = buf;
3773b93c56bScgd if (ocode >= 0) {
3783b93c56bScgd /* Get to the first byte. */
3793b93c56bScgd bp += (r_off >> 3);
3803b93c56bScgd r_off &= 7;
3813b93c56bScgd /*
3823b93c56bScgd * Since ocode is always >= 8 bits, only need to mask the first
3833b93c56bScgd * hunk on the left.
3843b93c56bScgd */
38587f4122cSlukem *bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
3863b93c56bScgd bp++;
3873b93c56bScgd bits -= (8 - r_off);
3883b93c56bScgd ocode >>= 8 - r_off;
3893b93c56bScgd /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
3903b93c56bScgd if (bits >= 8) {
3913b93c56bScgd *bp++ = ocode;
3923b93c56bScgd ocode >>= 8;
3933b93c56bScgd bits -= 8;
3943b93c56bScgd }
3953b93c56bScgd /* Last bits. */
3963b93c56bScgd if (bits)
3973b93c56bScgd *bp = ocode;
3983b93c56bScgd offset += n_bits;
3993b93c56bScgd if (offset == (n_bits << 3)) {
4003b93c56bScgd bp = buf;
4013b93c56bScgd bits = n_bits;
4023b93c56bScgd bytes_out += bits;
40336869194Slukem if (fwrite(bp, sizeof(char), bits, fp) != (size_t)bits)
4043b93c56bScgd return (-1);
4053b93c56bScgd bp += bits;
4063b93c56bScgd bits = 0;
4073b93c56bScgd offset = 0;
4083b93c56bScgd }
4093b93c56bScgd /*
4103b93c56bScgd * If the next entry is going to be too big for the ocode size,
4113b93c56bScgd * then increase it, if possible.
4123b93c56bScgd */
4133b93c56bScgd if (free_ent > maxcode || (clear_flg > 0)) {
4143b93c56bScgd /*
4153b93c56bScgd * Write the whole buffer, because the input side won't
4163b93c56bScgd * discover the size increase until after it has read it.
4173b93c56bScgd */
4183b93c56bScgd if (offset > 0) {
41936869194Slukem if (fwrite(buf, 1, n_bits, fp) != (size_t)n_bits)
4203b93c56bScgd return (-1);
4213b93c56bScgd bytes_out += n_bits;
4223b93c56bScgd }
4233b93c56bScgd offset = 0;
4243b93c56bScgd
4253b93c56bScgd if (clear_flg) {
4263b93c56bScgd maxcode = MAXCODE(n_bits = INIT_BITS);
4273b93c56bScgd clear_flg = 0;
4283b93c56bScgd } else {
4293b93c56bScgd n_bits++;
4303b93c56bScgd if (n_bits == maxbits)
4313b93c56bScgd maxcode = maxmaxcode;
4323b93c56bScgd else
4333b93c56bScgd maxcode = MAXCODE(n_bits);
4343b93c56bScgd }
4353b93c56bScgd }
4363b93c56bScgd } else {
4373b93c56bScgd /* At EOF, write the rest of the buffer. */
4383b93c56bScgd if (offset > 0) {
4393b93c56bScgd offset = (offset + 7) / 8;
44036869194Slukem if (fwrite(buf, 1, offset, fp) != (size_t)offset)
4413b93c56bScgd return (-1);
4423b93c56bScgd bytes_out += offset;
4433b93c56bScgd }
4443b93c56bScgd offset = 0;
4453b93c56bScgd }
4463b93c56bScgd return (0);
4473b93c56bScgd }
4483b93c56bScgd
4493b93c56bScgd /*
4503b93c56bScgd * Decompress read. This routine adapts to the codes in the file building
4513b93c56bScgd * the "string" table on-the-fly; requiring no table to be stored in the
4523b93c56bScgd * compressed file. The tables used herein are shared with those of the
4533b93c56bScgd * compress() routine. See the definitions above.
4543b93c56bScgd */
4553b93c56bScgd static int
zread(void * cookie,char * rbp,int num)456774f88b1Swiz zread(void *cookie, char *rbp, int num)
4573b93c56bScgd {
45887f4122cSlukem u_int count;
4593b93c56bScgd struct s_zstate *zs;
4603b93c56bScgd u_char *bp, header[3];
4613b93c56bScgd
4623b93c56bScgd if (num == 0)
4633b93c56bScgd return (0);
4643b93c56bScgd
4653b93c56bScgd zs = cookie;
4663b93c56bScgd count = num;
4673b93c56bScgd bp = (u_char *)rbp;
4683b93c56bScgd switch (state) {
4693b93c56bScgd case S_START:
4703b93c56bScgd state = S_MIDDLE;
4713b93c56bScgd break;
4723b93c56bScgd case S_MIDDLE:
4733b93c56bScgd goto middle;
4743b93c56bScgd case S_EOF:
4753b93c56bScgd goto eof;
4763b93c56bScgd }
4773b93c56bScgd
4783b93c56bScgd /* Check the magic number */
4793b93c56bScgd if (fread(header,
4803b93c56bScgd sizeof(char), sizeof(header), fp) != sizeof(header) ||
4813b93c56bScgd memcmp(header, magic_header, sizeof(magic_header)) != 0) {
4823b93c56bScgd errno = EFTYPE;
4833b93c56bScgd return (-1);
4843b93c56bScgd }
4853b93c56bScgd maxbits = header[2]; /* Set -b from file. */
4863b93c56bScgd block_compress = maxbits & BLOCK_MASK;
4873b93c56bScgd maxbits &= BIT_MASK;
4883b93c56bScgd maxmaxcode = 1L << maxbits;
48909b543b0Sjoerg if (maxbits > BITS || maxbits < 12) {
4903b93c56bScgd errno = EFTYPE;
4913b93c56bScgd return (-1);
4923b93c56bScgd }
4933b93c56bScgd /* As above, initialize the first 256 entries in the table. */
4943b93c56bScgd maxcode = MAXCODE(n_bits = INIT_BITS);
4953b93c56bScgd for (code = 255; code >= 0; code--) {
4963b93c56bScgd tab_prefixof(code) = 0;
4973b93c56bScgd tab_suffixof(code) = (char_type) code;
4983b93c56bScgd }
4993b93c56bScgd free_ent = block_compress ? FIRST : 256;
50009b543b0Sjoerg oldcode = -1;
5013b93c56bScgd stackp = de_stack;
5023b93c56bScgd
5033b93c56bScgd while ((code = getcode(zs)) > -1) {
5043b93c56bScgd
5053b93c56bScgd if ((code == CLEAR) && block_compress) {
5063b93c56bScgd for (code = 255; code >= 0; code--)
5073b93c56bScgd tab_prefixof(code) = 0;
5083b93c56bScgd clear_flg = 1;
50909b543b0Sjoerg free_ent = FIRST;
51009b543b0Sjoerg oldcode = -1;
51109b543b0Sjoerg continue;
5123b93c56bScgd }
5133b93c56bScgd incode = code;
5143b93c56bScgd
51509b543b0Sjoerg /* Special case for kWkWk string. */
5163b93c56bScgd if (code >= free_ent) {
51709b543b0Sjoerg if (code > free_ent || oldcode == -1) {
51809b543b0Sjoerg /* Bad stream. */
51909b543b0Sjoerg errno = EINVAL;
52009b543b0Sjoerg return (-1);
52109b543b0Sjoerg }
5223b93c56bScgd *stackp++ = finchar;
5233b93c56bScgd code = oldcode;
5243b93c56bScgd }
52509b543b0Sjoerg /*
52609b543b0Sjoerg * The above condition ensures that code < free_ent.
52709b543b0Sjoerg * The construction of tab_prefixof in turn guarantees that
52809b543b0Sjoerg * each iteration decreases code and therefore stack usage is
52909b543b0Sjoerg * bound by 1 << BITS - 256.
53009b543b0Sjoerg */
5313b93c56bScgd
5323b93c56bScgd /* Generate output characters in reverse order. */
5333b93c56bScgd while (code >= 256) {
5343b93c56bScgd *stackp++ = tab_suffixof(code);
5353b93c56bScgd code = tab_prefixof(code);
5363b93c56bScgd }
5373b93c56bScgd *stackp++ = finchar = tab_suffixof(code);
5383b93c56bScgd
5393b93c56bScgd /* And put them out in forward order. */
5403b93c56bScgd middle: do {
5413b93c56bScgd if (count-- == 0)
5423b93c56bScgd return (num);
5433b93c56bScgd *bp++ = *--stackp;
5443b93c56bScgd } while (stackp > de_stack);
5453b93c56bScgd
5463b93c56bScgd /* Generate the new entry. */
54709b543b0Sjoerg if ((code = free_ent) < maxmaxcode && oldcode != -1) {
5483b93c56bScgd tab_prefixof(code) = (u_short) oldcode;
5493b93c56bScgd tab_suffixof(code) = finchar;
5503b93c56bScgd free_ent = code + 1;
5513b93c56bScgd }
5523b93c56bScgd
5533b93c56bScgd /* Remember previous code. */
5543b93c56bScgd oldcode = incode;
5553b93c56bScgd }
5563b93c56bScgd state = S_EOF;
5573b93c56bScgd eof: return (num - count);
5583b93c56bScgd }
5593b93c56bScgd
5603b93c56bScgd /*-
5613b93c56bScgd * Read one code from the standard input. If EOF, return -1.
5623b93c56bScgd * Inputs:
5633b93c56bScgd * stdin
5643b93c56bScgd * Outputs:
5653b93c56bScgd * code or -1 is returned.
5663b93c56bScgd */
5673b93c56bScgd static code_int
getcode(struct s_zstate * zs)568774f88b1Swiz getcode(struct s_zstate *zs)
5693b93c56bScgd {
57087f4122cSlukem code_int gcode;
57187f4122cSlukem int r_off, bits;
57287f4122cSlukem char_type *bp;
5733b93c56bScgd
5743b93c56bScgd bp = gbuf;
5753b93c56bScgd if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
5763b93c56bScgd /*
5773b93c56bScgd * If the next entry will be too big for the current gcode
5783b93c56bScgd * size, then we must increase the size. This implies reading
5793b93c56bScgd * a new buffer full, too.
5803b93c56bScgd */
5813b93c56bScgd if (free_ent > maxcode) {
5823b93c56bScgd n_bits++;
5833b93c56bScgd if (n_bits == maxbits) /* Won't get any bigger now. */
5843b93c56bScgd maxcode = maxmaxcode;
5853b93c56bScgd else
5863b93c56bScgd maxcode = MAXCODE(n_bits);
5873b93c56bScgd }
5883b93c56bScgd if (clear_flg > 0) {
5893b93c56bScgd maxcode = MAXCODE(n_bits = INIT_BITS);
5903b93c56bScgd clear_flg = 0;
5913b93c56bScgd }
5923b93c56bScgd size = fread(gbuf, 1, n_bits, fp);
5933b93c56bScgd if (size <= 0) /* End of file. */
5943b93c56bScgd return (-1);
5953b93c56bScgd roffset = 0;
5963b93c56bScgd /* Round size down to integral number of codes. */
5973b93c56bScgd size = (size << 3) - (n_bits - 1);
5983b93c56bScgd }
5993b93c56bScgd r_off = roffset;
6003b93c56bScgd bits = n_bits;
6013b93c56bScgd
6023b93c56bScgd /* Get to the first byte. */
6033b93c56bScgd bp += (r_off >> 3);
6043b93c56bScgd r_off &= 7;
6053b93c56bScgd
6063b93c56bScgd /* Get first part (low order bits). */
6073b93c56bScgd gcode = (*bp++ >> r_off);
6083b93c56bScgd bits -= (8 - r_off);
6093b93c56bScgd r_off = 8 - r_off; /* Now, roffset into gcode word. */
6103b93c56bScgd
6113b93c56bScgd /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
6123b93c56bScgd if (bits >= 8) {
6133b93c56bScgd gcode |= *bp++ << r_off;
6143b93c56bScgd r_off += 8;
6153b93c56bScgd bits -= 8;
6163b93c56bScgd }
6173b93c56bScgd
6183b93c56bScgd /* High order bits. */
6193b93c56bScgd gcode |= (*bp & rmask[bits]) << r_off;
6203b93c56bScgd roffset += n_bits;
6213b93c56bScgd
6223b93c56bScgd return (gcode);
6233b93c56bScgd }
6243b93c56bScgd
6253b93c56bScgd static int
cl_block(struct s_zstate * zs)626774f88b1Swiz cl_block(struct s_zstate *zs) /* Table clear for block compress. */
6273b93c56bScgd {
62887f4122cSlukem long rat;
6293b93c56bScgd
6303b93c56bScgd checkpoint = in_count + CHECK_GAP;
6313b93c56bScgd
6323b93c56bScgd if (in_count > 0x007fffff) { /* Shift will overflow. */
6333b93c56bScgd rat = bytes_out >> 8;
6343b93c56bScgd if (rat == 0) /* Don't divide by zero. */
6353b93c56bScgd rat = 0x7fffffff;
6363b93c56bScgd else
6373b93c56bScgd rat = in_count / rat;
6383b93c56bScgd } else
6393b93c56bScgd rat = (in_count << 8) / bytes_out; /* 8 fractional bits. */
6403b93c56bScgd if (rat > ratio)
6413b93c56bScgd ratio = rat;
6423b93c56bScgd else {
6433b93c56bScgd ratio = 0;
644199b0f67Sjoerg memset(htab, 0xff, hsize * sizeof(count_int));
6453b93c56bScgd free_ent = FIRST;
6463b93c56bScgd clear_flg = 1;
6473b93c56bScgd if (output(zs, (code_int) CLEAR) == -1)
6483b93c56bScgd return (-1);
6493b93c56bScgd }
6503b93c56bScgd return (0);
6513b93c56bScgd }
6523b93c56bScgd
6533b93c56bScgd FILE *
zopen(const char * fname,const char * mode,int bits)654774f88b1Swiz zopen(const char *fname, const char *mode, int bits)
6553b93c56bScgd {
6563b93c56bScgd struct s_zstate *zs;
6573b93c56bScgd
65887f4122cSlukem if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
6593b93c56bScgd bits < 0 || bits > BITS) {
6603b93c56bScgd errno = EINVAL;
6613b93c56bScgd return (NULL);
6623b93c56bScgd }
6633b93c56bScgd
6643b93c56bScgd if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
6653b93c56bScgd return (NULL);
6663b93c56bScgd
6673b93c56bScgd maxbits = bits ? bits : BITS; /* User settable max # bits/code. */
66897e6ca09Sandrew maxmaxcode = 1 << maxbits; /* Should NEVER generate this code. */
6693b93c56bScgd hsize = HSIZE; /* For dynamic table sizing. */
6703b93c56bScgd free_ent = 0; /* First unused entry. */
6713b93c56bScgd block_compress = BLOCK_MASK;
6723b93c56bScgd clear_flg = 0;
6733b93c56bScgd ratio = 0;
6743b93c56bScgd checkpoint = CHECK_GAP;
6753b93c56bScgd in_count = 1; /* Length of input. */
6763b93c56bScgd out_count = 0; /* # of codes output (for debugging). */
6773b93c56bScgd state = S_START;
6783b93c56bScgd roffset = 0;
6793b93c56bScgd size = 0;
6803b93c56bScgd
6813b93c56bScgd /*
6823b93c56bScgd * Layering compress on top of stdio in order to provide buffering,
6833b93c56bScgd * and ensure that reads and write work with the data specified.
6843b93c56bScgd */
6853b93c56bScgd if ((fp = fopen(fname, mode)) == NULL) {
6863b93c56bScgd free(zs);
6873b93c56bScgd return (NULL);
6883b93c56bScgd }
6893b93c56bScgd switch (*mode) {
6903b93c56bScgd case 'r':
6913b93c56bScgd zmode = 'r';
6923b93c56bScgd return (funopen(zs, zread, NULL, NULL, zclose));
6933b93c56bScgd case 'w':
6943b93c56bScgd zmode = 'w';
6953b93c56bScgd return (funopen(zs, NULL, zwrite, NULL, zclose));
6963b93c56bScgd }
6973b93c56bScgd /* NOTREACHED */
69887f4122cSlukem return (NULL);
6993b93c56bScgd }
700