Make ICC more happy by spelling -f combo as -fcombo, so it can properly ignore "combo...
[libfirm] / ir / be / test / compress95.c
1 /*
2  * Compress - data compression program
3  */
4 #define min(a,b)        ((a>b) ? b : a)
5
6
7 /*
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
10  * for compression.
11  *
12  * SACREDMEM is the amount of physical memory saved for others; compress
13  * will hog the rest.
14  */
15 /* For SPEC95 use, SACREDMEM automatically set to 0.
16         Jeff Reilly, 1/15/95                            */
17
18 #define SACREDMEM       0
19
20 /* For SPEC95 use, USERMEM automatically set to 450000.
21         Jeff Reilly, 1/15/95                            */
22 # define USERMEM        450000  /* default user memory */
23
24 #ifdef interdata                /* (Perkin-Elmer) */
25 #define SIGNED_COMPARE_SLOW     /* signed compare is slower than unsigned */
26 #endif
27
28 /* For SPEC95 use, PBITS and BITS automatically set to 16.
29         Jeff Reilyy, 1/15/95                            */
30 #define PBITS   16
31 #define BITS 16
32 #define HSIZE   69001           /* 95% occupancy */
33
34
35 /*
36  * a code_int must be able to hold 2**BITS values of type int, and also -1
37  */
38 #if BITS > 15
39 typedef long int        code_int;
40 #else
41 typedef int             code_int;
42 #endif
43
44 #ifdef SIGNED_COMPARE_SLOW
45 typedef unsigned long int count_int;
46 typedef unsigned short int count_short;
47 #else
48 typedef long int          count_int;
49 #endif
50
51 #ifdef NO_UCHAR
52  typedef char   char_type;
53 #else
54  typedef        unsigned char   char_type;
55 #endif /* UCHAR */
56 char_type magic_header[] = { "\037\235" };      /* 1F 9D */
57
58 /* Defines for third byte of header */
59 #define BIT_MASK        0x1f
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).
63 */
64 #define INIT_BITS 9                     /* initial number of bits/code */
65
66 /* SPEC95, Original comments left  - Jeff Reilly, 1/18/95 */
67 /*
68  * compress.c - File compression ala IEEE Computer, June 1984.
69  *
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)
76  *
77  * $Header$
78  * $Log$
79  * Revision 1.2  2006/07/08 10:06:28  matze
80  * fixed some more testapps
81  *
82  * Revision 1.1  2006/03/17 14:47:52  chriswue
83  * added addtional test file
84  *
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.
87  *
88  * Revision 1.1  90/07/12  10:58:29  10:58:29  root ()
89  * Initial revision
90  *
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.
94  *
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.
99  *
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.
104  *
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.
108  *
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.
112  *
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).
116  *
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.
122  *
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.
127  *
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
131  * from Turkowski.
132  *
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.
136  *
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.
140  *
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.)
144  *
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.
148  *
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.
152  *
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.
158  *
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.
163  *
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.
171  *
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
177  * 1.8.
178  *
179  * Revision 1.8  84/08/09  23:15:00  joe
180  * Made it compatible with vax version, installed jim's fixes/enhancements
181  *
182  * Revision 1.6  84/08/01  22:08:00  joe
183  * Sped up algorithm significantly by sorting the compress chain.
184  *
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
188  * Perkin-Elmer)
189  *
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.)
193  *
194  * Revision 1.3  84/07/05  02:06:54  thomas
195  * Minor fixes.
196  *
197  * Revision 1.2  84/07/05  00:27:27  thomas
198  * Add variable bit length output.
199  *
200  */
201 static char rcs_ident[] = "$Header$";
202
203 #include <stdio.h>
204 #include <ctype.h>
205 #include <stdlib.h>
206 #ifdef VMS
207 #include <types.h>
208 #include <stat.h>
209 #define unlink delete
210 #else
211 #include <sys/types.h>
212 #include <sys/stat.h>
213 #endif  /* VMS  */
214
215 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
216
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)
223 #else
224 # define MAXCODE(n_bits)        ((1 << (n_bits)) - 1)
225 #endif /* COMPATIBLE */
226
227 #ifdef XENIX_16
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 };
239
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 };
248
249 #define codetabof(i)    (codetab[(i) >> 14][(i) & 0x3fff])
250
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 */
258 count_int fsize;
259
260 /*
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).
267  */
268
269 #define tab_prefixof(i) codetabof(i)
270 #ifdef XENIX_16
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 */
277
278 code_int free_ent = 0;                  /* first unused entry */
279 int exit_stat = 0;
280
281 code_int getcode();
282
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 */
286
287 /*
288  * block compression parameters -- after all codes are used up,
289  * and compression rate changes, start over.
290  */
291 int block_compress = BLOCK_MASK;
292 int clear_flg = 0;
293 long int ratio = 0;
294 #define CHECK_GAP 10000 /* ratio check interval */
295 count_int checkpoint = CHECK_GAP;
296 /*
297  * the next two codes should not be changed lightly, as they must not
298  * lie within the contiguous general code space.
299  */
300 #define FIRST   257     /* first free entry */
301 #define CLEAR   256     /* table clear output code */
302
303 int force = 0;
304 char ofname [100];
305 #ifdef DEBUG
306 int verbose = 0;
307 #endif /* DEBUG */
308
309 int do_decomp = 0;
310
311
312 int InCnt;
313 unsigned char *InBuff;
314 unsigned char *OutBuff;
315
316 /*****************************************************************
317  * TAG( main )
318  *
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.
321  *
322  * Usage: compress [-dfvc] [-b bits] [file ...]
323  * Inputs:
324  *      -d:         If given, decompression is done instead.
325  *
326  *      -c:         Write output on stdout, don't remove original.
327  *
328  *      -b:         Parameter limits the max number of bits/code.
329  *
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.
334  *
335  *      -v:         Write compression statistics
336  *
337  *      file ...:   Files to be compressed.  If none specified, stdin
338  *                  is used.
339  * Outputs:
340  *      file.Z:     Compressed form of file with same mode, owner, and utimes
341  *      or stdout   (if stdin used as input)
342  *
343  * Assumptions:
344  *      When filenames are given, replaces with the compressed version
345  *      (.Z suffix) only if the file decreases in size.
346  * Algorithm:
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.
351  *
352  *
353  * Changed from main to spec_select_action,
354  *      Jeff Reilly -   1/15/95 SPEC
355  */
356
357 spec_select_action(char* from_buf, int from_count, int action, char* to_buf)
358 {
359     char *rindex();
360 #ifdef SYSV
361     void onintr(), oops();
362 #else
363     int onintr(), oops();
364 #endif  /* SYSV */
365
366
367
368 #ifdef COMPATIBLE
369     nomagic = 1;        /* Original didn't have a magic number */
370 #endif /* COMPATIBLE */
371
372
373     if(maxbits < INIT_BITS) maxbits = INIT_BITS;
374     if (maxbits > BITS) maxbits = BITS;
375     maxmaxcode = 1 << maxbits;
376
377     InCnt = from_count;
378     InBuff = (unsigned char *)from_buf;
379     OutBuff = (unsigned char *)to_buf;
380     do_decomp = action;
381
382         if (do_decomp == 0) {
383                 compress();
384 #ifdef DEBUG
385                 if(verbose)             dump_tab();
386 #endif /* DEBUG */
387         } else {
388             /* Check the magic number */
389             if (nomagic == 0) {
390                 if ((getbyte() != (magic_header[0] & 0xFF))
391                  || (getbyte() != (magic_header[1] & 0xFF))) {
392                     fprintf(stderr, "stdin: not in compressed format\n");
393                     exit(1);
394                 }
395                 maxbits = getbyte();    /* set -b from file */
396                 block_compress = maxbits & BLOCK_MASK;
397                 maxbits &= BIT_MASK;
398                 maxmaxcode = 1 << maxbits;
399                 fsize = 100000;         /* assume stdin large for USERMEM */
400                 if(maxbits > BITS) {
401                         fprintf(stderr,
402                         "stdin: compressed with %d bits, can only handle %d bits\n",
403                         maxbits, BITS);
404                         exit(1);
405                 }
406             }
407 #ifndef DEBUG
408             decompress();
409 #else
410             if (debug == 0)     decompress();
411             else                printcodes();
412             if (verbose)        dump_tab();
413 #endif /* DEBUG */
414         }
415
416     return( OutBuff - (unsigned char *)to_buf );
417 }
418
419 static int offset;
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) */
423
424 /*
425  * compress (Originally: stdin to stdout -- Changed by SPEC to: memory to memory)
426  *
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.
438  */
439
440 compress() {
441     register long fcode;
442     register code_int i = 0;
443     register int c;
444     register code_int ent;
445 #ifdef XENIX_16
446     register code_int disp;
447 #else   /* Normal machine */
448     register int disp;
449 #endif
450     register code_int hsize_reg;
451     register int hshift;
452
453 #ifndef COMPATIBLE
454     if (nomagic == 0) {
455         putbyte(magic_header[0]); putbyte(magic_header[1]);
456         putbyte((char)(maxbits | block_compress));
457     }
458 #endif /* COMPATIBLE */
459
460     offset = 0;
461     bytes_out = 3;              /* includes 3-byte header mojo */
462     out_count = 0;
463     clear_flg = 0;
464     ratio = 0;
465     in_count = 1;
466     checkpoint = CHECK_GAP;
467     maxcode = MAXCODE(n_bits = INIT_BITS);
468     free_ent = ((block_compress) ? FIRST : 256 );
469
470     ent = getbyte ();
471
472     hshift = 0;
473     for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
474         hshift++;
475     hshift = 8 - hshift;                /* set hash code range bound */
476
477     hsize_reg = hsize;
478     cl_hash( (count_int) hsize_reg);            /* clear hash table */
479
480 #ifdef SIGNED_COMPARE_SLOW
481     while ( (c = getbyte()) != (unsigned) EOF ) {
482 #else
483     while ( (c = getbyte()) != EOF ) {
484 #endif
485         in_count++;
486         fcode = (long) (((long) c << maxbits) + ent);
487         i = ((c << hshift) ^ ent);      /* xor hashing */
488
489         if ( htabof (i) == fcode ) {
490             ent = codetabof (i);
491             continue;
492         } else if ( (long)htabof (i) < 0 )      /* empty slot */
493             goto nomatch;
494         disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
495         if ( i == 0 )
496             disp = 1;
497 probe:
498         if ( (i -= disp) < 0 )
499             i += hsize_reg;
500
501         if ( htabof (i) == fcode ) {
502             ent = codetabof (i);
503             continue;
504         }
505         if ( (long)htabof (i) > 0 )
506             goto probe;
507 nomatch:
508         output ( (code_int) ent );
509         out_count++;
510         ent = c;
511 #ifdef SIGNED_COMPARE_SLOW
512         if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
513 #else
514         if ( free_ent < maxmaxcode ) {
515 #endif
516             codetabof (i) = free_ent++; /* code -> hashtable */
517             htabof (i) = fcode;
518         }
519         else if ( (count_int)in_count >= checkpoint && block_compress )
520             cl_block ();
521     }
522     /*
523      * Put out the final code.
524      */
525     output( (code_int)ent );
526     out_count++;
527     output( (code_int)-1 );
528
529     /*
530      * Print out stats on stderr
531      */
532     if(zcat_flg == 0 && !quiet) {
533 #ifdef DEBUG
534         fprintf( stderr,
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 );
544 #else /* !DEBUG */
545         fprintf( stderr, "Compression: " );
546         prratio( stderr, in_count-bytes_out, in_count );
547 #endif /* DEBUG */
548     }
549     if(bytes_out > in_count)    /* exit(2) if no savings */
550         exit_stat = 2;
551     return;
552 }
553
554 /*****************************************************************
555  * TAG( output )
556  *
557  * Output the given code.
558  * Inputs:
559  *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
560  *              that n_bits =< (long)wordsize - 1.
561  * Outputs:
562  *      Outputs code to the file.
563  * Assumptions:
564  *      Chars are 8 bits long.
565  * Algorithm:
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.
569  */
570
571 static char buf[BITS];
572
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};
575
576 output( code )
577 code_int  code;
578 {
579 #ifdef DEBUG
580     static int col = 0;
581 #endif /* DEBUG */
582
583     /*
584      * On the VAX, it is important to have the register declarations
585      * in exactly the order given, or the asm will break.
586      */
587     register int r_off = offset, bits= n_bits;
588     register char * bp = buf;
589
590 #ifdef DEBUG
591         if ( verbose )
592             fprintf( stderr, "%5d%c", code,
593                     (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
594 #endif /* DEBUG */
595     if ( code >= 0 ) {
596 /*
597  * byte/bit numbering on the VAX is simulated by the following code
598  */
599         /*
600          * Get to the first byte.
601          */
602         bp += (r_off >> 3);
603         r_off &= 7;
604         /*
605          * Since code is always >= 8 bits, only need to mask the first
606          * hunk on the left.
607          */
608         *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
609         bp++;
610         bits -= (8 - r_off);
611         code >>= 8 - r_off;
612         /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
613         if ( bits >= 8 ) {
614             *bp++ = code;
615             code >>= 8;
616             bits -= 8;
617         }
618         /* Last bits. */
619         if(bits)
620             *bp = code;
621         offset += n_bits;
622         if ( offset == (n_bits << 3) ) {
623             bp = buf;
624             bits = n_bits;
625             bytes_out += bits;
626             do
627                 putbyte(*bp++);
628             while(--bits);
629             offset = 0;
630         }
631
632         /*
633          * If the next entry is going to be too big for the code size,
634          * then increase it, if possible.
635          */
636         if ( free_ent > maxcode || (clear_flg > 0))
637         {
638             /*
639              * Write the whole buffer, because the input side won't
640              * discover the size increase until after it has read it.
641              */
642             if ( offset > 0 ) {
643                 writebytes( buf, n_bits );
644                 bytes_out += n_bits;
645             }
646             offset = 0;
647
648             if ( clear_flg ) {
649                 maxcode = MAXCODE (n_bits = INIT_BITS);
650                 clear_flg = 0;
651             }
652             else {
653                 n_bits++;
654                 if ( n_bits == maxbits )
655                     maxcode = maxmaxcode;
656                 else
657                     maxcode = MAXCODE(n_bits);
658             }
659 #ifdef DEBUG
660             if ( debug ) {
661                 fprintf( stderr, "\nChange to %d bits\n", n_bits );
662                 col = 0;
663             }
664 #endif /* DEBUG */
665         }
666     } else {
667         /*
668          * At EOF, write the rest of the buffer.
669          */
670         if ( offset > 0 )
671             writebytes( buf, ((offset + 7) / 8) );
672         bytes_out += (offset + 7) / 8;
673         offset = 0;
674 #ifdef DEBUG
675         if ( verbose )
676             fprintf( stderr, "\n" );
677 #endif /* DEBUG */
678     }
679 }
680
681 /*
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.
686  */
687
688 decompress() {
689     register char_type *stackp;
690     register int finchar;
691     register code_int code, oldcode, incode;
692
693     /*
694      * As above, initialize the first 256 entries in the table.
695      */
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;
700     }
701     free_ent = ((block_compress) ? FIRST : 256 );
702
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 */
707     stackp = de_stack;
708
709     while ( (code = getcode()) > -1 ) {
710
711         if ( (code == CLEAR) && block_compress ) {
712             for ( code = 255; code >= 0; code-- )
713                 tab_prefixof(code) = 0;
714             clear_flg = 1;
715             free_ent = FIRST - 1;
716             if ( (code = getcode ()) == -1 )    /* O, untimely death! */
717                 break;
718         }
719         incode = code;
720         /*
721          * Special case for KwKwK string.
722          */
723         if ( code >= free_ent ) {
724             *stackp++ = finchar;
725             code = oldcode;
726         }
727
728         /*
729          * Generate output characters in reverse order
730          */
731 #ifdef SIGNED_COMPARE_SLOW
732         while ( ((unsigned long)code) >= ((unsigned long)256) ) {
733 #else
734         while ( code >= 256 ) {
735 #endif
736             *stackp++ = tab_suffixof(code);
737             code = tab_prefixof(code);
738         }
739         *stackp++ = finchar = tab_suffixof(code);
740
741         /*
742          * And put them out in forward order
743          */
744         do
745             putbyte ( *--stackp );
746         while ( stackp > de_stack );
747
748         /*
749          * Generate the new entry.
750          */
751         if ( (code=free_ent) < maxmaxcode ) {
752             tab_prefixof(code) = (unsigned short)oldcode;
753             tab_suffixof(code) = finchar;
754             free_ent = code+1;
755         }
756         /*
757          * Remember previous code.
758          */
759         oldcode = incode;
760     }
761 }
762
763 /*****************************************************************
764  * TAG( getcode )
765  *
766  * Read one code from the standard input.  If EOF, return -1.
767  * Inputs:
768  *      stdin
769  * Outputs:
770  *      code or -1 is returned.
771  */
772
773 code_int
774 getcode() {
775     /*
776      * On the VAX, it is important to have the register declarations
777      * in exactly the order given, or the asm will break.
778      */
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;
784
785     if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
786         /*
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.
790          */
791         if ( free_ent > maxcode ) {
792             n_bits++;
793             if ( n_bits == maxbits )
794                 maxcode = maxmaxcode;   /* won't get any bigger now */
795             else
796                 maxcode = MAXCODE(n_bits);
797         }
798         if ( clear_flg > 0) {
799             maxcode = MAXCODE (n_bits = INIT_BITS);
800             clear_flg = 0;
801         }
802         size = readbytes( buf, n_bits );
803         if ( size <= 0 )
804             return -1;                  /* end of file */
805         offset = 0;
806         /* Round size down to integral number of codes */
807         size = (size << 3) - (n_bits - 1);
808     }
809     r_off = offset;
810     bits = n_bits;
811         /*
812          * Get to the first byte.
813          */
814         bp += (r_off >> 3);
815         r_off &= 7;
816         /* Get first part (low order bits) */
817 #ifdef NO_UCHAR
818         code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
819 #else
820         code = (*bp++ >> r_off);
821 #endif /* NO_UCHAR */
822         bits -= (8 - r_off);
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). */
825         if ( bits >= 8 ) {
826 #ifdef NO_UCHAR
827             code |= (*bp++ & 0xff) << r_off;
828 #else
829             code |= *bp++ << r_off;
830 #endif /* NO_UCHAR */
831             r_off += 8;
832             bits -= 8;
833         }
834         /* high order bits. */
835         code |= (*bp & rmask[bits]) << r_off;
836     offset += n_bits;
837
838     return code;
839 }
840
841 char *
842 rindex(s, c)            /* For those who don't have it in libc.a */
843 register char *s, c;
844 {
845         char *p;
846         for (p = NULL; *s; s++)
847             if (*s == c)
848                 p = s;
849         return(p);
850 }
851
852 #ifdef DEBUG
853 printcodes()
854 {
855     /*
856      * Just print out codes from input file.  For debugging.
857      */
858     code_int code;
859     int col = 0, bits;
860
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;
867             clear_flg = 1;
868         }
869         else if ( free_ent < maxmaxcode )
870             free_ent++;
871         if ( bits != n_bits ) {
872             fprintf(stderr, "\nChange to %d bits\n", n_bits );
873             bits = n_bits;
874             col = 0;
875         }
876         fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
877     }
878     putc( '\n', stderr );
879     exit( 0 );
880 }
881
882 code_int sorttab[1<<BITS];      /* sorted pointers into htab */
883
884 dump_tab()      /* dump string table */
885 {
886     register int i, first;
887     register ent;
888 #define STACK_SIZE      15000
889     int stack_top = STACK_SIZE;
890     register c;
891
892     if(do_decomp == 0) {        /* compressing */
893         register int flag = 1;
894
895         for(i=0; i<hsize; i++) {        /* build sort pointers */
896                 if((long)htabof(i) >= 0) {
897                         sorttab[codetabof(i)] = i;
898                 }
899         }
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,
906                                      stack_top);
907                 for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
908                     ent > 256;
909                     ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
910                         stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
911                                                 stack_top);
912                 }
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;
916         }
917    } else if(!debug) {  /* decompressing */
918
919        for ( i = 0; i < free_ent; i++ ) {
920            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 );
925            else
926                fprintf( stderr, "%5d: %5d/\\%03o \"",
927                            ent, tab_prefixof(ent), c );
928            de_stack[--stack_top] = '\n';
929            de_stack[--stack_top] = '"';
930            for ( ; ent != NULL;
931                    ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
932                stack_top = in_stack(tab_suffixof(ent), stack_top);
933            }
934            fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
935            stack_top = STACK_SIZE;
936        }
937     }
938 }
939
940 int
941 in_stack(c, stack_top)
942         register c, stack_top;
943 {
944         if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
945             de_stack[--stack_top] = c;
946         } else {
947             switch( 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;
954             default:
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;
958                 break;
959             }
960             de_stack[--stack_top] = '\\';
961         }
962         return stack_top;
963 }
964 #endif /* DEBUG */
965
966
967 #ifdef SYSV
968 void
969 #endif  /* SYSV */
970 onintr ( )
971 {
972     unlink ( ofname );
973     exit ( 1 );
974 }
975
976 #ifdef SYSV
977 void
978 #endif  /* SYSV */
979 oops ( )        /* wild pointer -- assume bad input */
980 {
981     if ( do_decomp == 1 )
982         fprintf ( stderr, "uncompress: corrupt input\n" );
983     unlink ( ofname );
984     exit ( 1 );
985 }
986
987 cl_block ()             /* table clear for block compress */
988 {
989     register long int rat;
990
991     checkpoint = in_count + CHECK_GAP;
992 #ifdef DEBUG
993         if ( debug ) {
994                 fprintf ( stderr, "count: %ld, ratio: ", in_count );
995                 prratio ( stderr, in_count, bytes_out );
996                 fprintf ( stderr, "\n");
997         }
998 #endif /* DEBUG */
999
1000     if(in_count > 0x007fffff) { /* shift will overflow */
1001         rat = bytes_out >> 8;
1002         if(rat == 0) {          /* Don't divide by zero */
1003             rat = 0x7fffffff;
1004         } else {
1005             rat = in_count / rat;
1006         }
1007     } else {
1008         rat = (in_count << 8) / bytes_out;      /* 8 fractional bits */
1009     }
1010     if ( rat > ratio ) {
1011         ratio = rat;
1012     } else {
1013         ratio = 0;
1014 #ifdef DEBUG
1015         if(verbose)
1016                 dump_tab();     /* dump string table */
1017 #endif
1018         cl_hash ( (count_int) hsize );
1019         free_ent = FIRST;
1020         clear_flg = 1;
1021         output ( (code_int) CLEAR );
1022 #ifdef DEBUG
1023         if(debug)
1024                 fprintf ( stderr, "clear\n" );
1025 #endif /* DEBUG */
1026     }
1027 }
1028
1029 cl_hash(hsize)          /* reset code table */
1030         register count_int hsize;
1031 {
1032 #ifndef XENIX_16        /* Normal machine */
1033         register count_int *htab_p = htab+hsize;
1034 #else
1035         register j;
1036         register long k = hsize;
1037         register count_int *htab_p;
1038 #endif
1039         register long i;
1040         register long m1 = -1;
1041
1042 #ifdef XENIX_16
1043     for(j=0; j<=8 && k>=0; j++,k-=8192) {
1044         i = 8192;
1045         if(k < 8192) {
1046                 i = k;
1047         }
1048         htab_p = &(htab[j][i]);
1049         i -= 16;
1050         if(i > 0) {
1051 #else
1052         i = hsize - 16;
1053 #endif
1054         do {                            /* might use Sys V memset(3) here */
1055                 *(htab_p-16) = m1;
1056                 *(htab_p-15) = m1;
1057                 *(htab_p-14) = m1;
1058                 *(htab_p-13) = m1;
1059                 *(htab_p-12) = m1;
1060                 *(htab_p-11) = m1;
1061                 *(htab_p-10) = m1;
1062                 *(htab_p-9) = m1;
1063                 *(htab_p-8) = m1;
1064                 *(htab_p-7) = m1;
1065                 *(htab_p-6) = m1;
1066                 *(htab_p-5) = m1;
1067                 *(htab_p-4) = m1;
1068                 *(htab_p-3) = m1;
1069                 *(htab_p-2) = m1;
1070                 *(htab_p-1) = m1;
1071                 htab_p -= 16;
1072         } while ((i -= 16) >= 0);
1073 #ifdef XENIX_16
1074         }
1075     }
1076 #endif
1077         for ( i += 16; i > 0; i-- )
1078                 *--htab_p = m1;
1079 }
1080
1081 prratio(stream, num, den)
1082 FILE *stream;
1083 long int num, den;
1084 {
1085         register int q;                 /* Doesn't need to be long */
1086
1087         if(num > 214748L) {             /* 2147483647/10000 */
1088                 q = num / (den / 10000L);
1089         } else {
1090                 q = 10000L * num / den;         /* Long calculations, though */
1091         }
1092         if (q < 0) {
1093                 putc('-', stream);
1094                 q = -q;
1095         }
1096         fprintf(stream, "%d.%02d%%", q / 100, q % 100);
1097 }
1098
1099 version()
1100 {
1101         fprintf(stderr, "%s\n", rcs_ident);
1102         fprintf(stderr, "Options: ");
1103 #ifdef NO_UCHAR
1104         fprintf(stderr, "NO_UCHAR, ");
1105 #endif
1106 #ifdef SIGNED_COMPARE_SLOW
1107         fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
1108 #endif
1109 #ifdef XENIX_16
1110         fprintf(stderr, "XENIX_16, ");
1111 #endif
1112 #ifdef COMPATIBLE
1113         fprintf(stderr, "COMPATIBLE, ");
1114 #endif
1115 #ifdef DEBUG
1116         fprintf(stderr, "DEBUG, ");
1117 #endif
1118 #ifdef BSD4_2
1119         fprintf(stderr, "BSD4_2, ");
1120 #endif
1121         fprintf(stderr, "BITS = %d\n", BITS);
1122 }
1123
1124
1125
1126
1127 /* SPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPECSPEC */
1128 getbyte()
1129 {
1130         if( InCnt > 0 ) {
1131                 InCnt--;
1132                 return( (unsigned int)*InBuff++ );
1133         } else {
1134                 return( -1 );
1135         }
1136 }
1137
1138 putbyte( c )
1139 char c;
1140 {
1141         *OutBuff++ = c;
1142 }
1143
1144 readbytes( buf, n )
1145 char *buf;
1146 int n;
1147 {
1148         int i;
1149
1150         if( InCnt <= 0 )
1151                 return( -1 );
1152
1153         if( n > InCnt )
1154                 n = InCnt;
1155
1156         for( i=0; i<n; i++ ) {
1157                 buf[i] = *InBuff++;
1158                 InCnt--;
1159         }
1160
1161         return( i );
1162 }
1163
1164 writebytes( buf, n )
1165 char *buf;
1166 int n;
1167 {
1168         int i;
1169
1170         for( i=0; i<n; i++ )
1171                 *OutBuff++ = buf[i];
1172 }
1173
1174 int main()
1175 {
1176     return 0;
1177 }