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.
5 * $FreeBSD: src/gnu/usr.bin/gzip/unlzh.c,v 1.5 1999/08/27 23:35:53 peter Exp $
6 * $DragonFly: src/gnu/usr.bin/gzip/Attic/unlzh.c,v 1.2 2003/06/17 04:25:46 dillon Exp $
13 #include "lzw.h" /* just for consistency checking */
17 local unsigned decode OF((unsigned count, uch buffer[]));
18 local void decode_start OF((void));
21 local void huf_decode_start OF((void));
22 local unsigned decode_c OF((void));
23 local unsigned decode_p OF((void));
24 local void read_pt_len OF((int nn, int nbit, int i_special));
25 local void read_c_len OF((void));
28 local void fillbuf OF((int n));
29 local unsigned getbits OF((int n));
30 local void init_getbits OF((void));
34 local void make_table OF((int nchar, uch bitlen[],
35 int tablebits, ush table[]));
38 #define DICBIT 13 /* 12(-lh4-) or 13(-lh5-) */
39 #define DICSIZ ((unsigned) 1 << DICBIT)
46 # define UCHAR_MAX 255
49 #define BITBUFSIZ (CHAR_BIT * 2 * sizeof(char))
50 /* Do not use CHAR_BIT * sizeof(bitbuf), does not work on machines
51 * for which short is not on 16 bits (Cray).
54 /* encode.c and decode.c */
56 #define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
57 #define THRESHOLD 3 /* choose optimal value */
61 #define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
62 /* alphabet = {0, 1, 2, ..., NC - 1} */
63 #define CBIT 9 /* $\lfloor \log_2 NC \rfloor + 1$ */
64 #define CODE_BIT 16 /* codeword length */
66 #define NP (DICBIT + 1)
67 #define NT (CODE_BIT + 3)
68 #define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
69 #define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
76 /* local ush left[2 * NC - 1]; */
77 /* local ush right[2 * NC - 1]; */
80 #if NC > (1<<(BITS-2))
81 error cannot overlay left+right and prev
84 /* local uch c_len[NC]; */
87 error cannot overlay c_len and outbuf
90 local uch pt_len[NPT];
91 local unsigned blocksize;
92 local ush pt_table[256];
94 /* local ush c_table[4096]; */
96 #if (DIST_BUFSIZE-1) < 4095
97 error cannot overlay c_table and d_buf
100 /***********************************************************
102 ***********************************************************/
105 local unsigned subbitbuf;
108 local void fillbuf(n) /* Shift bitbuf n bits left, read n bits */
112 while (n > bitcount) {
113 bitbuf |= subbitbuf << (n -= bitcount);
114 subbitbuf = (unsigned)try_byte();
115 if ((int)subbitbuf == EOF) subbitbuf = 0;
118 bitbuf |= subbitbuf >> (bitcount -= n);
121 local unsigned getbits(n)
126 x = bitbuf >> (BITBUFSIZ - n); fillbuf(n);
130 local void init_getbits()
132 bitbuf = 0; subbitbuf = 0; bitcount = 0;
136 /***********************************************************
137 maketbl.c -- make table for decoding
138 ***********************************************************/
140 local void make_table(nchar, bitlen, tablebits, table)
146 ush count[17], weight[17], start[18], *p;
147 unsigned i, k, len, ch, jutbits, avail, nextcode, mask;
149 for (i = 1; i <= 16; i++) count[i] = 0;
150 for (i = 0; i < (unsigned)nchar; i++) count[bitlen[i]]++;
153 for (i = 1; i <= 16; i++)
154 start[i + 1] = start[i] + (count[i] << (16 - i));
155 if ((start[17] & 0xffff) != 0)
156 error("Bad table\n");
158 jutbits = 16 - tablebits;
159 for (i = 1; i <= (unsigned)tablebits; i++) {
160 start[i] >>= jutbits;
161 weight[i] = (unsigned) 1 << (tablebits - i);
164 weight[i] = (unsigned) 1 << (16 - i);
168 i = start[tablebits + 1] >> jutbits;
171 while (i != k) table[i++] = 0;
175 mask = (unsigned) 1 << (15 - tablebits);
176 for (ch = 0; ch < (unsigned)nchar; ch++) {
177 if ((len = bitlen[ch]) == 0) continue;
178 nextcode = start[len] + weight[len];
179 if (len <= (unsigned)tablebits) {
180 for (i = start[len]; i < nextcode; i++) table[i] = ch;
183 p = &table[k >> jutbits];
187 right[avail] = left[avail] = 0;
190 if (k & mask) p = &right[*p];
196 start[len] = nextcode;
200 /***********************************************************
201 huf.c -- static Huffman
202 ***********************************************************/
204 local void read_pt_len(nn, nbit, i_special)
215 for (i = 0; i < nn; i++) pt_len[i] = 0;
216 for (i = 0; i < 256; i++) pt_table[i] = c;
220 c = bitbuf >> (BITBUFSIZ - 3);
222 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 3);
223 while (mask & bitbuf) { mask >>= 1; c++; }
225 fillbuf((c < 7) ? 3 : c - 3);
227 if (i == i_special) {
229 while (--c >= 0) pt_len[i++] = 0;
232 while (i < nn) pt_len[i++] = 0;
233 make_table(nn, pt_len, 8, pt_table);
237 local void read_c_len()
245 for (i = 0; i < NC; i++) c_len[i] = 0;
246 for (i = 0; i < 4096; i++) c_table[i] = c;
250 c = pt_table[bitbuf >> (BITBUFSIZ - 8)];
252 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
254 if (bitbuf & mask) c = right[c];
259 fillbuf((int) pt_len[c]);
262 else if (c == 1) c = getbits(4) + 3;
263 else c = getbits(CBIT) + 20;
264 while (--c >= 0) c_len[i++] = 0;
265 } else c_len[i++] = c - 2;
267 while (i < NC) c_len[i++] = 0;
268 make_table(NC, c_len, 12, c_table);
272 local unsigned decode_c()
276 if (blocksize == 0) {
277 blocksize = getbits(16);
278 if (blocksize == 0) {
279 return NC; /* end of file */
281 read_pt_len(NT, TBIT, 3);
283 read_pt_len(NP, PBIT, -1);
286 j = c_table[bitbuf >> (BITBUFSIZ - 12)];
288 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 12);
290 if (bitbuf & mask) j = right[j];
295 fillbuf((int) c_len[j]);
299 local unsigned decode_p()
303 j = pt_table[bitbuf >> (BITBUFSIZ - 8)];
305 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
307 if (bitbuf & mask) j = right[j];
312 fillbuf((int) pt_len[j]);
313 if (j != 0) j = ((unsigned) 1 << (j - 1)) + getbits((int) (j - 1));
317 local void huf_decode_start()
319 init_getbits(); blocksize = 0;
322 /***********************************************************
324 ***********************************************************/
326 local int j; /* remaining bytes to copy */
327 local int done; /* set at end of input */
329 local void decode_start()
336 /* Decode the input and return the number of decoded bytes put in buffer
338 local unsigned decode(count, buffer)
341 /* The calling function must keep the number of
342 bytes to be processed. This function decodes
343 either 'count' bytes or 'DICSIZ' bytes, whichever
344 is smaller, into the array 'buffer[]' of size
346 Call decode_start() once for each new file
347 before calling this function.
355 buffer[r] = buffer[i];
356 i = (i + 1) & (DICSIZ - 1);
357 if (++r == count) return r;
365 if (c <= UCHAR_MAX) {
367 if (++r == count) return r;
369 j = c - (UCHAR_MAX + 1 - THRESHOLD);
370 i = (r - decode_p() - 1) & (DICSIZ - 1);
372 buffer[r] = buffer[i];
373 i = (i + 1) & (DICSIZ - 1);
374 if (++r == count) return r;
381 /* ===========================================================================
382 * Unlzh in to out. Return OK or ERROR.
394 n = decode((unsigned) DICSIZ, window);
395 if (!test && n > 0) {
396 write_buf(out, (char*)window, n);