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