3 * compress/uncompress archive
5 * Authors: Spencer W. Thomas (decvax!utah-cs!thomas)
6 * Jim McKie (decvax!mcvax!jim)
7 * Steve Davies (decvax!vax135!petsd!peora!srd)
8 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
9 * James A. Woods (decvax!ihnp4!ames!jaw)
10 * Joe Orost (decvax!vax135!petsd!joe)
12 * NOTE: these functions also support "squash" (which is just a
13 * 13-bit compress), and "crunch" (which is a 12-bit compress
14 * with additional run-length encoding). AJD
16 * $Header: compress.c 1.11 95/08/01 $
17 * $Log: compress.c,v $
18 * Revision 1.11 95/08/01 xx:xx:xx BB
19 * Quite a few changes for Borland C/C++
20 * Made htab and codetab arrays dynamic.
21 * (Compile with -DBB_HUGE_STATIC_ARRAYS if you DO want these
22 * huge static arrays in your executable.)
23 * Changed pointers to normalized or huge pointers because
24 * arrays span more than 64k.
25 * Changed a few types from int to long because 32bits integers
28 * Revision 1.10 95/01/25 12:49:43 arb
29 * Bug fixes caused by 1.9
31 * Revision 1.9 95/01/06 12:00:06 arb
34 * Revision 1.8 94/02/28 23:57:55 arb
35 * Fixed number of compression bits for ArcFS format archives
37 * Revision 1.7 93/08/20 11:35:20 arb
38 * Prevent printing of "uncompressed" etc. if quiet flag is set
40 * Revision 1.6 92/12/07 17:17:28 duplain
43 * Revision 1.5 92/11/09 14:48:00 duplain
44 * Initialised offset and size from getcode() each time uncompress() called.
46 * Revision 1.4 92/11/02 11:43:14 duplain
47 * Correct comment about crunch/squash in header.
49 * Revision 1.3 92/10/23 14:08:13 duplain
50 * Minor changes to printf's at end of uncompress.
52 * Revision 1.2 92/10/01 11:20:19 duplain
53 * Added check for EOF.
55 * Revision 1.1 92/09/29 18:02:14 duplain
69 /* BB changed next line because of conflict with Borland's io.h */
75 #if defined(__MSDOS__) && !defined(MSDOS32)
78 #define farcalloc halloc
80 #include <alloc.h> /* for farcalloc() */
83 #define farcalloc calloc
84 #endif /* __MSDOS__ */
89 #define COMPRESSBITS 16
91 /* BB changed constant in next line to long: 16bits 65536 == 0 ! */
93 #define INIT_BITS 9 /* initial number of bits/code */
95 /* BB changed next macros.
96 * Arrays htab and codetab both exceed 64k. To prevent wraparound
97 at the 64k boundary, normalized or huge pointers have to be used.
98 Since subscripts are 16 bit ints under the Borland compiler,
99 subscripts have to be made explicitely long.
100 And finally COMPRESSBITS == 16, but 1 << 16 == 0 for 16 bits
103 /* #define MAXCODE(n_bits) ((1 << (n_bits)) - 1) */
105 /* #define htabof(i) htab[i] */
107 /* #define codetabof(i) codetab[i] */
109 /* #define tab_prefixof(i) codetabof(i) */
111 /* #define tab_suffixof(i) ((char_type *)(htab))[i] */
113 /* #define de_stack ((char_type *)&tab_suffixof(1<<COMPRESSBITS)) */
114 #if defined(__MSDOS__) && !defined(MSDOS32)
115 #define MAXCODE(n_bits) ((long)(1L << (n_bits)) - 1L)
116 #define htabof(i) htab[(long)(i)]
117 #define codetabof(i) codetab[(long)(i)]
118 #define tab_prefixof(i) codetabof(i)
119 #define tab_suffixof(i) ((char_type huge *)(htab))[(long)(i)]
121 ((char_type huge *)&tab_suffixof(1L<<COMPRESSBITS))
123 #define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
124 #define htabof(i) htab[i]
125 #define codetabof(i) codetab[i]
126 #define tab_prefixof(i) codetabof(i)
127 #define tab_suffixof(i) ((char_type *)(htab))[i]
128 #define de_stack ((char_type *)&tab_suffixof(1<<COMPRESSBITS))
129 #endif /* __MSDOS__ */
130 #define FIRST 257 /* first free entry */
131 #define CLEAR 256 /* table clear output code */
133 /* BB changed next two lines. For 16 bits, the maximum code_int
134 becomes zero again! (1 << 16 == 0).
135 Debugging the un*x version shows that
136 count_int should be a 32bits integer! */
138 /* typedef int code_int; */
140 /* typedef int count_int; */
141 #if defined(__MSDOS__) && !defined(MSDOS32)
143 typedef long code_int
;
144 typedef long count_int
;
147 typedef int count_int
;
148 typedef int code_int
;
149 #endif /* __MSDOS__ */
150 typedef unsigned char char_type
;
152 static int n_bits
; /* number of bits/code */
153 static int maxbits
; /* user settable max # bits/code */
154 static code_int maxcode
; /* maximum code, given n_bits */
155 static code_int maxmaxcode
; /* should NEVER generate this code */
157 /* BB changed next two lines.
158 Under Borland C/C++ static arrays are REALLY static, i.e. they
159 clog the executable with some 384k of `empty space'. So use
160 dynamic arrays instead. */
162 /* static count_int htab[HSIZE]; */
164 /* static unsigned short codetab[HSIZE]; */
165 #if !defined(__MSDOS__) || defined(MSDOS32)
166 #define BB_HUGE_STATIC_ARRAYS
168 /* For those that do want to use static arrays:
169 define BB_HUGE_STATIC_ARRAYS. */
172 #ifdef BB_HUGE_STATIC_ARRAYS
173 static count_int NSHUGE htab
[HSIZE
];
174 static unsigned short NSHUGE codetab
[HSIZE
];
175 #else /* BB_HUGE_STATIC_ARRAYS */
176 static count_int NSHUGE
*htab
= NULL
;
177 static unsigned short NSHUGE
*codetab
= NULL
;
178 #endif /* BB_HUGE_STATIC_ARRAYS */
180 static char_type rmask
[9] =
181 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
182 static code_int free_ent
; /* first unused entry */
183 static int clear_flg
;
184 static long readsize
; /* number of bytes left to read */
186 static code_int offset
; /* from getcode() */
189 static code_int
getcode(FILE * ifp
);
193 uncompress(Header
*header
, FILE *ifp
, FILE *ofp
, CompType type
)
195 /* BB changed next line. stackp points to huge pointers. */
196 register char_type NSHUGE
*stackp
;
197 register code_int finchar
;
198 register code_int code
, oldcode
, incode
;
203 #if !defined(BB_HUGE_STATIC_ARRAYS)
205 htab
= (count_int NSHUGE
*) farcalloc(HSIZE
, sizeof(count_int
));
208 (unsigned short NSHUGE
*) farcalloc(HSIZE
,
209 sizeof(unsigned short));
210 if (!htab
|| !codetab
)
212 error("%s: uncompress: out of memory", ourname
);
215 #endif /* ! BB_HUGE_STATIC_ARRAYS */
221 readsize
= header
->complen
;
224 maxbits
= SQUASHBITS
;
225 else if (type
== UNIX_COMPRESS
)
227 /* Read the unix compress header */
230 maxbits
= read_byte(ifp
) & 0x1f;
237 maxbits
= arcfs_maxbits
;
238 ungarble('\0'); // Need to consume one byte of password.
242 maxbits
= read_byte(ifp
);
246 maxmaxcode
= MAXCODE(maxbits
);
249 * As above, initialize the first 256 entries in the table.
251 maxcode
= MAXCODE(n_bits
= INIT_BITS
);
252 for (code
= 255; code
>= 0; code
--)
254 tab_prefixof(code
) = 0;
255 tab_suffixof(code
) = (char_type
) code
;
259 finchar
= oldcode
= getcode(ifp
);
260 if (oldcode
== -1) /* EOF already? */
261 goto compress_exit
; /* Get out of here */
263 /* first code must be 8 bits = char */
267 /* BB changed next line for Borland C/C++ 4 */
268 putc_ncr(ofp
, (Byte
) finchar
);
272 /* BB changed next three lines for Borland C/C++ 4 */
274 write_byte(ofp
, (Byte
) finchar
);
275 calccrc((Byte
) finchar
);
280 while ((code
= getcode(ifp
)) != -1)
282 if (check_stream(ifp
) != FNOERR
)
286 for (code
= 255; code
>= 0; code
--)
287 tab_prefixof(code
) = 0;
289 free_ent
= FIRST
- 1;
290 if ((code
= getcode(ifp
)) == -1) /* O, untimely death! */
295 * Special case for KwKwK string.
297 if (code
>= free_ent
)
299 /* BB changed next line for Borland C/C++ 4 */
300 *stackp
++ = (char_type
) finchar
;
304 * Generate output characters in reverse order
309 if ((char NSHUGE
*)(stackp
+1) > (char NSHUGE
*)(&htab
[0] + HSIZE
))
311 error("%s: uncompress: corrupt or garbled archive file", ourname
);
314 *stackp
++ = tab_suffixof(code
);
315 code
= tab_prefixof(code
);
317 if ((char NSHUGE
*)(stackp
+1) > (char NSHUGE
*)(&htab
[0] + HSIZE
))
319 error("%s: uncompress: corrupt or garbled archive file", ourname
);
322 /* BB changed next line for Borland C/C++ 4 */
323 /* *stackp++ = finchar = tab_suffixof(code); */
324 finchar
= tab_suffixof(code
);
325 *stackp
++ = (char_type
) finchar
;
328 * And put them out in forward order
330 while (stackp
> de_stack
)
334 putc_ncr(ofp
, *stackp
);
338 write_byte(ofp
, *stackp
);
344 * Generate the new entry.
346 if ((code
= free_ent
) < maxmaxcode
)
348 /* BB changed next two lines for Borland C/C++ 4 */
349 tab_prefixof(code
) = (unsigned short) oldcode
;
350 tab_suffixof(code
) = (char_type
) finchar
;
354 * Remember previous code.
359 if (check_stream(ifp
) == FRWERR
)
361 if (!testing
&& check_stream(ofp
) == FRWERR
)
363 if ((Halfword
) crc
!= header
->crc
)
370 message
= "OK (compressed)";
373 message
= "OK (crunched)";
376 message
= "OK (squashed)";
379 message
= "internal error";
387 message
= "uncompressed";
390 message
= "uncrunched";
393 message
= "unsquashed";
396 message
= "internal error";
405 * Read one code from the input. If EOF, return -1.
410 register code_int code
;
411 static char_type buf
[COMPRESSBITS
];
412 register int r_off
, bits
;
414 /* BB changed next line. We are doing pointer-artithmatics
415 and that can be dangerous if other than normalized (huge)
416 pointers are being used. */
417 register char_type NSHUGE
*bp
= buf
;
419 if (clear_flg
> 0 || offset
>= size
|| free_ent
> maxcode
)
422 * If the next entry will be too big for the current code
423 * size, then we must increase the size. This implies
424 * reading a new buffer full, too.
426 if (free_ent
> maxcode
)
429 maxcode
= n_bits
== maxbits
? maxmaxcode
: MAXCODE(n_bits
);
433 maxcode
= MAXCODE(n_bits
= INIT_BITS
);
438 /* BB added cast to next line */
439 size
= readsize
< n_bits
? (size_t) readsize
: n_bits
;
440 size
= fread(buf
, 1, size
, ifp
);
442 return (-1); /* end of file */
443 for (i
= 0; i
< size
; i
++)
445 buf
[i
] = ungarble(buf
[i
]);
449 /* Round size down to integral number of codes */
450 size
= (size
<< 3) - (n_bits
- 1);
456 * Get to the first byte.
460 /* Get first part (low order bits) */
462 code
= (*bp
++ >> r_off
);
464 r_off
= 8 - r_off
; /* now, offset into code word */
465 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
468 code
|= *bp
++ << r_off
;
472 /* high order bits. */
473 code
|= (*bp
& rmask
[bits
]) << r_off
;