2 * Copyright (c) 1985, 1986 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * James A. Woods, derived from original work by Spencer Thomas
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
40 "@(#) Copyright (c) 1985, 1986 The Regents of the University of California.\n\
41 All rights reserved.\n";
45 static char sccsid[] = "@(#)compress.c 5.19 (Berkeley) 3/18/91";
49 * compress.c - File compression ala IEEE Computer, June 1984.
51 * Authors: Spencer W. Thomas (decvax!utah-cs!thomas)
52 * Jim McKie (decvax!mcvax!jim)
53 * Steve Davies (decvax!vax135!petsd!peora!srd)
54 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
55 * James A. Woods (decvax!ihnp4!ames!jaw)
56 * Joe Orost (decvax!vax135!petsd!joe)
59 #include <sys/param.h>
71 * Set USERMEM to the maximum amount of physical user memory available
72 * in bytes. USERMEM is used to determine the maximum BITS that can be used
75 * SACREDMEM is the amount of physical memory saved for others; compress
83 # define USERMEM 450000 /* default user memory */
87 # define BITS 12 /* max bits/code for 16-bit machine */
88 # define NO_UCHAR /* also if "unsigned char" functions as signed char */
90 #endif /* pdp11 */ /* don't forget to compile with -i */
93 # if USERMEM >= (433484+SACREDMEM)
96 # if USERMEM >= (229600+SACREDMEM)
99 # if USERMEM >= (127536+SACREDMEM)
102 # if USERMEM >= (73464+SACREDMEM)
113 #ifdef PBITS /* Preferred BITS for this memory size */
120 # define HSIZE 69001 /* 95% occupancy */
123 # define HSIZE 35023 /* 94% occupancy */
126 # define HSIZE 18013 /* 91% occupancy */
129 # define HSIZE 9001 /* 91% occupancy */
132 # define HSIZE 5003 /* 80% occupancy */
136 * a code_int must be able to hold 2**BITS values of type int, and also -1
139 typedef long int code_int;
141 typedef int code_int;
144 #ifdef SIGNED_COMPARE_SLOW
145 typedef unsigned long int count_int;
146 typedef unsigned short int count_short;
148 typedef long int count_int;
152 typedef char char_type;
154 typedef unsigned char char_type;
156 char_type magic_header[] = { "\037\235" }; /* 1F 9D */
158 /* Defines for third byte of header */
159 #define BIT_MASK 0x1f
160 #define BLOCK_MASK 0x80
161 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
162 a fourth header byte (for expansion).
164 #define INIT_BITS 9 /* initial number of bits/code */
166 int n_bits; /* number of bits/code */
167 int maxbits = BITS; /* user settable max # bits/code */
168 code_int maxcode; /* maximum code, given n_bits */
169 code_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */
170 #ifdef COMPATIBLE /* But wrong! */
171 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
173 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
174 #endif /* COMPATIBLE */
176 count_int htab [HSIZE];
177 unsigned short codetab [HSIZE];
179 #define htabof(i) htab[i]
180 #define codetabof(i) codetab[i]
181 code_int hsize = HSIZE; /* for dynamic table sizing */
185 * To save much memory, we overlay the table used by compress() with those
186 * used by decompress(). The tab_prefix table is the same size and type
187 * as the codetab. The tab_suffix table needs 2**BITS characters. We
188 * get this from the beginning of htab. The output stack uses the rest
189 * of htab, and contains characters. There is plenty of room for any
190 * possible stack (stack used to be 8000 characters).
193 #define tab_prefixof(i) codetabof(i)
194 # define tab_suffixof(i) ((char_type *)(htab))[i]
195 # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
197 code_int free_ent = 0; /* first unused entry */
198 int exit_stat = 0; /* per-file status */
199 int perm_stat = 0; /* permanent status */
203 int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
204 int zcat_flg = 0; /* Write output on stdout, suppress messages */
205 int precious = 1; /* Don't unlink output file on interrupt */
206 int quiet = 1; /* don't tell me about compression */
209 * block compression parameters -- after all codes are used up,
210 * and compression rate changes, start over.
212 int block_compress = BLOCK_MASK;
215 #define CHECK_GAP 10000 /* ratio check interval */
216 count_int checkpoint = CHECK_GAP;
218 * the next two codes should not be changed lightly, as they must not
219 * lie within the contiguous general code space.
221 #define FIRST 257 /* first free entry */
222 #define CLEAR 256 /* table clear output code */
235 * Algorithm from "A Technique for High Performance Data Compression",
236 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
238 * Usage: compress [-dfvc] [-b bits] [file ...]
240 * -d: If given, decompression is done instead.
242 * -c: Write output on stdout, don't remove original.
244 * -b: Parameter limits the max number of bits/code.
246 * -f: Forces output file to be generated, even if one already
247 * exists, and even if no space is saved by compressing.
248 * If -f is not used, the user will be prompted if stdin is
249 * a tty, otherwise, the output file will not be overwritten.
251 * -v: Write compression statistics
253 * file ...: Files to be compressed. If none specified, stdin
256 * file.Z: Compressed form of file with same mode, owner, and utimes
257 * or stdout (if stdin used as input)
260 * When filenames are given, replaces with the compressed version
261 * (.Z suffix) only if the file decreases in size.
263 * Modified Lempel-Ziv method (LZW). Basically finds common
264 * substrings and replaces them with a variable size code. This is
265 * deterministic, and can be done on the fly. Thus, the decompression
266 * procedure needs no input table, but tracks the way the table was built.
277 char **filelist, **fileptr, *cp, tempname[MAXPATHLEN];
278 void onintr(), oops();
280 /* This bg check only works for sh. */
281 if ((oldint = signal(SIGINT, SIG_IGN)) != SIG_IGN) {
282 (void)signal(SIGINT, onintr);
283 (void)signal(SIGSEGV, oops); /* XXX */
285 bgnd_flag = oldint != SIG_DFL;
288 nomagic = 1; /* Original didn't have a magic number */
291 if (cp = rindex(argv[0], '/'))
295 if (strcmp(cp, "uncompress") == 0)
297 else if(strcmp(cp, "zcat") == 0) {
303 * -b maxbits => maxbits.
304 * -C => generate output compatible with compress 2.0.
305 * -c => cat all output to stdout
308 * -f => force overwrite of output file
309 * -n => no header: useful to uncompress old files
310 * -V => print Version; debug verbose
316 while ((ch = getopt(argc, argv, "b:CcDdfnVv")) != EOF)
318 while ((ch = getopt(argc, argv, "b:Ccdfnv")) != EOF)
322 maxbits = atoi(optarg);
363 if (maxbits < INIT_BITS)
367 maxmaxcode = 1 << maxbits;
369 /* Build useless input file list. */
370 filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
372 *fileptr++ = *argv++;
375 if (*filelist != NULL) {
376 for (fileptr = filelist; *fileptr; fileptr++) {
378 if (do_decomp) { /* DECOMPRESSION */
379 /* Check for .Z suffix */
380 if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
381 /* No .Z: tack one on */
382 strcpy(tempname, *fileptr);
383 strcat(tempname, ".Z");
386 /* Open input file */
387 if ((freopen(*fileptr, "r", stdin)) == NULL) {
392 /* Check the magic number */
394 if ((getchar() != (magic_header[0] & 0xFF))
395 || (getchar() != (magic_header[1] & 0xFF))) {
396 fprintf(stderr, "%s: not in compressed format\n",
400 maxbits = getchar(); /* set -b from file */
401 block_compress = maxbits & BLOCK_MASK;
403 maxmaxcode = 1 << maxbits;
406 "%s: compressed with %d bits, can only handle %d bits\n",
407 *fileptr, maxbits, BITS);
411 /* Generate output filename */
412 strcpy(ofname, *fileptr);
413 ofname[strlen(*fileptr) - 2] = '\0'; /* Strip off .Z */
414 } else { /* COMPRESSION */
415 if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) {
416 fprintf(stderr, "%s: already has .Z suffix -- no change\n",
420 /* Open input file */
421 if ((freopen(*fileptr, "r", stdin)) == NULL) {
426 stat ( *fileptr, &statbuf );
427 fsize = (long) statbuf.st_size;
429 * tune hash table size for small files -- ad hoc,
430 * but the sizes match earlier #defines, which
431 * serve as upper bounds on the number of output codes.
434 if ( fsize < (1 << 12) )
435 hsize = MIN ( 5003, HSIZE );
436 else if ( fsize < (1 << 13) )
437 hsize = MIN ( 9001, HSIZE );
438 else if ( fsize < (1 << 14) )
439 hsize = MIN ( 18013, HSIZE );
440 else if ( fsize < (1 << 15) )
441 hsize = MIN ( 35023, HSIZE );
442 else if ( fsize < 47000 )
443 hsize = MIN ( 50021, HSIZE );
445 /* Generate output filename */
446 strcpy(ofname, *fileptr);
447 strcat(ofname, ".Z");
449 /* Check for overwrite of existing file */
450 if (overwrite == 0 && zcat_flg == 0) {
451 if (stat(ofname, &statbuf) == 0) {
454 fprintf(stderr, "%s already exists;", ofname);
455 if (bgnd_flag == 0 && isatty(2)) {
456 fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
459 read(2, response, 2);
460 while (response[1] != '\n') {
461 if (read(2, response+1, 1) < 0) { /* Ack! */
462 perror("stderr"); break;
466 if (response[0] != 'y') {
467 fprintf(stderr, "\tnot overwritten\n");
472 if(zcat_flg == 0) { /* Open output file */
473 if (freopen(ofname, "w", stdout) == NULL) {
480 fprintf(stderr, "%s: ", *fileptr);
483 /* Actually do the compression/decompression */
484 if (do_decomp == 0) compress();
488 else if (debug == 0) decompress();
490 if (verbose) dump_tab();
493 copystat(*fileptr, ofname); /* Copy stats */
495 if((exit_stat == 1) || (!quiet))
499 } else { /* Standard input */
500 if (do_decomp == 0) {
503 if(verbose) dump_tab();
508 /* Check the magic number */
510 if ((getchar()!=(magic_header[0] & 0xFF))
511 || (getchar()!=(magic_header[1] & 0xFF))) {
512 fprintf(stderr, "stdin: not in compressed format\n");
515 maxbits = getchar(); /* set -b from file */
516 block_compress = maxbits & BLOCK_MASK;
518 maxmaxcode = 1 << maxbits;
519 fsize = 100000; /* assume stdin large for USERMEM */
522 "stdin: compressed with %d bits, can only handle %d bits\n",
530 if (debug == 0) decompress();
532 if (verbose) dump_tab();
536 exit(perm_stat ? perm_stat : exit_stat);
540 long int in_count = 1; /* length of input */
541 long int bytes_out; /* length of compressed output */
542 long int out_count = 0; /* # of codes output (for debugging) */
545 * compress stdin to stdout
547 * Algorithm: use open addressing double hashing (no chaining) on the
548 * prefix code / next character combination. We do a variant of Knuth's
549 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
550 * secondary probe. Here, the modular division first probe is gives way
551 * to a faster exclusive-or manipulation. Also do block compression with
552 * an adaptive reset, whereby the code table is cleared when the compression
553 * ratio decreases, but after the table fills. The variable-length output
554 * codes are re-sized at this point, and a special CLEAR code is generated
555 * for the decompressor. Late addition: construct the table according to
556 * file size for noticeable speed improvement on small files. Please direct
557 * questions about this implementation to ames!jaw.
563 register code_int i = 0;
565 register code_int ent;
567 register code_int hsize_reg;
572 putchar(magic_header[0]);
573 putchar(magic_header[1]);
574 putchar((char)(maxbits | block_compress));
578 #endif /* COMPATIBLE */
581 bytes_out = 3; /* includes 3-byte header mojo */
586 checkpoint = CHECK_GAP;
587 maxcode = MAXCODE(n_bits = INIT_BITS);
588 free_ent = ((block_compress) ? FIRST : 256 );
593 for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
595 hshift = 8 - hshift; /* set hash code range bound */
598 cl_hash( (count_int) hsize_reg); /* clear hash table */
600 #ifdef SIGNED_COMPARE_SLOW
601 while ( (c = getchar()) != (unsigned) EOF ) {
603 while ( (c = getchar()) != EOF ) {
606 fcode = (long) (((long) c << maxbits) + ent);
607 i = ((c << hshift) ^ ent); /* xor hashing */
609 if ( htabof (i) == fcode ) {
612 } else if ( (long)htabof (i) < 0 ) /* empty slot */
614 disp = hsize_reg - i; /* secondary hash (after G. Knott) */
618 if ( (i -= disp) < 0 )
621 if ( htabof (i) == fcode ) {
625 if ( (long)htabof (i) > 0 )
628 output ( (code_int) ent );
631 #ifdef SIGNED_COMPARE_SLOW
632 if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
634 if ( free_ent < maxmaxcode ) {
636 codetabof (i) = free_ent++; /* code -> hashtable */
639 else if ( (count_int)in_count >= checkpoint && block_compress )
643 * Put out the final code.
645 output( (code_int)ent );
647 output( (code_int)-1 );
650 * Print out stats on stderr
652 if(zcat_flg == 0 && !quiet) {
655 "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
656 in_count, out_count, bytes_out );
657 prratio( stderr, in_count, bytes_out );
658 fprintf( stderr, "\n");
659 fprintf( stderr, "\tCompression as in compact: " );
660 prratio( stderr, in_count-bytes_out, in_count );
661 fprintf( stderr, "\n");
662 fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
663 free_ent - 1, n_bits );
665 fprintf( stderr, "Compression: " );
666 prratio( stderr, in_count-bytes_out, in_count );
669 if(bytes_out > in_count) /* exit(2) if no savings */
675 * Output the given code.
677 * code: A n_bits-bit integer. If == -1, then EOF. This assumes
678 * that n_bits =< (long)wordsize - 1.
680 * Outputs code to the file.
682 * Chars are 8 bits long.
684 * Maintain a BITS character long buffer (so that 8 codes will
685 * fit in it exactly). Use the VAX insv instruction to insert each
686 * code in turn. When the buffer fills up empty it and start over.
689 static char buf[BITS];
692 char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
693 char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
704 * On the VAX, it is important to have the register declarations
705 * in exactly the order given, or the asm will break.
707 register int r_off = offset, bits= n_bits;
708 register char * bp = buf;
712 fprintf( stderr, "%5d%c", code,
713 (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
716 #if defined(vax) && !defined(__GNUC__)
718 * VAX and PCC DEPENDENT!! Implementation on other machines is
721 * Translation: Insert BITS bits from the argument starting at
722 * offset bits from the beginning of buf.
724 0; /* Work around for pcc -O bug with asm and if stmt */
725 asm( "insv 4(ap),r11,r10,(r9)" );
728 * byte/bit numbering on the VAX is simulated by the following code
731 * Get to the first byte.
736 * Since code is always >= 8 bits, only need to mask the first
739 *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
743 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
754 if ( offset == (n_bits << 3) ) {
767 * If the next entry is going to be too big for the code size,
768 * then increase it, if possible.
770 if ( free_ent > maxcode || (clear_flg > 0))
773 * Write the whole buffer, because the input side won't
774 * discover the size increase until after it has read it.
777 if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
784 maxcode = MAXCODE (n_bits = INIT_BITS);
789 if ( n_bits == maxbits )
790 maxcode = maxmaxcode;
792 maxcode = MAXCODE(n_bits);
796 fprintf( stderr, "\nChange to %d bits\n", n_bits );
803 * At EOF, write the rest of the buffer.
806 offset = (offset + 7) / 8;
807 if( fwrite( buf, 1, offset, stdout ) != offset )
812 (void)fflush( stdout );
813 if( ferror( stdout ) )
817 fprintf( stderr, "\n" );
823 * Decompress stdin to stdout. This routine adapts to the codes in the
824 * file building the "string" table on-the-fly; requiring no table to
825 * be stored in the compressed file. The tables used herein are shared
826 * with those of the compress() routine. See the definitions above.
830 register char_type *stackp;
831 register int finchar;
832 register code_int code, oldcode, incode;
833 int n, nwritten, offset; /* Variables for buffered write */
834 char buff[BUFSIZ]; /* Buffer for buffered write */
838 * As above, initialize the first 256 entries in the table.
840 maxcode = MAXCODE(n_bits = INIT_BITS);
841 for ( code = 255; code >= 0; code-- ) {
842 tab_prefixof(code) = 0;
843 tab_suffixof(code) = (char_type)code;
845 free_ent = ((block_compress) ? FIRST : 256 );
847 finchar = oldcode = getcode();
848 if(oldcode == -1) /* EOF already? */
849 return; /* Get out of here */
851 /* first code must be 8 bits = char */
853 buff[n++] = (char)finchar;
857 while ( (code = getcode()) > -1 ) {
859 if ( (code == CLEAR) && block_compress ) {
860 for ( code = 255; code >= 0; code-- )
861 tab_prefixof(code) = 0;
863 free_ent = FIRST - 1;
864 if ( (code = getcode ()) == -1 ) /* O, untimely death! */
869 * Special case for KwKwK string.
871 if ( code >= free_ent ) {
877 * Generate output characters in reverse order
879 #ifdef SIGNED_COMPARE_SLOW
880 while ( ((unsigned long)code) >= ((unsigned long)256) ) {
882 while ( code >= 256 ) {
884 *stackp++ = tab_suffixof(code);
885 code = tab_prefixof(code);
887 *stackp++ = finchar = tab_suffixof(code);
890 * And put them out in forward order
894 * About 60% of the time is spent in the putchar() call
895 * that appeared here. It was originally
896 * putchar ( *--stackp );
897 * If we buffer the writes ourselves, we can go faster (about
900 * At this point, the next line is the next *big* time
901 * sink in the code. It takes up about 10% of the time.
903 buff[n++] = *--stackp;
907 nwritten = write(fileno(stdout), &buff[offset], n);
911 } while ((n -= nwritten) > 0);
913 } while ( stackp > de_stack );
916 * Generate the new entry.
918 if ( (code=free_ent) < maxmaxcode ) {
919 tab_prefixof(code) = (unsigned short)oldcode;
920 tab_suffixof(code) = finchar;
924 * Remember previous code.
929 * Flush the stuff remaining in our buffer...
933 nwritten = write(fileno(stdout), &buff[offset], n);
942 * Read one code from the standard input. If EOF, return -1.
946 * code or -1 is returned.
951 * On the VAX, it is important to have the register declarations
952 * in exactly the order given, or the asm will break.
954 register code_int code;
955 static int offset = 0, size = 0;
956 static char_type buf[BITS];
957 register int r_off, bits;
958 register char_type *bp = buf;
960 if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
962 * If the next entry will be too big for the current code
963 * size, then we must increase the size. This implies reading
964 * a new buffer full, too.
966 if ( free_ent > maxcode ) {
968 if ( n_bits == maxbits )
969 maxcode = maxmaxcode; /* won't get any bigger now */
971 maxcode = MAXCODE(n_bits);
973 if ( clear_flg > 0) {
974 maxcode = MAXCODE (n_bits = INIT_BITS);
977 size = fread( buf, 1, n_bits, stdin );
979 return -1; /* end of file */
981 /* Round size down to integral number of codes */
982 size = (size << 3) - (n_bits - 1);
987 asm( "extzv r10,r9,(r8),r11" );
988 #else /* not a vax */
990 * Get to the first byte.
994 /* Get first part (low order bits) */
996 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
998 code = (*bp++ >> r_off);
999 #endif /* NO_UCHAR */
1000 bits -= (8 - r_off);
1001 r_off = 8 - r_off; /* now, offset into code word */
1002 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
1005 code |= (*bp++ & 0xff) << r_off;
1007 code |= *bp++ << r_off;
1008 #endif /* NO_UCHAR */
1012 /* high order bits. */
1013 code |= (*bp & rmask[bits]) << r_off;
1024 * Just print out codes from input file. For debugging.
1029 bits = n_bits = INIT_BITS;
1030 maxcode = MAXCODE(n_bits);
1031 free_ent = ((block_compress) ? FIRST : 256 );
1032 while ( ( code = getcode() ) >= 0 ) {
1033 if ( (code == CLEAR) && block_compress ) {
1034 free_ent = FIRST - 1;
1037 else if ( free_ent < maxmaxcode )
1039 if ( bits != n_bits ) {
1040 fprintf(stderr, "\nChange to %d bits\n", n_bits );
1044 fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
1046 putc( '\n', stderr );
1050 code_int sorttab[1<<BITS]; /* sorted pointers into htab */
1052 dump_tab() /* dump string table */
1054 register int i, first;
1056 #define STACK_SIZE 15000
1057 int stack_top = STACK_SIZE;
1060 if(do_decomp == 0) { /* compressing */
1061 register int flag = 1;
1063 for(i=0; i<hsize; i++) { /* build sort pointers */
1064 if((long)htabof(i) >= 0) {
1065 sorttab[codetabof(i)] = i;
1068 first = block_compress ? FIRST : 256;
1069 for(i = first; i < free_ent; i++) {
1070 fprintf(stderr, "%5d: \"", i);
1071 de_stack[--stack_top] = '\n';
1072 de_stack[--stack_top] = '"';
1073 stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
1075 for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
1077 ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
1078 stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
1081 stack_top = in_stack(ent, stack_top);
1082 fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
1083 stack_top = STACK_SIZE;
1085 } else if(!debug) { /* decompressing */
1087 for ( i = 0; i < free_ent; i++ ) {
1089 c = tab_suffixof(ent);
1090 if ( isascii(c) && isprint(c) )
1091 fprintf( stderr, "%5d: %5d/'%c' \"",
1092 ent, tab_prefixof(ent), c );
1094 fprintf( stderr, "%5d: %5d/\\%03o \"",
1095 ent, tab_prefixof(ent), c );
1096 de_stack[--stack_top] = '\n';
1097 de_stack[--stack_top] = '"';
1098 for ( ; ent != NULL;
1099 ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
1100 stack_top = in_stack(tab_suffixof(ent), stack_top);
1102 fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
1103 stack_top = STACK_SIZE;
1109 in_stack(c, stack_top)
1110 register c, stack_top;
1112 if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
1113 de_stack[--stack_top] = c;
1116 case '\n': de_stack[--stack_top] = 'n'; break;
1117 case '\t': de_stack[--stack_top] = 't'; break;
1118 case '\b': de_stack[--stack_top] = 'b'; break;
1119 case '\f': de_stack[--stack_top] = 'f'; break;
1120 case '\r': de_stack[--stack_top] = 'r'; break;
1121 case '\\': de_stack[--stack_top] = '\\'; break;
1123 de_stack[--stack_top] = '0' + c % 8;
1124 de_stack[--stack_top] = '0' + (c / 8) % 8;
1125 de_stack[--stack_top] = '0' + c / 64;
1128 de_stack[--stack_top] = '\\';
1136 (void)fprintf(stderr, "compress: %s: %s\n",
1137 ofname[0] ? ofname : "stdout", strerror(errno));
1138 (void)unlink(ofname);
1142 copystat(ifname, ofname)
1143 char *ifname, *ofname;
1145 struct stat statbuf;
1150 if (stat(ifname, &statbuf)) { /* Get stat on input file */
1154 if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
1156 fprintf(stderr, "%s: ", ifname);
1157 fprintf(stderr, " -- not a regular file: unchanged");
1160 } else if (statbuf.st_nlink > 1) {
1162 fprintf(stderr, "%s: ", ifname);
1163 fprintf(stderr, " -- has %d other links: unchanged",
1164 statbuf.st_nlink - 1);
1167 } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
1169 fprintf(stderr, " -- file unchanged");
1170 } else { /* ***** Successful Compression ***** */
1172 mode = statbuf.st_mode & 07777;
1173 if (chmod(ofname, mode)) /* Copy modes */
1175 chown(ofname, statbuf.st_uid, statbuf.st_gid); /* Copy ownership */
1176 tp.actime = statbuf.st_atime;
1177 tp.modtime = statbuf.st_mtime;
1178 utime(ofname, &tp); /* Update last accessed and modified times */
1179 if (unlink(ifname)) /* Remove input file */
1182 fprintf(stderr, " -- replaced with %s", ofname);
1183 return; /* Successful return */
1186 /* Unsuccessful return -- one of the tests failed */
1200 oops ( ) /* wild pointer -- assume bad input */
1203 fprintf ( stderr, "uncompress: corrupt input\n" );
1208 cl_block () /* table clear for block compress */
1210 register long int rat;
1212 checkpoint = in_count + CHECK_GAP;
1215 fprintf ( stderr, "count: %ld, ratio: ", in_count );
1216 prratio ( stderr, in_count, bytes_out );
1217 fprintf ( stderr, "\n");
1221 if(in_count > 0x007fffff) { /* shift will overflow */
1222 rat = bytes_out >> 8;
1223 if(rat == 0) { /* Don't divide by zero */
1226 rat = in_count / rat;
1229 rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
1231 if ( rat > ratio ) {
1237 dump_tab(); /* dump string table */
1239 cl_hash ( (count_int) hsize );
1242 output ( (code_int) CLEAR );
1245 fprintf ( stderr, "clear\n" );
1250 cl_hash(hsize) /* reset code table */
1251 register count_int hsize;
1253 register count_int *htab_p = htab+hsize;
1255 register long m1 = -1;
1258 do { /* might use Sys V memset(3) here */
1276 } while ((i -= 16) >= 0);
1277 for ( i += 16; i > 0; i-- )
1281 prratio(stream, num, den)
1285 register int q; /* Doesn't need to be long */
1287 if(num > 214748L) { /* 2147483647/10000 */
1288 q = num / (den / 10000L);
1290 q = 10000L * num / den; /* Long calculations, though */
1296 fprintf(stream, "%d.%02d%%", q / 100, q % 100);
1301 (void)fprintf(stderr,
1303 "compress [-CDVcdfnv] [-b maxbits] [file ...]\n");
1305 "compress [-Ccdfnv] [-b maxbits] [file ...]\n");