1 /* compress - Reduce file size using Modified Lempel-Ziv encoding */
4 * compress.c - File compression ala IEEE Computer, June 1984.
6 * Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
7 * Jim McKie (decvax!mcvax!jim)
8 * Steve Davies (decvax!vax135!petsd!peora!srd)
9 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
10 * James A. Woods (decvax!ihnp4!ames!jaw)
11 * Joe Orost (decvax!vax135!petsd!joe)
13 * Richard Todd Port to MINIX
14 * Andy Tanenbaum Cleanup
17 * Algorithm from "A Technique for High Performance Data Compression",
18 * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
20 * Usage: compress [-dfvc] [-b bits] [file ...]
22 * -d: If given, decompression is done instead.
24 * -c: Write output on stdout.
26 * -b: Parameter limits the max number of bits/code.
28 * -f: Forces output file to be generated, even if one already
29 * exists, and even if no space is saved by compressing.
30 * If -f is not used, the user will be prompted if stdin is
31 * a tty, otherwise, the output file will not be overwritten.
33 * -v: Write compression statistics
35 * file ...: Files to be compressed. If none specified, stdin
38 * file.Z: Compressed form of file with same mode, owner, and utimes
39 * or stdout (if stdin used as input)
42 * When filenames are given, replaces with the compressed version
43 * (.Z suffix) only if the file decreases in size.
45 * Modified Lempel-Ziv method (LZW). Basically finds common
46 * substrings and replaces them with a variable size code. This is
47 * deterministic, and can be done on the fly. Thus, the decompression
48 * procedure needs no input table, but tracks the way the table was built.
54 #define min(a,b) ((a>b) ? b : a)
57 * Set USERMEM to the maximum amount of physical user memory available
58 * in bytes. USERMEM is used to determine the maximum BITS that can be used
61 * SACREDMEM is the amount of physical memory saved for others; compress
69 # define USERMEM 450000 /* default user memory */
72 #define REGISTER register
78 /* The default for Minix is -b13, but we can do -b16 if the machine can. */
79 #define DEFAULTBITS 13
87 # if USERMEM >= (433484+SACREDMEM)
90 # if USERMEM >= (229600+SACREDMEM)
93 # if USERMEM >= (127536+SACREDMEM)
96 # if USERMEM >= (73464+SACREDMEM)
107 #ifdef PBITS /* Preferred BITS for this memory size */
114 # define HSIZE 69001 /* 95% occupancy */
117 # define HSIZE 35023 /* 94% occupancy */
120 # define HSIZE 18013 /* 91% occupancy */
123 # define HSIZE 9001 /* 91% occupancy */
126 # define HSIZE 5003 /* 80% occupancy */
131 * a code_int must be able to hold 2**BITS values of type int, and also -1
134 typedef long int code_int
;
136 typedef int code_int
;
139 #ifdef SIGNED_COMPARE_SLOW
140 typedef unsigned long int count_int
;
141 typedef unsigned short int count_short
;
143 typedef long int count_int
;
147 typedef char char_type
;
149 typedef unsigned char char_type
;
151 char_type magic_header
[] = "\037\235"; /* 1F 9D */
153 /* Defines for third byte of header */
154 #define BIT_MASK 0x1f
155 #define BLOCK_MASK 0x80
156 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
157 a fourth header byte (for expansion).
159 #define INIT_BITS 9 /* initial number of bits/code */
161 #include <sys/types.h>
162 #include <sys/stat.h>
172 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
174 int n_bits
; /* number of bits/code */
175 int maxbits
= DEFAULTBITS
; /* user settable max # bits/code */
176 code_int maxcode
; /* maximum code, given n_bits */
177 code_int maxmaxcode
= 1 << BITS
; /* should NEVER generate this code */
178 #ifdef COMPATIBLE /* But wrong! */
179 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
181 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
182 #endif /* COMPATIBLE */
185 count_int htab
[HSIZE
];
186 unsigned short codetab
[HSIZE
];
189 unsigned short *codetab
;
190 # define HTABSIZE ((size_t)(HSIZE*sizeof(count_int)))
191 # define CODETABSIZE ((size_t)(HSIZE*sizeof(unsigned short)))
194 #define htabof(i) htab[i]
195 #define codetabof(i) codetab[i]
196 #endif /* XENIX_16 */
197 code_int hsize
= HSIZE
; /* for dynamic table sizing */
201 * To save much memory, we overlay the table used by compress() with those
202 * used by decompress(). The tab_prefix table is the same size and type
203 * as the codetab. The tab_suffix table needs 2**BITS characters. We
204 * get this from the beginning of htab. The output stack uses the rest
205 * of htab, and contains characters. There is plenty of room for any
206 * possible stack (stack used to be 8000 characters).
209 #define tab_prefixof(i) codetabof(i)
211 # define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
212 # define de_stack ((char_type *)(htab2))
213 #else /* Normal machine */
214 # define tab_suffixof(i) ((char_type *)(htab))[i]
215 # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
216 #endif /* XENIX_16 */
218 code_int free_ent
= 0; /* first unused entry */
221 _PROTOTYPE(int main
, (int argc
, char **argv
));
222 _PROTOTYPE(void Usage
, (void));
223 _PROTOTYPE(void compress
, (void));
224 _PROTOTYPE(void onintr
, (int dummy
));
225 _PROTOTYPE(void oops
, (int dummy
));
226 _PROTOTYPE(void output
, (code_int code
));
227 _PROTOTYPE(int foreground
, (void));
228 _PROTOTYPE(void decompress
, (void));
229 _PROTOTYPE(code_int getcode
, (void));
230 _PROTOTYPE(void writeerr
, (void));
231 _PROTOTYPE(void copystat
, (char *ifname
, char *ofname
));
232 _PROTOTYPE(int foreground
, (void));
233 _PROTOTYPE(void cl_block
, (void));
234 _PROTOTYPE(void cl_hash
, (count_int hsize
));
235 _PROTOTYPE(void prratio
, (FILE *stream
, long int num
, long int den
));
236 _PROTOTYPE(void version
, (void));
240 fprintf(stderr
,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
244 fprintf(stderr
,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n");
247 int nomagic
= 0; /* Use a 3-byte magic number header, unless old file */
248 int zcat_flg
= 0; /* Write output on stdout, suppress messages */
249 int quiet
= 0; /* don't tell me about compression */
252 * block compression parameters -- after all codes are used up,
253 * and compression rate changes, start over.
255 int block_compress
= BLOCK_MASK
;
258 #define CHECK_GAP 10000 /* ratio check interval */
259 count_int checkpoint
= CHECK_GAP
;
261 * the next two codes should not be changed lightly, as they must not
262 * lie within the contiguous general code space.
264 #define FIRST 257 /* first free entry */
265 #define CLEAR 256 /* table clear output code */
293 int overwrite
= 0; /* Do not overwrite unless given -f flag */
295 char **filelist
, **fileptr
;
299 if ( (bgnd_flag
= signal ( SIGINT
, SIG_IGN
)) != SIG_IGN
) {
300 signal ( SIGINT
, onintr
);
301 signal ( SIGSEGV
, oops
);
306 _setmode(NULL
,_ALL_FILES_BINARY
);
307 _setmode(stdin
,_BINARY
);
308 _setmode(stdout
,_BINARY
);
309 _setmode(stderr
,_TEXT
);
311 if (NULL
== (htab
= (count_int
*)malloc(HTABSIZE
)))
313 fprintf(stderr
,"Can't allocate htab\n");
316 if (NULL
== (codetab
= (unsigned short *)malloc(CODETABSIZE
)))
318 fprintf(stderr
,"Can't allocate codetab\n");
323 nomagic
= 1; /* Original didn't have a magic number */
324 #endif /* COMPATIBLE */
326 filelist
= fileptr
= (char **)(malloc((size_t)(argc
* sizeof(*argv
))));
329 if((cp
= strrchr(argv
[0], '/')) != 0) {
334 if(strcmp(cp
, "uncompress") == 0) {
336 } else if(strcmp(cp
, "zcat") == 0) {
342 /* 4.2BSD dependent - take it out if not */
343 setlinebuf( stderr
);
346 /* Argument Processing
347 * All flags are optional.
349 * -V => print Version; debug verbose
352 * -f => force overwrite of output file
353 * -n => no header: useful to uncompress old files
354 * -b maxbits => maxbits. If -b is specified, then maxbits MUST be
356 * -c => cat all output to stdout
357 * -C => generate output compatible with compress 2.0.
358 * if a string is left, must be an input filename.
360 for (argc
--, argv
++; argc
> 0; argc
--, argv
++)
363 { /* A flag argument */
365 { /* Process all flags in this arg */
401 fprintf(stderr
, "Missing maxbits\n");
405 maxbits
= atoi(*argv
);
414 fprintf(stderr
, "Unknown flag: '%c'; ", **argv
);
421 { /* Input file name */
422 *fileptr
++ = *argv
; /* Build input file list */
424 /* process nextarg; */
429 if(maxbits
< INIT_BITS
) maxbits
= INIT_BITS
;
430 if (maxbits
> BITS
) maxbits
= BITS
;
431 maxmaxcode
= 1 << maxbits
;
433 if (*filelist
!= NULL
)
435 for (fileptr
= filelist
; *fileptr
; fileptr
++)
439 { /* DECOMPRESSION */
440 /* Check for .Z suffix */
442 if (strcmp(*fileptr
+ strlen(*fileptr
) - 2, DOTZ
) != 0)
444 if (strcmp(*fileptr
+ strlen(*fileptr
) - 1, DOTZ
) != 0)
447 /* No .Z: tack one on */
448 strcpy(tempname
, *fileptr
);
450 strcat(tempname
, DOTZ
);
452 /* either tack one on or replace last character */
455 if (NULL
== (dot
= strchr(tempname
,'.')))
457 strcat(tempname
,".Z");
460 /* if there is a dot then either tack a z on
461 or replace last character */
464 strcat(tempname
,DOTZ
);
472 /* Open input file */
473 if ((freopen(*fileptr
, "r", stdin
)) == NULL
)
475 perror(*fileptr
); continue;
477 /* Check the magic number */
480 unsigned magic1
, magic2
;
481 if (((magic1
= getc(stdin
)) != (magic_header
[0] & 0xFF))
482 || ((magic2
= getc(stdin
)) != (magic_header
[1] & 0xFF)))
485 "%s: not in compressed format %x %x\n",
486 *fileptr
,magic1
,magic2
);
489 maxbits
= getc(stdin
); /* set -b from file */
490 block_compress
= maxbits
& BLOCK_MASK
;
492 maxmaxcode
= 1 << maxbits
;
496 "%s: compressed with %d bits, can only handle %d bits\n",
497 *fileptr
, maxbits
, BITS
);
501 /* Generate output filename */
502 strcpy(ofname
, *fileptr
);
504 ofname
[strlen(*fileptr
) - 2] = '\0'; /* Strip off .Z */
506 /* kludge to handle various common three character extension */
510 /* first off, map name to upper case */
511 for (dot
= ofname
; *dot
; dot
++)
512 *dot
= toupper(*dot
);
513 if (NULL
== (dot
= strchr(ofname
,'.')))
515 fprintf(stderr
,"Bad filename %s\n",ofname
);
518 if (strlen(dot
) == 4)
519 /* we got three letter extensions */
521 if (strcmp(dot
,".EXZ") == 0)
523 else if (strcmp(dot
,".COZ") == 0)
525 else if (strcmp(dot
,".BAZ") == 0)
527 else if (strcmp(dot
,".OBZ") == 0)
529 else if (strcmp(dot
,".SYZ") == 0)
531 else if (strcmp(dot
,".DOZ") == 0)
536 ofname
[strlen(*fileptr
) - 1] = fixup
;
541 if (strcmp(*fileptr
+ strlen(*fileptr
) - 2, DOTZ
) == 0)
543 fprintf(stderr
, "%s: already has .Z suffix -- no change\n",
547 /* Open input file */
548 if ((freopen(*fileptr
, "r", stdin
)) == NULL
)
550 perror(*fileptr
); continue;
552 (void)stat( *fileptr
, &statbuf
);
553 fsize
= (long) statbuf
.st_size
;
555 * tune hash table size for small files -- ad hoc,
556 * but the sizes match earlier #defines, which
557 * serve as upper bounds on the number of output codes.
559 hsize
= HSIZE
; /*lint -e506 -e712 */
560 if ( fsize
< (1 << 12) )
561 hsize
= min ( 5003, HSIZE
);
562 else if ( fsize
< (1 << 13) )
563 hsize
= min ( 9001, HSIZE
);
564 else if ( fsize
< (1 << 14) )
565 hsize
= min ( 18013, HSIZE
);
566 else if ( fsize
< (1 << 15) )
567 hsize
= min ( 35023, HSIZE
);
568 else if ( fsize
< 47000 )
569 hsize
= min ( 50021, HSIZE
); /*lint +e506 +e712 */
571 /* Generate output filename */
572 strcpy(ofname
, *fileptr
);
573 #ifndef BSD4_2 /* Short filenames */
574 if ((cp
=strrchr(ofname
,'/')) != NULL
)
578 if (strlen(cp
) >= _DIRENT_NAME_LEN
-3)
580 fprintf(stderr
,"%s: filename too long to tack on .Z\n",cp
);
586 /* either tack one on or replace last character */
588 if (NULL
== (dot
= strchr(cp
,'.')))
593 /* if there is a dot then either tack a z on
594 or replace last character */
603 #endif /* BSD4_2 Long filenames allowed */
605 /* PCDOS takes care of this above */
606 strcat(ofname
, DOTZ
);
609 /* Check for overwrite of existing file */
610 if (overwrite
== 0 && zcat_flg
== 0)
612 if (stat(ofname
, &statbuf
) == 0)
614 char response
[2]; int fd
;
616 fprintf(stderr
, "%s already exists;", ofname
);
619 fd
= open("/dev/tty", O_RDONLY
);
621 " do you wish to overwrite %s (y or n)? ", ofname
);
623 (void)read(fd
, response
, 2);
624 while (response
[1] != '\n')
626 if (read(fd
, response
+1, 1) < 0)
634 if (response
[0] != 'y')
636 fprintf(stderr
, "\tnot overwritten\n");
642 { /* Open output file */
643 if (freopen(ofname
, "w", stdout
) == NULL
)
649 fprintf(stderr
, "%s: ", *fileptr
);
652 /* Actually do the compression/decompression */
668 copystat(*fileptr
, ofname
); /* Copy stats */
669 if((exit_stat
== 1) || (!quiet
))
674 { /* Standard input */
679 if(verbose
) dump_tab();
685 /* Check the magic number */
688 if ((getc(stdin
)!=(magic_header
[0] & 0xFF))
689 || (getc(stdin
)!=(magic_header
[1] & 0xFF)))
691 fprintf(stderr
, "stdin: not in compressed format\n");
694 maxbits
= getc(stdin
); /* set -b from file */
695 block_compress
= maxbits
& BLOCK_MASK
;
697 maxmaxcode
= 1 << maxbits
;
698 fsize
= 100000; /* assume stdin large for USERMEM */
702 "stdin: compressed with %d bits, can only handle %d bits\n",
710 if (debug
== 0) decompress();
712 if (verbose
) dump_tab();
720 long int in_count
= 1; /* length of input */
721 long int bytes_out
; /* length of compressed output */
722 long int out_count
= 0; /* # of codes output (for debugging) */
725 * compress stdin to stdout
727 * Algorithm: use open addressing double hashing (no chaining) on the
728 * prefix code / next character combination. We do a variant of Knuth's
729 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
730 * secondary probe. Here, the modular division first probe is gives way
731 * to a faster exclusive-or manipulation. Also do block compression with
732 * an adaptive reset, whereby the code table is cleared when the compression
733 * ratio decreases, but after the table fills. The variable-length output
734 * codes are re-sized at this point, and a special CLEAR code is generated
735 * for the decompressor. Late addition: construct the table according to
736 * file size for noticeable speed improvement on small files. Please direct
737 * questions about this implementation to ames!jaw.
743 REGISTER code_int i
= 0;
745 REGISTER code_int ent
;
747 REGISTER code_int disp
;
748 #else /* Normal machine */
751 REGISTER code_int hsize_reg
;
757 putc(magic_header
[0],stdout
);
758 putc(magic_header
[1],stdout
);
759 putc((char)(maxbits
| block_compress
),stdout
);
763 #endif /* COMPATIBLE */
766 bytes_out
= 3; /* includes 3-byte header mojo */
771 checkpoint
= CHECK_GAP
;
772 maxcode
= MAXCODE(n_bits
= INIT_BITS
);
773 free_ent
= ((block_compress
) ? FIRST
: 256 );
778 for ( fcode
= (long) hsize
; fcode
< 65536L; fcode
*= 2L )
780 hshift
= 8 - hshift
; /* set hash code range bound */
783 cl_hash( (count_int
) hsize_reg
); /* clear hash table */
785 #ifdef SIGNED_COMPARE_SLOW
786 while ( (c
= getc(stdin
)) != (unsigned) EOF
)
788 while ( (c
= getc(stdin
)) != EOF
)
792 fcode
= (long) (((long) c
<< maxbits
) + ent
);
793 i
= ((c
<< hshift
) ^ ent
); /* xor hashing */
795 if ( htabof (i
) == fcode
)
799 } else if ( (long)htabof (i
) < 0 ) /* empty slot */
801 disp
= hsize_reg
- i
; /* secondary hash (after G. Knott) */
805 if ( (i
-= disp
) < 0 )
808 if ( htabof (i
) == fcode
)
813 if ( (long)htabof (i
) > 0 )
816 output ( (code_int
) ent
);
819 #ifdef SIGNED_COMPARE_SLOW
820 if ( (unsigned) free_ent
< (unsigned) maxmaxcode
)
822 if ( free_ent
< maxmaxcode
)
825 codetabof (i
) = free_ent
++; /* code -> hashtable */
828 else if ( (count_int
)in_count
>= checkpoint
&& block_compress
)
832 * Put out the final code.
834 output( (code_int
)ent
);
836 output( (code_int
)-1 );
839 * Print out stats on stderr
841 if(zcat_flg
== 0 && !quiet
)
845 "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
846 in_count
, out_count
, bytes_out
);
847 prratio( stderr
, in_count
, bytes_out
);
848 fprintf( stderr
, "\n");
849 fprintf( stderr
, "\tCompression as in compact: " );
850 prratio( stderr
, in_count
-bytes_out
, in_count
);
851 fprintf( stderr
, "\n");
852 fprintf( stderr
, "\tLargest code (of last block) was %d (%d bits)\n",
853 free_ent
- 1, n_bits
);
855 fprintf( stderr
, "Compression: " );
856 prratio( stderr
, in_count
-bytes_out
, in_count
);
859 if(bytes_out
> in_count
) /* exit(2) if no savings */
864 /*****************************************************************
867 * Output the given code.
869 * code: A n_bits-bit integer. If == -1, then EOF. This assumes
870 * that n_bits =< (long)wordsize - 1.
872 * Outputs code to the file.
874 * Chars are 8 bits long.
876 * Maintain a BITS character long buffer (so that 8 codes will
877 * fit in it exactly). Use the VAX insv instruction to insert each
878 * code in turn. When the buffer fills up empty it and start over.
881 static char buf
[BITS
];
884 char_type lmask
[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
885 char_type rmask
[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
895 * On the VAX, it is important to have the REGISTER declarations
896 * in exactly the order given, or the asm will break.
898 REGISTER
int r_off
= offset
, bits
= n_bits
;
899 REGISTER
char * bp
= buf
;
907 fprintf( stderr
, "%5d%c", code
,
908 (col
+=6) >= 74 ? (col
= 0, '\n') : ' ' );
913 /* VAX DEPENDENT!! Implementation on other machines is below.
915 * Translation: Insert BITS bits from the argument starting at
916 * offset bits from the beginning of buf.
918 0; /* Work around for pcc -O bug with asm and if stmt */
919 asm( "insv 4(ap),r11,r10,(r9)" );
920 #else /* not a vax */
922 * byte/bit numbering on the VAX is simulated by the following code
925 * Get to the first byte.
930 * Since code is always >= 8 bits, only need to mask the first
936 temp
= (code
<< r_off
) & lmask
[r_off
];
939 *bp
= (*bp
& rmask
[r_off
]) | ((code
<< r_off
) & lmask
[r_off
]);
942 *bp
= (*bp
& rmask
[r_off
]) | ((code
<< r_off
) & lmask
[r_off
]);
946 code
>>= (8 - r_off
);
947 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
959 if ( offset
== (n_bits
<< 3) )
971 * If the next entry is going to be too big for the code size,
972 * then increase it, if possible.
974 if ( free_ent
> maxcode
|| (clear_flg
> 0))
977 * Write the whole buffer, because the input side won't
978 * discover the size increase until after it has read it.
982 if( fwrite( buf
, (size_t)1, (size_t)n_bits
, stdout
) != n_bits
)
990 maxcode
= MAXCODE (n_bits
= INIT_BITS
);
996 if ( n_bits
== maxbits
)
997 maxcode
= maxmaxcode
;
999 maxcode
= MAXCODE(n_bits
);
1004 fprintf( stderr
, "\nChange to %d bits\n", n_bits
);
1012 * At EOF, write the rest of the buffer.
1015 fwrite( buf
, (size_t)1, (size_t)(offset
+ 7) / 8, stdout
);
1016 bytes_out
+= (offset
+ 7) / 8;
1021 fprintf( stderr
, "\n" );
1023 if( ferror( stdout
) )
1028 * Decompress stdin to stdout. This routine adapts to the codes in the
1029 * file building the "string" table on-the-fly; requiring no table to
1030 * be stored in the compressed file. The tables used herein are shared
1031 * with those of the compress() routine. See the definitions above.
1035 REGISTER char_type
*stackp
;
1036 REGISTER
int finchar
;
1037 REGISTER code_int code
, oldcode
, incode
;
1040 * As above, initialize the first 256 entries in the table.
1042 maxcode
= MAXCODE(n_bits
= INIT_BITS
);
1043 for ( code
= 255; code
>= 0; code
-- ) {
1044 tab_prefixof(code
) = 0;
1045 tab_suffixof(code
) = (char_type
)code
;
1047 free_ent
= ((block_compress
) ? FIRST
: 256 );
1049 finchar
= oldcode
= getcode();
1050 if(oldcode
== -1) /* EOF already? */
1051 return; /* Get out of here */
1052 putc( (char)finchar
,stdout
); /* first code must be 8 bits = char */
1053 if(ferror(stdout
)) /* Crash if can't write */
1057 while ( (code
= getcode()) > -1 ) {
1059 if ( (code
== CLEAR
) && block_compress
) {
1060 for ( code
= 255; code
>= 0; code
-- )
1061 tab_prefixof(code
) = 0;
1063 free_ent
= FIRST
- 1;
1064 if ( (code
= getcode ()) == -1 ) /* O, untimely death! */
1069 * Special case for KwKwK string.
1071 if ( code
>= free_ent
) {
1072 *stackp
++ = finchar
;
1077 * Generate output characters in reverse order
1079 #ifdef SIGNED_COMPARE_SLOW
1080 while ( ((unsigned long)code
) >= ((unsigned long)256) ) {
1082 while ( code
>= 256 ) {
1084 *stackp
++ = tab_suffixof(code
);
1085 code
= tab_prefixof(code
);
1087 *stackp
++ = finchar
= tab_suffixof(code
);
1090 * And put them out in forward order
1093 putc ( *--stackp
,stdout
);
1094 while ( stackp
> de_stack
);
1097 * Generate the new entry.
1099 if ( (code
=free_ent
) < maxmaxcode
)
1101 tab_prefixof(code
) = (unsigned short)oldcode
;
1102 tab_suffixof(code
) = finchar
;
1106 * Remember previous code.
1115 /*****************************************************************
1118 * Read one code from the standard input. If EOF, return -1.
1122 * code or -1 is returned.
1129 * On the VAX, it is important to have the REGISTER declarations
1130 * in exactly the order given, or the asm will break.
1132 REGISTER code_int code
;
1133 static int offset
= 0, size
= 0;
1134 static char_type buf
[BITS
];
1135 REGISTER
int r_off
, bits
;
1136 REGISTER char_type
*bp
= buf
;
1138 if ( clear_flg
> 0 || offset
>= size
|| free_ent
> maxcode
)
1141 * If the next entry will be too big for the current code
1142 * size, then we must increase the size. This implies reading
1143 * a new buffer full, too.
1145 if ( free_ent
> maxcode
)
1148 if ( n_bits
== maxbits
)
1149 maxcode
= maxmaxcode
; /* won't get any bigger now */
1151 maxcode
= MAXCODE(n_bits
);
1155 maxcode
= MAXCODE (n_bits
= INIT_BITS
);
1158 size
= fread( buf
, (size_t)1, (size_t)n_bits
, stdin
);
1160 return -1; /* end of file */
1162 /* Round size down to integral number of codes */
1163 size
= (size
<< 3) - (n_bits
- 1);
1168 asm( "extzv r10,r9,(r8),r11" );
1169 #else /* not a vax */
1171 * Get to the first byte.
1175 /* Get first part (low order bits) */
1177 code
= ((*bp
++ >> r_off
) & rmask
[8 - r_off
]) & 0xff;
1179 code
= (*bp
++ >> r_off
);
1180 #endif /* NO_UCHAR */
1181 bits
-= (8 - r_off
);
1182 r_off
= 8 - r_off
; /* now, offset into code word */
1183 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
1187 code
|= (*bp
++ & 0xff) << r_off
;
1189 code
|= *bp
++ << r_off
;
1190 #endif /* NO_UCHAR */
1194 /* high order bits. */
1195 code
|= (*bp
& rmask
[bits
]) << r_off
;
1204 strrchr(s
, c
) /* For those who don't have it in libc.a */
1205 REGISTER
char *s
, c
;
1208 for (p
= NULL
; *s
; s
++)
1221 * Just print out codes from input file. For debugging.
1226 bits
= n_bits
= INIT_BITS
;
1227 maxcode
= MAXCODE(n_bits
);
1228 free_ent
= ((block_compress
) ? FIRST
: 256 );
1229 while ( ( code
= getcode() ) >= 0 ) {
1230 if ( (code
== CLEAR
) && block_compress
) {
1231 free_ent
= FIRST
- 1;
1234 else if ( free_ent
< maxmaxcode
)
1236 if ( bits
!= n_bits
) {
1237 fprintf(stderr
, "\nChange to %d bits\n", n_bits
);
1241 fprintf(stderr
, "%5d%c", code
, (col
+=6) >= 74 ? (col
= 0, '\n') : ' ' );
1243 putc( '\n', stderr
);
1247 code_int sorttab
[1<<BITS
]; /* sorted pointers into htab */
1248 #define STACK_SIZE 500
1249 static char stack
[STACK_SIZE
];
1250 /* dumptab doesn't use main stack now -prevents distressing crashes */
1251 dump_tab() /* dump string table */
1253 REGISTER
int i
, first
;
1255 int stack_top
= STACK_SIZE
;
1258 if(do_decomp
== 0) { /* compressing */
1259 REGISTER
int flag
= 1;
1261 for(i
=0; i
<hsize
; i
++) { /* build sort pointers */
1262 if((long)htabof(i
) >= 0) {
1263 sorttab
[codetabof(i
)] = i
;
1266 first
= block_compress
? FIRST
: 256;
1267 for(i
= first
; i
< free_ent
; i
++) {
1268 fprintf(stderr
, "%5d: \"", i
);
1269 stack
[--stack_top
] = '\n';
1270 stack
[--stack_top
] = '"'; /* " */
1271 stack_top
= in_stack((int)(htabof(sorttab
[i
])>>maxbits
)&0xff,
1273 for(ent
=htabof(sorttab
[i
]) & ((1<<maxbits
)-1);
1275 ent
=htabof(sorttab
[ent
]) & ((1<<maxbits
)-1)) {
1276 stack_top
= in_stack((int)(htabof(sorttab
[ent
]) >> maxbits
),
1279 stack_top
= in_stack(ent
, stack_top
);
1280 fwrite( &stack
[stack_top
], (size_t)1, (size_t)(STACK_SIZE
-stack_top
), stderr
);
1281 stack_top
= STACK_SIZE
;
1283 } else if(!debug
) { /* decompressing */
1285 for ( i
= 0; i
< free_ent
; i
++ ) {
1287 c
= tab_suffixof(ent
);
1288 if ( isascii(c
) && isprint(c
) )
1289 fprintf( stderr
, "%5d: %5d/'%c' \"",
1290 ent
, tab_prefixof(ent
), c
);
1292 fprintf( stderr
, "%5d: %5d/\\%03o \"",
1293 ent
, tab_prefixof(ent
), c
);
1294 stack
[--stack_top
] = '\n';
1295 stack
[--stack_top
] = '"'; /* " */
1296 for ( ; ent
!= NULL
;
1297 ent
= (ent
>= FIRST
? tab_prefixof(ent
) : NULL
) ) {
1298 stack_top
= in_stack(tab_suffixof(ent
), stack_top
);
1300 fwrite( &stack
[stack_top
], (size_t)1, (size_t)(STACK_SIZE
- stack_top
), stderr
);
1301 stack_top
= STACK_SIZE
;
1307 in_stack(c
, stack_top
)
1308 REGISTER
int c
, stack_top
;
1310 if ( (isascii(c
) && isprint(c
) && c
!= '\\') || c
== ' ' ) {
1311 stack
[--stack_top
] = c
;
1314 case '\n': stack
[--stack_top
] = 'n'; break;
1315 case '\t': stack
[--stack_top
] = 't'; break;
1316 case '\b': stack
[--stack_top
] = 'b'; break;
1317 case '\f': stack
[--stack_top
] = 'f'; break;
1318 case '\r': stack
[--stack_top
] = 'r'; break;
1319 case '\\': stack
[--stack_top
] = '\\'; break;
1321 stack
[--stack_top
] = '0' + c
% 8;
1322 stack
[--stack_top
] = '0' + (c
/ 8) % 8;
1323 stack
[--stack_top
] = '0' + c
/ 64;
1326 stack
[--stack_top
] = '\\';
1329 fprintf(stderr
,"dump_tab stack overflow!!!\n");
1338 #endif /* METAWARE */
1347 void copystat(ifname
, ofname
)
1348 char *ifname
, *ofname
;
1350 struct stat statbuf
;
1355 unsigned long timep
[2];
1358 close(fileno(stdout
));
1359 if (stat(ifname
, &statbuf
))
1360 { /* Get stat on input file */
1365 /* meddling with UNIX-style file modes */
1366 if ((statbuf
.st_mode
& S_IFMT
/*0170000*/) != S_IFREG
/*0100000*/)
1369 fprintf(stderr
, "%s: ", ifname
);
1370 fprintf(stderr
, " -- not a regular file: unchanged");
1372 } else if (statbuf
.st_nlink
> 1)
1375 fprintf(stderr
, "%s: ", ifname
);
1376 fprintf(stderr
, " -- has %d other links: unchanged",
1377 statbuf
.st_nlink
- 1);
1381 if (exit_stat
== 2 && (!force
))
1382 { /* No compression: remove file.Z */
1384 fprintf(stderr
, " -- file unchanged");
1386 { /* ***** Successful Compression ***** */
1389 mode
= statbuf
.st_mode
& 07777;
1391 mode
= statbuf
.st_attr
& 07777;
1393 if (chmod(ofname
, mode
)) /* Copy modes */
1396 chown(ofname
, statbuf
.st_uid
, statbuf
.st_gid
); /* Copy ownership */
1397 timep
[0] = statbuf
.st_atime
;
1398 timep
[1] = statbuf
.st_mtime
;
1400 timep
[0] = statbuf
.st_mtime
;
1401 timep
[1] = statbuf
.st_mtime
;
1403 utime(ofname
, (struct utimbuf
*)timep
); /* Update last accessed and modified times */
1410 fprintf(stderr
, " -- compressed to %s", ofname
);
1412 fprintf(stderr
, " -- decompressed to %s", ofname
);
1413 return; /* Successful return */
1416 /* Unsuccessful return -- one of the tests failed */
1421 * This routine returns 1 if we are running in the foreground and stderr
1427 if(bgnd_flag
) { /* background? */
1429 } else { /* foreground */
1431 if(isatty(2)) { /* and stderr is a tty */
1442 int dummy
; /* to keep the compiler happy */
1444 (void)signal(SIGINT
,SIG_IGN
);
1449 void oops (dummy
) /* wild pointer -- assume bad input */
1450 int dummy
; /* to keep the compiler happy */
1452 (void)signal(SIGSEGV
,SIG_IGN
);
1453 if ( do_decomp
== 1 )
1454 fprintf ( stderr
, "uncompress: corrupt input\n" );
1459 void cl_block () /* table clear for block compress */
1461 REGISTER
long int rat
;
1463 checkpoint
= in_count
+ CHECK_GAP
;
1466 fprintf ( stderr
, "count: %ld, ratio: ", in_count
);
1467 prratio ( stderr
, in_count
, bytes_out
);
1468 fprintf ( stderr
, "\n");
1472 if(in_count
> 0x007fffff) { /* shift will overflow */
1473 rat
= bytes_out
>> 8;
1474 if(rat
== 0) { /* Don't divide by zero */
1477 rat
= in_count
/ rat
;
1480 rat
= (in_count
<< 8) / bytes_out
; /* 8 fractional bits */
1482 if ( rat
> ratio
) {
1488 dump_tab(); /* dump string table */
1490 cl_hash ( (count_int
) hsize
);
1493 output ( (code_int
) CLEAR
);
1496 fprintf ( stderr
, "clear\n" );
1501 void cl_hash(hsize
) /* reset code table */
1502 REGISTER count_int hsize
;
1506 /* This function only in PC-DOS lib, not in MINIX lib */
1507 memset(htab
,-1, hsize
* sizeof(count_int
));
1509 /* MINIX and all non-PC machines do it this way */
1510 #ifndef XENIX_16 /* Normal machine */
1511 REGISTER count_int
*htab_p
= htab
+hsize
;
1514 REGISTER
long k
= hsize
;
1515 REGISTER count_int
*htab_p
;
1518 REGISTER
long m1
= -1;
1521 for(j
=0; j
<=8 && k
>=0; j
++,k
-=8192)
1528 htab_p
= &(htab
[j
][i
]);
1536 { /* might use Sys V memset(3) here */
1554 } while ((i
-= 16) >= 0);
1559 for ( i
+= 16; i
> 0; i
-- )
1565 void prratio(stream
, num
, den
)
1570 REGISTER
int q
; /* Doesn't need to be long */
1572 { /* 2147483647/10000 */
1573 q
= (int)(num
/ (den
/ 10000L));
1576 q
= (int)(10000L * num
/ den
); /* Long calculations, though */
1583 fprintf(stream
, "%d.%02d%c", q
/ 100, q
% 100, '%');
1588 fprintf(stderr
, "compress 4.1\n");
1589 fprintf(stderr
, "Options: ");
1591 fprintf(stderr
, "vax, ");
1594 fprintf(stderr
, "MINIX, ");
1597 fprintf(stderr
, "NO_UCHAR, ");
1599 #ifdef SIGNED_COMPARE_SLOW
1600 fprintf(stderr
, "SIGNED_COMPARE_SLOW, ");
1603 fprintf(stderr
, "XENIX_16, ");
1606 fprintf(stderr
, "COMPATIBLE, ");
1609 fprintf(stderr
, "DEBUG, ");
1612 fprintf(stderr
, "BSD4_2, ");
1614 fprintf(stderr
, "BITS = %d\n", BITS
);
1616 /* End of text from uok.UUCP:net.sources */