2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: decompress.c /main/4 1995/10/26 12:36:44 rswiston $ */
25 * decompress - cat a compressed file
27 * (c) Copyright 1993, 1994 Hewlett-Packard Company
28 * (c) Copyright 1993, 1994 International Business Machines Corp.
29 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
30 * (c) Copyright 1993, 1994 Novell, Inc.
33 /* #include "fontmisc.h" */
44 * a code_int must be able to hold 2**BITS values of type int, and also -1
47 typedef long int code_int;
52 typedef long int count_int;
55 typedef char char_type;
57 typedef unsigned char char_type;
60 static int magic_header[] = { 0x1F, 0x9D };
62 /* Defines for third byte of header */
64 #define BLOCK_MASK 0x80
65 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
66 a fourth header byte (for expansion).
69 #define INIT_BITS 9 /* initial number of bits/code */
71 #ifdef COMPATIBLE /* But wrong! */
72 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
74 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
75 #endif /* COMPATIBLE */
77 static code_int getcode();
80 * the next two codes should not be changed lightly, as they must not
81 * lie within the contiguous general code space.
83 #define FIRST 257 /* first free entry */
84 #define CLEAR 256 /* table clear output code */
86 #define STACK_SIZE 8192
88 typedef struct _compressedFILE {
97 code_int maxcode, maxmaxcode;
107 char_type de_stack[STACK_SIZE];
108 char_type *tab_suffix;
109 unsigned short *tab_prefix;
113 static int hsize_table[] = {
114 5003, /* 12 bits - 80% occupancy */
115 9001, /* 13 bits - 91% occupancy */
116 18013, /* 14 bits - 91% occupancy */
117 35023, /* 15 bits - 94% occupancy */
118 69001 /* 16 bits - 95% occupancy */
121 static int BufCompressedFill(), BufCompressedSkip(), BufCompressedClose();
124 _DtHelpCeBufFilePushZ (BufFilePtr f)
129 CompressedFile *file;
132 if ((BufFileGet(f) != magic_header[0]) ||
133 (BufFileGet(f) != magic_header[1]))
137 code = BufFileGet (f);
138 maxbits = code & BIT_MASK;
139 if (maxbits > BITS || maxbits < 12)
141 hsize = hsize_table[maxbits - 12];
142 extra = (1 << maxbits) * sizeof (char_type) +
143 hsize * sizeof (unsigned short);
144 file = (CompressedFile *) malloc (sizeof (CompressedFile) + extra);
148 file->maxbits = maxbits;
149 file->block_compress = code & BLOCK_MASK;
150 file->maxmaxcode = 1 << file->maxbits;
151 file->tab_suffix = (char_type *) &file[1];
152 file->tab_prefix = (unsigned short *) (file->tab_suffix + file->maxmaxcode);
154 * As above, initialize the first 256 entries in the table.
156 file->maxcode = MAXCODE(file->n_bits = INIT_BITS);
157 for ( code = 255; code >= 0; code-- ) {
158 file->tab_prefix[code] = 0;
159 file->tab_suffix[code] = (char_type) code;
161 file->free_ent = ((file->block_compress) ? FIRST : 256 );
165 file->stackp = file->de_stack;
166 file->finchar = file->oldcode = getcode (file);
167 if (file->oldcode != -1)
168 *file->stackp++ = file->finchar;
169 return _DtHelpCeBufFileCreate ((char *) file,
180 CompressedFile *file;
183 file = (CompressedFile *) f->hidden;
186 _DtHelpCeBufFileClose (raw, doClose);
191 BufCompressedFill (BufFilePtr f)
193 CompressedFile *file;
194 register char_type *stackp, *de_stack;
195 register char_type finchar;
196 register code_int code, oldcode, incode;
197 BufChar *buf, *bufend;
199 file = (CompressedFile *) f->hidden;
202 bufend = buf + BUFFILESIZE;
203 stackp = file->stackp;
204 de_stack = file->de_stack;
205 finchar = file->finchar;
206 oldcode = file->oldcode;
207 while (buf < bufend) {
208 while (stackp > de_stack && buf < bufend)
217 code = getcode (file);
221 if ( (code == CLEAR) && file->block_compress ) {
222 for ( code = 255; code >= 0; code-- )
223 file->tab_prefix[code] = 0;
225 file->free_ent = FIRST - 1;
226 if ( (code = getcode (file)) == -1 ) /* O, untimely death! */
231 * Special case for KwKwK string.
233 if ( code >= file->free_ent ) {
239 * Generate output characters in reverse order
241 while ( code >= 256 )
243 *stackp++ = file->tab_suffix[code];
244 code = file->tab_prefix[code];
246 finchar = file->tab_suffix[code];
250 * Generate the new entry.
252 if ( (code=file->free_ent) < file->maxmaxcode ) {
253 file->tab_prefix[code] = (unsigned short)oldcode;
254 file->tab_suffix[code] = finchar;
255 file->free_ent = code+1;
258 * Remember previous code.
262 file->oldcode = oldcode;
263 file->stackp = stackp;
264 file->finchar = finchar;
265 if (buf == f->buffer) {
269 f->bufp = f->buffer + 1;
270 f->left = (buf - f->buffer) - 1;
274 /*****************************************************************
277 * Read one code from the standard input. If BUFFILEEOF, return -1.
281 * code or -1 is returned.
284 static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
287 getcode(CompressedFile *file)
289 register code_int code;
290 register int r_off, bits;
291 register char_type *bp = file->buf;
292 register BufFilePtr raw;
294 if ( file->clear_flg > 0 || file->offset >= file->size ||
295 file->free_ent > file->maxcode )
298 * If the next entry will be too big for the current code
299 * size, then we must increase the size. This implies reading
300 * a new buffer full, too.
302 if ( file->free_ent > file->maxcode ) {
304 if ( file->n_bits == file->maxbits )
305 file->maxcode = file->maxmaxcode; /* won't get any bigger now */
307 file->maxcode = MAXCODE(file->n_bits);
309 if ( file->clear_flg > 0) {
310 file->maxcode = MAXCODE (file->n_bits = INIT_BITS);
315 while (bits > 0 && (code = BufFileGet (raw)) != BUFFILEEOF)
321 if (bits == file->n_bits)
322 return -1; /* end of file */
323 file->size = file->n_bits - bits;
325 /* Round size down to integral number of codes */
326 file->size = (file->size << 3) - (file->n_bits - 1);
328 r_off = file->offset;
331 * Get to the first byte.
335 /* Get first part (low order bits) */
337 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
339 code = (*bp++ >> r_off);
340 #endif /* NO_UCHAR */
342 r_off = 8 - r_off; /* now, offset into code word */
343 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
346 code |= (*bp++ & 0xff) << r_off;
348 code |= *bp++ << r_off;
349 #endif /* NO_UCHAR */
353 /* high order bits. */
354 code |= (*bp & rmask[bits]) << r_off;
355 file->offset += file->n_bits;
366 while (bytes-- && ((c = BufFileGet(f)) != BUFFILEEOF))
372 _DtHelpCeUncompressFile(
376 BufFilePtr inputraw, input, output;
380 CECompressInfoPtr myInfo;
383 inFd = open(infile, O_RDONLY);
384 if (inFd < 0) return -1;
386 if (fstat(inFd, &statBuf) < 0)
392 outFd = open(outfile, O_CREAT | O_WRONLY, 0666);
399 myInfo = (CECompressInfoPtr) malloc (sizeof(CECompressInfo));
408 myInfo->size = statBuf.st_size;
410 inputraw = _DtHelpCeBufFileRdRawZ (myInfo);
411 input = _DtHelpCeBufFilePushZ (inputraw);
412 output = _DtHelpCeBufFileOpenWr (outFd);
413 while ((c = BufFileGet (input)) != -1)
414 BufFilePut (c, output);
418 _DtHelpCeBufFileClose (input, TRUE);
419 _DtHelpCeBufFileClose (output, TRUE);
421 if (c != 0) /* didn't consume all the bytes in the file */