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