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