2 * Copyright 1985, 1986 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * James A. Woods, derived from original work by Spencer Thomas
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by the University of California, Berkeley. The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 Copyright 1993, 1998 The Open Group
26 Permission to use, copy, modify, distribute, and sell this software and its
27 documentation for any purpose is hereby granted without fee, provided that
28 the above copyright notice appear in all copies and that both that
29 copyright notice and this permission notice appear in supporting
32 The above copyright notice and this permission notice shall be included in
33 all copies or substantial portions of the Software.
35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
39 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 Except as contained in this notice, the name of The Open Group shall not be
43 used in advertising or otherwise to promote the sale, use or other dealings
44 in this Software without prior written authorization from The Open Group.
48 * decompress - cat a compressed file
54 #include <X11/fonts/fontmisc.h>
55 #include <X11/fonts/bufio.h>
60 * a code_int must be able to hold 2**BITS values of type int, and also -1
63 typedef long int code_int;
68 typedef long int count_int;
71 typedef char char_type;
73 typedef unsigned char char_type;
76 static char_type magic_header[] = { "\037\235" }; /* 1F 9D */
78 /* Defines for third byte of header */
80 #define BLOCK_MASK 0x80
81 /* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
82 a fourth header byte (for expansion).
85 #define INIT_BITS 9 /* initial number of bits/code */
87 #ifdef COMPATIBLE /* But wrong! */
88 # define MAXCODE(n_bits) (1 << (n_bits) - 1)
90 # define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
91 #endif /* COMPATIBLE */
94 * the next two codes should not be changed lightly, as they must not
95 * lie within the contiguous general code space.
97 #define FIRST 257 /* first free entry */
98 #define CLEAR 256 /* table clear output code */
100 #define STACK_SIZE 65300
102 typedef struct _compressedFILE {
111 code_int maxcode, maxmaxcode;
121 char_type de_stack[STACK_SIZE];
122 char_type *tab_suffix;
123 unsigned short *tab_prefix;
127 static int BufCompressedClose ( BufFilePtr f, int doClose );
128 static int BufCompressedFill ( BufFilePtr f );
129 static code_int getcode ( CompressedFile *file );
130 static int BufCompressedSkip ( BufFilePtr f, int bytes );
133 BufFilePushCompressed (BufFilePtr f)
137 CompressedFile *file;
140 if ((BufFileGet(f) != (magic_header[0] & 0xFF)) ||
141 (BufFileGet(f) != (magic_header[1] & 0xFF)))
145 code = BufFileGet (f);
146 if (code == BUFFILEEOF) return 0;
148 maxbits = code & BIT_MASK;
149 if (maxbits > BITS || maxbits <= INIT_BITS)
151 extra = (1 << maxbits) * sizeof (char_type) +
152 (1 << maxbits) * sizeof (unsigned short);
153 file = malloc (sizeof (CompressedFile) + extra);
157 file->maxbits = maxbits;
158 file->block_compress = code & BLOCK_MASK;
159 file->maxmaxcode = 1 << file->maxbits;
160 file->tab_suffix = (char_type *) &file[1];
161 file->tab_prefix = (unsigned short *) (file->tab_suffix + file->maxmaxcode);
163 * As above, initialize the first 256 entries in the table.
165 file->maxcode = MAXCODE(file->n_bits = INIT_BITS);
166 for ( code = 255; code >= 0; code-- ) {
167 file->tab_prefix[code] = 0;
168 file->tab_suffix[code] = (char_type) code;
170 file->free_ent = ((file->block_compress) ? FIRST : 256 );
175 file->stackp = file->de_stack;
176 bzero(file->buf, BITS);
177 return BufFileCreate ((char *) file,
185 BufCompressedClose (BufFilePtr f, int doClose)
187 CompressedFile *file;
190 file = (CompressedFile *) f->private;
193 BufFileClose (raw, doClose);
198 BufCompressedFill (BufFilePtr f)
200 CompressedFile *file;
201 register char_type *stackp, *de_stack;
202 register char_type finchar;
203 register code_int code, oldcode, incode;
204 BufChar *buf, *bufend;
206 file = (CompressedFile *) f->private;
209 bufend = buf + BUFFILESIZE;
210 stackp = file->stackp;
211 de_stack = file->de_stack;
212 finchar = file->finchar;
213 oldcode = file->oldcode;
214 while (buf < bufend) {
215 while (stackp > de_stack && buf < bufend)
221 code = getcode (file);
225 if ( (code == CLEAR) && file->block_compress ) {
226 for ( code = 255; code >= 0; code-- )
227 file->tab_prefix[code] = 0;
229 file->free_ent = FIRST;
235 * Special case for KwKwK string.
237 if ( code >= file->free_ent ) {
238 if ( code > file->free_ent || oldcode == -1 ) {
246 * The above condition ensures that code < free_ent.
247 * The construction of tab_prefixof in turn guarantees that
248 * each iteration decreases code and therefore stack usage is
249 * bound by 1 << BITS - 256.
253 * Generate output characters in reverse order
255 while ( code >= 256 )
257 *stackp++ = file->tab_suffix[code];
258 code = file->tab_prefix[code];
260 finchar = file->tab_suffix[code];
264 * Generate the new entry.
266 if ( (code=file->free_ent) < file->maxmaxcode && oldcode != -1) {
267 file->tab_prefix[code] = (unsigned short)oldcode;
268 file->tab_suffix[code] = finchar;
269 file->free_ent = code+1;
272 * Remember previous code.
276 file->oldcode = oldcode;
277 file->stackp = stackp;
278 file->finchar = finchar;
279 if (buf == f->buffer) {
283 f->bufp = f->buffer + 1;
284 f->left = (buf - f->buffer) - 1;
288 /*****************************************************************
291 * Read one code from the standard input. If BUFFILEEOF, return -1.
295 * code or -1 is returned.
298 static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
301 getcode(CompressedFile *file)
303 register code_int code;
304 register int r_off, bits;
305 register char_type *bp = file->buf;
306 register BufFilePtr raw;
308 if ( file->clear_flg > 0 || file->offset >= file->size ||
309 file->free_ent > file->maxcode )
312 * If the next entry will be too big for the current code
313 * size, then we must increase the size. This implies reading
314 * a new buffer full, too.
316 if ( file->free_ent > file->maxcode ) {
318 if ( file->n_bits == file->maxbits )
319 file->maxcode = file->maxmaxcode; /* won't get any bigger now */
321 file->maxcode = MAXCODE(file->n_bits);
323 if ( file->clear_flg > 0) {
324 file->maxcode = MAXCODE (file->n_bits = INIT_BITS);
329 while (bits > 0 && (code = BufFileGet (raw)) != BUFFILEEOF)
335 if (bits == file->n_bits)
336 return -1; /* end of file */
337 file->size = file->n_bits - bits;
339 /* Round size down to integral number of codes */
340 file->size = (file->size << 3) - (file->n_bits - 1);
342 r_off = file->offset;
345 * Get to the first byte.
349 /* Get first part (low order bits) */
351 code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
353 code = (*bp++ >> r_off);
354 #endif /* NO_UCHAR */
356 r_off = 8 - r_off; /* now, offset into code word */
357 /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
360 code |= (*bp++ & 0xff) << r_off;
362 code |= *bp++ << r_off;
363 #endif /* NO_UCHAR */
367 /* high order bits. */
368 code |= (*bp & rmask[bits]) << r_off;
369 file->offset += file->n_bits;
375 BufCompressedSkip (BufFilePtr f, int bytes)
389 main (int argc, char *argv[])
391 BufFilePtr inputraw, input, output;
394 inputraw = BufFileOpenRead (0);
395 input = BufFilePushCompressed (inputraw);
396 output = BufFileOpenWrite (1);
397 while ((c = BufFileGet (input)) != BUFFILEEOF)
398 BufFilePut (c, output);
399 BufFileClose (input, FALSE);
400 BufFileClose (output, FALSE);