2 static char *SccsId = "@(#)compress.c 1.14 9/24/87";
4 static char rcs_ident[] = "Based on compress.c,v 4.0 85/07/30 12:50:00 joe Release";
7 * Compress - data compression program
9 #define min(a,b) ((a>b) ? b : a)
12 * machine variants which require cc -Dmachine: pdp11, z8000, pcxt
16 * Set USERMEM to the maximum amount of physical user memory available
17 * in bytes. USERMEM is used to determine the maximum BITS that can be used
20 * SACREDMEM is the amount of physical memory saved for others; compress
28 # define USERMEM 450000 /* default user memory */
31 #ifdef interdata /* (Perkin-Elmer) */
32 #define SIGNED_COMPARE_SLOW /* signed compare is slower than unsigned */
36 # define BITS 12 /* max bits/code for 16-bit machine */
37 # define NO_UCHAR /* also if "unsigned char" functions as signed char */
39 #endif /* pdp11 */ /* don't forget to compile with -i */
43 # undef vax /* weird preprocessor */
53 # if USERMEM >= (433484+SACREDMEM)
56 # if USERMEM >= (229600+SACREDMEM)
59 # if USERMEM >= (127536+SACREDMEM)
62 # if USERMEM >= (73464+SACREDMEM)
73 #ifdef PBITS /* Preferred BITS for this memory size */
80 # define HSIZE 69001 /* 95% occupancy */
83 # define HSIZE 35023 /* 94% occupancy */
86 # define HSIZE 18013 /* 91% occupancy */
89 # define HSIZE 9001 /* 91% occupancy */
92 # define HSIZE 5003 /* 80% occupancy */
95 #ifdef M_XENIX /* Stupid compiler can't handle arrays with */
96 # if BITS == 16 /* more than 65535 bytes - so we fake it */
99 # if BITS > 13 /* Code only handles BITS = 12, 13, or 16 */
106 * a code_int must be able to hold 2**BITS values of type int, and also -1
109 typedef long int code_int;
111 typedef int code_int;
114 #ifdef SIGNED_COMPARE_SLOW
115 typedef unsigned long int count_int;
116 typedef unsigned short int count_short;
118 typedef long int count_int;
122 typedef char char_type;
124 typedef unsigned char char_type;
126 char_type magic_header[] = { "\037\235" }; /* 1F 9D */
128 /* Defines for third byte of header */
129 #define BIT_MASK 0x1f
130 #define BLOCK_MASK 0x80
131 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
132 a fourth header byte (for expansion).
134 #define INIT_BITS 9 /* initial number of bits/code */
137 * compress.c - File compression ala IEEE Computer, June 1984.
139 * Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
140 * Jim McKie (decvax!mcvax!jim)
141 * Steve Davies (decvax!vax135!petsd!peora!srd)
142 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
143 * James A. Woods (decvax!ihnp4!ames!jaw)
144 * Joe Orost (decvax!vax135!petsd!joe)
151 #include <sys/types.h>
152 #include <sys/stat.h>
154 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
156 int n_bits; /* number of bits/code */
157 int maxbits = BITS; /* user settable max # bits/code */
158 code_int maxcode; /* maximum code, given n_bits */
159 code_int maxmaxcode = 1L << BITS; /* should NEVER generate this code */
160 #ifdef COMPATIBLE /* But wrong! */
161 # define MAXCODE(n_bits) (1L << (n_bits) - 1)
163 # define MAXCODE(n_bits) ((1L << (n_bits)) - 1)
164 #endif /* COMPATIBLE */
167 count_int htab0[8192];
168 count_int htab1[8192];
169 count_int htab2[8192];
170 count_int htab3[8192];
171 count_int htab4[8192];
172 count_int htab5[8192];
173 count_int htab6[8192];
174 count_int htab7[8192];
175 count_int htab8[HSIZE-65536];
176 count_int * htab[9] = {
177 htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 };
179 #define htabof(i) (htab[(i) >> 13][(i) & 0x1fff])
180 unsigned short code0tab[16384];
181 unsigned short code1tab[16384];
182 unsigned short code2tab[16384];
183 unsigned short code3tab[16384];
184 unsigned short code4tab[16384];
185 unsigned short * codetab[5] = {
186 code0tab, code1tab, code2tab, code3tab, code4tab };
188 #define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff])
190 #else /* Normal machine */
192 /* support gould base register problems */
194 count_int htab [HSIZE];
195 unsigned short codetab [HSIZE];
198 count_int htab [HSIZE];
199 unsigned short codetab [HSIZE];
201 #define htabof(i) htab[i]
202 #define codetabof(i) codetab[i]
203 #endif /* !XENIX_16 */
204 code_int hsize = HSIZE; /* for dynamic table sizing */
208 * To save much memory, we overlay the table used by compress() with those
209 * used by decompress(). The tab_prefix table is the same size and type
210 * as the codetab. The tab_suffix table needs 2**BITS characters. We
211 * get this from the beginning of htab. The output stack uses the rest
212 * of htab, and contains characters. There is plenty of room for any
213 * possible stack (stack used to be 8000 characters).
216 #define tab_prefixof(i) codetabof(i)
218 # define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
219 # define de_stack ((char_type *)(htab2))
220 #else /* Normal machine */
221 # define tab_suffixof(i) ((char_type *)(htab))[i]
222 # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
223 #endif /* XENIX_16 */
225 code_int free_ent = 0; /* first unused entry */
232 fprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
236 fprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n");
239 int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
240 int zcat_flg = 0; /* Write output on stdout, suppress messages */
241 int quiet = 1; /* don't tell me about compression */
244 * block compression parameters -- after all codes are used up,
245 * and compression rate changes, start over.
247 int block_compress = BLOCK_MASK;
250 #define CHECK_GAP 10000 /* ratio check interval */
251 count_int checkpoint = CHECK_GAP;
253 * the next two codes should not be changed lightly, as they must not
254 * lie within the contiguous general code space.
256 #define FIRST 257 /* first free entry */
257 #define CLEAR 256 /* table clear output code */
268 /*****************************************************************
271 * Algorithm from "A Technique for High Performance Data Compression",
272 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
274 * Usage: compress [-dfvc] [-b bits] [file ...]
276 * -d: If given, decompression is done instead.
278 * -c: Write output on stdout, don't remove original.
280 * -b: Parameter limits the max number of bits/code.
282 * -f: Forces output file to be generated, even if one already
283 * exists, and even if no space is saved by compressing.
284 * If -f is not used, the user will be prompted if stdin is
285 * a tty, otherwise, the output file will not be overwritten.
287 * -v: Write compression statistics
289 * file ...: Files to be compressed. If none specified, stdin
292 * file.Z: Compressed form of file with same mode, owner, and utimes
293 * or stdout (if stdin used as input)
296 * When filenames are given, replaces with the compressed version
297 * (.Z suffix) only if the file decreases in size.
299 * Modified Lempel-Ziv method (LZW). Basically finds common
300 * substrings and replaces them with a variable size code. This is
301 * deterministic, and can be done on the fly. Thus, the decompression
302 * procedure needs no input table, but tracks the way the table was built.
306 register int argc; char **argv;
308 int overwrite = 0; /* Do not overwrite unless given -f flag */
310 char **filelist, **fileptr;
311 char *cp, *rindex(), *malloc();
313 extern onintr(), oops();
316 if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
317 signal ( SIGINT, onintr );
318 signal ( SIGSEGV, oops );
322 nomagic = 1; /* Original didn't have a magic number */
323 #endif /* COMPATIBLE */
325 filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
328 if((cp = rindex(argv[0], '/')) != 0) {
333 if(strcmp(cp, "uncompress") == 0) {
335 } else if(strcmp(cp, "zcat") == 0) {
341 /* 4.2BSD dependent - take it out if not */
342 setlinebuf( stderr );
345 /* Argument Processing
346 * All flags are optional.
348 * -V => print Version; debug verbose
351 * -f => force overwrite of output file
352 * -n => no header: useful to uncompress old files
353 * -b maxbits => maxbits. If -b is specified, then maxbits MUST be
355 * -c => cat all output to stdout
356 * -C => generate output compatible with compress 2.0.
357 * if a string is left, must be an input filename.
359 for (argc--, argv++; argc > 0; argc--, argv++) {
360 if (**argv == '-') { /* A flag argument */
361 while (*++(*argv)) { /* Process all flags in this arg */
395 fprintf(stderr, "Missing maxbits\n");
399 maxbits = atoi(*argv);
408 fprintf(stderr, "Unknown flag: '%c'; ", **argv);
414 else { /* Input file name */
415 *fileptr++ = *argv; /* Build input file list */
417 /* process nextarg; */
422 if(maxbits < INIT_BITS) maxbits = INIT_BITS;
423 if (maxbits > BITS) maxbits = BITS;
424 maxmaxcode = 1L << maxbits;
426 if (*filelist != NULL) {
427 for (fileptr = filelist; *fileptr; fileptr++) {
429 if (do_decomp != 0) { /* DECOMPRESSION */
430 /* Check for .Z suffix */
431 if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
432 /* No .Z: tack one on */
433 strcpy(tempname, *fileptr);
434 strcat(tempname, ".Z");
437 /* Open input file */
438 if ((freopen(*fileptr, "r", stdin)) == NULL) {
439 perror(*fileptr); continue;
441 /* Check the magic number */
443 if ((getchar() != (magic_header[0] & 0xFF))
444 || (getchar() != (magic_header[1] & 0xFF))) {
445 fprintf(stderr, "%s: not in compressed format\n",
449 maxbits = getchar(); /* set -b from file */
450 block_compress = maxbits & BLOCK_MASK;
452 maxmaxcode = 1L << maxbits;
455 "%s: compressed with %d bits, can only handle %d bits\n",
456 *fileptr, maxbits, BITS);
460 /* Generate output filename */
461 strcpy(ofname, *fileptr);
462 ofname[strlen(*fileptr) - 2] = '\0'; /* Strip off .Z */
463 } else { /* COMPRESSION */
464 if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) {
465 fprintf(stderr, "%s: already has .Z suffix -- no change\n",
469 /* Open input file */
470 if ((freopen(*fileptr, "r", stdin)) == NULL) {
471 perror(*fileptr); continue;
473 stat ( *fileptr, &statbuf );
474 fsize = (long) statbuf.st_size;
476 * tune hash table size for small files -- ad hoc,
477 * but the sizes match earlier #defines, which
478 * serve as upper bounds on the number of output codes.
481 if ( fsize < (1 << 12) )
482 hsize = min ( 5003, HSIZE );
483 else if ( fsize < (1 << 13) )
484 hsize = min ( 9001, HSIZE );
485 else if ( fsize < (1 << 14) )
486 hsize = min ( 18013, HSIZE );
487 else if ( fsize < (1 << 15) )
488 hsize = min ( 35023, HSIZE );
489 else if ( fsize < 47000 )
490 hsize = min ( 50021, HSIZE );
492 /* Generate output filename */
493 strcpy(ofname, *fileptr);
494 #ifndef BSD4_2 /* Short filenames */
495 if ((cp=rindex(ofname,'/')) != NULL) cp++;
497 if (strlen(cp) > 12) {
498 fprintf(stderr,"%s: filename too long to tack on .Z\n",cp);
501 #endif /* BSD4_2 Long filenames allowed */
502 strcat(ofname, ".Z");
504 /* Check for overwrite of existing file */
505 if (overwrite == 0 && zcat_flg == 0) {
506 if (stat(ofname, &statbuf) == 0) {
509 fprintf(stderr, "%s already exists;", ofname);
511 fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
514 read(2, response, 2);
515 while (response[1] != '\n') {
516 if (read(2, response+1, 1) < 0) { /* Ack! */
517 perror("stderr"); break;
521 if (response[0] != 'y') {
522 fprintf(stderr, "\tnot overwritten\n");
527 if(zcat_flg == 0) { /* Open output file */
528 if (freopen(ofname, "w", stdout) == NULL) {
533 fprintf(stderr, "%s: ", *fileptr);
536 /* Actually do the compression/decompression */
537 if (do_decomp == 0) compress();
541 else if (debug == 0) decompress();
543 if (verbose) dump_tab();
546 copystat(*fileptr, ofname); /* Copy stats */
547 if((exit_stat == 1) || (!quiet))
551 } else { /* Standard input */
552 if (do_decomp == 0) {
555 if(verbose) dump_tab();
560 /* Check the magic number */
562 if ((getchar()!=(magic_header[0] & 0xFF))
563 || (getchar()!=(magic_header[1] & 0xFF))) {
564 fprintf(stderr, "stdin: not in compressed format\n");
567 maxbits = getchar(); /* set -b from file */
568 block_compress = maxbits & BLOCK_MASK;
570 maxmaxcode = 1L << maxbits;
571 fsize = 100000; /* assume stdin large for USERMEM */
574 "stdin: compressed with %d bits, can only handle %d bits\n",
582 if (debug == 0) decompress();
584 if (verbose) dump_tab();
592 long int in_count = 1; /* length of input */
593 long int bytes_out; /* length of compressed output */
594 long int out_count = 0; /* # of codes output (for debugging) */
597 * compress stdin to stdout
599 * Algorithm: use open addressing double hashing (no chaining) on the
600 * prefix code / next character combination. We do a variant of Knuth's
601 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
602 * secondary probe. Here, the modular division first probe is gives way
603 * to a faster exclusive-or manipulation. Also do block compression with
604 * an adaptive reset, whereby the code table is cleared when the compression
605 * ratio decreases, but after the table fills. The variable-length output
606 * codes are re-sized at this point, and a special CLEAR code is generated
607 * for the decompressor. Late addition: construct the table according to
608 * file size for noticeable speed improvement on small files. Please direct
609 * questions about this implementation to ames!jaw.
614 register code_int i = 0;
616 register code_int ent;
618 register code_int disp;
619 #else /* Normal machine */
622 register code_int hsize_reg;
627 putchar(magic_header[0]); putchar(magic_header[1]);
628 putchar((char)(maxbits | block_compress));
632 #endif /* COMPATIBLE */
635 bytes_out = 3; /* includes 3-byte header mojo */
640 checkpoint = CHECK_GAP;
641 maxcode = MAXCODE(n_bits = INIT_BITS);
642 free_ent = ((block_compress) ? FIRST : 256 );
647 for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
649 hshift = 8 - hshift; /* set hash code range bound */
652 cl_hash( (count_int) hsize_reg); /* clear hash table */
654 #ifdef SIGNED_COMPARE_SLOW
655 while ( (c = getchar()) != (unsigned) EOF ) {
657 while ( (c = getchar()) != EOF ) {
660 fcode = (long) (((long) c << maxbits) + ent);
661 i = (((long)c << hshift) ^ ent); /* xor hashing */
663 if ( htabof (i) == fcode ) {
666 } else if ( (long)htabof (i) < 0 ) /* empty slot */
668 disp = hsize_reg - i; /* secondary hash (after G. Knott) */
672 if ( (i -= disp) < 0 )
675 if ( htabof (i) == fcode ) {
679 if ( (long)htabof (i) > 0 )
682 output ( (code_int) ent );
685 #ifdef SIGNED_COMPARE_SLOW
686 if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
688 if ( free_ent < maxmaxcode ) {
690 codetabof (i) = free_ent++; /* code -> hashtable */
693 else if ( (count_int)in_count >= checkpoint && block_compress )
697 * Put out the final code.
699 output( (code_int)ent );
701 output( (code_int)-1 );
704 * Print out stats on stderr
706 if(zcat_flg == 0 && !quiet) {
709 "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
710 in_count, out_count, bytes_out );
711 prratio( stderr, in_count, bytes_out );
712 fprintf( stderr, "\n");
713 fprintf( stderr, "\tCompression as in compact: " );
714 prratio( stderr, in_count-bytes_out, in_count );
715 fprintf( stderr, "\n");
716 fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
717 free_ent - 1, n_bits );
719 fprintf( stderr, "Compression: " );
720 prratio( stderr, in_count-bytes_out, in_count );
723 if(bytes_out > in_count) /* exit(2) if no savings */
728 /*****************************************************************
731 * Output the given code.
733 * code: A n_bits-bit integer. If == -1, then EOF. This assumes
734 * that n_bits =< (long)wordsize - 1.
736 * Outputs code to the file.
738 * Chars are 8 bits long.
740 * Maintain a BITS character long buffer (so that 8 codes will
741 * fit in it exactly). Use the VAX insv instruction to insert each
742 * code in turn. When the buffer fills up empty it and start over.
745 static char buf[BITS];
748 char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
749 char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
760 * On the VAX, it is important to have the register declarations
761 * in exactly the order given, or the asm will break.
763 register int r_off = offset, bits= n_bits;
764 register char * bp = buf;
768 fprintf( stderr, "%5d%c", code,
769 (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
773 /* VAX DEPENDENT!! Implementation on other machines is below.
775 * Translation: Insert BITS bits from the argument starting at
776 * offset bits from the beginning of buf.
778 0; /* Work around for pcc -O bug with asm and if stmt */
779 asm( "insv 4(ap),r11,r10,(r9)" );
780 #else /* not a vax */
782 * byte/bit numbering on the VAX is simulated by the following code
785 * Get to the first byte.
790 * Since code is always >= 8 bits, only need to mask the first
793 *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
797 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
808 if ( offset == (n_bits << 3) ) {
819 * If the next entry is going to be too big for the code size,
820 * then increase it, if possible.
822 if ( free_ent > maxcode || (clear_flg > 0))
825 * Write the whole buffer, because the input side won't
826 * discover the size increase until after it has read it.
829 if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
836 maxcode = MAXCODE (n_bits = INIT_BITS);
841 if ( n_bits == maxbits )
842 maxcode = maxmaxcode;
844 maxcode = MAXCODE(n_bits);
848 fprintf( stderr, "\nChange to %d bits\n", n_bits );
855 * At EOF, write the rest of the buffer.
858 fwrite( buf, 1, (offset + 7) / 8, stdout );
859 bytes_out += (offset + 7) / 8;
864 fprintf( stderr, "\n" );
866 if( ferror( stdout ) )
872 * Decompress stdin to stdout. This routine adapts to the codes in the
873 * file building the "string" table on-the-fly; requiring no table to
874 * be stored in the compressed file. The tables used herein are shared
875 * with those of the compress() routine. See the definitions above.
879 register char_type *stackp;
880 register int finchar;
881 register code_int code, oldcode, incode;
884 * As above, initialize the first 256 entries in the table.
886 maxcode = MAXCODE(n_bits = INIT_BITS);
887 for ( code = 255; code >= 0; code-- ) {
888 tab_prefixof(code) = 0;
889 tab_suffixof(code) = (char_type)code;
891 free_ent = ((block_compress) ? FIRST : 256 );
893 finchar = oldcode = getcode();
894 if(oldcode == -1) /* EOF already? */
895 return; /* Get out of here */
896 putchar( (char)finchar ); /* first code must be 8 bits = char */
897 if(ferror(stdout)) /* Crash if can't write */
901 while ( (code = getcode()) > -1 ) {
903 if ( (code == CLEAR) && block_compress ) {
904 for ( code = 255; code >= 0; code-- )
905 tab_prefixof(code) = 0;
907 free_ent = FIRST - 1;
908 if ( (code = getcode ()) == -1 ) /* O, untimely death! */
913 * Special case for KwKwK string.
915 if ( code >= free_ent ) {
921 * Generate output characters in reverse order
923 #ifdef SIGNED_COMPARE_SLOW
924 while ( ((unsigned long)code) >= ((unsigned long)256) ) {
926 while ( code >= 256 ) {
928 *stackp++ = tab_suffixof(code);
929 code = tab_prefixof(code);
931 *stackp++ = finchar = tab_suffixof(code);
934 * And put them out in forward order
937 putchar ( *--stackp );
938 while ( stackp > de_stack );
941 * Generate the new entry.
943 if ( (code=free_ent) < maxmaxcode ) {
944 tab_prefixof(code) = (unsigned short)oldcode;
945 tab_suffixof(code) = finchar;
949 * Remember previous code.
958 /*****************************************************************
961 * Read one code from the standard input. If EOF, return -1.
965 * code or -1 is returned.
971 * On the VAX, it is important to have the register declarations
972 * in exactly the order given, or the asm will break.
974 register code_int code;
975 static int offset = 0, size = 0;
976 static char_type buf[BITS];
977 register int r_off, bits;
978 register char_type *bp = buf;
980 if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
982 * If the next entry will be too big for the current code
983 * size, then we must increase the size. This implies reading
984 * a new buffer full, too.
986 if ( free_ent > maxcode ) {
988 if ( n_bits == maxbits )
989 maxcode = maxmaxcode; /* won't get any bigger now */
991 maxcode = MAXCODE(n_bits);
993 if ( clear_flg > 0) {
994 maxcode = MAXCODE (n_bits = INIT_BITS);
997 size = fread( buf, 1, n_bits, stdin );
999 return -1; /* end of file */
1001 /* Round size down to integral number of codes */
1002 size = (size << 3) - (n_bits - 1);
1007 asm( "extzv r10,r9,(r8),r11" );
1008 #else /* not a vax */
1010 * Get to the first byte.
1014 /* Get first part (low order bits) */
1016 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
1018 code = (*bp++ >> r_off);
1019 #endif /* NO_UCHAR */
1020 bits -= (8 - r_off);
1021 r_off = 8 - r_off; /* now, offset into code word */
1022 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
1025 code |= (*bp++ & 0xff) << r_off;
1027 code |= *bp++ << r_off;
1028 #endif /* NO_UCHAR */
1032 /* high order bits. */
1033 code |= (*bp & rmask[bits]) << r_off;
1041 rindex(s, c) /* For those who don't have it in libc.a */
1042 register char *s, c;
1045 for (p = NULL; *s; s++)
1055 * Just print out codes from input file. For debugging.
1060 bits = n_bits = INIT_BITS;
1061 maxcode = MAXCODE(n_bits);
1062 free_ent = ((block_compress) ? FIRST : 256 );
1063 while ( ( code = getcode() ) >= 0 ) {
1064 if ( (code == CLEAR) && block_compress ) {
1065 free_ent = FIRST - 1;
1068 else if ( free_ent < maxmaxcode )
1070 if ( bits != n_bits ) {
1071 fprintf(stderr, "\nChange to %d bits\n", n_bits );
1075 fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
1077 putc( '\n', stderr );
1082 code_int stab1[8192] ;
1083 code_int stab2[8192] ;
1084 code_int stab3[8192] ;
1085 code_int stab4[8192] ;
1086 code_int stab5[8192] ;
1087 code_int stab6[8192] ;
1088 code_int stab7[8192] ;
1089 code_int stab8[8192] ;
1090 code_int * sorttab[8] = {stab1, stab2, stab3, stab4, stab5, stab6, stab7,
1092 #define stabof(i) (sorttab[(i) >> 13][(i) & 0x1fff])
1094 code_int sorttab[HSIZE]; /* sorted pointers into htab */
1095 #define stabof(i) (sorttab[i])
1098 dump_tab() /* dump string table */
1100 register int i, first;
1102 #define STACK_SIZE 15000
1103 int stack_top = STACK_SIZE;
1107 if(do_decomp == 0) { /* compressing */
1108 register int flag = 1;
1110 for(i=0; i<hsize; i++) { /* build sort pointers */
1111 if((long)htabof(i) >= 0) {
1112 stabof(codetabof(i)) = i;
1115 first = block_compress ? FIRST : 256;
1116 for(i = first; i < free_ent; i++) {
1117 fprintf(stderr, "%5d: \"", i);
1118 de_stack[--stack_top] = '\n';
1119 de_stack[--stack_top] = '"';
1120 stack_top = in_stack((htabof(stabof(i))>>maxbits)&0xff,
1122 /* for(ent=htabof(stabof(i)) & ((1<<maxbits)-1); */
1123 mbshift = ((1 << maxbits) - 1) ;
1124 ent = htabof(stabof(i)) & mbshift ;
1127 /* ent=htabof(stabof(ent)) & ((1<<maxbits)-1)) { */
1128 ent=htabof(stabof(ent)) & mbshift) {
1129 stack_top = in_stack(htabof(stabof(ent)) >> maxbits,
1132 stack_top = in_stack(ent, stack_top);
1133 fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
1134 stack_top = STACK_SIZE;
1136 } else if(!debug) { /* decompressing */
1138 for ( i = 0; i < free_ent; i++ ) {
1140 c = tab_suffixof(ent);
1141 if ( isascii(c) && isprint(c) )
1142 fprintf( stderr, "%5d: %5d/'%c' \"",
1143 ent, tab_prefixof(ent), c );
1145 fprintf( stderr, "%5d: %5d/\\%03o \"",
1146 ent, tab_prefixof(ent), c );
1147 de_stack[--stack_top] = '\n';
1148 de_stack[--stack_top] = '"';
1150 ent = (ent >= FIRST ? tab_prefixof(ent) : 0) ) {
1151 stack_top = in_stack(tab_suffixof(ent), stack_top);
1153 fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
1154 stack_top = STACK_SIZE;
1160 in_stack(c, stack_top)
1161 register c, stack_top;
1163 if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
1164 de_stack[--stack_top] = c;
1167 case '\n': de_stack[--stack_top] = 'n'; break;
1168 case '\t': de_stack[--stack_top] = 't'; break;
1169 case '\b': de_stack[--stack_top] = 'b'; break;
1170 case '\f': de_stack[--stack_top] = 'f'; break;
1171 case '\r': de_stack[--stack_top] = 'r'; break;
1172 case '\\': de_stack[--stack_top] = '\\'; break;
1174 de_stack[--stack_top] = '0' + c % 8;
1175 de_stack[--stack_top] = '0' + (c / 8) % 8;
1176 de_stack[--stack_top] = '0' + c / 64;
1179 de_stack[--stack_top] = '\\';
1192 copystat(ifname, ofname)
1193 char *ifname, *ofname;
1195 struct stat statbuf;
1200 if (stat(ifname, &statbuf)) { /* Get stat on input file */
1204 if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
1206 fprintf(stderr, "%s: ", ifname);
1207 fprintf(stderr, " -- not a regular file: unchanged");
1209 } else if (statbuf.st_nlink > 1) {
1211 fprintf(stderr, "%s: ", ifname);
1212 fprintf(stderr, " -- has %d other links: unchanged",
1213 statbuf.st_nlink - 1);
1215 } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
1217 fprintf(stderr, " -- file unchanged");
1218 } else { /* ***** Successful Compression ***** */
1220 mode = statbuf.st_mode & 07777;
1221 if (chmod(ofname, mode)) /* Copy modes */
1223 chown(ofname, statbuf.st_uid, statbuf.st_gid); /* Copy ownership */
1224 timep[0] = statbuf.st_atime;
1225 timep[1] = statbuf.st_mtime;
1226 utime(ofname, timep); /* Update last accessed and modified times */
1227 if (unlink(ifname)) /* Remove input file */
1230 fprintf(stderr, " -- replaced with %s", ofname);
1231 return; /* Successful return */
1234 /* Unsuccessful return -- one of the tests failed */
1239 * This routine returns 1 if we are running in the foreground and stderr
1244 if(bgnd_flag) { /* background? */
1246 } else { /* foreground */
1247 if(isatty(2)) { /* and stderr is a tty */
1261 oops ( ) /* wild pointer -- assume bad input */
1263 if ( do_decomp == 1 )
1264 fprintf ( stderr, "uncompress: corrupt input\n" );
1269 cl_block () /* table clear for block compress */
1271 register long int rat;
1273 checkpoint = in_count + CHECK_GAP;
1276 fprintf ( stderr, "count: %ld, ratio: ", in_count );
1277 prratio ( stderr, in_count, bytes_out );
1278 fprintf ( stderr, "\n");
1282 if(in_count > 0x007fffff) { /* shift will overflow */
1283 rat = bytes_out >> 8;
1284 if(rat == 0) { /* Don't divide by zero */
1287 rat = in_count / rat;
1290 rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
1292 if ( rat > ratio ) {
1298 dump_tab(); /* dump string table */
1300 cl_hash ( (count_int) hsize );
1303 output ( (code_int) CLEAR );
1306 fprintf ( stderr, "clear\n" );
1311 cl_hash(hsize) /* reset code table */
1312 register count_int hsize;
1314 #ifndef XENIX_16 /* Normal machine */
1315 register count_int *htab_p = htab+hsize;
1318 register long k = hsize;
1319 register count_int *htab_p;
1322 register long m1 = -1;
1325 for(j=0; j<=8 && k>=0; j++,k-=8192) {
1330 htab_p = &(htab[j][i]);
1336 do { /* might use Sys V memset(3) here */
1354 } while ((i -= 16) >= 0);
1359 for ( i += 16; i > 0; i-- )
1363 prratio(stream, num, den)
1367 register int q; /* Doesn't need to be long */
1369 if(num > 214748L) { /* 2147483647/10000 */
1370 q = num / (den / 10000L);
1372 q = 10000L * num / den; /* Long calculations, though */
1378 fprintf(stream, "%d.%02d%%", q / 100, q % 100);
1383 fprintf(stderr, "%s\n", rcs_ident);
1384 fprintf(stderr, "Options: ");
1386 fprintf(stderr, "vax, ");
1389 fprintf(stderr, "NO_UCHAR, ");
1391 #ifdef SIGNED_COMPARE_SLOW
1392 fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
1395 fprintf(stderr, "XENIX_16, ");
1398 fprintf(stderr, "COMPATIBLE, ");
1401 fprintf(stderr, "DEBUG, ");
1404 fprintf(stderr, "BSD4_2, ");
1406 fprintf(stderr, "BITS = %d\n", BITS);