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.
12 #include "lzw.h" /* just for consistency checking */
16 static unsigned decode (unsigned count
, uch buffer
[]);
17 static void decode_start (void);
20 static void huf_decode_start (void);
21 static unsigned decode_c (void);
22 static unsigned decode_p (void);
23 static void read_pt_len (int nn
, int nbit
, int i_special
);
24 static void read_c_len (void);
27 static void fillbuf (int n
);
28 static unsigned getbits (int n
);
29 static void init_getbits (void);
33 static void make_table (int nchar
, uch bitlen
[], int tablebits
, ush table
[]);
36 #define DICBIT 13 /* 12(-lh4-) or 13(-lh5-) */
37 #define DICSIZ ((unsigned) 1 << DICBIT)
39 #define BITBUFSIZ (CHAR_BIT * 2 * sizeof(char))
40 /* Do not use CHAR_BIT * sizeof(bitbuf), does not work on machines
41 * for which short is not on 16 bits (Cray).
44 /* encode.c and decode.c */
46 #define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
47 #define THRESHOLD 3 /* choose optimal value */
51 #define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
52 /* alphabet = {0, 1, 2, ..., NC - 1} */
53 #define CBIT 9 /* $\lfloor \log_2 NC \rfloor + 1$ */
54 #define CODE_BIT 16 /* codeword length */
56 #define NP (DICBIT + 1)
57 #define NT (CODE_BIT + 3)
58 #define PBIT 4 /* smallest integer such that (1U << PBIT) > NP */
59 #define TBIT 5 /* smallest integer such that (1U << TBIT) > NT */
60 #define NPT (1 << TBIT)
62 /* static ush left[2 * NC - 1]; */
63 /* static ush right[2 * NC - 1]; */
66 #if NC > (1<<(BITS-2))
67 error cannot overlay left
+right
and prev
70 /* static uch c_len[NC]; */
73 error cannot overlay c_len
and outbuf
76 static uch pt_len
[NPT
];
77 static unsigned blocksize
;
78 static ush pt_table
[256];
80 /* static ush c_table[4096]; */
82 #if (DIST_BUFSIZE-1) < 4095
83 error cannot overlay c_table
and d_buf
86 /***********************************************************
88 ***********************************************************/
91 static unsigned subbitbuf
;
94 /* Shift bitbuf N bits left, read N bits. */
99 while (n
> bitcount
) {
100 bitbuf
|= subbitbuf
<< (n
-= bitcount
);
101 subbitbuf
= (unsigned)try_byte();
102 if ((int)subbitbuf
== EOF
) subbitbuf
= 0;
105 bitbuf
|= subbitbuf
>> (bitcount
-= n
);
113 x
= bitbuf
>> (BITBUFSIZ
- n
); fillbuf(n
);
120 bitbuf
= 0; subbitbuf
= 0; bitcount
= 0;
124 /***********************************************************
125 maketbl.c -- make table for decoding
126 ***********************************************************/
129 make_table (int nchar
, uch bitlen
[], int tablebits
, ush table
[])
131 ush count
[17], weight
[17], start
[18], *p
;
132 unsigned i
, k
, len
, ch
, jutbits
, avail
, nextcode
, mask
;
134 for (i
= 1; i
<= 16; i
++) count
[i
] = 0;
135 for (i
= 0; i
< (unsigned)nchar
; i
++) count
[bitlen
[i
]]++;
138 for (i
= 1; i
<= 16; i
++)
139 start
[i
+ 1] = start
[i
] + (count
[i
] << (16 - i
));
140 if ((start
[17] & 0xffff) != 0)
141 gzip_error ("Bad table\n");
143 jutbits
= 16 - tablebits
;
144 for (i
= 1; i
<= (unsigned)tablebits
; i
++) {
145 start
[i
] >>= jutbits
;
146 weight
[i
] = (unsigned) 1 << (tablebits
- i
);
149 weight
[i
] = (unsigned) 1 << (16 - i
);
153 i
= start
[tablebits
+ 1] >> jutbits
;
156 while (i
!= k
) table
[i
++] = 0;
160 mask
= (unsigned) 1 << (15 - tablebits
);
161 for (ch
= 0; ch
< (unsigned)nchar
; ch
++) {
162 if ((len
= bitlen
[ch
]) == 0) continue;
163 nextcode
= start
[len
] + weight
[len
];
164 if (len
<= (unsigned)tablebits
) {
165 if ((unsigned) 1 << tablebits
< nextcode
)
166 gzip_error ("Bad table\n");
167 for (i
= start
[len
]; i
< nextcode
; i
++) table
[i
] = ch
;
170 p
= &table
[k
>> jutbits
];
174 right
[avail
] = left
[avail
] = 0;
177 if (k
& mask
) p
= &right
[*p
];
183 start
[len
] = nextcode
;
187 /***********************************************************
188 huf.c -- static Huffman
189 ***********************************************************/
192 read_pt_len (int nn
, int nbit
, int i_special
)
200 for (i
= 0; i
< nn
; i
++) pt_len
[i
] = 0;
201 for (i
= 0; i
< 256; i
++) pt_table
[i
] = c
;
205 c
= bitbuf
>> (BITBUFSIZ
- 3);
207 mask
= (unsigned) 1 << (BITBUFSIZ
- 1 - 3);
208 while (mask
& bitbuf
) { mask
>>= 1; c
++; }
210 gzip_error ("Bad table\n");
212 fillbuf((c
< 7) ? 3 : c
- 3);
214 if (i
== i_special
) {
216 while (--c
>= 0) pt_len
[i
++] = 0;
219 while (i
< nn
) pt_len
[i
++] = 0;
220 make_table(nn
, pt_len
, 8, pt_table
);
233 for (i
= 0; i
< NC
; i
++) c_len
[i
] = 0;
234 for (i
= 0; i
< 4096; i
++) c_table
[i
] = c
;
238 c
= pt_table
[bitbuf
>> (BITBUFSIZ
- 8)];
240 mask
= (unsigned) 1 << (BITBUFSIZ
- 1 - 8);
242 if (bitbuf
& mask
) c
= right
[c
];
247 fillbuf((int) pt_len
[c
]);
250 else if (c
== 1) c
= getbits(4) + 3;
251 else c
= getbits(CBIT
) + 20;
252 while (--c
>= 0) c_len
[i
++] = 0;
253 } else c_len
[i
++] = c
- 2;
255 while (i
< NC
) c_len
[i
++] = 0;
256 make_table(NC
, c_len
, 12, c_table
);
265 if (blocksize
== 0) {
266 blocksize
= getbits(16);
267 if (blocksize
== 0) {
268 return NC
; /* end of file */
270 read_pt_len(NT
, TBIT
, 3);
272 read_pt_len(NP
, PBIT
, -1);
275 j
= c_table
[bitbuf
>> (BITBUFSIZ
- 12)];
277 mask
= (unsigned) 1 << (BITBUFSIZ
- 1 - 12);
279 if (bitbuf
& mask
) j
= right
[j
];
284 fillbuf((int) c_len
[j
]);
293 j
= pt_table
[bitbuf
>> (BITBUFSIZ
- 8)];
295 mask
= (unsigned) 1 << (BITBUFSIZ
- 1 - 8);
297 if (bitbuf
& mask
) j
= right
[j
];
302 fillbuf((int) pt_len
[j
]);
303 if (j
!= 0) j
= ((unsigned) 1 << (j
- 1)) + getbits((int) (j
- 1));
310 init_getbits(); blocksize
= 0;
313 /***********************************************************
315 ***********************************************************/
317 static int j
; /* remaining bytes to copy */
318 static int done
; /* set at end of input */
328 /* Decode the input and return the number of decoded bytes put in buffer
331 decode (unsigned count
, uch buffer
[])
332 /* The calling function must keep the number of
333 bytes to be processed. This function decodes
334 either 'count' bytes or 'DICSIZ' bytes, whichever
335 is smaller, into the array 'buffer[]' of size
337 Call decode_start() once for each new file
338 before calling this function.
346 buffer
[r
] = buffer
[i
];
347 i
= (i
+ 1) & (DICSIZ
- 1);
348 if (++r
== count
) return r
;
356 if (c
<= UCHAR_MAX
) {
358 if (++r
== count
) return r
;
360 j
= c
- (UCHAR_MAX
+ 1 - THRESHOLD
);
361 i
= (r
- decode_p() - 1) & (DICSIZ
- 1);
363 buffer
[r
] = buffer
[i
];
364 i
= (i
+ 1) & (DICSIZ
- 1);
365 if (++r
== count
) return r
;
372 /* ===========================================================================
373 * Unlzh in to out. Return OK or ERROR.
376 unlzh (int in
, int out
)
384 n
= decode((unsigned) DICSIZ
, window
);
386 write_buf (out
, window
, n
);