1 /* $XConsortium: decompress.c /main/4 1995/10/26 12:36:44 rswiston $ */
3 * decompress - cat a compressed file
5 * (c) Copyright 1993, 1994 Hewlett-Packard Company
6 * (c) Copyright 1993, 1994 International Business Machines Corp.
7 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
8 * (c) Copyright 1993, 1994 Novell, Inc.
11 /* #include "fontmisc.h" */
21 * a code_int must be able to hold 2**BITS values of type int, and also -1
24 typedef long int code_int;
29 typedef long int count_int;
32 typedef char char_type;
34 typedef unsigned char char_type;
37 static char_type magic_header[] = { "\037\235" }; /* 1F 9D */
39 /* Defines for third byte of header */
41 #define BLOCK_MASK 0x80
42 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
43 a fourth header byte (for expansion).
46 #define INIT_BITS 9 /* initial number of bits/code */
48 #ifdef COMPATIBLE /* But wrong! */
49 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
51 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
52 #endif /* COMPATIBLE */
54 static code_int getcode();
57 * the next two codes should not be changed lightly, as they must not
58 * lie within the contiguous general code space.
60 #define FIRST 257 /* first free entry */
61 #define CLEAR 256 /* table clear output code */
63 #define STACK_SIZE 8192
65 typedef struct _compressedFILE {
74 code_int maxcode, maxmaxcode;
84 char_type de_stack[STACK_SIZE];
85 char_type *tab_suffix;
86 unsigned short *tab_prefix;
90 static int hsize_table[] = {
91 5003, /* 12 bits - 80% occupancy */
92 9001, /* 13 bits - 91% occupancy */
93 18013, /* 14 bits - 91% occupancy */
94 35023, /* 15 bits - 94% occupancy */
95 69001 /* 16 bits - 95% occupancy */
98 static int BufCompressedFill(), BufCompressedSkip(), BufCompressedClose();
101 _DtHelpCeBufFilePushZ (BufFilePtr f)
106 CompressedFile *file;
109 if ((BufFileGet(f) != (magic_header[0] & 0xFF)) ||
110 (BufFileGet(f) != (magic_header[1] & 0xFF)))
114 code = BufFileGet (f);
115 maxbits = code & BIT_MASK;
116 if (maxbits > BITS || maxbits < 12)
118 hsize = hsize_table[maxbits - 12];
119 extra = (1 << maxbits) * sizeof (char_type) +
120 hsize * sizeof (unsigned short);
121 file = (CompressedFile *) malloc (sizeof (CompressedFile) + extra);
125 file->maxbits = maxbits;
126 file->block_compress = code & BLOCK_MASK;
127 file->maxmaxcode = 1 << file->maxbits;
128 file->tab_suffix = (char_type *) &file[1];
129 file->tab_prefix = (unsigned short *) (file->tab_suffix + file->maxmaxcode);
131 * As above, initialize the first 256 entries in the table.
133 file->maxcode = MAXCODE(file->n_bits = INIT_BITS);
134 for ( code = 255; code >= 0; code-- ) {
135 file->tab_prefix[code] = 0;
136 file->tab_suffix[code] = (char_type) code;
138 file->free_ent = ((file->block_compress) ? FIRST : 256 );
142 file->stackp = file->de_stack;
143 file->finchar = file->oldcode = getcode (file);
144 if (file->oldcode != -1)
145 *file->stackp++ = file->finchar;
146 return _DtHelpCeBufFileCreate ((char *) file,
157 CompressedFile *file;
160 file = (CompressedFile *) f->hidden;
163 _DtHelpCeBufFileClose (raw, doClose);
168 BufCompressedFill (BufFilePtr f)
170 CompressedFile *file;
171 register char_type *stackp, *de_stack;
172 register char_type finchar;
173 register code_int code, oldcode, incode;
174 BufChar *buf, *bufend;
176 file = (CompressedFile *) f->hidden;
179 bufend = buf + BUFFILESIZE;
180 stackp = file->stackp;
181 de_stack = file->de_stack;
182 finchar = file->finchar;
183 oldcode = file->oldcode;
184 while (buf < bufend) {
185 while (stackp > de_stack && buf < bufend)
194 code = getcode (file);
198 if ( (code == CLEAR) && file->block_compress ) {
199 for ( code = 255; code >= 0; code-- )
200 file->tab_prefix[code] = 0;
202 file->free_ent = FIRST - 1;
203 if ( (code = getcode (file)) == -1 ) /* O, untimely death! */
208 * Special case for KwKwK string.
210 if ( code >= file->free_ent ) {
216 * Generate output characters in reverse order
218 while ( code >= 256 )
220 *stackp++ = file->tab_suffix[code];
221 code = file->tab_prefix[code];
223 finchar = file->tab_suffix[code];
227 * Generate the new entry.
229 if ( (code=file->free_ent) < file->maxmaxcode ) {
230 file->tab_prefix[code] = (unsigned short)oldcode;
231 file->tab_suffix[code] = finchar;
232 file->free_ent = code+1;
235 * Remember previous code.
239 file->oldcode = oldcode;
240 file->stackp = stackp;
241 file->finchar = finchar;
242 if (buf == f->buffer) {
246 f->bufp = f->buffer + 1;
247 f->left = (buf - f->buffer) - 1;
251 /*****************************************************************
254 * Read one code from the standard input. If BUFFILEEOF, return -1.
258 * code or -1 is returned.
261 static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
264 getcode(CompressedFile *file)
266 register code_int code;
267 register int r_off, bits;
268 register char_type *bp = file->buf;
269 register BufFilePtr raw;
271 if ( file->clear_flg > 0 || file->offset >= file->size ||
272 file->free_ent > file->maxcode )
275 * If the next entry will be too big for the current code
276 * size, then we must increase the size. This implies reading
277 * a new buffer full, too.
279 if ( file->free_ent > file->maxcode ) {
281 if ( file->n_bits == file->maxbits )
282 file->maxcode = file->maxmaxcode; /* won't get any bigger now */
284 file->maxcode = MAXCODE(file->n_bits);
286 if ( file->clear_flg > 0) {
287 file->maxcode = MAXCODE (file->n_bits = INIT_BITS);
292 while (bits > 0 && (code = BufFileGet (raw)) != BUFFILEEOF)
298 if (bits == file->n_bits)
299 return -1; /* end of file */
300 file->size = file->n_bits - bits;
302 /* Round size down to integral number of codes */
303 file->size = (file->size << 3) - (file->n_bits - 1);
305 r_off = file->offset;
308 * Get to the first byte.
312 /* Get first part (low order bits) */
314 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
316 code = (*bp++ >> r_off);
317 #endif /* NO_UCHAR */
319 r_off = 8 - r_off; /* now, offset into code word */
320 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
323 code |= (*bp++ & 0xff) << r_off;
325 code |= *bp++ << r_off;
326 #endif /* NO_UCHAR */
330 /* high order bits. */
331 code |= (*bp & rmask[bits]) << r_off;
332 file->offset += file->n_bits;
343 while (bytes-- && ((c = BufFileGet(f)) != BUFFILEEOF))
349 _DtHelpCeUncompressFile(
353 BufFilePtr inputraw, input, output;
357 CECompressInfoPtr myInfo;
360 inFd = open(infile, O_RDONLY);
361 if (inFd < 0) return -1;
363 if (fstat(inFd, &statBuf) < 0)
369 outFd = open(outfile, O_CREAT | O_WRONLY, 0666);
376 myInfo = (CECompressInfoPtr) malloc (sizeof(CECompressInfo));
385 myInfo->size = statBuf.st_size;
387 inputraw = _DtHelpCeBufFileRdRawZ (myInfo);
388 input = _DtHelpCeBufFilePushZ (inputraw);
389 output = _DtHelpCeBufFileOpenWr (outFd);
390 while ((c = BufFileGet (input)) != -1)
391 BufFilePut (c, output);
395 _DtHelpCeBufFileClose (input, TRUE);
396 _DtHelpCeBufFileClose (output, TRUE);
398 if (c != 0) /* didn't consume all the bytes in the file */