zopen.c revision 1.9 1 1.9 hubertf /* $NetBSD: zopen.c,v 1.9 2007/02/07 14:20:58 hubertf Exp $ */
2 1.5 glass
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1985, 1986, 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Diomidis Spinellis and James A. Woods, derived from original
9 1.1 cgd * work by Spencer Thomas and Joseph Orost.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.8 agc * 3. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
37 1.5 glass #if 0
38 1.5 glass static char sccsid[] = "@(#)zopen.c 8.1 (Berkeley) 6/27/93";
39 1.5 glass #else
40 1.9 hubertf static char rcsid[] = "$NetBSD: zopen.c,v 1.9 2007/02/07 14:20:58 hubertf Exp $";
41 1.5 glass #endif
42 1.1 cgd #endif /* LIBC_SCCS and not lint */
43 1.1 cgd
44 1.1 cgd /*-
45 1.1 cgd * fcompress.c - File compression ala IEEE Computer, June 1984.
46 1.1 cgd *
47 1.1 cgd * Compress authors:
48 1.1 cgd * Spencer W. Thomas (decvax!utah-cs!thomas)
49 1.1 cgd * Jim McKie (decvax!mcvax!jim)
50 1.1 cgd * Steve Davies (decvax!vax135!petsd!peora!srd)
51 1.1 cgd * Ken Turkowski (decvax!decwrl!turtlevax!ken)
52 1.1 cgd * James A. Woods (decvax!ihnp4!ames!jaw)
53 1.1 cgd * Joe Orost (decvax!vax135!petsd!joe)
54 1.1 cgd *
55 1.1 cgd * Cleaned up and converted to library returning I/O streams by
56 1.1 cgd * Diomidis Spinellis <dds (at) doc.ic.ac.uk>.
57 1.1 cgd *
58 1.1 cgd * zopen(filename, mode, bits)
59 1.1 cgd * Returns a FILE * that can be used for read or write. The modes
60 1.1 cgd * supported are only "r" and "w". Seeking is not allowed. On
61 1.1 cgd * reading the file is decompressed, on writing it is compressed.
62 1.1 cgd * The output is compatible with compress(1) with 16 bit tables.
63 1.1 cgd * Any file produced by compress(1) can be read.
64 1.1 cgd */
65 1.1 cgd
66 1.1 cgd #include <sys/param.h>
67 1.1 cgd #include <sys/stat.h>
68 1.1 cgd
69 1.1 cgd #include <errno.h>
70 1.1 cgd #include <signal.h>
71 1.1 cgd #include <stdio.h>
72 1.1 cgd #include <stdlib.h>
73 1.1 cgd #include <string.h>
74 1.1 cgd #include <unistd.h>
75 1.1 cgd
76 1.1 cgd #define BITS 16 /* Default bits. */
77 1.1 cgd #define HSIZE 69001 /* 95% occupancy */
78 1.1 cgd
79 1.1 cgd /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
80 1.1 cgd typedef long code_int;
81 1.1 cgd typedef long count_int;
82 1.1 cgd
83 1.1 cgd typedef u_char char_type;
84 1.1 cgd static char_type magic_header[] =
85 1.1 cgd {'\037', '\235'}; /* 1F 9D */
86 1.1 cgd
87 1.1 cgd #define BIT_MASK 0x1f /* Defines for third byte of header. */
88 1.1 cgd #define BLOCK_MASK 0x80
89 1.1 cgd
90 1.1 cgd /*
91 1.1 cgd * Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
92 1.1 cgd * a fourth header byte (for expansion).
93 1.1 cgd */
94 1.1 cgd #define INIT_BITS 9 /* Initial number of bits/code. */
95 1.1 cgd
96 1.1 cgd #define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
97 1.1 cgd
98 1.1 cgd struct s_zstate {
99 1.1 cgd FILE *zs_fp; /* File stream for I/O */
100 1.1 cgd char zs_mode; /* r or w */
101 1.1 cgd enum {
102 1.1 cgd S_START, S_MIDDLE, S_EOF
103 1.1 cgd } zs_state; /* State of computation */
104 1.1 cgd int zs_n_bits; /* Number of bits/code. */
105 1.1 cgd int zs_maxbits; /* User settable max # bits/code. */
106 1.1 cgd code_int zs_maxcode; /* Maximum code, given n_bits. */
107 1.1 cgd code_int zs_maxmaxcode; /* Should NEVER generate this code. */
108 1.1 cgd count_int zs_htab [HSIZE];
109 1.1 cgd u_short zs_codetab [HSIZE];
110 1.1 cgd code_int zs_hsize; /* For dynamic table sizing. */
111 1.1 cgd code_int zs_free_ent; /* First unused entry. */
112 1.1 cgd /*
113 1.1 cgd * Block compression parameters -- after all codes are used up,
114 1.1 cgd * and compression rate changes, start over.
115 1.1 cgd */
116 1.1 cgd int zs_block_compress;
117 1.1 cgd int zs_clear_flg;
118 1.1 cgd long zs_ratio;
119 1.1 cgd count_int zs_checkpoint;
120 1.1 cgd int zs_offset;
121 1.1 cgd long zs_in_count; /* Length of input. */
122 1.1 cgd long zs_bytes_out; /* Length of compressed output. */
123 1.1 cgd long zs_out_count; /* # of codes output (for debugging). */
124 1.1 cgd char_type zs_buf[BITS];
125 1.1 cgd union {
126 1.1 cgd struct {
127 1.1 cgd long zs_fcode;
128 1.1 cgd code_int zs_ent;
129 1.1 cgd code_int zs_hsize_reg;
130 1.1 cgd int zs_hshift;
131 1.1 cgd } w; /* Write paramenters */
132 1.1 cgd struct {
133 1.1 cgd char_type *zs_stackp;
134 1.1 cgd int zs_finchar;
135 1.1 cgd code_int zs_code, zs_oldcode, zs_incode;
136 1.1 cgd int zs_roffset, zs_size;
137 1.1 cgd char_type zs_gbuf[BITS];
138 1.1 cgd } r; /* Read parameters */
139 1.1 cgd } u;
140 1.1 cgd };
141 1.1 cgd
142 1.1 cgd /* Definitions to retain old variable names */
143 1.1 cgd #define fp zs->zs_fp
144 1.1 cgd #define zmode zs->zs_mode
145 1.1 cgd #define state zs->zs_state
146 1.1 cgd #define n_bits zs->zs_n_bits
147 1.1 cgd #define maxbits zs->zs_maxbits
148 1.1 cgd #define maxcode zs->zs_maxcode
149 1.1 cgd #define maxmaxcode zs->zs_maxmaxcode
150 1.1 cgd #define htab zs->zs_htab
151 1.1 cgd #define codetab zs->zs_codetab
152 1.1 cgd #define hsize zs->zs_hsize
153 1.1 cgd #define free_ent zs->zs_free_ent
154 1.1 cgd #define block_compress zs->zs_block_compress
155 1.1 cgd #define clear_flg zs->zs_clear_flg
156 1.1 cgd #define ratio zs->zs_ratio
157 1.1 cgd #define checkpoint zs->zs_checkpoint
158 1.1 cgd #define offset zs->zs_offset
159 1.1 cgd #define in_count zs->zs_in_count
160 1.1 cgd #define bytes_out zs->zs_bytes_out
161 1.1 cgd #define out_count zs->zs_out_count
162 1.1 cgd #define buf zs->zs_buf
163 1.1 cgd #define fcode zs->u.w.zs_fcode
164 1.1 cgd #define hsize_reg zs->u.w.zs_hsize_reg
165 1.1 cgd #define ent zs->u.w.zs_ent
166 1.1 cgd #define hshift zs->u.w.zs_hshift
167 1.1 cgd #define stackp zs->u.r.zs_stackp
168 1.1 cgd #define finchar zs->u.r.zs_finchar
169 1.1 cgd #define code zs->u.r.zs_code
170 1.1 cgd #define oldcode zs->u.r.zs_oldcode
171 1.1 cgd #define incode zs->u.r.zs_incode
172 1.1 cgd #define roffset zs->u.r.zs_roffset
173 1.1 cgd #define size zs->u.r.zs_size
174 1.1 cgd #define gbuf zs->u.r.zs_gbuf
175 1.1 cgd
176 1.1 cgd /*
177 1.1 cgd * To save much memory, we overlay the table used by compress() with those
178 1.1 cgd * used by decompress(). The tab_prefix table is the same size and type as
179 1.1 cgd * the codetab. The tab_suffix table needs 2**BITS characters. We get this
180 1.1 cgd * from the beginning of htab. The output stack uses the rest of htab, and
181 1.1 cgd * contains characters. There is plenty of room for any possible stack
182 1.1 cgd * (stack used to be 8000 characters).
183 1.1 cgd */
184 1.1 cgd
185 1.1 cgd #define htabof(i) htab[i]
186 1.1 cgd #define codetabof(i) codetab[i]
187 1.1 cgd
188 1.1 cgd #define tab_prefixof(i) codetabof(i)
189 1.1 cgd #define tab_suffixof(i) ((char_type *)(htab))[i]
190 1.1 cgd #define de_stack ((char_type *)&tab_suffixof(1 << BITS))
191 1.1 cgd
192 1.1 cgd #define CHECK_GAP 10000 /* Ratio check interval. */
193 1.1 cgd
194 1.1 cgd /*
195 1.1 cgd * the next two codes should not be changed lightly, as they must not
196 1.1 cgd * lie within the contiguous general code space.
197 1.1 cgd */
198 1.1 cgd #define FIRST 257 /* First free entry. */
199 1.1 cgd #define CLEAR 256 /* Table clear output code. */
200 1.1 cgd
201 1.7 wiz static int cl_block(struct s_zstate *);
202 1.7 wiz static void cl_hash(struct s_zstate *, count_int);
203 1.7 wiz static code_int getcode(struct s_zstate *);
204 1.7 wiz static int output(struct s_zstate *, code_int);
205 1.7 wiz static int zclose(void *);
206 1.7 wiz FILE *zopen(const char *, const char *, int);
207 1.7 wiz static int zread(void *, char *, int);
208 1.7 wiz static int zwrite(void *, const char *, int);
209 1.1 cgd
210 1.1 cgd /*-
211 1.1 cgd * Algorithm from "A Technique for High Performance Data Compression",
212 1.1 cgd * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
213 1.1 cgd *
214 1.1 cgd * Algorithm:
215 1.1 cgd * Modified Lempel-Ziv method (LZW). Basically finds common
216 1.1 cgd * substrings and replaces them with a variable size code. This is
217 1.1 cgd * deterministic, and can be done on the fly. Thus, the decompression
218 1.1 cgd * procedure needs no input table, but tracks the way the table was built.
219 1.1 cgd */
220 1.1 cgd
221 1.1 cgd /*-
222 1.1 cgd * compress write
223 1.1 cgd *
224 1.1 cgd * Algorithm: use open addressing double hashing (no chaining) on the
225 1.1 cgd * prefix code / next character combination. We do a variant of Knuth's
226 1.1 cgd * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
227 1.1 cgd * secondary probe. Here, the modular division first probe is gives way
228 1.1 cgd * to a faster exclusive-or manipulation. Also do block compression with
229 1.1 cgd * an adaptive reset, whereby the code table is cleared when the compression
230 1.1 cgd * ratio decreases, but after the table fills. The variable-length output
231 1.1 cgd * codes are re-sized at this point, and a special CLEAR code is generated
232 1.1 cgd * for the decompressor. Late addition: construct the table according to
233 1.1 cgd * file size for noticeable speed improvement on small files. Please direct
234 1.1 cgd * questions about this implementation to ames!jaw.
235 1.1 cgd */
236 1.1 cgd static int
237 1.7 wiz zwrite(void *cookie, const char *wbp, int num)
238 1.1 cgd {
239 1.6 lukem code_int i;
240 1.6 lukem int c, disp;
241 1.1 cgd struct s_zstate *zs;
242 1.1 cgd const u_char *bp;
243 1.1 cgd u_char tmp;
244 1.1 cgd int count;
245 1.1 cgd
246 1.1 cgd if (num == 0)
247 1.1 cgd return (0);
248 1.1 cgd
249 1.1 cgd zs = cookie;
250 1.1 cgd count = num;
251 1.1 cgd bp = (u_char *)wbp;
252 1.1 cgd if (state == S_MIDDLE)
253 1.1 cgd goto middle;
254 1.1 cgd state = S_MIDDLE;
255 1.1 cgd
256 1.3 andrew maxmaxcode = 1L << maxbits;
257 1.1 cgd if (fwrite(magic_header,
258 1.1 cgd sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
259 1.1 cgd return (-1);
260 1.3 andrew tmp = (u_char)(maxbits | block_compress);
261 1.1 cgd if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
262 1.1 cgd return (-1);
263 1.1 cgd
264 1.1 cgd offset = 0;
265 1.1 cgd bytes_out = 3; /* Includes 3-byte header mojo. */
266 1.1 cgd out_count = 0;
267 1.1 cgd clear_flg = 0;
268 1.1 cgd ratio = 0;
269 1.1 cgd in_count = 1;
270 1.1 cgd checkpoint = CHECK_GAP;
271 1.1 cgd maxcode = MAXCODE(n_bits = INIT_BITS);
272 1.1 cgd free_ent = ((block_compress) ? FIRST : 256);
273 1.1 cgd
274 1.1 cgd ent = *bp++;
275 1.1 cgd --count;
276 1.1 cgd
277 1.1 cgd hshift = 0;
278 1.1 cgd for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
279 1.1 cgd hshift++;
280 1.1 cgd hshift = 8 - hshift; /* Set hash code range bound. */
281 1.1 cgd
282 1.1 cgd hsize_reg = hsize;
283 1.1 cgd cl_hash(zs, (count_int)hsize_reg); /* Clear hash table. */
284 1.1 cgd
285 1.1 cgd middle: for (i = 0; count--;) {
286 1.1 cgd c = *bp++;
287 1.1 cgd in_count++;
288 1.1 cgd fcode = (long)(((long)c << maxbits) + ent);
289 1.1 cgd i = ((c << hshift) ^ ent); /* Xor hashing. */
290 1.1 cgd
291 1.1 cgd if (htabof(i) == fcode) {
292 1.1 cgd ent = codetabof(i);
293 1.1 cgd continue;
294 1.1 cgd } else if ((long)htabof(i) < 0) /* Empty slot. */
295 1.1 cgd goto nomatch;
296 1.1 cgd disp = hsize_reg - i; /* Secondary hash (after G. Knott). */
297 1.1 cgd if (i == 0)
298 1.1 cgd disp = 1;
299 1.1 cgd probe: if ((i -= disp) < 0)
300 1.1 cgd i += hsize_reg;
301 1.1 cgd
302 1.1 cgd if (htabof(i) == fcode) {
303 1.1 cgd ent = codetabof(i);
304 1.1 cgd continue;
305 1.1 cgd }
306 1.1 cgd if ((long)htabof(i) >= 0)
307 1.1 cgd goto probe;
308 1.1 cgd nomatch: if (output(zs, (code_int) ent) == -1)
309 1.1 cgd return (-1);
310 1.1 cgd out_count++;
311 1.1 cgd ent = c;
312 1.1 cgd if (free_ent < maxmaxcode) {
313 1.1 cgd codetabof(i) = free_ent++; /* code -> hashtable */
314 1.1 cgd htabof(i) = fcode;
315 1.1 cgd } else if ((count_int)in_count >=
316 1.1 cgd checkpoint && block_compress) {
317 1.1 cgd if (cl_block(zs) == -1)
318 1.1 cgd return (-1);
319 1.1 cgd }
320 1.1 cgd }
321 1.1 cgd return (num);
322 1.1 cgd }
323 1.1 cgd
324 1.1 cgd static int
325 1.7 wiz zclose(void *cookie)
326 1.1 cgd {
327 1.1 cgd struct s_zstate *zs;
328 1.1 cgd int rval;
329 1.1 cgd
330 1.1 cgd zs = cookie;
331 1.1 cgd if (zmode == 'w') { /* Put out the final code. */
332 1.1 cgd if (output(zs, (code_int) ent) == -1) {
333 1.1 cgd (void)fclose(fp);
334 1.1 cgd free(zs);
335 1.1 cgd return (-1);
336 1.1 cgd }
337 1.1 cgd out_count++;
338 1.1 cgd if (output(zs, (code_int) - 1) == -1) {
339 1.1 cgd (void)fclose(fp);
340 1.1 cgd free(zs);
341 1.1 cgd return (-1);
342 1.1 cgd }
343 1.1 cgd }
344 1.1 cgd rval = fclose(fp) == EOF ? -1 : 0;
345 1.1 cgd free(zs);
346 1.1 cgd return (rval);
347 1.1 cgd }
348 1.1 cgd
349 1.1 cgd /*-
350 1.1 cgd * Output the given code.
351 1.1 cgd * Inputs:
352 1.1 cgd * code: A n_bits-bit integer. If == -1, then EOF. This assumes
353 1.1 cgd * that n_bits =< (long)wordsize - 1.
354 1.1 cgd * Outputs:
355 1.1 cgd * Outputs code to the file.
356 1.1 cgd * Assumptions:
357 1.1 cgd * Chars are 8 bits long.
358 1.1 cgd * Algorithm:
359 1.1 cgd * Maintain a BITS character long buffer (so that 8 codes will
360 1.1 cgd * fit in it exactly). Use the VAX insv instruction to insert each
361 1.1 cgd * code in turn. When the buffer fills up empty it and start over.
362 1.1 cgd */
363 1.1 cgd
364 1.1 cgd static char_type lmask[9] =
365 1.1 cgd {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
366 1.1 cgd static char_type rmask[9] =
367 1.1 cgd {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
368 1.1 cgd
369 1.1 cgd static int
370 1.7 wiz output(struct s_zstate *zs, code_int ocode)
371 1.1 cgd {
372 1.6 lukem int bits, r_off;
373 1.6 lukem char_type *bp;
374 1.1 cgd
375 1.1 cgd r_off = offset;
376 1.1 cgd bits = n_bits;
377 1.1 cgd bp = buf;
378 1.1 cgd if (ocode >= 0) {
379 1.1 cgd /* Get to the first byte. */
380 1.1 cgd bp += (r_off >> 3);
381 1.1 cgd r_off &= 7;
382 1.1 cgd /*
383 1.1 cgd * Since ocode is always >= 8 bits, only need to mask the first
384 1.1 cgd * hunk on the left.
385 1.1 cgd */
386 1.6 lukem *bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
387 1.1 cgd bp++;
388 1.1 cgd bits -= (8 - r_off);
389 1.1 cgd ocode >>= 8 - r_off;
390 1.1 cgd /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
391 1.1 cgd if (bits >= 8) {
392 1.1 cgd *bp++ = ocode;
393 1.1 cgd ocode >>= 8;
394 1.1 cgd bits -= 8;
395 1.1 cgd }
396 1.1 cgd /* Last bits. */
397 1.1 cgd if (bits)
398 1.1 cgd *bp = ocode;
399 1.1 cgd offset += n_bits;
400 1.1 cgd if (offset == (n_bits << 3)) {
401 1.1 cgd bp = buf;
402 1.1 cgd bits = n_bits;
403 1.1 cgd bytes_out += bits;
404 1.1 cgd if (fwrite(bp, sizeof(char), bits, fp) != bits)
405 1.1 cgd return (-1);
406 1.1 cgd bp += bits;
407 1.1 cgd bits = 0;
408 1.1 cgd offset = 0;
409 1.1 cgd }
410 1.1 cgd /*
411 1.1 cgd * If the next entry is going to be too big for the ocode size,
412 1.1 cgd * then increase it, if possible.
413 1.1 cgd */
414 1.1 cgd if (free_ent > maxcode || (clear_flg > 0)) {
415 1.1 cgd /*
416 1.1 cgd * Write the whole buffer, because the input side won't
417 1.1 cgd * discover the size increase until after it has read it.
418 1.1 cgd */
419 1.1 cgd if (offset > 0) {
420 1.1 cgd if (fwrite(buf, 1, n_bits, fp) != n_bits)
421 1.1 cgd return (-1);
422 1.1 cgd bytes_out += n_bits;
423 1.1 cgd }
424 1.1 cgd offset = 0;
425 1.1 cgd
426 1.1 cgd if (clear_flg) {
427 1.1 cgd maxcode = MAXCODE(n_bits = INIT_BITS);
428 1.1 cgd clear_flg = 0;
429 1.1 cgd } else {
430 1.1 cgd n_bits++;
431 1.1 cgd if (n_bits == maxbits)
432 1.1 cgd maxcode = maxmaxcode;
433 1.1 cgd else
434 1.1 cgd maxcode = MAXCODE(n_bits);
435 1.1 cgd }
436 1.1 cgd }
437 1.1 cgd } else {
438 1.1 cgd /* At EOF, write the rest of the buffer. */
439 1.1 cgd if (offset > 0) {
440 1.1 cgd offset = (offset + 7) / 8;
441 1.1 cgd if (fwrite(buf, 1, offset, fp) != offset)
442 1.1 cgd return (-1);
443 1.1 cgd bytes_out += offset;
444 1.1 cgd }
445 1.1 cgd offset = 0;
446 1.1 cgd }
447 1.1 cgd return (0);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd /*
451 1.1 cgd * Decompress read. This routine adapts to the codes in the file building
452 1.1 cgd * the "string" table on-the-fly; requiring no table to be stored in the
453 1.1 cgd * compressed file. The tables used herein are shared with those of the
454 1.1 cgd * compress() routine. See the definitions above.
455 1.1 cgd */
456 1.1 cgd static int
457 1.7 wiz zread(void *cookie, char *rbp, int num)
458 1.1 cgd {
459 1.6 lukem u_int count;
460 1.1 cgd struct s_zstate *zs;
461 1.1 cgd u_char *bp, header[3];
462 1.1 cgd
463 1.1 cgd if (num == 0)
464 1.1 cgd return (0);
465 1.1 cgd
466 1.1 cgd zs = cookie;
467 1.1 cgd count = num;
468 1.1 cgd bp = (u_char *)rbp;
469 1.1 cgd switch (state) {
470 1.1 cgd case S_START:
471 1.1 cgd state = S_MIDDLE;
472 1.1 cgd break;
473 1.1 cgd case S_MIDDLE:
474 1.1 cgd goto middle;
475 1.1 cgd case S_EOF:
476 1.1 cgd goto eof;
477 1.1 cgd }
478 1.1 cgd
479 1.1 cgd /* Check the magic number */
480 1.1 cgd if (fread(header,
481 1.1 cgd sizeof(char), sizeof(header), fp) != sizeof(header) ||
482 1.1 cgd memcmp(header, magic_header, sizeof(magic_header)) != 0) {
483 1.1 cgd errno = EFTYPE;
484 1.1 cgd return (-1);
485 1.1 cgd }
486 1.1 cgd maxbits = header[2]; /* Set -b from file. */
487 1.1 cgd block_compress = maxbits & BLOCK_MASK;
488 1.1 cgd maxbits &= BIT_MASK;
489 1.1 cgd maxmaxcode = 1L << maxbits;
490 1.1 cgd if (maxbits > BITS) {
491 1.1 cgd errno = EFTYPE;
492 1.1 cgd return (-1);
493 1.1 cgd }
494 1.1 cgd /* As above, initialize the first 256 entries in the table. */
495 1.1 cgd maxcode = MAXCODE(n_bits = INIT_BITS);
496 1.1 cgd for (code = 255; code >= 0; code--) {
497 1.1 cgd tab_prefixof(code) = 0;
498 1.1 cgd tab_suffixof(code) = (char_type) code;
499 1.1 cgd }
500 1.1 cgd free_ent = block_compress ? FIRST : 256;
501 1.1 cgd
502 1.1 cgd finchar = oldcode = getcode(zs);
503 1.1 cgd if (oldcode == -1) /* EOF already? */
504 1.1 cgd return (0); /* Get out of here */
505 1.1 cgd
506 1.1 cgd /* First code must be 8 bits = char. */
507 1.1 cgd *bp++ = (u_char)finchar;
508 1.1 cgd count--;
509 1.1 cgd stackp = de_stack;
510 1.1 cgd
511 1.1 cgd while ((code = getcode(zs)) > -1) {
512 1.1 cgd
513 1.1 cgd if ((code == CLEAR) && block_compress) {
514 1.1 cgd for (code = 255; code >= 0; code--)
515 1.1 cgd tab_prefixof(code) = 0;
516 1.1 cgd clear_flg = 1;
517 1.1 cgd free_ent = FIRST - 1;
518 1.1 cgd if ((code = getcode(zs)) == -1) /* O, untimely death! */
519 1.1 cgd break;
520 1.1 cgd }
521 1.1 cgd incode = code;
522 1.1 cgd
523 1.1 cgd /* Special case for KwKwK string. */
524 1.1 cgd if (code >= free_ent) {
525 1.1 cgd *stackp++ = finchar;
526 1.1 cgd code = oldcode;
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd /* Generate output characters in reverse order. */
530 1.1 cgd while (code >= 256) {
531 1.1 cgd *stackp++ = tab_suffixof(code);
532 1.1 cgd code = tab_prefixof(code);
533 1.1 cgd }
534 1.1 cgd *stackp++ = finchar = tab_suffixof(code);
535 1.1 cgd
536 1.1 cgd /* And put them out in forward order. */
537 1.1 cgd middle: do {
538 1.1 cgd if (count-- == 0)
539 1.1 cgd return (num);
540 1.1 cgd *bp++ = *--stackp;
541 1.1 cgd } while (stackp > de_stack);
542 1.1 cgd
543 1.1 cgd /* Generate the new entry. */
544 1.1 cgd if ((code = free_ent) < maxmaxcode) {
545 1.1 cgd tab_prefixof(code) = (u_short) oldcode;
546 1.1 cgd tab_suffixof(code) = finchar;
547 1.1 cgd free_ent = code + 1;
548 1.1 cgd }
549 1.1 cgd
550 1.1 cgd /* Remember previous code. */
551 1.1 cgd oldcode = incode;
552 1.1 cgd }
553 1.1 cgd state = S_EOF;
554 1.1 cgd eof: return (num - count);
555 1.1 cgd }
556 1.1 cgd
557 1.1 cgd /*-
558 1.1 cgd * Read one code from the standard input. If EOF, return -1.
559 1.1 cgd * Inputs:
560 1.1 cgd * stdin
561 1.1 cgd * Outputs:
562 1.1 cgd * code or -1 is returned.
563 1.1 cgd */
564 1.1 cgd static code_int
565 1.7 wiz getcode(struct s_zstate *zs)
566 1.1 cgd {
567 1.6 lukem code_int gcode;
568 1.6 lukem int r_off, bits;
569 1.6 lukem char_type *bp;
570 1.1 cgd
571 1.1 cgd bp = gbuf;
572 1.1 cgd if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {
573 1.1 cgd /*
574 1.1 cgd * If the next entry will be too big for the current gcode
575 1.1 cgd * size, then we must increase the size. This implies reading
576 1.1 cgd * a new buffer full, too.
577 1.1 cgd */
578 1.1 cgd if (free_ent > maxcode) {
579 1.1 cgd n_bits++;
580 1.1 cgd if (n_bits == maxbits) /* Won't get any bigger now. */
581 1.1 cgd maxcode = maxmaxcode;
582 1.1 cgd else
583 1.1 cgd maxcode = MAXCODE(n_bits);
584 1.1 cgd }
585 1.1 cgd if (clear_flg > 0) {
586 1.1 cgd maxcode = MAXCODE(n_bits = INIT_BITS);
587 1.1 cgd clear_flg = 0;
588 1.1 cgd }
589 1.1 cgd size = fread(gbuf, 1, n_bits, fp);
590 1.1 cgd if (size <= 0) /* End of file. */
591 1.1 cgd return (-1);
592 1.1 cgd roffset = 0;
593 1.1 cgd /* Round size down to integral number of codes. */
594 1.1 cgd size = (size << 3) - (n_bits - 1);
595 1.1 cgd }
596 1.1 cgd r_off = roffset;
597 1.1 cgd bits = n_bits;
598 1.1 cgd
599 1.1 cgd /* Get to the first byte. */
600 1.1 cgd bp += (r_off >> 3);
601 1.1 cgd r_off &= 7;
602 1.1 cgd
603 1.1 cgd /* Get first part (low order bits). */
604 1.1 cgd gcode = (*bp++ >> r_off);
605 1.1 cgd bits -= (8 - r_off);
606 1.1 cgd r_off = 8 - r_off; /* Now, roffset into gcode word. */
607 1.1 cgd
608 1.1 cgd /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
609 1.1 cgd if (bits >= 8) {
610 1.1 cgd gcode |= *bp++ << r_off;
611 1.1 cgd r_off += 8;
612 1.1 cgd bits -= 8;
613 1.1 cgd }
614 1.1 cgd
615 1.1 cgd /* High order bits. */
616 1.1 cgd gcode |= (*bp & rmask[bits]) << r_off;
617 1.1 cgd roffset += n_bits;
618 1.1 cgd
619 1.1 cgd return (gcode);
620 1.1 cgd }
621 1.1 cgd
622 1.1 cgd static int
623 1.7 wiz cl_block(struct s_zstate *zs) /* Table clear for block compress. */
624 1.1 cgd {
625 1.6 lukem long rat;
626 1.1 cgd
627 1.1 cgd checkpoint = in_count + CHECK_GAP;
628 1.1 cgd
629 1.1 cgd if (in_count > 0x007fffff) { /* Shift will overflow. */
630 1.1 cgd rat = bytes_out >> 8;
631 1.1 cgd if (rat == 0) /* Don't divide by zero. */
632 1.1 cgd rat = 0x7fffffff;
633 1.1 cgd else
634 1.1 cgd rat = in_count / rat;
635 1.1 cgd } else
636 1.1 cgd rat = (in_count << 8) / bytes_out; /* 8 fractional bits. */
637 1.1 cgd if (rat > ratio)
638 1.1 cgd ratio = rat;
639 1.1 cgd else {
640 1.1 cgd ratio = 0;
641 1.1 cgd cl_hash(zs, (count_int) hsize);
642 1.1 cgd free_ent = FIRST;
643 1.1 cgd clear_flg = 1;
644 1.1 cgd if (output(zs, (code_int) CLEAR) == -1)
645 1.1 cgd return (-1);
646 1.1 cgd }
647 1.1 cgd return (0);
648 1.1 cgd }
649 1.1 cgd
650 1.1 cgd static void
651 1.7 wiz cl_hash(struct s_zstate *zs, count_int cl_hsize) /* Reset code table. */
652 1.1 cgd {
653 1.6 lukem count_int *htab_p;
654 1.6 lukem long i, m1;
655 1.1 cgd
656 1.1 cgd m1 = -1;
657 1.1 cgd htab_p = htab + cl_hsize;
658 1.1 cgd i = cl_hsize - 16;
659 1.1 cgd do { /* Might use Sys V memset(3) here. */
660 1.1 cgd *(htab_p - 16) = m1;
661 1.1 cgd *(htab_p - 15) = m1;
662 1.1 cgd *(htab_p - 14) = m1;
663 1.1 cgd *(htab_p - 13) = m1;
664 1.1 cgd *(htab_p - 12) = m1;
665 1.1 cgd *(htab_p - 11) = m1;
666 1.1 cgd *(htab_p - 10) = m1;
667 1.1 cgd *(htab_p - 9) = m1;
668 1.1 cgd *(htab_p - 8) = m1;
669 1.1 cgd *(htab_p - 7) = m1;
670 1.1 cgd *(htab_p - 6) = m1;
671 1.1 cgd *(htab_p - 5) = m1;
672 1.1 cgd *(htab_p - 4) = m1;
673 1.1 cgd *(htab_p - 3) = m1;
674 1.1 cgd *(htab_p - 2) = m1;
675 1.1 cgd *(htab_p - 1) = m1;
676 1.1 cgd htab_p -= 16;
677 1.1 cgd } while ((i -= 16) >= 0);
678 1.1 cgd for (i += 16; i > 0; i--)
679 1.1 cgd *--htab_p = m1;
680 1.1 cgd }
681 1.1 cgd
682 1.1 cgd FILE *
683 1.7 wiz zopen(const char *fname, const char *mode, int bits)
684 1.1 cgd {
685 1.1 cgd struct s_zstate *zs;
686 1.1 cgd
687 1.6 lukem if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
688 1.1 cgd bits < 0 || bits > BITS) {
689 1.1 cgd errno = EINVAL;
690 1.1 cgd return (NULL);
691 1.1 cgd }
692 1.1 cgd
693 1.1 cgd if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
694 1.1 cgd return (NULL);
695 1.1 cgd
696 1.1 cgd maxbits = bits ? bits : BITS; /* User settable max # bits/code. */
697 1.3 andrew maxmaxcode = 1 << maxbits; /* Should NEVER generate this code. */
698 1.1 cgd hsize = HSIZE; /* For dynamic table sizing. */
699 1.1 cgd free_ent = 0; /* First unused entry. */
700 1.1 cgd block_compress = BLOCK_MASK;
701 1.1 cgd clear_flg = 0;
702 1.1 cgd ratio = 0;
703 1.1 cgd checkpoint = CHECK_GAP;
704 1.1 cgd in_count = 1; /* Length of input. */
705 1.1 cgd out_count = 0; /* # of codes output (for debugging). */
706 1.1 cgd state = S_START;
707 1.1 cgd roffset = 0;
708 1.1 cgd size = 0;
709 1.1 cgd
710 1.1 cgd /*
711 1.1 cgd * Layering compress on top of stdio in order to provide buffering,
712 1.1 cgd * and ensure that reads and write work with the data specified.
713 1.1 cgd */
714 1.1 cgd if ((fp = fopen(fname, mode)) == NULL) {
715 1.1 cgd free(zs);
716 1.1 cgd return (NULL);
717 1.1 cgd }
718 1.1 cgd switch (*mode) {
719 1.1 cgd case 'r':
720 1.1 cgd zmode = 'r';
721 1.1 cgd return (funopen(zs, zread, NULL, NULL, zclose));
722 1.1 cgd case 'w':
723 1.1 cgd zmode = 'w';
724 1.1 cgd return (funopen(zs, NULL, zwrite, NULL, zclose));
725 1.1 cgd }
726 1.1 cgd /* NOTREACHED */
727 1.6 lukem return (NULL);
728 1.1 cgd }
729