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.1 2006/03/17 14:47:52 chriswue
80 * added addtional test file
82 * Revision 1.3 90/07/18 20:22:34 mips
83 * a few small changes for VMS, all of the ifdef VAX is gone.
85 * Revision 1.1 90/07/12 10:58:29 10:58:29 root ()
88 * Revision 4.0 85/07/30 12:50:00 joe
89 * Removed ferror() calls in output routine on every output except first.
90 * Prepared for release to the world.
92 * Revision 3.6 85/07/04 01:22:21 joe
93 * Remove much wasted storage by overlaying hash table with the tables
94 * used by decompress: tab_suffix[1<<BITS], stack[8000]. Updated USERMEM
95 * computations. Fixed dump_tab() DEBUG routine.
97 * Revision 3.5 85/06/30 20:47:21 jaw
98 * Change hash function to use exclusive-or. Rip out hash cache. These
99 * speedups render the megamemory version defunct, for now. Make decoder
100 * stack global. Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply.
102 * Revision 3.4 85/06/27 12:00:00 ken
103 * Get rid of all floating-point calculations by doing all compression ratio
104 * calculations in fixed point.
106 * Revision 3.3 85/06/24 21:53:24 joe
107 * Incorporate portability suggestion for M_XENIX. Got rid of text on #else
108 * and #endif lines. Cleaned up #ifdefs for vax and interdata.
110 * Revision 3.2 85/06/06 21:53:24 jaw
111 * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list.
112 * Default to "quiet" output (no compression statistics).
114 * Revision 3.1 85/05/12 18:56:13 jaw
115 * Integrate decompress() stack speedups (from early pointer mods by McKie).
116 * Repair multi-file USERMEM gaffe. Unify 'force' flags to mimic semantics
117 * of SVR2 'pack'. Streamline block-compress table clear logic. Increase
118 * output byte count by magic number size.
120 * Revision 3.0 84/11/27 11:50:00 petsd!joe
121 * Set HSIZE depending on BITS. Set BITS depending on USERMEM. Unrolled
122 * loops in clear routines. Added "-C" flag for 2.0 compatibility. Used
123 * unsigned compares on Perkin-Elmer. Fixed foreground check.
125 * Revision 2.7 84/11/16 19:35:39 ames!jaw
126 * Cache common hash codes based on input statistics; this improves
127 * performance for low-density raster images. Pass on #ifdef bundle
130 * Revision 2.6 84/11/05 19:18:21 ames!jaw
131 * Vary size of hash tables to reduce time for small files.
132 * Tune PDP-11 hash function.
134 * Revision 2.5 84/10/30 20:15:14 ames!jaw
135 * Junk chaining; replace with the simpler (and, on the VAX, faster)
136 * double hashing, discussed within. Make block compression standard.
138 * Revision 2.4 84/10/16 11:11:11 ames!jaw
139 * Introduce adaptive reset for block compression, to boost the rate
140 * another several percent. (See mailing list notes.)
142 * Revision 2.3 84/09/22 22:00:00 petsd!joe
143 * Implemented "-B" block compress. Implemented REVERSE sorting of tab_next.
144 * Bug fix for last bits. Changed fwrite to putchar loop everywhere.
146 * Revision 2.2 84/09/18 14:12:21 ames!jaw
147 * Fold in news changes, small machine typedef from thomas,
148 * #ifdef interdata from joe.
150 * Revision 2.1 84/09/10 12:34:56 ames!jaw
151 * Configured fast table lookup for 32-bit machines.
152 * This cuts user time in half for b <= FBITS, and is useful for news batching
153 * from VAX to PDP sites. Also sped up decompress() [fwrite->putc] and
154 * added signal catcher [plus beef in writeerr()] to delete effluvia.
156 * Revision 2.0 84/08/28 22:00:00 petsd!joe
157 * Add check for foreground before prompting user. Insert maxbits into
158 * compressed file. Force file being uncompressed to end with ".Z".
159 * Added "-c" flag and "zcat". Prepared for release.
161 * Revision 1.10 84/08/24 18:28:00 turtlevax!ken
162 * Will only compress regular files (no directories), added a magic number
163 * header (plus an undocumented -n flag to handle old files without headers),
164 * added -f flag to force overwriting of possibly existing destination file,
165 * otherwise the user is prompted for a response. Will tack on a .Z to a
166 * filename if it doesn't have one when decompressing. Will only replace
167 * file if it was compressed.
169 * Revision 1.9 84/08/16 17:28:00 turtlevax!ken
170 * Removed scanargs(), getopt(), added .Z extension and unlimited number of
171 * filenames to compress. Flags may be clustered (-Ddvb12) or separated
172 * (-D -d -v -b 12), or combination thereof. Modes and other status is
173 * copied with copystat(). -O bug for 4.2 seems to have disappeared with
176 * Revision 1.8 84/08/09 23:15:00 joe
177 * Made it compatible with vax version, installed jim's fixes/enhancements
179 * Revision 1.6 84/08/01 22:08:00 joe
180 * Sped up algorithm significantly by sorting the compress chain.
182 * Revision 1.5 84/07/13 13:11:00 srd
183 * Added C version of vax asm routines. Changed structure to arrays to
184 * save much memory. Do unsigned compares where possible (faster on
187 * Revision 1.4 84/07/05 03:11:11 thomas
188 * Clean up the code a little and lint it. (Lint complains about all
189 * the regs used in the asm, but I'm not going to "fix" this.)
191 * Revision 1.3 84/07/05 02:06:54 thomas
194 * Revision 1.2 84/07/05 00:27:27 thomas
195 * Add variable bit length output.
198 static char rcs_ident[] = "$Header$";
205 #define unlink delete
207 #include <sys/types.h>
208 #include <sys/stat.h>
211 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
213 int n_bits; /* number of bits/code */
214 int maxbits = BITS; /* user settable max # bits/code */
215 code_int maxcode; /* maximum code, given n_bits */
216 code_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */
217 #ifdef COMPATIBLE /* But wrong! */
218 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
220 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
221 #endif /* COMPATIBLE */
224 count_int htab0[8192];
225 count_int htab1[8192];
226 count_int htab2[8192];
227 count_int htab3[8192];
228 count_int htab4[8192];
229 count_int htab5[8192];
230 count_int htab6[8192];
231 count_int htab7[8192];
232 count_int htab8[HSIZE-65536];
233 count_int * htab[9] = {
234 htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 };
236 #define htabof(i) (htab[(i) >> 13][(i) & 0x1fff])
237 unsigned short code0tab[16384];
238 unsigned short code1tab[16384];
239 unsigned short code2tab[16384];
240 unsigned short code3tab[16384];
241 unsigned short code4tab[16384];
242 unsigned short * codetab[5] = {
243 code0tab, code1tab, code2tab, code3tab, code4tab };
245 #define codetabof(i) (codetab[(i) >> 14][(i) & 0x3fff])
247 #else /* Normal machine */
248 count_int htab [HSIZE];
249 unsigned short codetab [HSIZE];
250 #define htabof(i) htab[i]
251 #define codetabof(i) codetab[i]
252 #endif /* XENIX_16 */
253 code_int hsize = HSIZE; /* for dynamic table sizing */
257 * To save much memory, we overlay the table used by compress() with those
258 * used by decompress(). The tab_prefix table is the same size and type
259 * as the codetab. The tab_suffix table needs 2**BITS characters. We
260 * get this from the beginning of htab. The output stack uses the rest
261 * of htab, and contains characters. There is plenty of room for any
262 * possible stack (stack used to be 8000 characters).
265 #define tab_prefixof(i) codetabof(i)
267 # define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
268 # define de_stack ((char_type *)(htab2))
269 #else /* Normal machine */
270 # define tab_suffixof(i) ((char_type *)(htab))[i]
271 # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
272 #endif /* XENIX_16 */
274 code_int free_ent = 0; /* first unused entry */
279 int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
280 int zcat_flg = 0; /* Write output on stdout, suppress messages */
281 int quiet = 1; /* don't tell me about compression */
284 * block compression parameters -- after all codes are used up,
285 * and compression rate changes, start over.
287 int block_compress = BLOCK_MASK;
290 #define CHECK_GAP 10000 /* ratio check interval */
291 count_int checkpoint = CHECK_GAP;
293 * the next two codes should not be changed lightly, as they must not
294 * lie within the contiguous general code space.
296 #define FIRST 257 /* first free entry */
297 #define CLEAR 256 /* table clear output code */
309 unsigned char *InBuff;
310 unsigned char *OutBuff;
312 /*****************************************************************
315 * Algorithm from "A Technique for High Performance Data Compression",
316 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
318 * Usage: compress [-dfvc] [-b bits] [file ...]
320 * -d: If given, decompression is done instead.
322 * -c: Write output on stdout, don't remove original.
324 * -b: Parameter limits the max number of bits/code.
326 * -f: Forces output file to be generated, even if one already
327 * exists, and even if no space is saved by compressing.
328 * If -f is not used, the user will be prompted if stdin is
329 * a tty, otherwise, the output file will not be overwritten.
331 * -v: Write compression statistics
333 * file ...: Files to be compressed. If none specified, stdin
336 * file.Z: Compressed form of file with same mode, owner, and utimes
337 * or stdout (if stdin used as input)
340 * When filenames are given, replaces with the compressed version
341 * (.Z suffix) only if the file decreases in size.
343 * Modified Lempel-Ziv method (LZW). Basically finds common
344 * substrings and replaces them with a variable size code. This is
345 * deterministic, and can be done on the fly. Thus, the decompression
346 * procedure needs no input table, but tracks the way the table was built.
349 * Changed from main to spec_select_action,
350 * Jeff Reilly - 1/15/95 SPEC
353 spec_select_action(char* from_buf, int from_count, int action, char* to_buf)
355 char *cp, *rindex(), *malloc();
358 void onintr(), oops();
360 int onintr(), oops();
366 nomagic = 1; /* Original didn't have a magic number */
367 #endif /* COMPATIBLE */
370 if(maxbits < INIT_BITS) maxbits = INIT_BITS;
371 if (maxbits > BITS) maxbits = BITS;
372 maxmaxcode = 1 << maxbits;
375 InBuff = (unsigned char *)from_buf;
376 OutBuff = (unsigned char *)to_buf;
379 if (do_decomp == 0) {
382 if(verbose) dump_tab();
385 /* Check the magic number */
387 if ((getbyte() != (magic_header[0] & 0xFF))
388 || (getbyte() != (magic_header[1] & 0xFF))) {
389 fprintf(stderr, "stdin: not in compressed format\n");
392 maxbits = getbyte(); /* set -b from file */
393 block_compress = maxbits & BLOCK_MASK;
395 maxmaxcode = 1 << maxbits;
396 fsize = 100000; /* assume stdin large for USERMEM */
399 "stdin: compressed with %d bits, can only handle %d bits\n",
407 if (debug == 0) decompress();
409 if (verbose) dump_tab();
413 return( OutBuff - (unsigned char *)to_buf );
417 long int in_count = 1; /* length of input */
418 long int bytes_out; /* length of compressed output */
419 long int out_count = 0; /* # of codes output (for debugging) */
422 * compress (Originally: stdin to stdout -- Changed by SPEC to: memory to memory)
424 * Algorithm: use open addressing double hashing (no chaining) on the
425 * prefix code / next character combination. We do a variant of Knuth's
426 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
427 * secondary probe. Here, the modular division first probe is gives way
428 * to a faster exclusive-or manipulation. Also do block compression with
429 * an adaptive reset, whereby the code table is cleared when the compression
430 * ratio decreases, but after the table fills. The variable-length output
431 * codes are re-sized at this point, and a special CLEAR code is generated
432 * for the decompressor. Late addition: construct the table according to
433 * file size for noticeable speed improvement on small files. Please direct
434 * questions about this implementation to ames!jaw.
439 register code_int i = 0;
441 register code_int ent;
443 register code_int disp;
444 #else /* Normal machine */
447 register code_int hsize_reg;
452 putbyte(magic_header[0]); putbyte(magic_header[1]);
453 putbyte((char)(maxbits | block_compress));
455 #endif /* COMPATIBLE */
458 bytes_out = 3; /* includes 3-byte header mojo */
463 checkpoint = CHECK_GAP;
464 maxcode = MAXCODE(n_bits = INIT_BITS);
465 free_ent = ((block_compress) ? FIRST : 256 );
470 for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
472 hshift = 8 - hshift; /* set hash code range bound */
475 cl_hash( (count_int) hsize_reg); /* clear hash table */
477 #ifdef SIGNED_COMPARE_SLOW
478 while ( (c = getbyte()) != (unsigned) EOF ) {
480 while ( (c = getbyte()) != EOF ) {
483 fcode = (long) (((long) c << maxbits) + ent);
484 i = ((c << hshift) ^ ent); /* xor hashing */
486 if ( htabof (i) == fcode ) {
489 } else if ( (long)htabof (i) < 0 ) /* empty slot */
491 disp = hsize_reg - i; /* secondary hash (after G. Knott) */
495 if ( (i -= disp) < 0 )
498 if ( htabof (i) == fcode ) {
502 if ( (long)htabof (i) > 0 )
505 output ( (code_int) ent );
508 #ifdef SIGNED_COMPARE_SLOW
509 if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
511 if ( free_ent < maxmaxcode ) {
513 codetabof (i) = free_ent++; /* code -> hashtable */
516 else if ( (count_int)in_count >= checkpoint && block_compress )
520 * Put out the final code.
522 output( (code_int)ent );
524 output( (code_int)-1 );
527 * Print out stats on stderr
529 if(zcat_flg == 0 && !quiet) {
532 "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
533 in_count, out_count, bytes_out );
534 prratio( stderr, in_count, bytes_out );
535 fprintf( stderr, "\n");
536 fprintf( stderr, "\tCompression as in compact: " );
537 prratio( stderr, in_count-bytes_out, in_count );
538 fprintf( stderr, "\n");
539 fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
540 free_ent - 1, n_bits );
542 fprintf( stderr, "Compression: " );
543 prratio( stderr, in_count-bytes_out, in_count );
546 if(bytes_out > in_count) /* exit(2) if no savings */
551 /*****************************************************************
554 * Output the given code.
556 * code: A n_bits-bit integer. If == -1, then EOF. This assumes
557 * that n_bits =< (long)wordsize - 1.
559 * Outputs code to the file.
561 * Chars are 8 bits long.
563 * Maintain a BITS character long buffer (so that 8 codes will
564 * fit in it exactly). Use the VAX insv instruction to insert each
565 * code in turn. When the buffer fills up empty it and start over.
568 static char buf[BITS];
570 char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
571 char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
581 * On the VAX, it is important to have the register declarations
582 * in exactly the order given, or the asm will break.
584 register int r_off = offset, bits= n_bits;
585 register char * bp = buf;
589 fprintf( stderr, "%5d%c", code,
590 (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
594 * byte/bit numbering on the VAX is simulated by the following code
597 * Get to the first byte.
602 * Since code is always >= 8 bits, only need to mask the first
605 *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
609 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
619 if ( offset == (n_bits << 3) ) {
630 * If the next entry is going to be too big for the code size,
631 * then increase it, if possible.
633 if ( free_ent > maxcode || (clear_flg > 0))
636 * Write the whole buffer, because the input side won't
637 * discover the size increase until after it has read it.
640 writebytes( buf, n_bits );
646 maxcode = MAXCODE (n_bits = INIT_BITS);
651 if ( n_bits == maxbits )
652 maxcode = maxmaxcode;
654 maxcode = MAXCODE(n_bits);
658 fprintf( stderr, "\nChange to %d bits\n", n_bits );
665 * At EOF, write the rest of the buffer.
668 writebytes( buf, ((offset + 7) / 8) );
669 bytes_out += (offset + 7) / 8;
673 fprintf( stderr, "\n" );
679 * Decompress stdin to stdout. This routine adapts to the codes in the
680 * file building the "string" table on-the-fly; requiring no table to
681 * be stored in the compressed file. The tables used herein are shared
682 * with those of the compress() routine. See the definitions above.
686 register char_type *stackp;
687 register int finchar;
688 register code_int code, oldcode, incode;
691 * As above, initialize the first 256 entries in the table.
693 maxcode = MAXCODE(n_bits = INIT_BITS);
694 for ( code = 255; code >= 0; code-- ) {
695 tab_prefixof(code) = 0;
696 tab_suffixof(code) = (char_type)code;
698 free_ent = ((block_compress) ? FIRST : 256 );
700 finchar = oldcode = getcode();
701 if(oldcode == -1) /* EOF already? */
702 return; /* Get out of here */
703 putbyte( (char)finchar ); /* first code must be 8 bits = char */
706 while ( (code = getcode()) > -1 ) {
708 if ( (code == CLEAR) && block_compress ) {
709 for ( code = 255; code >= 0; code-- )
710 tab_prefixof(code) = 0;
712 free_ent = FIRST - 1;
713 if ( (code = getcode ()) == -1 ) /* O, untimely death! */
718 * Special case for KwKwK string.
720 if ( code >= free_ent ) {
726 * Generate output characters in reverse order
728 #ifdef SIGNED_COMPARE_SLOW
729 while ( ((unsigned long)code) >= ((unsigned long)256) ) {
731 while ( code >= 256 ) {
733 *stackp++ = tab_suffixof(code);
734 code = tab_prefixof(code);
736 *stackp++ = finchar = tab_suffixof(code);
739 * And put them out in forward order
742 putbyte ( *--stackp );
743 while ( stackp > de_stack );
746 * Generate the new entry.
748 if ( (code=free_ent) < maxmaxcode ) {
749 tab_prefixof(code) = (unsigned short)oldcode;
750 tab_suffixof(code) = finchar;
754 * Remember previous code.
760 /*****************************************************************
763 * Read one code from the standard input. If EOF, return -1.
767 * code or -1 is returned.
773 * On the VAX, it is important to have the register declarations
774 * in exactly the order given, or the asm will break.
776 register code_int code;
777 static int offset = 0, size = 0;
778 static char_type buf[BITS];
779 register int r_off, bits;
780 register char_type *bp = buf;
782 if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
784 * If the next entry will be too big for the current code
785 * size, then we must increase the size. This implies reading
786 * a new buffer full, too.
788 if ( free_ent > maxcode ) {
790 if ( n_bits == maxbits )
791 maxcode = maxmaxcode; /* won't get any bigger now */
793 maxcode = MAXCODE(n_bits);
795 if ( clear_flg > 0) {
796 maxcode = MAXCODE (n_bits = INIT_BITS);
799 size = readbytes( buf, n_bits );
801 return -1; /* end of file */
803 /* Round size down to integral number of codes */
804 size = (size << 3) - (n_bits - 1);
809 * Get to the first byte.
813 /* Get first part (low order bits) */
815 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
817 code = (*bp++ >> r_off);
818 #endif /* NO_UCHAR */
820 r_off = 8 - r_off; /* now, offset into code word */
821 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
824 code |= (*bp++ & 0xff) << r_off;
826 code |= *bp++ << r_off;
827 #endif /* NO_UCHAR */
831 /* high order bits. */
832 code |= (*bp & rmask[bits]) << r_off;
839 rindex(s, c) /* For those who don't have it in libc.a */
843 for (p = NULL; *s; s++)
853 * Just print out codes from input file. For debugging.
858 bits = n_bits = INIT_BITS;
859 maxcode = MAXCODE(n_bits);
860 free_ent = ((block_compress) ? FIRST : 256 );
861 while ( ( code = getcode() ) >= 0 ) {
862 if ( (code == CLEAR) && block_compress ) {
863 free_ent = FIRST - 1;
866 else if ( free_ent < maxmaxcode )
868 if ( bits != n_bits ) {
869 fprintf(stderr, "\nChange to %d bits\n", n_bits );
873 fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
875 putc( '\n', stderr );
879 code_int sorttab[1<<BITS]; /* sorted pointers into htab */
881 dump_tab() /* dump string table */
883 register int i, first;
885 #define STACK_SIZE 15000
886 int stack_top = STACK_SIZE;
889 if(do_decomp == 0) { /* compressing */
890 register int flag = 1;
892 for(i=0; i<hsize; i++) { /* build sort pointers */
893 if((long)htabof(i) >= 0) {
894 sorttab[codetabof(i)] = i;
897 first = block_compress ? FIRST : 256;
898 for(i = first; i < free_ent; i++) {
899 fprintf(stderr, "%5d: \"", i);
900 de_stack[--stack_top] = '\n';
901 de_stack[--stack_top] = '"';
902 stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
904 for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
906 ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
907 stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
910 stack_top = in_stack(ent, stack_top);
911 fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
912 stack_top = STACK_SIZE;
914 } else if(!debug) { /* decompressing */
916 for ( i = 0; i < free_ent; i++ ) {
918 c = tab_suffixof(ent);
919 if ( isascii(c) && isprint(c) )
920 fprintf( stderr, "%5d: %5d/'%c' \"",
921 ent, tab_prefixof(ent), c );
923 fprintf( stderr, "%5d: %5d/\\%03o \"",
924 ent, tab_prefixof(ent), c );
925 de_stack[--stack_top] = '\n';
926 de_stack[--stack_top] = '"';
928 ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
929 stack_top = in_stack(tab_suffixof(ent), stack_top);
931 fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
932 stack_top = STACK_SIZE;
938 in_stack(c, stack_top)
939 register c, stack_top;
941 if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
942 de_stack[--stack_top] = c;
945 case '\n': de_stack[--stack_top] = 'n'; break;
946 case '\t': de_stack[--stack_top] = 't'; break;
947 case '\b': de_stack[--stack_top] = 'b'; break;
948 case '\f': de_stack[--stack_top] = 'f'; break;
949 case '\r': de_stack[--stack_top] = 'r'; break;
950 case '\\': de_stack[--stack_top] = '\\'; break;
952 de_stack[--stack_top] = '0' + c % 8;
953 de_stack[--stack_top] = '0' + (c / 8) % 8;
954 de_stack[--stack_top] = '0' + c / 64;
957 de_stack[--stack_top] = '\\';
976 oops ( ) /* wild pointer -- assume bad input */
978 if ( do_decomp == 1 )
979 fprintf ( stderr, "uncompress: corrupt input\n" );
984 cl_block () /* table clear for block compress */
986 register long int rat;
988 checkpoint = in_count + CHECK_GAP;
991 fprintf ( stderr, "count: %ld, ratio: ", in_count );
992 prratio ( stderr, in_count, bytes_out );
993 fprintf ( stderr, "\n");
997 if(in_count > 0x007fffff) { /* shift will overflow */
998 rat = bytes_out >> 8;
999 if(rat == 0) { /* Don't divide by zero */
1002 rat = in_count / rat;
1005 rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
1007 if ( rat > ratio ) {
1013 dump_tab(); /* dump string table */
1015 cl_hash ( (count_int) hsize );
1018 output ( (code_int) CLEAR );
1021 fprintf ( stderr, "clear\n" );
1026 cl_hash(hsize) /* reset code table */
1027 register count_int hsize;
1029 #ifndef XENIX_16 /* Normal machine */
1030 register count_int *htab_p = htab+hsize;
1033 register long k = hsize;
1034 register count_int *htab_p;
1037 register long m1 = -1;
1040 for(j=0; j<=8 && k>=0; j++,k-=8192) {
1045 htab_p = &(htab[j][i]);
1051 do { /* might use Sys V memset(3) here */
1069 } while ((i -= 16) >= 0);
1074 for ( i += 16; i > 0; i-- )
1078 prratio(stream, num, den)
1082 register int q; /* Doesn't need to be long */
1084 if(num > 214748L) { /* 2147483647/10000 */
1085 q = num / (den / 10000L);
1087 q = 10000L * num / den; /* Long calculations, though */
1093 fprintf(stream, "%d.%02d%%", q / 100, q % 100);
1098 fprintf(stderr, "%s\n", rcs_ident);
1099 fprintf(stderr, "Options: ");
1101 fprintf(stderr, "NO_UCHAR, ");
1103 #ifdef SIGNED_COMPARE_SLOW
1104 fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
1107 fprintf(stderr, "XENIX_16, ");
1110 fprintf(stderr, "COMPATIBLE, ");
1113 fprintf(stderr, "DEBUG, ");
1116 fprintf(stderr, "BSD4_2, ");
1118 fprintf(stderr, "BITS = %d\n", BITS);
1124 /* SPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPEC */
1129 return( (unsigned int)*InBuff++ );
1153 for( i=0; i<n; i++ ) {
1161 writebytes( buf, n )
1167 for( i=0; i<n; i++ )
1168 *OutBuff++ = buf[i];