1 /* $NetBSD: compress.c,v 1.5 2012/02/22 17:53:51 christos Exp $ */ 2 3 /* 4 * Copyright (c) Ian F. Darwin 1986-1995. 5 * Software written by Ian F. Darwin and others; 6 * maintained 1995-present by Christos Zoulas and others. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice immediately at the beginning of the file, without modification, 13 * this list of conditions, and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 /* 31 * compress routines: 32 * zmagic() - returns 0 if not recognized, uncompresses and prints 33 * information if recognized 34 * uncompress(method, old, n, newch) - uncompress old into new, 35 * using method, return sizeof new 36 */ 37 #include "file.h" 38 39 #ifndef lint 40 #if 0 41 FILE_RCSID("@(#)$File: compress.c,v 1.68 2011/12/08 12:38:24 rrt Exp $") 42 #else 43 __RCSID("$NetBSD: compress.c,v 1.5 2012/02/22 17:53:51 christos Exp $"); 44 #endif 45 #endif 46 47 #include "magic.h" 48 #include <stdlib.h> 49 #ifdef HAVE_UNISTD_H 50 #include <unistd.h> 51 #endif 52 #include <string.h> 53 #include <errno.h> 54 #ifndef __MINGW32__ 55 #include <sys/ioctl.h> 56 #endif 57 #ifdef HAVE_SYS_WAIT_H 58 #include <sys/wait.h> 59 #endif 60 #if defined(HAVE_SYS_TIME_H) 61 #include <sys/time.h> 62 #endif 63 #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ) 64 #define BUILTIN_DECOMPRESS 65 #include <zlib.h> 66 #endif 67 68 private const struct { 69 const char magic[8]; 70 size_t maglen; 71 const char *argv[3]; 72 int silent; 73 } compr[] = { 74 { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */ 75 /* Uncompress can get stuck; so use gzip first if we have it 76 * Idea from Damien Clark, thanks! */ 77 { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */ 78 { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */ 79 { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */ 80 { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */ 81 /* the standard pack utilities do not accept standard input */ 82 { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */ 83 { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */ 84 /* ...only first file examined */ 85 { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */ 86 { "LZIP", 4, { "lzip", "-cdq", NULL }, 1 }, 87 { "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */ 88 { "LRZI", 4, { "lrzip", "-dqo-", NULL }, 1 }, /* LRZIP */ 89 }; 90 91 #define NODATA ((size_t)~0) 92 93 private ssize_t swrite(int, const void *, size_t); 94 #if HAVE_FORK 95 private size_t ncompr = sizeof(compr) / sizeof(compr[0]); 96 private size_t uncompressbuf(struct magic_set *, int, size_t, 97 const unsigned char *, unsigned char **, size_t); 98 #ifdef BUILTIN_DECOMPRESS 99 private size_t uncompressgzipped(struct magic_set *, const unsigned char *, 100 unsigned char **, size_t); 101 #endif 102 103 protected int 104 file_zmagic(struct magic_set *ms, int fd, const char *name, 105 const unsigned char *buf, size_t nbytes) 106 { 107 unsigned char *newbuf = NULL; 108 size_t i, nsz; 109 int rv = 0; 110 int mime = ms->flags & MAGIC_MIME; 111 112 if ((ms->flags & MAGIC_COMPRESS) == 0) 113 return 0; 114 115 for (i = 0; i < ncompr; i++) { 116 if (nbytes < compr[i].maglen) 117 continue; 118 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 && 119 (nsz = uncompressbuf(ms, fd, i, buf, &newbuf, 120 nbytes)) != NODATA) { 121 ms->flags &= ~MAGIC_COMPRESS; 122 rv = -1; 123 if (file_buffer(ms, -1, name, newbuf, nsz) == -1) 124 goto error; 125 126 if (mime == MAGIC_MIME || mime == 0) { 127 if (file_printf(ms, mime ? 128 " compressed-encoding=" : " (") == -1) 129 goto error; 130 } 131 132 if ((mime == 0 || mime & MAGIC_MIME_ENCODING) && 133 file_buffer(ms, -1, NULL, buf, nbytes) == -1) 134 goto error; 135 136 if (!mime && file_printf(ms, ")") == -1) 137 goto error; 138 rv = 1; 139 break; 140 } 141 } 142 error: 143 free(newbuf); 144 ms->flags |= MAGIC_COMPRESS; 145 return rv; 146 } 147 #endif 148 /* 149 * `safe' write for sockets and pipes. 150 */ 151 private ssize_t 152 swrite(int fd, const void *buf, size_t n) 153 { 154 ssize_t rv; 155 size_t rn = n; 156 157 do 158 switch (rv = write(fd, buf, n)) { 159 case -1: 160 if (errno == EINTR) 161 continue; 162 return -1; 163 default: 164 n -= rv; 165 buf = CAST(const char *, buf) + rv; 166 break; 167 } 168 while (n > 0); 169 return rn; 170 } 171 172 173 /* 174 * `safe' read for sockets and pipes. 175 */ 176 protected ssize_t 177 sread(int fd, void *buf, size_t n, int canbepipe __attribute__ ((unused))) 178 { 179 ssize_t rv; 180 #ifdef FD_ZERO 181 ssize_t cnt; 182 #endif 183 #ifdef FIONREAD 184 int t = 0; 185 #endif 186 size_t rn = n; 187 188 if (fd == STDIN_FILENO) 189 goto nocheck; 190 191 #ifdef FIONREAD 192 if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) { 193 #ifdef FD_ZERO 194 for (cnt = 0;; cnt++) { 195 fd_set check; 196 struct timeval tout = {0, 100 * 1000}; 197 int selrv; 198 199 FD_ZERO(&check); 200 FD_SET(fd, &check); 201 202 /* 203 * Avoid soft deadlock: do not read if there 204 * is nothing to read from sockets and pipes. 205 */ 206 selrv = select(fd + 1, &check, NULL, NULL, &tout); 207 if (selrv == -1) { 208 if (errno == EINTR || errno == EAGAIN) 209 continue; 210 } else if (selrv == 0 && cnt >= 5) { 211 return 0; 212 } else 213 break; 214 } 215 #endif 216 (void)ioctl(fd, FIONREAD, &t); 217 } 218 219 if (t > 0 && (size_t)t < n) { 220 n = t; 221 rn = n; 222 } 223 #endif 224 225 nocheck: 226 do 227 switch ((rv = read(fd, buf, n))) { 228 case -1: 229 if (errno == EINTR) 230 continue; 231 return -1; 232 case 0: 233 return rn - n; 234 default: 235 n -= rv; 236 buf = ((char *)buf) + rv; 237 break; 238 } 239 while (n > 0); 240 return rn; 241 } 242 243 protected int 244 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, 245 size_t nbytes) 246 { 247 char buf[4096]; 248 ssize_t r; 249 int tfd; 250 #ifdef HAVE_MKSTEMP 251 int te; 252 #endif 253 254 (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf); 255 #ifndef HAVE_MKSTEMP 256 { 257 char *ptr = mktemp(buf); 258 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600); 259 r = errno; 260 (void)unlink(ptr); 261 errno = r; 262 } 263 #else 264 tfd = mkstemp(buf); 265 te = errno; 266 (void)unlink(buf); 267 errno = te; 268 #endif 269 if (tfd == -1) { 270 file_error(ms, errno, 271 "cannot create temporary file for pipe copy"); 272 return -1; 273 } 274 275 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes) 276 r = 1; 277 else { 278 while ((r = sread(fd, buf, sizeof(buf), 1)) > 0) 279 if (swrite(tfd, buf, (size_t)r) != r) 280 break; 281 } 282 283 switch (r) { 284 case -1: 285 file_error(ms, errno, "error copying from pipe to temp file"); 286 return -1; 287 case 0: 288 break; 289 default: 290 file_error(ms, errno, "error while writing to temp file"); 291 return -1; 292 } 293 294 /* 295 * We duplicate the file descriptor, because fclose on a 296 * tmpfile will delete the file, but any open descriptors 297 * can still access the phantom inode. 298 */ 299 if ((fd = dup2(tfd, fd)) == -1) { 300 file_error(ms, errno, "could not dup descriptor for temp file"); 301 return -1; 302 } 303 (void)close(tfd); 304 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 305 file_badseek(ms); 306 return -1; 307 } 308 return fd; 309 } 310 #if HAVE_FORK 311 #ifdef BUILTIN_DECOMPRESS 312 313 #define FHCRC (1 << 1) 314 #define FEXTRA (1 << 2) 315 #define FNAME (1 << 3) 316 #define FCOMMENT (1 << 4) 317 318 private size_t 319 uncompressgzipped(struct magic_set *ms, const unsigned char *old, 320 unsigned char **newch, size_t n) 321 { 322 unsigned char flg = old[3]; 323 size_t data_start = 10; 324 z_stream z; 325 int rc; 326 327 if (flg & FEXTRA) { 328 if (data_start+1 >= n) 329 return 0; 330 data_start += 2 + old[data_start] + old[data_start + 1] * 256; 331 } 332 if (flg & FNAME) { 333 while(data_start < n && old[data_start]) 334 data_start++; 335 data_start++; 336 } 337 if(flg & FCOMMENT) { 338 while(data_start < n && old[data_start]) 339 data_start++; 340 data_start++; 341 } 342 if(flg & FHCRC) 343 data_start += 2; 344 345 if (data_start >= n) 346 return 0; 347 if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) { 348 return 0; 349 } 350 351 /* XXX: const castaway, via strchr */ 352 z.next_in = (Bytef *)strchr((const char *)old + data_start, 353 old[data_start]); 354 z.avail_in = CAST(uint32_t, (n - data_start)); 355 z.next_out = *newch; 356 z.avail_out = HOWMANY; 357 z.zalloc = Z_NULL; 358 z.zfree = Z_NULL; 359 z.opaque = Z_NULL; 360 361 /* LINTED bug in header macro */ 362 rc = inflateInit2(&z, -15); 363 if (rc != Z_OK) { 364 file_error(ms, 0, "zlib: %s", z.msg); 365 return 0; 366 } 367 368 rc = inflate(&z, Z_SYNC_FLUSH); 369 if (rc != Z_OK && rc != Z_STREAM_END) { 370 file_error(ms, 0, "zlib: %s", z.msg); 371 return 0; 372 } 373 374 n = (size_t)z.total_out; 375 (void)inflateEnd(&z); 376 377 /* let's keep the nul-terminate tradition */ 378 (*newch)[n] = '\0'; 379 380 return n; 381 } 382 #endif 383 384 private size_t 385 uncompressbuf(struct magic_set *ms, int fd, size_t method, 386 const unsigned char *old, unsigned char **newch, size_t n) 387 { 388 int fdin[2], fdout[2]; 389 ssize_t r; 390 pid_t pid; 391 392 #ifdef BUILTIN_DECOMPRESS 393 /* FIXME: This doesn't cope with bzip2 */ 394 if (method == 2) 395 return uncompressgzipped(ms, old, newch, n); 396 #endif 397 (void)fflush(stdout); 398 (void)fflush(stderr); 399 400 if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) { 401 file_error(ms, errno, "cannot create pipe"); 402 return NODATA; 403 } 404 switch (pid = fork()) { 405 case 0: /* child */ 406 (void) close(0); 407 if (fd != -1) { 408 (void) dup(fd); 409 (void) lseek(0, (off_t)0, SEEK_SET); 410 } else { 411 (void) dup(fdin[0]); 412 (void) close(fdin[0]); 413 (void) close(fdin[1]); 414 } 415 416 (void) close(1); 417 (void) dup(fdout[1]); 418 (void) close(fdout[0]); 419 (void) close(fdout[1]); 420 #ifndef DEBUG 421 if (compr[method].silent) 422 (void)close(2); 423 #endif 424 425 (void)execvp(compr[method].argv[0], 426 (char *const *)(intptr_t)compr[method].argv); 427 #ifdef DEBUG 428 (void)fprintf(stderr, "exec `%s' failed (%s)\n", 429 compr[method].argv[0], strerror(errno)); 430 #endif 431 exit(1); 432 /*NOTREACHED*/ 433 case -1: 434 file_error(ms, errno, "could not fork"); 435 return NODATA; 436 437 default: /* parent */ 438 (void) close(fdout[1]); 439 if (fd == -1) { 440 (void) close(fdin[0]); 441 /* 442 * fork again, to avoid blocking because both 443 * pipes filled 444 */ 445 switch (fork()) { 446 case 0: /* child */ 447 (void)close(fdout[0]); 448 if (swrite(fdin[1], old, n) != (ssize_t)n) { 449 #ifdef DEBUG 450 (void)fprintf(stderr, 451 "Write failed (%s)\n", 452 strerror(errno)); 453 #endif 454 exit(1); 455 } 456 exit(0); 457 /*NOTREACHED*/ 458 459 case -1: 460 #ifdef DEBUG 461 (void)fprintf(stderr, "Fork failed (%s)\n", 462 strerror(errno)); 463 #endif 464 exit(1); 465 /*NOTREACHED*/ 466 467 default: /* parent */ 468 break; 469 } 470 (void) close(fdin[1]); 471 fdin[1] = -1; 472 } 473 474 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) { 475 #ifdef DEBUG 476 (void)fprintf(stderr, "Malloc failed (%s)\n", 477 strerror(errno)); 478 #endif 479 n = 0; 480 goto err; 481 } 482 if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) { 483 #ifdef DEBUG 484 (void)fprintf(stderr, "Read failed (%s)\n", 485 strerror(errno)); 486 #endif 487 free(*newch); 488 n = 0; 489 newch[0] = '\0'; 490 goto err; 491 } else { 492 n = r; 493 } 494 /* NUL terminate, as every buffer is handled here. */ 495 (*newch)[n] = '\0'; 496 err: 497 if (fdin[1] != -1) 498 (void) close(fdin[1]); 499 (void) close(fdout[0]); 500 #ifdef WNOHANG 501 while (waitpid(pid, NULL, WNOHANG) != -1) 502 continue; 503 #else 504 (void)wait(NULL); 505 #endif 506 (void) close(fdin[0]); 507 508 return n; 509 } 510 } 511 #endif 512