build: support --program-prefix etc.
[gzip.git] / unlzw.c
blobba824e463ec4fea607cafa07d70346c438c1ce85
1 /* unlzw.c -- decompress files in LZW format.
2 * The code in this file is directly derived from the public domain 'compress'
3 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
4 * Ken Turkowski, Dave Mack and Peter Jannesen.
6 * This is a temporary version which will be rewritten in some future version
7 * to accommodate in-memory decompression.
8 */
10 #include <config.h>
12 #include <unistd.h>
13 #include <fcntl.h>
15 #include "tailor.h"
16 #include "gzip.h"
17 #include "lzw.h"
19 typedef unsigned char char_type;
20 typedef long code_int;
21 typedef unsigned long count_int;
22 typedef unsigned short count_short;
23 typedef unsigned long cmp_code_int;
25 #define MAXCODE(n) (1L << (n))
27 #ifndef BYTEORDER
28 # define BYTEORDER 0000
29 #endif
31 #ifndef NOALLIGN
32 # define NOALLIGN 0
33 #endif
36 union bytes {
37 long word;
38 struct {
39 #if BYTEORDER == 4321
40 char_type b1;
41 char_type b2;
42 char_type b3;
43 char_type b4;
44 #else
45 #if BYTEORDER == 1234
46 char_type b4;
47 char_type b3;
48 char_type b2;
49 char_type b1;
50 #else
51 # undef BYTEORDER
52 int dummy;
53 #endif
54 #endif
55 } bytes;
58 #if BYTEORDER == 4321 && NOALLIGN == 1
59 # define input(b,o,c,n,m){ \
60 (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
61 (o) += (n); \
63 #else
64 # define input(b,o,c,n,m){ \
65 char_type *p = &(b)[(o)>>3]; \
66 (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
67 ((long)(p[2])<<16))>>((o)&0x7))&(m); \
68 (o) += (n); \
70 #endif
72 #ifndef MAXSEG_64K
73 /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
74 # define tab_prefixof(i) tab_prefix[i]
75 # define clear_tab_prefixof() memzero(tab_prefix, 256);
76 #else
77 /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
78 /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd codes */
79 ush *tab_prefix[2];
80 # define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
81 # define clear_tab_prefixof() \
82 memzero(tab_prefix0, 128), \
83 memzero(tab_prefix1, 128);
84 #endif
85 #define de_stack ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
86 #define tab_suffixof(i) tab_suffix[i]
88 /* block compress mode -C compatible with 2.0 */
89 static int block_mode = BLOCK_MODE;
91 /* ============================================================================
92 * Decompress in to out. This routine adapts to the codes in the
93 * file building the "string" table on-the-fly; requiring no table to
94 * be stored in the compressed file.
95 * IN assertions: the buffer inbuf contains already the beginning of
96 * the compressed data, from offsets iptr to insize-1 included.
97 * The magic header has already been checked and skipped.
98 * bytes_in and bytes_out have been initialized.
100 int unlzw(in, out)
101 int in, out; /* input and output file descriptors */
103 char_type *stackp;
104 code_int code;
105 int finchar;
106 code_int oldcode;
107 code_int incode;
108 long inbits;
109 long posbits;
110 int outpos;
111 /* int insize; (global) */
112 unsigned bitmask;
113 code_int free_ent;
114 code_int maxcode;
115 code_int maxmaxcode;
116 int n_bits;
117 int rsize;
119 #ifdef MAXSEG_64K
120 tab_prefix[0] = tab_prefix0;
121 tab_prefix[1] = tab_prefix1;
122 #endif
123 maxbits = get_byte();
124 block_mode = maxbits & BLOCK_MODE;
125 if ((maxbits & LZW_RESERVED) != 0) {
126 WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
127 program_name, ifname, (unsigned int) maxbits & LZW_RESERVED));
129 maxbits &= BIT_MASK;
130 maxmaxcode = MAXCODE(maxbits);
132 if (maxbits > BITS) {
133 fprintf(stderr,
134 "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
135 program_name, ifname, maxbits, BITS);
136 exit_code = ERROR;
137 return ERROR;
139 rsize = insize;
140 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
141 bitmask = (1<<n_bits)-1;
142 oldcode = -1;
143 finchar = 0;
144 outpos = 0;
145 posbits = inptr<<3;
147 free_ent = ((block_mode) ? FIRST : 256);
149 clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
151 for (code = 255 ; code >= 0 ; --code) {
152 tab_suffixof(code) = (char_type)code;
154 do {
155 int i;
156 int e;
157 int o;
159 resetbuf:
160 o = posbits >> 3;
161 e = o <= insize ? insize - o : 0;
163 for (i = 0 ; i < e ; ++i) {
164 inbuf[i] = inbuf[i+o];
166 insize = e;
167 posbits = 0;
169 if (insize < INBUF_EXTRA) {
170 rsize = read_buffer (in, (char *) inbuf + insize, INBUFSIZ);
171 if (rsize == -1) {
172 read_error();
174 insize += rsize;
175 bytes_in += (off_t)rsize;
177 inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 :
178 ((long)insize<<3)-(n_bits-1));
180 while (inbits > posbits) {
181 if (free_ent > maxcode) {
182 posbits = ((posbits-1) +
183 ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
184 ++n_bits;
185 if (n_bits == maxbits) {
186 maxcode = maxmaxcode;
187 } else {
188 maxcode = MAXCODE(n_bits)-1;
190 bitmask = (1<<n_bits)-1;
191 goto resetbuf;
193 input(inbuf,posbits,code,n_bits,bitmask);
194 Tracev((stderr, "%ld ", code));
196 if (oldcode == -1) {
197 if (256 <= code)
198 gzip_error ("corrupt input.");
199 outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
200 continue;
202 if (code == CLEAR && block_mode) {
203 clear_tab_prefixof();
204 free_ent = FIRST - 1;
205 posbits = ((posbits-1) +
206 ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
207 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
208 bitmask = (1<<n_bits)-1;
209 goto resetbuf;
211 incode = code;
212 stackp = de_stack;
214 if (code >= free_ent) { /* Special case for KwKwK string. */
215 if (code > free_ent) {
216 #ifdef DEBUG
217 char_type *p;
219 posbits -= n_bits;
220 p = &inbuf[posbits>>3];
221 fprintf(stderr,
222 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
223 code, free_ent, n_bits, insize);
224 fprintf(stderr,
225 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
226 posbits, p[-1],p[0],p[1],p[2],p[3]);
227 #endif
228 if (outpos > 0)
229 write_buf (out, outbuf, outpos);
230 gzip_error (to_stdout
231 ? "corrupt input."
232 : "corrupt input. Use zcat to recover some data.");
234 *--stackp = (char_type)finchar;
235 code = oldcode;
238 while ((cmp_code_int)code >= (cmp_code_int)256) {
239 /* Generate output characters in reverse order */
240 *--stackp = tab_suffixof(code);
241 code = tab_prefixof(code);
243 *--stackp = (char_type)(finchar = tab_suffixof(code));
245 /* And put them out in forward order */
247 int i;
249 if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
250 do {
251 if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
253 if (i > 0) {
254 memcpy(outbuf+outpos, stackp, i);
255 outpos += i;
257 if (outpos >= OUTBUFSIZ) {
258 write_buf (out, outbuf, outpos);
259 outpos = 0;
261 stackp+= i;
262 } while ((i = (de_stack-stackp)) > 0);
263 } else {
264 memcpy(outbuf+outpos, stackp, i);
265 outpos += i;
269 if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
271 tab_prefixof(code) = (unsigned short)oldcode;
272 tab_suffixof(code) = (char_type)finchar;
273 free_ent = code+1;
275 oldcode = incode; /* Remember previous code. */
277 } while (rsize != 0);
279 if (outpos > 0)
280 write_buf (out, outbuf, outpos);
281 return OK;