1 /* unlzh.c -- decompress files in SCO compress -H (LZH) format.
2 * The code in this file is directly derived from the public domain 'ar002'
3 * written by Haruhiko Okumura.
7 static char rcsid[] = "$FreeBSD: src/gnu/usr.bin/gzip/unlzh.c,v 1.5 1999/08/27 23:35:53 peter Exp $";
14 #include "lzw.h" /* just for consistency checking */
18 local unsigned decode OF((unsigned count, uch buffer[]));
19 local void decode_start OF((void));
22 local void huf_decode_start OF((void));
23 local unsigned decode_c OF((void));
24 local unsigned decode_p OF((void));
25 local void read_pt_len OF((int nn, int nbit, int i_special));
26 local void read_c_len OF((void));
29 local void fillbuf OF((int n));
30 local unsigned getbits OF((int n));
31 local void init_getbits OF((void));
35 local void make_table OF((int nchar, uch bitlen[],
36 int tablebits, ush table[]));
39 #define DICBIT 13 /* 12(-lh4-) or 13(-lh5-) */
40 #define DICSIZ ((unsigned) 1 << DICBIT)
47 # define UCHAR_MAX 255
50 #define BITBUFSIZ (CHAR_BIT * 2 * sizeof(char))
51 /* Do not use CHAR_BIT * sizeof(bitbuf), does not work on machines
52 * for which short is not on 16 bits (Cray).
55 /* encode.c and decode.c */
57 #define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
58 #define THRESHOLD 3 /* choose optimal value */
62 #define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
63 /* alphabet = {0, 1, 2, ..., NC - 1} */
64 #define CBIT 9 /* $\lfloor \log_2 NC \rfloor + 1$ */
65 #define CODE_BIT 16 /* codeword length */
67 #define NP (DICBIT + 1)
68 #define NT (CODE_BIT + 3)
69 #define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
70 #define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
77 /* local ush left[2 * NC - 1]; */
78 /* local ush right[2 * NC - 1]; */
81 #if NC > (1<<(BITS-2))
82 error cannot overlay left+right and prev
85 /* local uch c_len[NC]; */
88 error cannot overlay c_len and outbuf
91 local uch pt_len[NPT];
92 local unsigned blocksize;
93 local ush pt_table[256];
95 /* local ush c_table[4096]; */
97 #if (DIST_BUFSIZE-1) < 4095
98 error cannot overlay c_table and d_buf
101 /***********************************************************
103 ***********************************************************/
106 local unsigned subbitbuf;
109 local void fillbuf(n) /* Shift bitbuf n bits left, read n bits */
113 while (n > bitcount) {
114 bitbuf |= subbitbuf << (n -= bitcount);
115 subbitbuf = (unsigned)try_byte();
116 if ((int)subbitbuf == EOF) subbitbuf = 0;
119 bitbuf |= subbitbuf >> (bitcount -= n);
122 local unsigned getbits(n)
127 x = bitbuf >> (BITBUFSIZ - n); fillbuf(n);
131 local void init_getbits()
133 bitbuf = 0; subbitbuf = 0; bitcount = 0;
137 /***********************************************************
138 maketbl.c -- make table for decoding
139 ***********************************************************/
141 local void make_table(nchar, bitlen, tablebits, table)
147 ush count[17], weight[17], start[18], *p;
148 unsigned i, k, len, ch, jutbits, avail, nextcode, mask;
150 for (i = 1; i <= 16; i++) count[i] = 0;
151 for (i = 0; i < (unsigned)nchar; i++) count[bitlen[i]]++;
154 for (i = 1; i <= 16; i++)
155 start[i + 1] = start[i] + (count[i] << (16 - i));
156 if ((start[17] & 0xffff) != 0)
157 error("Bad table\n");
159 jutbits = 16 - tablebits;
160 for (i = 1; i <= (unsigned)tablebits; i++) {
161 start[i] >>= jutbits;
162 weight[i] = (unsigned) 1 << (tablebits - i);
165 weight[i] = (unsigned) 1 << (16 - i);
169 i = start[tablebits + 1] >> jutbits;
172 while (i != k) table[i++] = 0;
176 mask = (unsigned) 1 << (15 - tablebits);
177 for (ch = 0; ch < (unsigned)nchar; ch++) {
178 if ((len = bitlen[ch]) == 0) continue;
179 nextcode = start[len] + weight[len];
180 if (len <= (unsigned)tablebits) {
181 for (i = start[len]; i < nextcode; i++) table[i] = ch;
184 p = &table[k >> jutbits];
188 right[avail] = left[avail] = 0;
191 if (k & mask) p = &right[*p];
197 start[len] = nextcode;
201 /***********************************************************
202 huf.c -- static Huffman
203 ***********************************************************/
205 local void read_pt_len(nn, nbit, i_special)
216 for (i = 0; i < nn; i++) pt_len[i] = 0;
217 for (i = 0; i < 256; i++) pt_table[i] = c;
221 c = bitbuf >> (BITBUFSIZ - 3);
223 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 3);
224 while (mask & bitbuf) { mask >>= 1; c++; }
226 fillbuf((c < 7) ? 3 : c - 3);
228 if (i == i_special) {
230 while (--c >= 0) pt_len[i++] = 0;
233 while (i < nn) pt_len[i++] = 0;
234 make_table(nn, pt_len, 8, pt_table);
238 local void read_c_len()
246 for (i = 0; i < NC; i++) c_len[i] = 0;
247 for (i = 0; i < 4096; i++) c_table[i] = c;
251 c = pt_table[bitbuf >> (BITBUFSIZ - 8)];
253 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
255 if (bitbuf & mask) c = right[c];
260 fillbuf((int) pt_len[c]);
263 else if (c == 1) c = getbits(4) + 3;
264 else c = getbits(CBIT) + 20;
265 while (--c >= 0) c_len[i++] = 0;
266 } else c_len[i++] = c - 2;
268 while (i < NC) c_len[i++] = 0;
269 make_table(NC, c_len, 12, c_table);
273 local unsigned decode_c()
277 if (blocksize == 0) {
278 blocksize = getbits(16);
279 if (blocksize == 0) {
280 return NC; /* end of file */
282 read_pt_len(NT, TBIT, 3);
284 read_pt_len(NP, PBIT, -1);
287 j = c_table[bitbuf >> (BITBUFSIZ - 12)];
289 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 12);
291 if (bitbuf & mask) j = right[j];
296 fillbuf((int) c_len[j]);
300 local unsigned decode_p()
304 j = pt_table[bitbuf >> (BITBUFSIZ - 8)];
306 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
308 if (bitbuf & mask) j = right[j];
313 fillbuf((int) pt_len[j]);
314 if (j != 0) j = ((unsigned) 1 << (j - 1)) + getbits((int) (j - 1));
318 local void huf_decode_start()
320 init_getbits(); blocksize = 0;
323 /***********************************************************
325 ***********************************************************/
327 local int j; /* remaining bytes to copy */
328 local int done; /* set at end of input */
330 local void decode_start()
337 /* Decode the input and return the number of decoded bytes put in buffer
339 local unsigned decode(count, buffer)
342 /* The calling function must keep the number of
343 bytes to be processed. This function decodes
344 either 'count' bytes or 'DICSIZ' bytes, whichever
345 is smaller, into the array 'buffer[]' of size
347 Call decode_start() once for each new file
348 before calling this function.
356 buffer[r] = buffer[i];
357 i = (i + 1) & (DICSIZ - 1);
358 if (++r == count) return r;
366 if (c <= UCHAR_MAX) {
368 if (++r == count) return r;
370 j = c - (UCHAR_MAX + 1 - THRESHOLD);
371 i = (r - decode_p() - 1) & (DICSIZ - 1);
373 buffer[r] = buffer[i];
374 i = (i + 1) & (DICSIZ - 1);
375 if (++r == count) return r;
382 /* ===========================================================================
383 * Unlzh in to out. Return OK or ERROR.
395 n = decode((unsigned) DICSIZ, window);
396 if (!test && n > 0) {
397 write_buf(out, (char*)window, n);