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