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