2 * Compress - data compression program
4 #define min(a,b) ((a>b) ? b : a)
8 * Set USERMEM to the maximum amount of physical user memory available
9 * in bytes. USERMEM is used to determine the maximum BITS that can be used
12 * SACREDMEM is the amount of physical memory saved for others; compress
15 /* For SPEC95 use, SACREDMEM automatically set to 0.
16 Jeff Reilly, 1/15/95 */
20 /* For SPEC95 use, USERMEM automatically set to 450000.
21 Jeff Reilly, 1/15/95 */
22 # define USERMEM 450000 /* default user memory */
24 #ifdef interdata /* (Perkin-Elmer) */
25 #define SIGNED_COMPARE_SLOW /* signed compare is slower than unsigned */
28 /* For SPEC95 use, PBITS and BITS automatically set to 16.
29 Jeff Reilyy, 1/15/95 */
32 #define HSIZE 69001 /* 95% occupancy */
36 * a code_int must be able to hold 2**BITS values of type int, and also -1
39 typedef long int code_int;
44 #ifdef SIGNED_COMPARE_SLOW
45 typedef unsigned long int count_int;
46 typedef unsigned short int count_short;
48 typedef long int count_int;
52 typedef char char_type;
54 typedef unsigned char char_type;
56 char_type magic_header[] = { "\037\235" }; /* 1F 9D */
58 /* Defines for third byte of header */
60 #define BLOCK_MASK 0x80
61 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
62 a fourth header byte (for expansion).
64 #define INIT_BITS 9 /* initial number of bits/code */
66 /* SPEC95, Original comments left - Jeff Reilly, 1/18/95 */
68 * compress.c - File compression ala IEEE Computer, June 1984.
70 * Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
71 * Jim McKie (decvax!mcvax!jim)
72 * Steve Davies (decvax!vax135!petsd!peora!srd)
73 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
74 * James A. Woods (decvax!ihnp4!ames!jaw)
75 * Joe Orost (decvax!vax135!petsd!joe)
79 * Revision 1.2 2006/07/08 10:06:28 matze
80 * fixed some more testapps
82 * Revision 1.1 2006/03/17 14:47:52 chriswue
83 * added addtional test file
85 * Revision 1.3 90/07/18 20:22:34 mips
86 * a few small changes for VMS, all of the ifdef VAX is gone.
88 * Revision 1.1 90/07/12 10:58:29 10:58:29 root ()
91 * Revision 4.0 85/07/30 12:50:00 joe
92 * Removed ferror() calls in output routine on every output except first.
93 * Prepared for release to the world.
95 * Revision 3.6 85/07/04 01:22:21 joe
96 * Remove much wasted storage by overlaying hash table with the tables
97 * used by decompress: tab_suffix[1<<BITS], stack[8000]. Updated USERMEM
98 * computations. Fixed dump_tab() DEBUG routine.
100 * Revision 3.5 85/06/30 20:47:21 jaw
101 * Change hash function to use exclusive-or. Rip out hash cache. These
102 * speedups render the megamemory version defunct, for now. Make decoder
103 * stack global. Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply.
105 * Revision 3.4 85/06/27 12:00:00 ken
106 * Get rid of all floating-point calculations by doing all compression ratio
107 * calculations in fixed point.
109 * Revision 3.3 85/06/24 21:53:24 joe
110 * Incorporate portability suggestion for M_XENIX. Got rid of text on #else
111 * and #endif lines. Cleaned up #ifdefs for vax and interdata.
113 * Revision 3.2 85/06/06 21:53:24 jaw
114 * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list.
115 * Default to "quiet" output (no compression statistics).
117 * Revision 3.1 85/05/12 18:56:13 jaw
118 * Integrate decompress() stack speedups (from early pointer mods by McKie).
119 * Repair multi-file USERMEM gaffe. Unify 'force' flags to mimic semantics
120 * of SVR2 'pack'. Streamline block-compress table clear logic. Increase
121 * output byte count by magic number size.
123 * Revision 3.0 84/11/27 11:50:00 petsd!joe
124 * Set HSIZE depending on BITS. Set BITS depending on USERMEM. Unrolled
125 * loops in clear routines. Added "-C" flag for 2.0 compatibility. Used
126 * unsigned compares on Perkin-Elmer. Fixed foreground check.
128 * Revision 2.7 84/11/16 19:35:39 ames!jaw
129 * Cache common hash codes based on input statistics; this improves
130 * performance for low-density raster images. Pass on #ifdef bundle
133 * Revision 2.6 84/11/05 19:18:21 ames!jaw
134 * Vary size of hash tables to reduce time for small files.
135 * Tune PDP-11 hash function.
137 * Revision 2.5 84/10/30 20:15:14 ames!jaw
138 * Junk chaining; replace with the simpler (and, on the VAX, faster)
139 * double hashing, discussed within. Make block compression standard.
141 * Revision 2.4 84/10/16 11:11:11 ames!jaw
142 * Introduce adaptive reset for block compression, to boost the rate
143 * another several percent. (See mailing list notes.)
145 * Revision 2.3 84/09/22 22:00:00 petsd!joe
146 * Implemented "-B" block compress. Implemented REVERSE sorting of tab_next.
147 * Bug fix for last bits. Changed fwrite to putchar loop everywhere.
149 * Revision 2.2 84/09/18 14:12:21 ames!jaw
150 * Fold in news changes, small machine typedef from thomas,
151 * #ifdef interdata from joe.
153 * Revision 2.1 84/09/10 12:34:56 ames!jaw
154 * Configured fast table lookup for 32-bit machines.
155 * This cuts user time in half for b <= FBITS, and is useful for news batching
156 * from VAX to PDP sites. Also sped up decompress() [fwrite->putc] and
157 * added signal catcher [plus beef in writeerr()] to delete effluvia.
159 * Revision 2.0 84/08/28 22:00:00 petsd!joe
160 * Add check for foreground before prompting user. Insert maxbits into
161 * compressed file. Force file being uncompressed to end with ".Z".
162 * Added "-c" flag and "zcat". Prepared for release.
164 * Revision 1.10 84/08/24 18:28:00 turtlevax!ken
165 * Will only compress regular files (no directories), added a magic number
166 * header (plus an undocumented -n flag to handle old files without headers),
167 * added -f flag to force overwriting of possibly existing destination file,
168 * otherwise the user is prompted for a response. Will tack on a .Z to a
169 * filename if it doesn't have one when decompressing. Will only replace
170 * file if it was compressed.
172 * Revision 1.9 84/08/16 17:28:00 turtlevax!ken
173 * Removed scanargs(), getopt(), added .Z extension and unlimited number of
174 * filenames to compress. Flags may be clustered (-Ddvb12) or separated
175 * (-D -d -v -b 12), or combination thereof. Modes and other status is
176 * copied with copystat(). -O bug for 4.2 seems to have disappeared with
179 * Revision 1.8 84/08/09 23:15:00 joe
180 * Made it compatible with vax version, installed jim's fixes/enhancements
182 * Revision 1.6 84/08/01 22:08:00 joe
183 * Sped up algorithm significantly by sorting the compress chain.
185 * Revision 1.5 84/07/13 13:11:00 srd
186 * Added C version of vax asm routines. Changed structure to arrays to
187 * save much memory. Do unsigned compares where possible (faster on
190 * Revision 1.4 84/07/05 03:11:11 thomas
191 * Clean up the code a little and lint it. (Lint complains about all
192 * the regs used in the asm, but I'm not going to "fix" this.)
194 * Revision 1.3 84/07/05 02:06:54 thomas
197 * Revision 1.2 84/07/05 00:27:27 thomas
198 * Add variable bit length output.
201 static char rcs_ident[] = "$Header$";
209 #define unlink delete
211 #include <sys/types.h>
212 #include <sys/stat.h>
215 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
217 int n_bits; /* number of bits/code */
218 int maxbits = BITS; /* user settable max # bits/code */
219 code_int maxcode; /* maximum code, given n_bits */
220 code_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */
221 #ifdef COMPATIBLE /* But wrong! */
222 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
224 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
225 #endif /* COMPATIBLE */
228 count_int htab0[8192];
229 count_int htab1[8192];
230 count_int htab2[8192];
231 count_int htab3[8192];
232 count_int htab4[8192];
233 count_int htab5[8192];
234 count_int htab6[8192];
235 count_int htab7[8192];
236 count_int htab8[HSIZE-65536];
237 count_int * htab[9] = {
238 htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 };
240 #define htabof(i) (htab[(i) >> 13][(i) & 0x1fff])
241 unsigned short code0tab[16384];
242 unsigned short code1tab[16384];
243 unsigned short code2tab[16384];
244 unsigned short code3tab[16384];
245 unsigned short code4tab[16384];
246 unsigned short * codetab[5] = {
247 code0tab, code1tab, code2tab, code3tab, code4tab };
249 #define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff])
251 #else /* Normal machine */
252 count_int htab [HSIZE];
253 unsigned short codetab [HSIZE];
254 #define htabof(i) htab[i]
255 #define codetabof(i) codetab[i]
256 #endif /* XENIX_16 */
257 code_int hsize = HSIZE; /* for dynamic table sizing */
261 * To save much memory, we overlay the table used by compress() with those
262 * used by decompress(). The tab_prefix table is the same size and type
263 * as the codetab. The tab_suffix table needs 2**BITS characters. We
264 * get this from the beginning of htab. The output stack uses the rest
265 * of htab, and contains characters. There is plenty of room for any
266 * possible stack (stack used to be 8000 characters).
269 #define tab_prefixof(i) codetabof(i)
271 # define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
272 # define de_stack ((char_type *)(htab2))
273 #else /* Normal machine */
274 # define tab_suffixof(i) ((char_type *)(htab))[i]
275 # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
276 #endif /* XENIX_16 */
278 code_int free_ent = 0; /* first unused entry */
283 int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
284 int zcat_flg = 0; /* Write output on stdout, suppress messages */
285 int quiet = 1; /* don't tell me about compression */
288 * block compression parameters -- after all codes are used up,
289 * and compression rate changes, start over.
291 int block_compress = BLOCK_MASK;
294 #define CHECK_GAP 10000 /* ratio check interval */
295 count_int checkpoint = CHECK_GAP;
297 * the next two codes should not be changed lightly, as they must not
298 * lie within the contiguous general code space.
300 #define FIRST 257 /* first free entry */
301 #define CLEAR 256 /* table clear output code */
313 unsigned char *InBuff;
314 unsigned char *OutBuff;
316 /*****************************************************************
319 * Algorithm from "A Technique for High Performance Data Compression",
320 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
322 * Usage: compress [-dfvc] [-b bits] [file ...]
324 * -d: If given, decompression is done instead.
326 * -c: Write output on stdout, don't remove original.
328 * -b: Parameter limits the max number of bits/code.
330 * -f: Forces output file to be generated, even if one already
331 * exists, and even if no space is saved by compressing.
332 * If -f is not used, the user will be prompted if stdin is
333 * a tty, otherwise, the output file will not be overwritten.
335 * -v: Write compression statistics
337 * file ...: Files to be compressed. If none specified, stdin
340 * file.Z: Compressed form of file with same mode, owner, and utimes
341 * or stdout (if stdin used as input)
344 * When filenames are given, replaces with the compressed version
345 * (.Z suffix) only if the file decreases in size.
347 * Modified Lempel-Ziv method (LZW). Basically finds common
348 * substrings and replaces them with a variable size code. This is
349 * deterministic, and can be done on the fly. Thus, the decompression
350 * procedure needs no input table, but tracks the way the table was built.
353 * Changed from main to spec_select_action,
354 * Jeff Reilly - 1/15/95 SPEC
357 spec_select_action(char* from_buf, int from_count, int action, char* to_buf)
361 void onintr(), oops();
363 int onintr(), oops();
369 nomagic = 1; /* Original didn't have a magic number */
370 #endif /* COMPATIBLE */
373 if(maxbits < INIT_BITS) maxbits = INIT_BITS;
374 if (maxbits > BITS) maxbits = BITS;
375 maxmaxcode = 1 << maxbits;
378 InBuff = (unsigned char *)from_buf;
379 OutBuff = (unsigned char *)to_buf;
382 if (do_decomp == 0) {
385 if(verbose) dump_tab();
388 /* Check the magic number */
390 if ((getbyte() != (magic_header[0] & 0xFF))
391 || (getbyte() != (magic_header[1] & 0xFF))) {
392 fprintf(stderr, "stdin: not in compressed format\n");
395 maxbits = getbyte(); /* set -b from file */
396 block_compress = maxbits & BLOCK_MASK;
398 maxmaxcode = 1 << maxbits;
399 fsize = 100000; /* assume stdin large for USERMEM */
402 "stdin: compressed with %d bits, can only handle %d bits\n",
410 if (debug == 0) decompress();
412 if (verbose) dump_tab();
416 return( OutBuff - (unsigned char *)to_buf );
420 long int in_count = 1; /* length of input */
421 long int bytes_out; /* length of compressed output */
422 long int out_count = 0; /* # of codes output (for debugging) */
425 * compress (Originally: stdin to stdout -- Changed by SPEC to: memory to memory)
427 * Algorithm: use open addressing double hashing (no chaining) on the
428 * prefix code / next character combination. We do a variant of Knuth's
429 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
430 * secondary probe. Here, the modular division first probe is gives way
431 * to a faster exclusive-or manipulation. Also do block compression with
432 * an adaptive reset, whereby the code table is cleared when the compression
433 * ratio decreases, but after the table fills. The variable-length output
434 * codes are re-sized at this point, and a special CLEAR code is generated
435 * for the decompressor. Late addition: construct the table according to
436 * file size for noticeable speed improvement on small files. Please direct
437 * questions about this implementation to ames!jaw.
442 register code_int i = 0;
444 register code_int ent;
446 register code_int disp;
447 #else /* Normal machine */
450 register code_int hsize_reg;
455 putbyte(magic_header[0]); putbyte(magic_header[1]);
456 putbyte((char)(maxbits | block_compress));
458 #endif /* COMPATIBLE */
461 bytes_out = 3; /* includes 3-byte header mojo */
466 checkpoint = CHECK_GAP;
467 maxcode = MAXCODE(n_bits = INIT_BITS);
468 free_ent = ((block_compress) ? FIRST : 256 );
473 for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
475 hshift = 8 - hshift; /* set hash code range bound */
478 cl_hash( (count_int) hsize_reg); /* clear hash table */
480 #ifdef SIGNED_COMPARE_SLOW
481 while ( (c = getbyte()) != (unsigned) EOF ) {
483 while ( (c = getbyte()) != EOF ) {
486 fcode = (long) (((long) c << maxbits) + ent);
487 i = ((c << hshift) ^ ent); /* xor hashing */
489 if ( htabof (i) == fcode ) {
492 } else if ( (long)htabof (i) < 0 ) /* empty slot */
494 disp = hsize_reg - i; /* secondary hash (after G. Knott) */
498 if ( (i -= disp) < 0 )
501 if ( htabof (i) == fcode ) {
505 if ( (long)htabof (i) > 0 )
508 output ( (code_int) ent );
511 #ifdef SIGNED_COMPARE_SLOW
512 if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
514 if ( free_ent < maxmaxcode ) {
516 codetabof (i) = free_ent++; /* code -> hashtable */
519 else if ( (count_int)in_count >= checkpoint && block_compress )
523 * Put out the final code.
525 output( (code_int)ent );
527 output( (code_int)-1 );
530 * Print out stats on stderr
532 if(zcat_flg == 0 && !quiet) {
535 "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
536 in_count, out_count, bytes_out );
537 prratio( stderr, in_count, bytes_out );
538 fprintf( stderr, "\n");
539 fprintf( stderr, "\tCompression as in compact: " );
540 prratio( stderr, in_count-bytes_out, in_count );
541 fprintf( stderr, "\n");
542 fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
543 free_ent - 1, n_bits );
545 fprintf( stderr, "Compression: " );
546 prratio( stderr, in_count-bytes_out, in_count );
549 if(bytes_out > in_count) /* exit(2) if no savings */
554 /*****************************************************************
557 * Output the given code.
559 * code: A n_bits-bit integer. If == -1, then EOF. This assumes
560 * that n_bits =< (long)wordsize - 1.
562 * Outputs code to the file.
564 * Chars are 8 bits long.
566 * Maintain a BITS character long buffer (so that 8 codes will
567 * fit in it exactly). Use the VAX insv instruction to insert each
568 * code in turn. When the buffer fills up empty it and start over.
571 static char buf[BITS];
573 char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
574 char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
584 * On the VAX, it is important to have the register declarations
585 * in exactly the order given, or the asm will break.
587 register int r_off = offset, bits= n_bits;
588 register char * bp = buf;
592 fprintf( stderr, "%5d%c", code,
593 (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
597 * byte/bit numbering on the VAX is simulated by the following code
600 * Get to the first byte.
605 * Since code is always >= 8 bits, only need to mask the first
608 *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
612 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
622 if ( offset == (n_bits << 3) ) {
633 * If the next entry is going to be too big for the code size,
634 * then increase it, if possible.
636 if ( free_ent > maxcode || (clear_flg > 0))
639 * Write the whole buffer, because the input side won't
640 * discover the size increase until after it has read it.
643 writebytes( buf, n_bits );
649 maxcode = MAXCODE (n_bits = INIT_BITS);
654 if ( n_bits == maxbits )
655 maxcode = maxmaxcode;
657 maxcode = MAXCODE(n_bits);
661 fprintf( stderr, "\nChange to %d bits\n", n_bits );
668 * At EOF, write the rest of the buffer.
671 writebytes( buf, ((offset + 7) / 8) );
672 bytes_out += (offset + 7) / 8;
676 fprintf( stderr, "\n" );
682 * Decompress stdin to stdout. This routine adapts to the codes in the
683 * file building the "string" table on-the-fly; requiring no table to
684 * be stored in the compressed file. The tables used herein are shared
685 * with those of the compress() routine. See the definitions above.
689 register char_type *stackp;
690 register int finchar;
691 register code_int code, oldcode, incode;
694 * As above, initialize the first 256 entries in the table.
696 maxcode = MAXCODE(n_bits = INIT_BITS);
697 for ( code = 255; code >= 0; code-- ) {
698 tab_prefixof(code) = 0;
699 tab_suffixof(code) = (char_type)code;
701 free_ent = ((block_compress) ? FIRST : 256 );
703 finchar = oldcode = getcode();
704 if(oldcode == -1) /* EOF already? */
705 return; /* Get out of here */
706 putbyte( (char)finchar ); /* first code must be 8 bits = char */
709 while ( (code = getcode()) > -1 ) {
711 if ( (code == CLEAR) && block_compress ) {
712 for ( code = 255; code >= 0; code-- )
713 tab_prefixof(code) = 0;
715 free_ent = FIRST - 1;
716 if ( (code = getcode ()) == -1 ) /* O, untimely death! */
721 * Special case for KwKwK string.
723 if ( code >= free_ent ) {
729 * Generate output characters in reverse order
731 #ifdef SIGNED_COMPARE_SLOW
732 while ( ((unsigned long)code) >= ((unsigned long)256) ) {
734 while ( code >= 256 ) {
736 *stackp++ = tab_suffixof(code);
737 code = tab_prefixof(code);
739 *stackp++ = finchar = tab_suffixof(code);
742 * And put them out in forward order
745 putbyte ( *--stackp );
746 while ( stackp > de_stack );
749 * Generate the new entry.
751 if ( (code=free_ent) < maxmaxcode ) {
752 tab_prefixof(code) = (unsigned short)oldcode;
753 tab_suffixof(code) = finchar;
757 * Remember previous code.
763 /*****************************************************************
766 * Read one code from the standard input. If EOF, return -1.
770 * code or -1 is returned.
776 * On the VAX, it is important to have the register declarations
777 * in exactly the order given, or the asm will break.
779 register code_int code;
780 static int offset = 0, size = 0;
781 static char_type buf[BITS];
782 register int r_off, bits;
783 register char_type *bp = buf;
785 if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
787 * If the next entry will be too big for the current code
788 * size, then we must increase the size. This implies reading
789 * a new buffer full, too.
791 if ( free_ent > maxcode ) {
793 if ( n_bits == maxbits )
794 maxcode = maxmaxcode; /* won't get any bigger now */
796 maxcode = MAXCODE(n_bits);
798 if ( clear_flg > 0) {
799 maxcode = MAXCODE (n_bits = INIT_BITS);
802 size = readbytes( buf, n_bits );
804 return -1; /* end of file */
806 /* Round size down to integral number of codes */
807 size = (size << 3) - (n_bits - 1);
812 * Get to the first byte.
816 /* Get first part (low order bits) */
818 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
820 code = (*bp++ >> r_off);
821 #endif /* NO_UCHAR */
823 r_off = 8 - r_off; /* now, offset into code word */
824 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
827 code |= (*bp++ & 0xff) << r_off;
829 code |= *bp++ << r_off;
830 #endif /* NO_UCHAR */
834 /* high order bits. */
835 code |= (*bp & rmask[bits]) << r_off;
842 rindex(s, c) /* For those who don't have it in libc.a */
846 for (p = NULL; *s; s++)
856 * Just print out codes from input file. For debugging.
861 bits = n_bits = INIT_BITS;
862 maxcode = MAXCODE(n_bits);
863 free_ent = ((block_compress) ? FIRST : 256 );
864 while ( ( code = getcode() ) >= 0 ) {
865 if ( (code == CLEAR) && block_compress ) {
866 free_ent = FIRST - 1;
869 else if ( free_ent < maxmaxcode )
871 if ( bits != n_bits ) {
872 fprintf(stderr, "\nChange to %d bits\n", n_bits );
876 fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
878 putc( '\n', stderr );
882 code_int sorttab[1<<BITS]; /* sorted pointers into htab */
884 dump_tab() /* dump string table */
886 register int i, first;
888 #define STACK_SIZE 15000
889 int stack_top = STACK_SIZE;
892 if(do_decomp == 0) { /* compressing */
893 register int flag = 1;
895 for(i=0; i<hsize; i++) { /* build sort pointers */
896 if((long)htabof(i) >= 0) {
897 sorttab[codetabof(i)] = i;
900 first = block_compress ? FIRST : 256;
901 for(i = first; i < free_ent; i++) {
902 fprintf(stderr, "%5d: \"", i);
903 de_stack[--stack_top] = '\n';
904 de_stack[--stack_top] = '"';
905 stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
907 for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
909 ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
910 stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
913 stack_top = in_stack(ent, stack_top);
914 fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
915 stack_top = STACK_SIZE;
917 } else if(!debug) { /* decompressing */
919 for ( i = 0; i < free_ent; i++ ) {
921 c = tab_suffixof(ent);
922 if ( isascii(c) && isprint(c) )
923 fprintf( stderr, "%5d: %5d/'%c' \"",
924 ent, tab_prefixof(ent), c );
926 fprintf( stderr, "%5d: %5d/\\%03o \"",
927 ent, tab_prefixof(ent), c );
928 de_stack[--stack_top] = '\n';
929 de_stack[--stack_top] = '"';
931 ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
932 stack_top = in_stack(tab_suffixof(ent), stack_top);
934 fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
935 stack_top = STACK_SIZE;
941 in_stack(c, stack_top)
942 register c, stack_top;
944 if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
945 de_stack[--stack_top] = c;
948 case '\n': de_stack[--stack_top] = 'n'; break;
949 case '\t': de_stack[--stack_top] = 't'; break;
950 case '\b': de_stack[--stack_top] = 'b'; break;
951 case '\f': de_stack[--stack_top] = 'f'; break;
952 case '\r': de_stack[--stack_top] = 'r'; break;
953 case '\\': de_stack[--stack_top] = '\\'; break;
955 de_stack[--stack_top] = '0' + c % 8;
956 de_stack[--stack_top] = '0' + (c / 8) % 8;
957 de_stack[--stack_top] = '0' + c / 64;
960 de_stack[--stack_top] = '\\';
979 oops ( ) /* wild pointer -- assume bad input */
981 if ( do_decomp == 1 )
982 fprintf ( stderr, "uncompress: corrupt input\n" );
987 cl_block () /* table clear for block compress */
989 register long int rat;
991 checkpoint = in_count + CHECK_GAP;
994 fprintf ( stderr, "count: %ld, ratio: ", in_count );
995 prratio ( stderr, in_count, bytes_out );
996 fprintf ( stderr, "\n");
1000 if(in_count > 0x007fffff) { /* shift will overflow */
1001 rat = bytes_out >> 8;
1002 if(rat == 0) { /* Don't divide by zero */
1005 rat = in_count / rat;
1008 rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
1010 if ( rat > ratio ) {
1016 dump_tab(); /* dump string table */
1018 cl_hash ( (count_int) hsize );
1021 output ( (code_int) CLEAR );
1024 fprintf ( stderr, "clear\n" );
1029 cl_hash(hsize) /* reset code table */
1030 register count_int hsize;
1032 #ifndef XENIX_16 /* Normal machine */
1033 register count_int *htab_p = htab+hsize;
1036 register long k = hsize;
1037 register count_int *htab_p;
1040 register long m1 = -1;
1043 for(j=0; j<=8 && k>=0; j++,k-=8192) {
1048 htab_p = &(htab[j][i]);
1054 do { /* might use Sys V memset(3) here */
1072 } while ((i -= 16) >= 0);
1077 for ( i += 16; i > 0; i-- )
1081 prratio(stream, num, den)
1085 register int q; /* Doesn't need to be long */
1087 if(num > 214748L) { /* 2147483647/10000 */
1088 q = num / (den / 10000L);
1090 q = 10000L * num / den; /* Long calculations, though */
1096 fprintf(stream, "%d.%02d%%", q / 100, q % 100);
1101 fprintf(stderr, "%s\n", rcs_ident);
1102 fprintf(stderr, "Options: ");
1104 fprintf(stderr, "NO_UCHAR, ");
1106 #ifdef SIGNED_COMPARE_SLOW
1107 fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
1110 fprintf(stderr, "XENIX_16, ");
1113 fprintf(stderr, "COMPATIBLE, ");
1116 fprintf(stderr, "DEBUG, ");
1119 fprintf(stderr, "BSD4_2, ");
1121 fprintf(stderr, "BITS = %d\n", BITS);
1127 /* SPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPEC */
1132 return( (unsigned int)*InBuff++ );
1156 for( i=0; i<n; i++ ) {
1164 writebytes( buf, n )
1170 for( i=0; i<n; i++ )
1171 *OutBuff++ = buf[i];