Debian Codegraph @

compressed.c

raw symbols SCIP
1 /*
2 * compressed.c
3 * Layered data source for compressed files.
4 *
5 * Copyright (c) 2003 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 #include <signal.h>
31 #include <sys/wait.h>
32
33 #define DEBUG 0
34
35 #if !defined(FD_ZERO)
36 #define DECOMPRESS 0
37 #warning Transparent decompression disabled, select() macros not defined
38 #elif defined(__amigaos__) && !defined(__ixemul__)
39 #define DECOMPRESS 0
40 #warning Transparent decompression disabled, ixemul not available
41 #else
42 #define DECOMPRESS 1
43 #endif
44
45 /*
46 * types
47 */
48
49 #if DECOMPRESS
50 typedef struct compressed_source {
51 SOURCE c;
52 u8 offset, write_pos, write_max;
53 int write_pipe, read_pipe, nfds;
54 pid_t pid;
55 } COMPRESSED_SOURCE;
56 #endif
57
58 /*
59 * helper functions
60 */
61
62 static void handle_compressed(SECTION *section, int level,
63 int off, const char *program);
64
65 #if DECOMPRESS
66 static SOURCE *init_compressed_source(SOURCE *foundation, u8 offset, u8 size,
67 const char *program);
68 static u8 read_compressed(SOURCE *s, u8 pos, u8 len, void *buf);
69 static void close_compressed(SOURCE *s);
70 #endif
71
72 /*
73 * compressed file detection
74 */
75
76 void detect_compressed(SECTION *section, int level)
77 {
78 int fill, off, sector;
79 unsigned char *buf;
80
81 fill = get_buffer(section, 0, 4096, (void **)&buf);
82
83 /* look for signatures at sector beginnings */
84 for (off = 0; off + 512 <= fill; off += 512) {
85 sector = off >> 9;
86
87 /* compress */
88 if (buf[off] == 037 && buf[off+1] == 0235) {
89 if (sector > 0)
90 print_line(level, "compress-compressed data at sector %d", sector);
91 else
92 print_line(level, "compress-compressed data");
93
94 handle_compressed(section, level, off, "gzip");
95
96 break;
97 }
98
99 /* gzip */
100 if (buf[off] == 037 && (buf[off+1] == 0213 || buf[off+1] == 0236)) {
101 if (sector > 0)
102 print_line(level, "gzip-compressed data at sector %d", sector);
103 else
104 print_line(level, "gzip-compressed data");
105
106 handle_compressed(section, level, off, "gzip");
107
108 break;
109 }
110
111 /* bzip2 */
112 if (memcmp(buf + off, "BZh", 3) == 0) {
113 if (sector > 0)
114 print_line(level, "bzip2-compressed data at sector %d", sector);
115 else
116 print_line(level, "bzip2-compressed data");
117
118 handle_compressed(section, level, off, "bzip2");
119
120 break;
121 }
122 }
123 }
124
125 static void handle_compressed(SECTION *section, int level,
126 int off, const char *program)
127 {
128 #if DECOMPRESS
129 SOURCE *s;
130 u8 size;
131
132 /* create decompression data source */
133 size = section->size;
134 if (size > 0)
135 size -= off;
136 s = init_compressed_source(section->source,
137 section->pos + off, size, program);
138 analyze_source(s, level + 1);
139 close_source(s);
140 #else
141 print_line(level + 1, "Decompression disabled on this system");
142 #endif
143 }
144
145 /*
146 * initialize the decompression
147 */
148
149 #if DECOMPRESS
150
151 static SOURCE *init_compressed_source(SOURCE *foundation, u8 offset, u8 size,
152 const char *program)
153 {
154 COMPRESSED_SOURCE *cs;
155 int write_pipe[2], read_pipe[2], flags;
156
157 cs = (COMPRESSED_SOURCE *)malloc(sizeof(COMPRESSED_SOURCE));
158 if (cs == NULL)
159 bailout("Out of memory");
160 memset(cs, 0, sizeof(COMPRESSED_SOURCE));
161
162 cs->c.sequential = 1;
163 cs->c.seq_pos = 0;
164 cs->c.foundation = foundation;
165 cs->c.read_bytes = read_compressed;
166 cs->c.close = close_compressed;
167 /* size is not known in advance by definition */
168
169 cs->offset = offset;
170 cs->write_pos = 0;
171 cs->write_max = size;
172
173 /* open "gzip -dc" in a dual pipe */
174 if (pipe(write_pipe) < 0)
175 bailoute("pipe for decompression");
176 if (pipe(read_pipe) < 0)
177 bailoute("pipe for decompression");
178 cs->write_pipe = write_pipe[1];
179 cs->read_pipe = read_pipe[0];
180
181 cs->pid = fork();
182 if (cs->pid < 0) {
183 bailoute("fork");
184 }
185 if (cs->pid == 0) { /* we're the child process */
186 /* set up pipe */
187 dup2(write_pipe[0], 0);
188 if (write_pipe[0] > 2)
189 close(write_pipe[0]);
190 close(write_pipe[1]);
191 close(read_pipe[0]);
192 dup2(read_pipe[1], 1);
193 if (read_pipe[1] > 2)
194 close(read_pipe[1]);
195
196 /* execute decompressor (gzip or bzip2) */
197 execlp(program, program, "-dc", NULL);
198 exit(0);
199 }
200 /* we're the parent process */
201 close(write_pipe[0]);
202 close(read_pipe[1]);
203
204 /* set non-blocking I/O */
205 if ((flags = fcntl(cs->write_pipe, F_GETFL, 0)) >= 0)
206 fcntl(cs->write_pipe, F_SETFL, flags | O_NONBLOCK);
207 else
208 bailoute("set pipe flags");
209 if ((flags = fcntl(cs->read_pipe, F_GETFL, 0)) >= 0)
210 fcntl(cs->read_pipe, F_SETFL, flags | O_NONBLOCK);
211 else
212 bailoute("set pipe flags");
213 cs->nfds = ((cs->read_pipe > cs->write_pipe) ?
214 cs->read_pipe : cs->write_pipe) + 1;
215
216 return (SOURCE *)cs;
217 }
218
219 /*
220 * raw read
221 */
222
223 static u8 read_compressed(SOURCE *s, u8 pos, u8 len, void *buf)
224 {
225 COMPRESSED_SOURCE *cs = (COMPRESSED_SOURCE *)s;
226 SOURCE *fs = s->foundation;
227 char *p, *filebuf;
228 u8 got, fill;
229 int askfor, selresult;
230 ssize_t result;
231 fd_set read_set;
232 fd_set write_set;
233
234 #if DEBUG
235 printf("rc got asked for pos %llu len %llu\n", pos, len);
236 #endif
237
238 p = (char *)buf;
239 got = 0;
240
241 if (cs->read_pipe < 0) /* closed for reading */
242 return got;
243
244 while (got < len) {
245 result = read(cs->read_pipe, p, len - got);
246 #if DEBUG
247 printf("rc read got %d\n", result);
248 #endif
249 if (result == 0) { /* end of file */
250 /* remember size for buffer layer */
251 s->size_known = 1;
252 s->size = s->seq_pos + got;
253 /* close pipe (stops future read attempts in the track) */
254 close(cs->read_pipe);
255 cs->read_pipe = -1;
256 /* we're done */
257 break;
258 } else if (result > 0) { /* got data */
259 p += result;
260 got += result;
261 continue;
262 } else { /* error return */
263 if (errno == EINTR)
264 continue;
265 if (errno != EAGAIN) {
266 errore("read from pipe");
267 break;
268 }
269 }
270
271 /* no data available for reading right now, so try to write
272 some uncompressed data down the other pipe for a change */
273
274 /* calculate how much data to write */
275 askfor = 4096;
276 if (cs->write_max && cs->write_pos + askfor > cs->write_max)
277 askfor = cs->write_max - cs->write_pos;
278 if (askfor <= 0 && cs->write_pipe >= 0) {
279 /* there's no more data to write, close the pipe */
280 close(cs->write_pipe);
281 cs->write_pipe = -1;
282 }
283 if (cs->write_pipe < 0) {
284 /* no more data to write, just wait for input using select */
285 FD_ZERO(&read_set);
286 FD_SET(cs->read_pipe, &read_set);
287 #if DEBUG
288 printf("rc starting select\n");
289 #endif
290 selresult = select(cs->nfds, &read_set, NULL, NULL, NULL);
291 #if DEBUG
292 printf("rc select got %d\n", selresult);
293 #endif
294 if (selresult < 0 && errno != EINTR) {
295 errore("select");
296 break;
297 }
298 continue;
299 }
300
301 /* get data from lower layer */
302 fill = get_buffer_real(fs, cs->offset + cs->write_pos, askfor,
303 NULL, (void **)&filebuf);
304 #if DEBUG
305 printf("rc get_buffer asked for pos %llu len %d got %llu\n",
306 cs->offset + cs->write_pos, askfor, fill);
307 #endif
308 if (fill < askfor) {
309 /* we reached the end of compressed input, note that down */
310 cs->write_max = cs->write_pos + fill;
311 }
312 if (fill <= 0) {
313 /* didn't get any data to write, so no need trying */
314 /* NOTE: in this case, the above if() also caught on and the next
315 time through the loop, the write pipe will be closed. */
316 continue;
317 }
318
319 /* try a write right now */
320 result = write(cs->write_pipe, filebuf, fill);
321 #if DEBUG
322 printf("rc write got %d\n", result);
323 #endif
324 if (result >= 0) {
325 cs->write_pos += result;
326 continue; /* see if that made more data available for reading */
327 } else {
328 if (errno == EINTR)
329 continue;
330 if (errno != EAGAIN) {
331 errore("write to pipe");
332 break;
333 }
334 }
335
336 /* both pipes are blocked right now. wait using select(). */
337 FD_ZERO(&read_set);
338 FD_ZERO(&write_set);
339 FD_SET(cs->read_pipe, &read_set);
340 FD_SET(cs->write_pipe, &write_set);
341 #if DEBUG
342 printf("rc starting select\n");
343 #endif
344 selresult = select(cs->nfds, &read_set, &write_set, NULL, NULL);
345 #if DEBUG
346 printf("rc select got %d\n", selresult);
347 #endif
348 if (selresult < 0 && errno != EINTR) {
349 errore("select");
350 break;
351 }
352 }
353
354 return got;
355 }
356
357 /*
358 * close cleanup
359 */
360
361 static void close_compressed(SOURCE *s)
362 {
363 COMPRESSED_SOURCE *cs = (COMPRESSED_SOURCE *)s;
364 int status;
365
366 if (cs->write_pipe >= 0)
367 close(cs->write_pipe);
368 if (cs->read_pipe >= 0)
369 close(cs->read_pipe);
370 kill(cs->pid, SIGHUP);
371 waitpid(cs->pid, &status, 0);
372 }
373
374 #endif /* DECOMPRESS */
375
376 /* EOF */