| 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 |
#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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 84 |
for (off = 0; off + 512 <= fill; off += 512) { |
| 85 |
sector = off >> 9; |
| 86 |
|
| 87 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 168 |
|
| 169 |
cs->offset = offset; |
| 170 |
cs->write_pos = 0; |
| 171 |
cs->write_max = size; |
| 172 |
|
| 173 |
|
| 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) { |
| 186 |
|
| 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 |
|
| 197 |
execlp(program, program, "-dc", NULL); |
| 198 |
exit(0); |
| 199 |
} |
| 200 |
|
| 201 |
close(write_pipe[0]); |
| 202 |
close(read_pipe[1]); |
| 203 |
|
| 204 |
|
| 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 |
|
| 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) |
| 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) { |
| 250 |
|
| 251 |
s->size_known = 1; |
| 252 |
s->size = s->seq_pos + got; |
| 253 |
|
| 254 |
close(cs->read_pipe); |
| 255 |
cs->read_pipe = -1; |
| 256 |
|
| 257 |
break; |
| 258 |
} else if (result > 0) { |
| 259 |
p += result; |
| 260 |
got += result; |
| 261 |
continue; |
| 262 |
} else { |
| 263 |
if (errno == EINTR) |
| 264 |
continue; |
| 265 |
if (errno != EAGAIN) { |
| 266 |
errore("read from pipe"); |
| 267 |
break; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 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 |
|
| 280 |
close(cs->write_pipe); |
| 281 |
cs->write_pipe = -1; |
| 282 |
} |
| 283 |
if (cs->write_pipe < 0) { |
| 284 |
|
| 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 |
|
| 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 |
|
| 310 |
cs->write_max = cs->write_pos + fill; |
| 311 |
} |
| 312 |
if (fill <= 0) { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
continue; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 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; |
| 327 |
} else { |
| 328 |
if (errno == EINTR) |
| 329 |
continue; |
| 330 |
if (errno != EAGAIN) { |
| 331 |
errore("write to pipe"); |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 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 |
|
| 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 |
| 375 |
|
| 376 |
|