compress.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1985, 1986 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * James A. Woods, derived from original work by Spencer Thomas
7 1.1 cgd * and Joseph Orost.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.1 cgd * 3. All advertising materials mentioning features or use of this software
18 1.1 cgd * must display the following acknowledgement:
19 1.1 cgd * This product includes software developed by the University of
20 1.1 cgd * California, Berkeley and its contributors.
21 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
22 1.1 cgd * may be used to endorse or promote products derived from this software
23 1.1 cgd * without specific prior written permission.
24 1.1 cgd *
25 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 cgd * SUCH DAMAGE.
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.1 cgd char copyright[] =
40 1.1 cgd "@(#) Copyright (c) 1985, 1986 The Regents of the University of California.\n\
41 1.1 cgd All rights reserved.\n";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #ifndef lint
45 1.1 cgd static char sccsid[] = "@(#)compress.c 5.19 (Berkeley) 3/18/91";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * compress.c - File compression ala IEEE Computer, June 1984.
50 1.1 cgd *
51 1.1 cgd * Authors: Spencer W. Thomas (decvax!utah-cs!thomas)
52 1.1 cgd * Jim McKie (decvax!mcvax!jim)
53 1.1 cgd * Steve Davies (decvax!vax135!petsd!peora!srd)
54 1.1 cgd * Ken Turkowski (decvax!decwrl!turtlevax!ken)
55 1.1 cgd * James A. Woods (decvax!ihnp4!ames!jaw)
56 1.1 cgd * Joe Orost (decvax!vax135!petsd!joe)
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include <sys/param.h>
60 1.1 cgd #include <sys/stat.h>
61 1.1 cgd #include <signal.h>
62 1.1 cgd #include <utime.h>
63 1.1 cgd #include <errno.h>
64 1.1 cgd #include <unistd.h>
65 1.1 cgd #include <stdio.h>
66 1.1 cgd #include <ctype.h>
67 1.1 cgd #include <stdlib.h>
68 1.1 cgd #include <string.h>
69 1.1 cgd
70 1.1 cgd /*
71 1.1 cgd * Set USERMEM to the maximum amount of physical user memory available
72 1.1 cgd * in bytes. USERMEM is used to determine the maximum BITS that can be used
73 1.1 cgd * for compression.
74 1.1 cgd *
75 1.1 cgd * SACREDMEM is the amount of physical memory saved for others; compress
76 1.1 cgd * will hog the rest.
77 1.1 cgd */
78 1.1 cgd #ifndef SACREDMEM
79 1.1 cgd #define SACREDMEM 0
80 1.1 cgd #endif
81 1.1 cgd
82 1.1 cgd #ifndef USERMEM
83 1.1 cgd # define USERMEM 450000 /* default user memory */
84 1.1 cgd #endif
85 1.1 cgd
86 1.1 cgd #ifdef pdp11
87 1.1 cgd # define BITS 12 /* max bits/code for 16-bit machine */
88 1.1 cgd # define NO_UCHAR /* also if "unsigned char" functions as signed char */
89 1.1 cgd # undef USERMEM
90 1.1 cgd #endif /* pdp11 */ /* don't forget to compile with -i */
91 1.1 cgd
92 1.1 cgd #ifdef USERMEM
93 1.1 cgd # if USERMEM >= (433484+SACREDMEM)
94 1.1 cgd # define PBITS 16
95 1.1 cgd # else
96 1.1 cgd # if USERMEM >= (229600+SACREDMEM)
97 1.1 cgd # define PBITS 15
98 1.1 cgd # else
99 1.1 cgd # if USERMEM >= (127536+SACREDMEM)
100 1.1 cgd # define PBITS 14
101 1.1 cgd # else
102 1.1 cgd # if USERMEM >= (73464+SACREDMEM)
103 1.1 cgd # define PBITS 13
104 1.1 cgd # else
105 1.1 cgd # define PBITS 12
106 1.1 cgd # endif
107 1.1 cgd # endif
108 1.1 cgd # endif
109 1.1 cgd # endif
110 1.1 cgd # undef USERMEM
111 1.1 cgd #endif /* USERMEM */
112 1.1 cgd
113 1.1 cgd #ifdef PBITS /* Preferred BITS for this memory size */
114 1.1 cgd # ifndef BITS
115 1.1 cgd # define BITS PBITS
116 1.1 cgd # endif BITS
117 1.1 cgd #endif /* PBITS */
118 1.1 cgd
119 1.1 cgd #if BITS == 16
120 1.1 cgd # define HSIZE 69001 /* 95% occupancy */
121 1.1 cgd #endif
122 1.1 cgd #if BITS == 15
123 1.1 cgd # define HSIZE 35023 /* 94% occupancy */
124 1.1 cgd #endif
125 1.1 cgd #if BITS == 14
126 1.1 cgd # define HSIZE 18013 /* 91% occupancy */
127 1.1 cgd #endif
128 1.1 cgd #if BITS == 13
129 1.1 cgd # define HSIZE 9001 /* 91% occupancy */
130 1.1 cgd #endif
131 1.1 cgd #if BITS <= 12
132 1.1 cgd # define HSIZE 5003 /* 80% occupancy */
133 1.1 cgd #endif
134 1.1 cgd
135 1.1 cgd /*
136 1.1 cgd * a code_int must be able to hold 2**BITS values of type int, and also -1
137 1.1 cgd */
138 1.1 cgd #if BITS > 15
139 1.1 cgd typedef long int code_int;
140 1.1 cgd #else
141 1.1 cgd typedef int code_int;
142 1.1 cgd #endif
143 1.1 cgd
144 1.1 cgd #ifdef SIGNED_COMPARE_SLOW
145 1.1 cgd typedef unsigned long int count_int;
146 1.1 cgd typedef unsigned short int count_short;
147 1.1 cgd #else
148 1.1 cgd typedef long int count_int;
149 1.1 cgd #endif
150 1.1 cgd
151 1.1 cgd #ifdef NO_UCHAR
152 1.1 cgd typedef char char_type;
153 1.1 cgd #else
154 1.1 cgd typedef unsigned char char_type;
155 1.1 cgd #endif /* UCHAR */
156 1.1 cgd char_type magic_header[] = { "\037\235" }; /* 1F 9D */
157 1.1 cgd
158 1.1 cgd /* Defines for third byte of header */
159 1.1 cgd #define BIT_MASK 0x1f
160 1.1 cgd #define BLOCK_MASK 0x80
161 1.1 cgd /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
162 1.1 cgd a fourth header byte (for expansion).
163 1.1 cgd */
164 1.1 cgd #define INIT_BITS 9 /* initial number of bits/code */
165 1.1 cgd
166 1.1 cgd int n_bits; /* number of bits/code */
167 1.1 cgd int maxbits = BITS; /* user settable max # bits/code */
168 1.1 cgd code_int maxcode; /* maximum code, given n_bits */
169 1.1 cgd code_int maxmaxcode = 1 << BITS; /* should NEVER generate this code */
170 1.1 cgd #ifdef COMPATIBLE /* But wrong! */
171 1.1 cgd # define MAXCODE(n_bits) (1 << (n_bits) - 1)
172 1.1 cgd #else
173 1.1 cgd # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
174 1.1 cgd #endif /* COMPATIBLE */
175 1.1 cgd
176 1.1 cgd count_int htab [HSIZE];
177 1.1 cgd unsigned short codetab [HSIZE];
178 1.1 cgd
179 1.1 cgd #define htabof(i) htab[i]
180 1.1 cgd #define codetabof(i) codetab[i]
181 1.1 cgd code_int hsize = HSIZE; /* for dynamic table sizing */
182 1.1 cgd count_int fsize;
183 1.1 cgd
184 1.1 cgd /*
185 1.1 cgd * To save much memory, we overlay the table used by compress() with those
186 1.1 cgd * used by decompress(). The tab_prefix table is the same size and type
187 1.1 cgd * as the codetab. The tab_suffix table needs 2**BITS characters. We
188 1.1 cgd * get this from the beginning of htab. The output stack uses the rest
189 1.1 cgd * of htab, and contains characters. There is plenty of room for any
190 1.1 cgd * possible stack (stack used to be 8000 characters).
191 1.1 cgd */
192 1.1 cgd
193 1.1 cgd #define tab_prefixof(i) codetabof(i)
194 1.1 cgd # define tab_suffixof(i) ((char_type *)(htab))[i]
195 1.1 cgd # define de_stack ((char_type *)&tab_suffixof(1<<BITS))
196 1.1 cgd
197 1.1 cgd code_int free_ent = 0; /* first unused entry */
198 1.1 cgd int exit_stat = 0; /* per-file status */
199 1.1 cgd int perm_stat = 0; /* permanent status */
200 1.1 cgd
201 1.1 cgd code_int getcode();
202 1.1 cgd
203 1.1 cgd int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
204 1.1 cgd int zcat_flg = 0; /* Write output on stdout, suppress messages */
205 1.1 cgd int precious = 1; /* Don't unlink output file on interrupt */
206 1.1 cgd int quiet = 1; /* don't tell me about compression */
207 1.1 cgd
208 1.1 cgd /*
209 1.1 cgd * block compression parameters -- after all codes are used up,
210 1.1 cgd * and compression rate changes, start over.
211 1.1 cgd */
212 1.1 cgd int block_compress = BLOCK_MASK;
213 1.1 cgd int clear_flg = 0;
214 1.1 cgd long int ratio = 0;
215 1.1 cgd #define CHECK_GAP 10000 /* ratio check interval */
216 1.1 cgd count_int checkpoint = CHECK_GAP;
217 1.1 cgd /*
218 1.1 cgd * the next two codes should not be changed lightly, as they must not
219 1.1 cgd * lie within the contiguous general code space.
220 1.1 cgd */
221 1.1 cgd #define FIRST 257 /* first free entry */
222 1.1 cgd #define CLEAR 256 /* table clear output code */
223 1.1 cgd
224 1.1 cgd int force = 0;
225 1.1 cgd char ofname [100];
226 1.1 cgd #ifdef DEBUG
227 1.1 cgd int debug, verbose;
228 1.1 cgd #endif
229 1.1 cgd sig_t oldint;
230 1.1 cgd int bgnd_flag;
231 1.1 cgd
232 1.1 cgd int do_decomp = 0;
233 1.1 cgd
234 1.1 cgd /*-
235 1.1 cgd * Algorithm from "A Technique for High Performance Data Compression",
236 1.1 cgd * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
237 1.1 cgd *
238 1.1 cgd * Usage: compress [-dfvc] [-b bits] [file ...]
239 1.1 cgd * Inputs:
240 1.1 cgd * -d: If given, decompression is done instead.
241 1.1 cgd *
242 1.1 cgd * -c: Write output on stdout, don't remove original.
243 1.1 cgd *
244 1.1 cgd * -b: Parameter limits the max number of bits/code.
245 1.1 cgd *
246 1.1 cgd * -f: Forces output file to be generated, even if one already
247 1.1 cgd * exists, and even if no space is saved by compressing.
248 1.1 cgd * If -f is not used, the user will be prompted if stdin is
249 1.1 cgd * a tty, otherwise, the output file will not be overwritten.
250 1.1 cgd *
251 1.1 cgd * -v: Write compression statistics
252 1.1 cgd *
253 1.1 cgd * file ...: Files to be compressed. If none specified, stdin
254 1.1 cgd * is used.
255 1.1 cgd * Outputs:
256 1.1 cgd * file.Z: Compressed form of file with same mode, owner, and utimes
257 1.1 cgd * or stdout (if stdin used as input)
258 1.1 cgd *
259 1.1 cgd * Assumptions:
260 1.1 cgd * When filenames are given, replaces with the compressed version
261 1.1 cgd * (.Z suffix) only if the file decreases in size.
262 1.1 cgd * Algorithm:
263 1.1 cgd * Modified Lempel-Ziv method (LZW). Basically finds common
264 1.1 cgd * substrings and replaces them with a variable size code. This is
265 1.1 cgd * deterministic, and can be done on the fly. Thus, the decompression
266 1.1 cgd * procedure needs no input table, but tracks the way the table was built.
267 1.1 cgd */
268 1.1 cgd
269 1.1 cgd main(argc, argv)
270 1.1 cgd int argc;
271 1.1 cgd char **argv;
272 1.1 cgd {
273 1.1 cgd extern int optind;
274 1.1 cgd extern char *optarg;
275 1.1 cgd struct stat statbuf;
276 1.1 cgd int ch, overwrite;
277 1.1 cgd char **filelist, **fileptr, *cp, tempname[MAXPATHLEN];
278 1.1 cgd void onintr(), oops();
279 1.1 cgd
280 1.1 cgd /* This bg check only works for sh. */
281 1.1 cgd if ((oldint = signal(SIGINT, SIG_IGN)) != SIG_IGN) {
282 1.1 cgd (void)signal(SIGINT, onintr);
283 1.1 cgd (void)signal(SIGSEGV, oops); /* XXX */
284 1.1 cgd }
285 1.1 cgd bgnd_flag = oldint != SIG_DFL;
286 1.1 cgd
287 1.1 cgd #ifdef COMPATIBLE
288 1.1 cgd nomagic = 1; /* Original didn't have a magic number */
289 1.1 cgd #endif
290 1.1 cgd
291 1.1 cgd if (cp = rindex(argv[0], '/'))
292 1.1 cgd ++cp;
293 1.1 cgd else
294 1.1 cgd cp = argv[0];
295 1.1 cgd if (strcmp(cp, "uncompress") == 0)
296 1.1 cgd do_decomp = 1;
297 1.1 cgd else if(strcmp(cp, "zcat") == 0) {
298 1.1 cgd do_decomp = 1;
299 1.1 cgd zcat_flg = 1;
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd /*
303 1.1 cgd * -b maxbits => maxbits.
304 1.1 cgd * -C => generate output compatible with compress 2.0.
305 1.1 cgd * -c => cat all output to stdout
306 1.1 cgd * -D => debug
307 1.1 cgd * -d => do_decomp
308 1.1 cgd * -f => force overwrite of output file
309 1.1 cgd * -n => no header: useful to uncompress old files
310 1.1 cgd * -V => print Version; debug verbose
311 1.1 cgd * -v => unquiet
312 1.1 cgd */
313 1.1 cgd
314 1.1 cgd overwrite = 0;
315 1.1 cgd #ifdef DEBUG
316 1.1 cgd while ((ch = getopt(argc, argv, "b:CcDdfnVv")) != EOF)
317 1.1 cgd #else
318 1.1 cgd while ((ch = getopt(argc, argv, "b:Ccdfnv")) != EOF)
319 1.1 cgd #endif
320 1.1 cgd switch(ch) {
321 1.1 cgd case 'b':
322 1.1 cgd maxbits = atoi(optarg);
323 1.1 cgd break;
324 1.1 cgd case 'C':
325 1.1 cgd block_compress = 0;
326 1.1 cgd break;
327 1.1 cgd case 'c':
328 1.1 cgd zcat_flg = 1;
329 1.1 cgd break;
330 1.1 cgd #ifdef DEBUG
331 1.1 cgd case 'D':
332 1.1 cgd debug = 1;
333 1.1 cgd break;
334 1.1 cgd #endif
335 1.1 cgd case 'd':
336 1.1 cgd do_decomp = 1;
337 1.1 cgd break;
338 1.1 cgd case 'f':
339 1.1 cgd overwrite = 1;
340 1.1 cgd force = 1;
341 1.1 cgd break;
342 1.1 cgd case 'n':
343 1.1 cgd nomagic = 1;
344 1.1 cgd break;
345 1.1 cgd case 'q':
346 1.1 cgd quiet = 1;
347 1.1 cgd break;
348 1.1 cgd #ifdef DEBUG
349 1.1 cgd case 'V':
350 1.1 cgd verbose = 1;
351 1.1 cgd break;
352 1.1 cgd #endif
353 1.1 cgd case 'v':
354 1.1 cgd quiet = 0;
355 1.1 cgd break;
356 1.1 cgd case '?':
357 1.1 cgd default:
358 1.1 cgd usage();
359 1.1 cgd }
360 1.1 cgd argc -= optind;
361 1.1 cgd argv += optind;
362 1.1 cgd
363 1.1 cgd if (maxbits < INIT_BITS)
364 1.1 cgd maxbits = INIT_BITS;
365 1.1 cgd if (maxbits > BITS)
366 1.1 cgd maxbits = BITS;
367 1.1 cgd maxmaxcode = 1 << maxbits;
368 1.1 cgd
369 1.1 cgd /* Build useless input file list. */
370 1.1 cgd filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
371 1.1 cgd while (*argv)
372 1.1 cgd *fileptr++ = *argv++;
373 1.1 cgd *fileptr = NULL;
374 1.1 cgd
375 1.1 cgd if (*filelist != NULL) {
376 1.1 cgd for (fileptr = filelist; *fileptr; fileptr++) {
377 1.1 cgd exit_stat = 0;
378 1.1 cgd if (do_decomp) { /* DECOMPRESSION */
379 1.1 cgd /* Check for .Z suffix */
380 1.1 cgd if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
381 1.1 cgd /* No .Z: tack one on */
382 1.1 cgd strcpy(tempname, *fileptr);
383 1.1 cgd strcat(tempname, ".Z");
384 1.1 cgd *fileptr = tempname;
385 1.1 cgd }
386 1.1 cgd /* Open input file */
387 1.1 cgd if ((freopen(*fileptr, "r", stdin)) == NULL) {
388 1.1 cgd perror(*fileptr);
389 1.1 cgd perm_stat = 1;
390 1.1 cgd continue;
391 1.1 cgd }
392 1.1 cgd /* Check the magic number */
393 1.1 cgd if (nomagic == 0) {
394 1.1 cgd if ((getchar() != (magic_header[0] & 0xFF))
395 1.1 cgd || (getchar() != (magic_header[1] & 0xFF))) {
396 1.1 cgd fprintf(stderr, "%s: not in compressed format\n",
397 1.1 cgd *fileptr);
398 1.1 cgd continue;
399 1.1 cgd }
400 1.1 cgd maxbits = getchar(); /* set -b from file */
401 1.1 cgd block_compress = maxbits & BLOCK_MASK;
402 1.1 cgd maxbits &= BIT_MASK;
403 1.1 cgd maxmaxcode = 1 << maxbits;
404 1.1 cgd if(maxbits > BITS) {
405 1.1 cgd fprintf(stderr,
406 1.1 cgd "%s: compressed with %d bits, can only handle %d bits\n",
407 1.1 cgd *fileptr, maxbits, BITS);
408 1.1 cgd continue;
409 1.1 cgd }
410 1.1 cgd }
411 1.1 cgd /* Generate output filename */
412 1.1 cgd strcpy(ofname, *fileptr);
413 1.1 cgd ofname[strlen(*fileptr) - 2] = '\0'; /* Strip off .Z */
414 1.1 cgd } else { /* COMPRESSION */
415 1.1 cgd if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) {
416 1.1 cgd fprintf(stderr, "%s: already has .Z suffix -- no change\n",
417 1.1 cgd *fileptr);
418 1.1 cgd continue;
419 1.1 cgd }
420 1.1 cgd /* Open input file */
421 1.1 cgd if ((freopen(*fileptr, "r", stdin)) == NULL) {
422 1.1 cgd perror(*fileptr);
423 1.1 cgd perm_stat = 1;
424 1.1 cgd continue;
425 1.1 cgd }
426 1.1 cgd stat ( *fileptr, &statbuf );
427 1.1 cgd fsize = (long) statbuf.st_size;
428 1.1 cgd /*
429 1.1 cgd * tune hash table size for small files -- ad hoc,
430 1.1 cgd * but the sizes match earlier #defines, which
431 1.1 cgd * serve as upper bounds on the number of output codes.
432 1.1 cgd */
433 1.1 cgd hsize = HSIZE;
434 1.1 cgd if ( fsize < (1 << 12) )
435 1.1 cgd hsize = MIN ( 5003, HSIZE );
436 1.1 cgd else if ( fsize < (1 << 13) )
437 1.1 cgd hsize = MIN ( 9001, HSIZE );
438 1.1 cgd else if ( fsize < (1 << 14) )
439 1.1 cgd hsize = MIN ( 18013, HSIZE );
440 1.1 cgd else if ( fsize < (1 << 15) )
441 1.1 cgd hsize = MIN ( 35023, HSIZE );
442 1.1 cgd else if ( fsize < 47000 )
443 1.1 cgd hsize = MIN ( 50021, HSIZE );
444 1.1 cgd
445 1.1 cgd /* Generate output filename */
446 1.1 cgd strcpy(ofname, *fileptr);
447 1.1 cgd strcat(ofname, ".Z");
448 1.1 cgd }
449 1.1 cgd /* Check for overwrite of existing file */
450 1.1 cgd if (overwrite == 0 && zcat_flg == 0) {
451 1.1 cgd if (stat(ofname, &statbuf) == 0) {
452 1.1 cgd char response[2];
453 1.1 cgd response[0] = 'n';
454 1.1 cgd fprintf(stderr, "%s already exists;", ofname);
455 1.1 cgd if (bgnd_flag == 0 && isatty(2)) {
456 1.1 cgd fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
457 1.1 cgd ofname);
458 1.1 cgd fflush(stderr);
459 1.1 cgd read(2, response, 2);
460 1.1 cgd while (response[1] != '\n') {
461 1.1 cgd if (read(2, response+1, 1) < 0) { /* Ack! */
462 1.1 cgd perror("stderr"); break;
463 1.1 cgd }
464 1.1 cgd }
465 1.1 cgd }
466 1.1 cgd if (response[0] != 'y') {
467 1.1 cgd fprintf(stderr, "\tnot overwritten\n");
468 1.1 cgd continue;
469 1.1 cgd }
470 1.1 cgd }
471 1.1 cgd }
472 1.1 cgd if(zcat_flg == 0) { /* Open output file */
473 1.1 cgd if (freopen(ofname, "w", stdout) == NULL) {
474 1.1 cgd perror(ofname);
475 1.1 cgd perm_stat = 1;
476 1.1 cgd continue;
477 1.1 cgd }
478 1.1 cgd precious = 0;
479 1.1 cgd if(!quiet)
480 1.1 cgd fprintf(stderr, "%s: ", *fileptr);
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd /* Actually do the compression/decompression */
484 1.1 cgd if (do_decomp == 0) compress();
485 1.1 cgd #ifndef DEBUG
486 1.1 cgd else decompress();
487 1.1 cgd #else
488 1.1 cgd else if (debug == 0) decompress();
489 1.1 cgd else printcodes();
490 1.1 cgd if (verbose) dump_tab();
491 1.1 cgd #endif /* DEBUG */
492 1.1 cgd if(zcat_flg == 0) {
493 1.1 cgd copystat(*fileptr, ofname); /* Copy stats */
494 1.1 cgd precious = 1;
495 1.1 cgd if((exit_stat == 1) || (!quiet))
496 1.1 cgd putc('\n', stderr);
497 1.1 cgd }
498 1.1 cgd }
499 1.1 cgd } else { /* Standard input */
500 1.1 cgd if (do_decomp == 0) {
501 1.1 cgd compress();
502 1.1 cgd #ifdef DEBUG
503 1.1 cgd if(verbose) dump_tab();
504 1.1 cgd #endif /* DEBUG */
505 1.1 cgd if(!quiet)
506 1.1 cgd putc('\n', stderr);
507 1.1 cgd } else {
508 1.1 cgd /* Check the magic number */
509 1.1 cgd if (nomagic == 0) {
510 1.1 cgd if ((getchar()!=(magic_header[0] & 0xFF))
511 1.1 cgd || (getchar()!=(magic_header[1] & 0xFF))) {
512 1.1 cgd fprintf(stderr, "stdin: not in compressed format\n");
513 1.1 cgd exit(1);
514 1.1 cgd }
515 1.1 cgd maxbits = getchar(); /* set -b from file */
516 1.1 cgd block_compress = maxbits & BLOCK_MASK;
517 1.1 cgd maxbits &= BIT_MASK;
518 1.1 cgd maxmaxcode = 1 << maxbits;
519 1.1 cgd fsize = 100000; /* assume stdin large for USERMEM */
520 1.1 cgd if(maxbits > BITS) {
521 1.1 cgd fprintf(stderr,
522 1.1 cgd "stdin: compressed with %d bits, can only handle %d bits\n",
523 1.1 cgd maxbits, BITS);
524 1.1 cgd exit(1);
525 1.1 cgd }
526 1.1 cgd }
527 1.1 cgd #ifndef DEBUG
528 1.1 cgd decompress();
529 1.1 cgd #else
530 1.1 cgd if (debug == 0) decompress();
531 1.1 cgd else printcodes();
532 1.1 cgd if (verbose) dump_tab();
533 1.1 cgd #endif /* DEBUG */
534 1.1 cgd }
535 1.1 cgd }
536 1.1 cgd exit(perm_stat ? perm_stat : exit_stat);
537 1.1 cgd }
538 1.1 cgd
539 1.1 cgd static int offset;
540 1.1 cgd long int in_count = 1; /* length of input */
541 1.1 cgd long int bytes_out; /* length of compressed output */
542 1.1 cgd long int out_count = 0; /* # of codes output (for debugging) */
543 1.1 cgd
544 1.1 cgd /*
545 1.1 cgd * compress stdin to stdout
546 1.1 cgd *
547 1.1 cgd * Algorithm: use open addressing double hashing (no chaining) on the
548 1.1 cgd * prefix code / next character combination. We do a variant of Knuth's
549 1.1 cgd * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
550 1.1 cgd * secondary probe. Here, the modular division first probe is gives way
551 1.1 cgd * to a faster exclusive-or manipulation. Also do block compression with
552 1.1 cgd * an adaptive reset, whereby the code table is cleared when the compression
553 1.1 cgd * ratio decreases, but after the table fills. The variable-length output
554 1.1 cgd * codes are re-sized at this point, and a special CLEAR code is generated
555 1.1 cgd * for the decompressor. Late addition: construct the table according to
556 1.1 cgd * file size for noticeable speed improvement on small files. Please direct
557 1.1 cgd * questions about this implementation to ames!jaw.
558 1.1 cgd */
559 1.1 cgd
560 1.1 cgd compress()
561 1.1 cgd {
562 1.1 cgd register long fcode;
563 1.1 cgd register code_int i = 0;
564 1.1 cgd register int c;
565 1.1 cgd register code_int ent;
566 1.1 cgd register int disp;
567 1.1 cgd register code_int hsize_reg;
568 1.1 cgd register int hshift;
569 1.1 cgd
570 1.1 cgd #ifndef COMPATIBLE
571 1.1 cgd if (nomagic == 0) {
572 1.1 cgd putchar(magic_header[0]);
573 1.1 cgd putchar(magic_header[1]);
574 1.1 cgd putchar((char)(maxbits | block_compress));
575 1.1 cgd if(ferror(stdout))
576 1.1 cgd writeerr();
577 1.1 cgd }
578 1.1 cgd #endif /* COMPATIBLE */
579 1.1 cgd
580 1.1 cgd offset = 0;
581 1.1 cgd bytes_out = 3; /* includes 3-byte header mojo */
582 1.1 cgd out_count = 0;
583 1.1 cgd clear_flg = 0;
584 1.1 cgd ratio = 0;
585 1.1 cgd in_count = 1;
586 1.1 cgd checkpoint = CHECK_GAP;
587 1.1 cgd maxcode = MAXCODE(n_bits = INIT_BITS);
588 1.1 cgd free_ent = ((block_compress) ? FIRST : 256 );
589 1.1 cgd
590 1.1 cgd ent = getchar ();
591 1.1 cgd
592 1.1 cgd hshift = 0;
593 1.1 cgd for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
594 1.1 cgd hshift++;
595 1.1 cgd hshift = 8 - hshift; /* set hash code range bound */
596 1.1 cgd
597 1.1 cgd hsize_reg = hsize;
598 1.1 cgd cl_hash( (count_int) hsize_reg); /* clear hash table */
599 1.1 cgd
600 1.1 cgd #ifdef SIGNED_COMPARE_SLOW
601 1.1 cgd while ( (c = getchar()) != (unsigned) EOF ) {
602 1.1 cgd #else
603 1.1 cgd while ( (c = getchar()) != EOF ) {
604 1.1 cgd #endif
605 1.1 cgd in_count++;
606 1.1 cgd fcode = (long) (((long) c << maxbits) + ent);
607 1.1 cgd i = ((c << hshift) ^ ent); /* xor hashing */
608 1.1 cgd
609 1.1 cgd if ( htabof (i) == fcode ) {
610 1.1 cgd ent = codetabof (i);
611 1.1 cgd continue;
612 1.1 cgd } else if ( (long)htabof (i) < 0 ) /* empty slot */
613 1.1 cgd goto nomatch;
614 1.1 cgd disp = hsize_reg - i; /* secondary hash (after G. Knott) */
615 1.1 cgd if ( i == 0 )
616 1.1 cgd disp = 1;
617 1.1 cgd probe:
618 1.1 cgd if ( (i -= disp) < 0 )
619 1.1 cgd i += hsize_reg;
620 1.1 cgd
621 1.1 cgd if ( htabof (i) == fcode ) {
622 1.1 cgd ent = codetabof (i);
623 1.1 cgd continue;
624 1.1 cgd }
625 1.1 cgd if ( (long)htabof (i) > 0 )
626 1.1 cgd goto probe;
627 1.1 cgd nomatch:
628 1.1 cgd output ( (code_int) ent );
629 1.1 cgd out_count++;
630 1.1 cgd ent = c;
631 1.1 cgd #ifdef SIGNED_COMPARE_SLOW
632 1.1 cgd if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
633 1.1 cgd #else
634 1.1 cgd if ( free_ent < maxmaxcode ) {
635 1.1 cgd #endif
636 1.1 cgd codetabof (i) = free_ent++; /* code -> hashtable */
637 1.1 cgd htabof (i) = fcode;
638 1.1 cgd }
639 1.1 cgd else if ( (count_int)in_count >= checkpoint && block_compress )
640 1.1 cgd cl_block ();
641 1.1 cgd }
642 1.1 cgd /*
643 1.1 cgd * Put out the final code.
644 1.1 cgd */
645 1.1 cgd output( (code_int)ent );
646 1.1 cgd out_count++;
647 1.1 cgd output( (code_int)-1 );
648 1.1 cgd
649 1.1 cgd /*
650 1.1 cgd * Print out stats on stderr
651 1.1 cgd */
652 1.1 cgd if(zcat_flg == 0 && !quiet) {
653 1.1 cgd #ifdef DEBUG
654 1.1 cgd fprintf( stderr,
655 1.1 cgd "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
656 1.1 cgd in_count, out_count, bytes_out );
657 1.1 cgd prratio( stderr, in_count, bytes_out );
658 1.1 cgd fprintf( stderr, "\n");
659 1.1 cgd fprintf( stderr, "\tCompression as in compact: " );
660 1.1 cgd prratio( stderr, in_count-bytes_out, in_count );
661 1.1 cgd fprintf( stderr, "\n");
662 1.1 cgd fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
663 1.1 cgd free_ent - 1, n_bits );
664 1.1 cgd #else /* !DEBUG */
665 1.1 cgd fprintf( stderr, "Compression: " );
666 1.1 cgd prratio( stderr, in_count-bytes_out, in_count );
667 1.1 cgd #endif /* DEBUG */
668 1.1 cgd }
669 1.1 cgd if(bytes_out > in_count) /* exit(2) if no savings */
670 1.1 cgd exit_stat = 2;
671 1.1 cgd return;
672 1.1 cgd }
673 1.1 cgd
674 1.1 cgd /*-
675 1.1 cgd * Output the given code.
676 1.1 cgd * Inputs:
677 1.1 cgd * code: A n_bits-bit integer. If == -1, then EOF. This assumes
678 1.1 cgd * that n_bits =< (long)wordsize - 1.
679 1.1 cgd * Outputs:
680 1.1 cgd * Outputs code to the file.
681 1.1 cgd * Assumptions:
682 1.1 cgd * Chars are 8 bits long.
683 1.1 cgd * Algorithm:
684 1.1 cgd * Maintain a BITS character long buffer (so that 8 codes will
685 1.1 cgd * fit in it exactly). Use the VAX insv instruction to insert each
686 1.1 cgd * code in turn. When the buffer fills up empty it and start over.
687 1.1 cgd */
688 1.1 cgd
689 1.1 cgd static char buf[BITS];
690 1.1 cgd
691 1.1 cgd #ifndef vax
692 1.1 cgd char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
693 1.1 cgd char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
694 1.1 cgd #endif /* vax */
695 1.1 cgd
696 1.1 cgd output( code )
697 1.1 cgd code_int code;
698 1.1 cgd {
699 1.1 cgd #ifdef DEBUG
700 1.1 cgd static int col = 0;
701 1.1 cgd #endif /* DEBUG */
702 1.1 cgd
703 1.1 cgd /*
704 1.1 cgd * On the VAX, it is important to have the register declarations
705 1.1 cgd * in exactly the order given, or the asm will break.
706 1.1 cgd */
707 1.1 cgd register int r_off = offset, bits= n_bits;
708 1.1 cgd register char * bp = buf;
709 1.1 cgd
710 1.1 cgd #ifdef DEBUG
711 1.1 cgd if ( verbose )
712 1.1 cgd fprintf( stderr, "%5d%c", code,
713 1.1 cgd (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
714 1.1 cgd #endif /* DEBUG */
715 1.1 cgd if ( code >= 0 ) {
716 1.1 cgd #if defined(vax) && !defined(__GNUC__)
717 1.1 cgd /*
718 1.1 cgd * VAX and PCC DEPENDENT!! Implementation on other machines is
719 1.1 cgd * below.
720 1.1 cgd *
721 1.1 cgd * Translation: Insert BITS bits from the argument starting at
722 1.1 cgd * offset bits from the beginning of buf.
723 1.1 cgd */
724 1.1 cgd 0; /* Work around for pcc -O bug with asm and if stmt */
725 1.1 cgd asm( "insv 4(ap),r11,r10,(r9)" );
726 1.1 cgd #else
727 1.1 cgd /*
728 1.1 cgd * byte/bit numbering on the VAX is simulated by the following code
729 1.1 cgd */
730 1.1 cgd /*
731 1.1 cgd * Get to the first byte.
732 1.1 cgd */
733 1.1 cgd bp += (r_off >> 3);
734 1.1 cgd r_off &= 7;
735 1.1 cgd /*
736 1.1 cgd * Since code is always >= 8 bits, only need to mask the first
737 1.1 cgd * hunk on the left.
738 1.1 cgd */
739 1.1 cgd *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
740 1.1 cgd bp++;
741 1.1 cgd bits -= (8 - r_off);
742 1.1 cgd code >>= 8 - r_off;
743 1.1 cgd /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
744 1.1 cgd if ( bits >= 8 ) {
745 1.1 cgd *bp++ = code;
746 1.1 cgd code >>= 8;
747 1.1 cgd bits -= 8;
748 1.1 cgd }
749 1.1 cgd /* Last bits. */
750 1.1 cgd if(bits)
751 1.1 cgd *bp = code;
752 1.1 cgd #endif /* vax */
753 1.1 cgd offset += n_bits;
754 1.1 cgd if ( offset == (n_bits << 3) ) {
755 1.1 cgd bp = buf;
756 1.1 cgd bits = n_bits;
757 1.1 cgd bytes_out += bits;
758 1.1 cgd do {
759 1.1 cgd putchar(*bp++);
760 1.1 cgd if (ferror(stdout))
761 1.1 cgd writeerr();
762 1.1 cgd } while(--bits);
763 1.1 cgd offset = 0;
764 1.1 cgd }
765 1.1 cgd
766 1.1 cgd /*
767 1.1 cgd * If the next entry is going to be too big for the code size,
768 1.1 cgd * then increase it, if possible.
769 1.1 cgd */
770 1.1 cgd if ( free_ent > maxcode || (clear_flg > 0))
771 1.1 cgd {
772 1.1 cgd /*
773 1.1 cgd * Write the whole buffer, because the input side won't
774 1.1 cgd * discover the size increase until after it has read it.
775 1.1 cgd */
776 1.1 cgd if ( offset > 0 ) {
777 1.1 cgd if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
778 1.1 cgd writeerr();
779 1.1 cgd bytes_out += n_bits;
780 1.1 cgd }
781 1.1 cgd offset = 0;
782 1.1 cgd
783 1.1 cgd if ( clear_flg ) {
784 1.1 cgd maxcode = MAXCODE (n_bits = INIT_BITS);
785 1.1 cgd clear_flg = 0;
786 1.1 cgd }
787 1.1 cgd else {
788 1.1 cgd n_bits++;
789 1.1 cgd if ( n_bits == maxbits )
790 1.1 cgd maxcode = maxmaxcode;
791 1.1 cgd else
792 1.1 cgd maxcode = MAXCODE(n_bits);
793 1.1 cgd }
794 1.1 cgd #ifdef DEBUG
795 1.1 cgd if ( debug ) {
796 1.1 cgd fprintf( stderr, "\nChange to %d bits\n", n_bits );
797 1.1 cgd col = 0;
798 1.1 cgd }
799 1.1 cgd #endif /* DEBUG */
800 1.1 cgd }
801 1.1 cgd } else {
802 1.1 cgd /*
803 1.1 cgd * At EOF, write the rest of the buffer.
804 1.1 cgd */
805 1.1 cgd if ( offset > 0 ) {
806 1.1 cgd offset = (offset + 7) / 8;
807 1.1 cgd if( fwrite( buf, 1, offset, stdout ) != offset )
808 1.1 cgd writeerr();
809 1.1 cgd bytes_out += offset;
810 1.1 cgd }
811 1.1 cgd offset = 0;
812 1.1 cgd (void)fflush( stdout );
813 1.1 cgd if( ferror( stdout ) )
814 1.1 cgd writeerr();
815 1.1 cgd #ifdef DEBUG
816 1.1 cgd if ( verbose )
817 1.1 cgd fprintf( stderr, "\n" );
818 1.1 cgd #endif
819 1.1 cgd }
820 1.1 cgd }
821 1.1 cgd
822 1.1 cgd /*
823 1.1 cgd * Decompress stdin to stdout. This routine adapts to the codes in the
824 1.1 cgd * file building the "string" table on-the-fly; requiring no table to
825 1.1 cgd * be stored in the compressed file. The tables used herein are shared
826 1.1 cgd * with those of the compress() routine. See the definitions above.
827 1.1 cgd */
828 1.1 cgd
829 1.1 cgd decompress() {
830 1.1 cgd register char_type *stackp;
831 1.1 cgd register int finchar;
832 1.1 cgd register code_int code, oldcode, incode;
833 1.1 cgd int n, nwritten, offset; /* Variables for buffered write */
834 1.1 cgd char buff[BUFSIZ]; /* Buffer for buffered write */
835 1.1 cgd
836 1.1 cgd
837 1.1 cgd /*
838 1.1 cgd * As above, initialize the first 256 entries in the table.
839 1.1 cgd */
840 1.1 cgd maxcode = MAXCODE(n_bits = INIT_BITS);
841 1.1 cgd for ( code = 255; code >= 0; code-- ) {
842 1.1 cgd tab_prefixof(code) = 0;
843 1.1 cgd tab_suffixof(code) = (char_type)code;
844 1.1 cgd }
845 1.1 cgd free_ent = ((block_compress) ? FIRST : 256 );
846 1.1 cgd
847 1.1 cgd finchar = oldcode = getcode();
848 1.1 cgd if(oldcode == -1) /* EOF already? */
849 1.1 cgd return; /* Get out of here */
850 1.1 cgd
851 1.1 cgd /* first code must be 8 bits = char */
852 1.1 cgd n=0;
853 1.1 cgd buff[n++] = (char)finchar;
854 1.1 cgd
855 1.1 cgd stackp = de_stack;
856 1.1 cgd
857 1.1 cgd while ( (code = getcode()) > -1 ) {
858 1.1 cgd
859 1.1 cgd if ( (code == CLEAR) && block_compress ) {
860 1.1 cgd for ( code = 255; code >= 0; code-- )
861 1.1 cgd tab_prefixof(code) = 0;
862 1.1 cgd clear_flg = 1;
863 1.1 cgd free_ent = FIRST - 1;
864 1.1 cgd if ( (code = getcode ()) == -1 ) /* O, untimely death! */
865 1.1 cgd break;
866 1.1 cgd }
867 1.1 cgd incode = code;
868 1.1 cgd /*
869 1.1 cgd * Special case for KwKwK string.
870 1.1 cgd */
871 1.1 cgd if ( code >= free_ent ) {
872 1.1 cgd *stackp++ = finchar;
873 1.1 cgd code = oldcode;
874 1.1 cgd }
875 1.1 cgd
876 1.1 cgd /*
877 1.1 cgd * Generate output characters in reverse order
878 1.1 cgd */
879 1.1 cgd #ifdef SIGNED_COMPARE_SLOW
880 1.1 cgd while ( ((unsigned long)code) >= ((unsigned long)256) ) {
881 1.1 cgd #else
882 1.1 cgd while ( code >= 256 ) {
883 1.1 cgd #endif
884 1.1 cgd *stackp++ = tab_suffixof(code);
885 1.1 cgd code = tab_prefixof(code);
886 1.1 cgd }
887 1.1 cgd *stackp++ = finchar = tab_suffixof(code);
888 1.1 cgd
889 1.1 cgd /*
890 1.1 cgd * And put them out in forward order
891 1.1 cgd */
892 1.1 cgd do {
893 1.1 cgd /*
894 1.1 cgd * About 60% of the time is spent in the putchar() call
895 1.1 cgd * that appeared here. It was originally
896 1.1 cgd * putchar ( *--stackp );
897 1.1 cgd * If we buffer the writes ourselves, we can go faster (about
898 1.1 cgd * 30%).
899 1.1 cgd *
900 1.1 cgd * At this point, the next line is the next *big* time
901 1.1 cgd * sink in the code. It takes up about 10% of the time.
902 1.1 cgd */
903 1.1 cgd buff[n++] = *--stackp;
904 1.1 cgd if (n == BUFSIZ) {
905 1.1 cgd offset = 0;
906 1.1 cgd do {
907 1.1 cgd nwritten = write(fileno(stdout), &buff[offset], n);
908 1.1 cgd if (nwritten < 0)
909 1.1 cgd writeerr();
910 1.1 cgd offset += nwritten;
911 1.1 cgd } while ((n -= nwritten) > 0);
912 1.1 cgd }
913 1.1 cgd } while ( stackp > de_stack );
914 1.1 cgd
915 1.1 cgd /*
916 1.1 cgd * Generate the new entry.
917 1.1 cgd */
918 1.1 cgd if ( (code=free_ent) < maxmaxcode ) {
919 1.1 cgd tab_prefixof(code) = (unsigned short)oldcode;
920 1.1 cgd tab_suffixof(code) = finchar;
921 1.1 cgd free_ent = code+1;
922 1.1 cgd }
923 1.1 cgd /*
924 1.1 cgd * Remember previous code.
925 1.1 cgd */
926 1.1 cgd oldcode = incode;
927 1.1 cgd }
928 1.1 cgd /*
929 1.1 cgd * Flush the stuff remaining in our buffer...
930 1.1 cgd */
931 1.1 cgd offset = 0;
932 1.1 cgd while (n > 0) {
933 1.1 cgd nwritten = write(fileno(stdout), &buff[offset], n);
934 1.1 cgd if (nwritten < 0)
935 1.1 cgd writeerr();
936 1.1 cgd offset += nwritten;
937 1.1 cgd n -= nwritten;
938 1.1 cgd }
939 1.1 cgd }
940 1.1 cgd
941 1.1 cgd /*-
942 1.1 cgd * Read one code from the standard input. If EOF, return -1.
943 1.1 cgd * Inputs:
944 1.1 cgd * stdin
945 1.1 cgd * Outputs:
946 1.1 cgd * code or -1 is returned.
947 1.1 cgd */
948 1.1 cgd code_int
949 1.1 cgd getcode() {
950 1.1 cgd /*
951 1.1 cgd * On the VAX, it is important to have the register declarations
952 1.1 cgd * in exactly the order given, or the asm will break.
953 1.1 cgd */
954 1.1 cgd register code_int code;
955 1.1 cgd static int offset = 0, size = 0;
956 1.1 cgd static char_type buf[BITS];
957 1.1 cgd register int r_off, bits;
958 1.1 cgd register char_type *bp = buf;
959 1.1 cgd
960 1.1 cgd if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
961 1.1 cgd /*
962 1.1 cgd * If the next entry will be too big for the current code
963 1.1 cgd * size, then we must increase the size. This implies reading
964 1.1 cgd * a new buffer full, too.
965 1.1 cgd */
966 1.1 cgd if ( free_ent > maxcode ) {
967 1.1 cgd n_bits++;
968 1.1 cgd if ( n_bits == maxbits )
969 1.1 cgd maxcode = maxmaxcode; /* won't get any bigger now */
970 1.1 cgd else
971 1.1 cgd maxcode = MAXCODE(n_bits);
972 1.1 cgd }
973 1.1 cgd if ( clear_flg > 0) {
974 1.1 cgd maxcode = MAXCODE (n_bits = INIT_BITS);
975 1.1 cgd clear_flg = 0;
976 1.1 cgd }
977 1.1 cgd size = fread( buf, 1, n_bits, stdin );
978 1.1 cgd if ( size <= 0 )
979 1.1 cgd return -1; /* end of file */
980 1.1 cgd offset = 0;
981 1.1 cgd /* Round size down to integral number of codes */
982 1.1 cgd size = (size << 3) - (n_bits - 1);
983 1.1 cgd }
984 1.1 cgd r_off = offset;
985 1.1 cgd bits = n_bits;
986 1.1 cgd #ifdef vax
987 1.1 cgd asm( "extzv r10,r9,(r8),r11" );
988 1.1 cgd #else /* not a vax */
989 1.1 cgd /*
990 1.1 cgd * Get to the first byte.
991 1.1 cgd */
992 1.1 cgd bp += (r_off >> 3);
993 1.1 cgd r_off &= 7;
994 1.1 cgd /* Get first part (low order bits) */
995 1.1 cgd #ifdef NO_UCHAR
996 1.1 cgd code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
997 1.1 cgd #else
998 1.1 cgd code = (*bp++ >> r_off);
999 1.1 cgd #endif /* NO_UCHAR */
1000 1.1 cgd bits -= (8 - r_off);
1001 1.1 cgd r_off = 8 - r_off; /* now, offset into code word */
1002 1.1 cgd /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
1003 1.1 cgd if ( bits >= 8 ) {
1004 1.1 cgd #ifdef NO_UCHAR
1005 1.1 cgd code |= (*bp++ & 0xff) << r_off;
1006 1.1 cgd #else
1007 1.1 cgd code |= *bp++ << r_off;
1008 1.1 cgd #endif /* NO_UCHAR */
1009 1.1 cgd r_off += 8;
1010 1.1 cgd bits -= 8;
1011 1.1 cgd }
1012 1.1 cgd /* high order bits. */
1013 1.1 cgd code |= (*bp & rmask[bits]) << r_off;
1014 1.1 cgd #endif /* vax */
1015 1.1 cgd offset += n_bits;
1016 1.1 cgd
1017 1.1 cgd return code;
1018 1.1 cgd }
1019 1.1 cgd
1020 1.1 cgd #ifdef DEBUG
1021 1.1 cgd printcodes()
1022 1.1 cgd {
1023 1.1 cgd /*
1024 1.1 cgd * Just print out codes from input file. For debugging.
1025 1.1 cgd */
1026 1.1 cgd code_int code;
1027 1.1 cgd int col = 0, bits;
1028 1.1 cgd
1029 1.1 cgd bits = n_bits = INIT_BITS;
1030 1.1 cgd maxcode = MAXCODE(n_bits);
1031 1.1 cgd free_ent = ((block_compress) ? FIRST : 256 );
1032 1.1 cgd while ( ( code = getcode() ) >= 0 ) {
1033 1.1 cgd if ( (code == CLEAR) && block_compress ) {
1034 1.1 cgd free_ent = FIRST - 1;
1035 1.1 cgd clear_flg = 1;
1036 1.1 cgd }
1037 1.1 cgd else if ( free_ent < maxmaxcode )
1038 1.1 cgd free_ent++;
1039 1.1 cgd if ( bits != n_bits ) {
1040 1.1 cgd fprintf(stderr, "\nChange to %d bits\n", n_bits );
1041 1.1 cgd bits = n_bits;
1042 1.1 cgd col = 0;
1043 1.1 cgd }
1044 1.1 cgd fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
1045 1.1 cgd }
1046 1.1 cgd putc( '\n', stderr );
1047 1.1 cgd exit( 0 );
1048 1.1 cgd }
1049 1.1 cgd
1050 1.1 cgd code_int sorttab[1<<BITS]; /* sorted pointers into htab */
1051 1.1 cgd
1052 1.1 cgd dump_tab() /* dump string table */
1053 1.1 cgd {
1054 1.1 cgd register int i, first;
1055 1.1 cgd register ent;
1056 1.1 cgd #define STACK_SIZE 15000
1057 1.1 cgd int stack_top = STACK_SIZE;
1058 1.1 cgd register c;
1059 1.1 cgd
1060 1.1 cgd if(do_decomp == 0) { /* compressing */
1061 1.1 cgd register int flag = 1;
1062 1.1 cgd
1063 1.1 cgd for(i=0; i<hsize; i++) { /* build sort pointers */
1064 1.1 cgd if((long)htabof(i) >= 0) {
1065 1.1 cgd sorttab[codetabof(i)] = i;
1066 1.1 cgd }
1067 1.1 cgd }
1068 1.1 cgd first = block_compress ? FIRST : 256;
1069 1.1 cgd for(i = first; i < free_ent; i++) {
1070 1.1 cgd fprintf(stderr, "%5d: \"", i);
1071 1.1 cgd de_stack[--stack_top] = '\n';
1072 1.1 cgd de_stack[--stack_top] = '"';
1073 1.1 cgd stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
1074 1.1 cgd stack_top);
1075 1.1 cgd for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
1076 1.1 cgd ent > 256;
1077 1.1 cgd ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
1078 1.1 cgd stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
1079 1.1 cgd stack_top);
1080 1.1 cgd }
1081 1.1 cgd stack_top = in_stack(ent, stack_top);
1082 1.1 cgd fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
1083 1.1 cgd stack_top = STACK_SIZE;
1084 1.1 cgd }
1085 1.1 cgd } else if(!debug) { /* decompressing */
1086 1.1 cgd
1087 1.1 cgd for ( i = 0; i < free_ent; i++ ) {
1088 1.1 cgd ent = i;
1089 1.1 cgd c = tab_suffixof(ent);
1090 1.1 cgd if ( isascii(c) && isprint(c) )
1091 1.1 cgd fprintf( stderr, "%5d: %5d/'%c' \"",
1092 1.1 cgd ent, tab_prefixof(ent), c );
1093 1.1 cgd else
1094 1.1 cgd fprintf( stderr, "%5d: %5d/\\%03o \"",
1095 1.1 cgd ent, tab_prefixof(ent), c );
1096 1.1 cgd de_stack[--stack_top] = '\n';
1097 1.1 cgd de_stack[--stack_top] = '"';
1098 1.1 cgd for ( ; ent != NULL;
1099 1.1 cgd ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
1100 1.1 cgd stack_top = in_stack(tab_suffixof(ent), stack_top);
1101 1.1 cgd }
1102 1.1 cgd fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
1103 1.1 cgd stack_top = STACK_SIZE;
1104 1.1 cgd }
1105 1.1 cgd }
1106 1.1 cgd }
1107 1.1 cgd
1108 1.1 cgd int
1109 1.1 cgd in_stack(c, stack_top)
1110 1.1 cgd register c, stack_top;
1111 1.1 cgd {
1112 1.1 cgd if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
1113 1.1 cgd de_stack[--stack_top] = c;
1114 1.1 cgd } else {
1115 1.1 cgd switch( c ) {
1116 1.1 cgd case '\n': de_stack[--stack_top] = 'n'; break;
1117 1.1 cgd case '\t': de_stack[--stack_top] = 't'; break;
1118 1.1 cgd case '\b': de_stack[--stack_top] = 'b'; break;
1119 1.1 cgd case '\f': de_stack[--stack_top] = 'f'; break;
1120 1.1 cgd case '\r': de_stack[--stack_top] = 'r'; break;
1121 1.1 cgd case '\\': de_stack[--stack_top] = '\\'; break;
1122 1.1 cgd default:
1123 1.1 cgd de_stack[--stack_top] = '0' + c % 8;
1124 1.1 cgd de_stack[--stack_top] = '0' + (c / 8) % 8;
1125 1.1 cgd de_stack[--stack_top] = '0' + c / 64;
1126 1.1 cgd break;
1127 1.1 cgd }
1128 1.1 cgd de_stack[--stack_top] = '\\';
1129 1.1 cgd }
1130 1.1 cgd return stack_top;
1131 1.1 cgd }
1132 1.1 cgd #endif /* DEBUG */
1133 1.1 cgd
1134 1.1 cgd writeerr()
1135 1.1 cgd {
1136 1.1 cgd (void)fprintf(stderr, "compress: %s: %s\n",
1137 1.1 cgd ofname[0] ? ofname : "stdout", strerror(errno));
1138 1.1 cgd (void)unlink(ofname);
1139 1.1 cgd exit(1);
1140 1.1 cgd }
1141 1.1 cgd
1142 1.1 cgd copystat(ifname, ofname)
1143 1.1 cgd char *ifname, *ofname;
1144 1.1 cgd {
1145 1.1 cgd struct stat statbuf;
1146 1.1 cgd int mode;
1147 1.1 cgd struct utimbuf tp;
1148 1.1 cgd
1149 1.1 cgd fclose(stdout);
1150 1.1 cgd if (stat(ifname, &statbuf)) { /* Get stat on input file */
1151 1.1 cgd perror(ifname);
1152 1.1 cgd return;
1153 1.1 cgd }
1154 1.1 cgd if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
1155 1.1 cgd if(quiet)
1156 1.1 cgd fprintf(stderr, "%s: ", ifname);
1157 1.1 cgd fprintf(stderr, " -- not a regular file: unchanged");
1158 1.1 cgd exit_stat = 1;
1159 1.1 cgd perm_stat = 1;
1160 1.1 cgd } else if (statbuf.st_nlink > 1) {
1161 1.1 cgd if(quiet)
1162 1.1 cgd fprintf(stderr, "%s: ", ifname);
1163 1.1 cgd fprintf(stderr, " -- has %d other links: unchanged",
1164 1.1 cgd statbuf.st_nlink - 1);
1165 1.1 cgd exit_stat = 1;
1166 1.1 cgd perm_stat = 1;
1167 1.1 cgd } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
1168 1.1 cgd if(!quiet)
1169 1.1 cgd fprintf(stderr, " -- file unchanged");
1170 1.1 cgd } else { /* ***** Successful Compression ***** */
1171 1.1 cgd exit_stat = 0;
1172 1.1 cgd mode = statbuf.st_mode & 07777;
1173 1.1 cgd if (chmod(ofname, mode)) /* Copy modes */
1174 1.1 cgd perror(ofname);
1175 1.1 cgd chown(ofname, statbuf.st_uid, statbuf.st_gid); /* Copy ownership */
1176 1.1 cgd tp.actime = statbuf.st_atime;
1177 1.1 cgd tp.modtime = statbuf.st_mtime;
1178 1.1 cgd utime(ofname, &tp); /* Update last accessed and modified times */
1179 1.1 cgd if (unlink(ifname)) /* Remove input file */
1180 1.1 cgd perror(ifname);
1181 1.1 cgd if(!quiet)
1182 1.1 cgd fprintf(stderr, " -- replaced with %s", ofname);
1183 1.1 cgd return; /* Successful return */
1184 1.1 cgd }
1185 1.1 cgd
1186 1.1 cgd /* Unsuccessful return -- one of the tests failed */
1187 1.1 cgd if (unlink(ofname))
1188 1.1 cgd perror(ofname);
1189 1.1 cgd }
1190 1.1 cgd
1191 1.1 cgd void
1192 1.1 cgd onintr ( )
1193 1.1 cgd {
1194 1.1 cgd if (!precious)
1195 1.1 cgd unlink ( ofname );
1196 1.1 cgd exit ( 1 );
1197 1.1 cgd }
1198 1.1 cgd
1199 1.1 cgd void
1200 1.1 cgd oops ( ) /* wild pointer -- assume bad input */
1201 1.1 cgd {
1202 1.1 cgd if ( do_decomp )
1203 1.1 cgd fprintf ( stderr, "uncompress: corrupt input\n" );
1204 1.1 cgd unlink ( ofname );
1205 1.1 cgd exit ( 1 );
1206 1.1 cgd }
1207 1.1 cgd
1208 1.1 cgd cl_block () /* table clear for block compress */
1209 1.1 cgd {
1210 1.1 cgd register long int rat;
1211 1.1 cgd
1212 1.1 cgd checkpoint = in_count + CHECK_GAP;
1213 1.1 cgd #ifdef DEBUG
1214 1.1 cgd if ( debug ) {
1215 1.1 cgd fprintf ( stderr, "count: %ld, ratio: ", in_count );
1216 1.1 cgd prratio ( stderr, in_count, bytes_out );
1217 1.1 cgd fprintf ( stderr, "\n");
1218 1.1 cgd }
1219 1.1 cgd #endif /* DEBUG */
1220 1.1 cgd
1221 1.1 cgd if(in_count > 0x007fffff) { /* shift will overflow */
1222 1.1 cgd rat = bytes_out >> 8;
1223 1.1 cgd if(rat == 0) { /* Don't divide by zero */
1224 1.1 cgd rat = 0x7fffffff;
1225 1.1 cgd } else {
1226 1.1 cgd rat = in_count / rat;
1227 1.1 cgd }
1228 1.1 cgd } else {
1229 1.1 cgd rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
1230 1.1 cgd }
1231 1.1 cgd if ( rat > ratio ) {
1232 1.1 cgd ratio = rat;
1233 1.1 cgd } else {
1234 1.1 cgd ratio = 0;
1235 1.1 cgd #ifdef DEBUG
1236 1.1 cgd if(verbose)
1237 1.1 cgd dump_tab(); /* dump string table */
1238 1.1 cgd #endif
1239 1.1 cgd cl_hash ( (count_int) hsize );
1240 1.1 cgd free_ent = FIRST;
1241 1.1 cgd clear_flg = 1;
1242 1.1 cgd output ( (code_int) CLEAR );
1243 1.1 cgd #ifdef DEBUG
1244 1.1 cgd if(debug)
1245 1.1 cgd fprintf ( stderr, "clear\n" );
1246 1.1 cgd #endif /* DEBUG */
1247 1.1 cgd }
1248 1.1 cgd }
1249 1.1 cgd
1250 1.1 cgd cl_hash(hsize) /* reset code table */
1251 1.1 cgd register count_int hsize;
1252 1.1 cgd {
1253 1.1 cgd register count_int *htab_p = htab+hsize;
1254 1.1 cgd register long i;
1255 1.1 cgd register long m1 = -1;
1256 1.1 cgd
1257 1.1 cgd i = hsize - 16;
1258 1.1 cgd do { /* might use Sys V memset(3) here */
1259 1.1 cgd *(htab_p-16) = m1;
1260 1.1 cgd *(htab_p-15) = m1;
1261 1.1 cgd *(htab_p-14) = m1;
1262 1.1 cgd *(htab_p-13) = m1;
1263 1.1 cgd *(htab_p-12) = m1;
1264 1.1 cgd *(htab_p-11) = m1;
1265 1.1 cgd *(htab_p-10) = m1;
1266 1.1 cgd *(htab_p-9) = m1;
1267 1.1 cgd *(htab_p-8) = m1;
1268 1.1 cgd *(htab_p-7) = m1;
1269 1.1 cgd *(htab_p-6) = m1;
1270 1.1 cgd *(htab_p-5) = m1;
1271 1.1 cgd *(htab_p-4) = m1;
1272 1.1 cgd *(htab_p-3) = m1;
1273 1.1 cgd *(htab_p-2) = m1;
1274 1.1 cgd *(htab_p-1) = m1;
1275 1.1 cgd htab_p -= 16;
1276 1.1 cgd } while ((i -= 16) >= 0);
1277 1.1 cgd for ( i += 16; i > 0; i-- )
1278 1.1 cgd *--htab_p = m1;
1279 1.1 cgd }
1280 1.1 cgd
1281 1.1 cgd prratio(stream, num, den)
1282 1.1 cgd FILE *stream;
1283 1.1 cgd long int num, den;
1284 1.1 cgd {
1285 1.1 cgd register int q; /* Doesn't need to be long */
1286 1.1 cgd
1287 1.1 cgd if(num > 214748L) { /* 2147483647/10000 */
1288 1.1 cgd q = num / (den / 10000L);
1289 1.1 cgd } else {
1290 1.1 cgd q = 10000L * num / den; /* Long calculations, though */
1291 1.1 cgd }
1292 1.1 cgd if (q < 0) {
1293 1.1 cgd putc('-', stream);
1294 1.1 cgd q = -q;
1295 1.1 cgd }
1296 1.1 cgd fprintf(stream, "%d.%02d%%", q / 100, q % 100);
1297 1.1 cgd }
1298 1.1 cgd
1299 1.1 cgd usage()
1300 1.1 cgd {
1301 1.1 cgd (void)fprintf(stderr,
1302 1.1 cgd #ifdef DEBUG
1303 1.1 cgd "compress [-CDVcdfnv] [-b maxbits] [file ...]\n");
1304 1.1 cgd #else
1305 1.1 cgd "compress [-Ccdfnv] [-b maxbits] [file ...]\n");
1306 1.1 cgd #endif
1307 1.1 cgd exit(1);
1308 1.1 cgd }
1309