1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/* $TOG: decompress.c /main/6 1998/02/09 15:12:01 kaleb $ */
/*
Copyright 1990, 1998 The Open Group
All Rights Reserved.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/
/* $XFree86: xc/programs/Xserver/os/decompress.c,v 1.0tsi Exp $ */
/*
* decompress - cat a compressed file
*/
#include <stdio.h>
#ifdef TEST
#define xalloc(s) malloc(s)
#define xfree(s) free(s)
typedef char *FID;
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#else
#include "Xos.h"
#include "misc.h"
#endif
#undef BITS
#define BITS 16
/*
* a code_int must be able to hold 2**BITS values of type int, and also -1
*/
#if BITS > 15
typedef long int code_int;
#else
typedef int code_int;
#endif
typedef long int count_int;
#ifdef NO_UCHAR
typedef char char_type;
#else
typedef unsigned char char_type;
#endif /* UCHAR */
static char_type magic_header[] = { "\037\235" }; /* 1F 9D */
/* Defines for third byte of header */
#define BIT_MASK 0x1f
#define BLOCK_MASK 0x80
/* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
a fourth header byte (for expansion).
*/
#define INIT_BITS 9 /* initial number of bits/code */
#ifdef COMPATIBLE /* But wrong! */
# define MAXCODE(n_bits) ((code_int)1 << (n_bits) - 1)
#else
# define MAXCODE(n_bits) (((code_int)1 << (n_bits)) - 1)
#endif /* COMPATIBLE */
static code_int getcode();
/*
* the next two codes should not be changed lightly, as they must not
* lie within the contiguous general code space.
*/
#define FIRST 257 /* first free entry */
#define CLEAR 256 /* table clear output code */
#define STACK_SIZE 8192
typedef struct _compressedFILE {
FILE *file;
char_type *stackp;
code_int oldcode;
char_type finchar;
int block_compress;
int maxbits;
code_int maxcode, maxmaxcode;
code_int free_ent;
int clear_flg;
int n_bits;
/* bit buffer */
int offset, size;
char_type buf[BITS];
char_type de_stack[STACK_SIZE];
char_type *tab_suffix;
unsigned short *tab_prefix;
} CompressedFile;
static int hsize_table[] = {
5003, /* 12 bits - 80% occupancy */
9001, /* 13 bits - 91% occupancy */
18013, /* 14 bits - 91% occupancy */
35023, /* 15 bits - 94% occupancy */
69001 /* 16 bits - 95% occupancy */
};
FID
CompressedFontFileInit (f)
FILE *f;
{
int code;
int maxbits;
int hsize;
CompressedFile *file;
int extra;
if ((getc(f) != (magic_header[0] & 0xFF)) ||
(getc(f) != (magic_header[1] & 0xFF)))
{
return 0;
}
code = getc (f);
maxbits = code & BIT_MASK;
if (maxbits > BITS || maxbits < 12)
return 0;
hsize = hsize_table[maxbits - 12];
extra = (1 << maxbits) * sizeof (char_type) +
hsize * sizeof (unsigned short);
file = (CompressedFile *) xalloc (sizeof (CompressedFile) + extra);
if (!file)
return 0;
file->file = f;
file->maxbits = maxbits;
file->block_compress = code & BLOCK_MASK;
file->maxmaxcode = (code_int)1 << file->maxbits;
file->tab_suffix = (char_type *) &file[1];
file->tab_prefix = (unsigned short *) (file->tab_suffix + file->maxmaxcode);
/*
* As above, initialize the first 256 entries in the table.
*/
file->maxcode = MAXCODE(file->n_bits = INIT_BITS);
for ( code = 255; code >= 0; code-- ) {
file->tab_prefix[code] = 0;
file->tab_suffix[code] = (char_type) code;
}
file->free_ent = ((file->block_compress) ? FIRST : 256 );
file->clear_flg = 0;
file->offset = 0;
file->size = 0;
file->stackp = file->de_stack;
file->finchar = file->oldcode = getcode (file);
if (file->oldcode != -1)
*file->stackp++ = file->finchar;
return (FID) file;
}
FILE *
CompressedFontFileDone (fid)
FID fid;
{
CompressedFile *file;
FILE *f;
file = (CompressedFile *) fid;
f = file->file;
xfree (file);
return f;
}
#define getdcchar(file) ((file)->stackp > (file)->de_stack ? (*--((file)->stackp)) : _filldcbuf (file))
_filldcbuf (file)
CompressedFile *file;
{
register char_type *stackp;
register code_int code, incode;
if (file->stackp > file->de_stack)
return *--file->stackp;
if (file->oldcode == -1)
return EOF;
stackp = file->stackp;
code = getcode (file);
if (code == -1)
return EOF;
if ( (code == CLEAR) && file->block_compress ) {
for ( code = 255; code >= 0; code-- )
file->tab_prefix[code] = 0;
file->clear_flg = 1;
file->free_ent = FIRST - 1;
if ( (code = getcode (file)) == -1 ) /* O, untimely death! */
return EOF;
}
incode = code;
/*
* Special case for KwKwK string.
*/
if ( code >= file->free_ent ) {
*stackp++ = file->finchar;
code = file->oldcode;
}
/*
* Generate output characters in reverse order
*/
while ( code >= 256 )
{
*stackp++ = file->tab_suffix[code];
code = file->tab_prefix[code];
}
file->finchar = file->tab_suffix[code];
/*
* Generate the new entry.
*/
if ( (code=file->free_ent) < file->maxmaxcode ) {
file->tab_prefix[code] = (unsigned short)file->oldcode;
file->tab_suffix[code] = file->finchar;
file->free_ent = code+1;
}
/*
* Remember previous code.
*/
file->oldcode = incode;
file->stackp = stackp;
return file->finchar;
}
/*****************************************************************
* TAG( getcode )
*
* Read one code from the standard input. If EOF, return -1.
* Inputs:
* stdin
* Outputs:
* code or -1 is returned.
*/
static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
static code_int
getcode(file)
CompressedFile *file;
{
register code_int code;
register int r_off, bits;
register char_type *bp = file->buf;
register FILE *fp;
if ( file->clear_flg > 0 || file->offset >= file->size ||
file->free_ent > file->maxcode )
{
/*
* If the next entry will be too big for the current code
* size, then we must increase the size. This implies reading
* a new buffer full, too.
*/
if ( file->free_ent > file->maxcode ) {
file->n_bits++;
if ( file->n_bits == file->maxbits )
file->maxcode = file->maxmaxcode; /* won't get any bigger now */
else
file->maxcode = MAXCODE(file->n_bits);
}
if ( file->clear_flg > 0) {
file->maxcode = MAXCODE (file->n_bits = INIT_BITS);
file->clear_flg = 0;
}
bits = file->n_bits;
fp = file->file;
while (bits > 0 && (code = getc (fp)) != EOF)
{
*bp++ = code;
--bits;
}
bp = file->buf;
if (bits == file->n_bits)
return -1; /* end of file */
file->size = file->n_bits - bits;
file->offset = 0;
/* Round size down to integral number of codes */
file->size = (file->size << 3) - (file->n_bits - 1);
}
r_off = file->offset;
bits = file->n_bits;
/*
* Get to the first byte.
*/
bp += (r_off >> 3);
r_off &= 7;
/* Get first part (low order bits) */
#ifdef NO_UCHAR
code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
#else
code = (*bp++ >> r_off);
#endif /* NO_UCHAR */
bits -= (8 - r_off);
r_off = 8 - r_off; /* now, offset into code word */
/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
if ( bits >= 8 ) {
#ifdef NO_UCHAR
code |= (*bp++ & 0xff) << r_off;
#else
code |= *bp++ << r_off;
#endif /* NO_UCHAR */
r_off += 8;
bits -= 8;
}
/* high order bits. */
code |= (*bp & rmask[bits]) << r_off;
file->offset += file->n_bits;
return code;
}
CompressedFontFileRead (buf, itemsize, nitems, fid)
char *buf;
unsigned itemsize;
unsigned nitems;
FID fid;
{
CompressedFile *file;
int c;
int nbytes;
file = (CompressedFile *) fid;
nbytes = nitems * itemsize;
while (nbytes)
{
if ((c = getdcchar (file)) == EOF)
break;
*buf++ = c;
--nbytes;
}
return nitems - nbytes / itemsize;
}
CompressedFontFileSkip (bytes, fid)
unsigned bytes;
FID fid;
{
int c;
while (bytes-- && ((c = getdcchar((CompressedFile *)fid)) != EOF))
;
return c;
}
#ifdef TEST
main ()
{
CompressedFile *input;
int c;
input = (CompressedFile *) CompressedFontFileInit (stdin);
while ((c = getdcchar (input)) != -1)
putchar (c);
}
#endif
|