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