Annotation of embedaddon/php/ext/fileinfo/libmagic/compress.c, revision 1.1
1.1 ! misho 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 "config.h"
! 36: #include "file.h"
! 37:
! 38: #ifndef lint
! 39: FILE_RCSID("@(#)$File: compress.c,v 1.63 2009/03/23 14:21:51 christos Exp $")
! 40: #endif
! 41:
! 42: #include "magic.h"
! 43: #include <stdlib.h>
! 44: #ifdef HAVE_UNISTD_H
! 45: #include <unistd.h>
! 46: #endif
! 47: #include <string.h>
! 48: #include <errno.h>
! 49: #include <sys/types.h>
! 50: #ifndef PHP_WIN32
! 51: #include <sys/ioctl.h>
! 52: #endif
! 53: #ifdef HAVE_SYS_WAIT_H
! 54: #include <sys/wait.h>
! 55: #endif
! 56: #if defined(HAVE_SYS_TIME_H)
! 57: #include <sys/time.h>
! 58: #endif
! 59: #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
! 60: #define BUILTIN_DECOMPRESS
! 61: #include <zlib.h>
! 62: #endif
! 63:
! 64: #undef FIONREAD
! 65:
! 66:
! 67: private const struct {
! 68: const char magic[8];
! 69: size_t maglen;
! 70: const char *argv[3];
! 71: int silent;
! 72: } compr[] = {
! 73: { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
! 74: /* Uncompress can get stuck; so use gzip first if we have it
! 75: * Idea from Damien Clark, thanks! */
! 76: { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
! 77: { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
! 78: { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
! 79: { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
! 80: /* the standard pack utilities do not accept standard input */
! 81: { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
! 82: { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */
! 83: /* ...only first file examined */
! 84: { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
! 85: { "LZIP", 4, { "lzip", "-cdq", NULL }, 1 },
! 86: { "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */
! 87: };
! 88:
! 89: #define NODATA ((size_t)~0)
! 90:
! 91: private ssize_t swrite(int, const void *, size_t);
! 92: #ifdef PHP_FILEINFO_UNCOMPRESS
! 93: private size_t uncompressbuf(struct magic_set *, int, size_t,
! 94: const unsigned char *, unsigned char **, size_t);
! 95: #ifdef BUILTIN_DECOMPRESS
! 96: private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
! 97: unsigned char **, size_t);
! 98: #endif
! 99:
! 100: protected int
! 101: file_zmagic(struct magic_set *ms, int fd, const char *name,
! 102: const unsigned char *buf, size_t nbytes)
! 103: {
! 104: unsigned char *newbuf = NULL;
! 105: size_t i, nsz;
! 106: int rv = 0;
! 107: int mime = ms->flags & MAGIC_MIME;
! 108: size_t ncompr;
! 109:
! 110: if ((ms->flags & MAGIC_COMPRESS) == 0)
! 111: return 0;
! 112:
! 113: ncompr = sizeof(compr) / sizeof(compr[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: if (newbuf)
! 144: efree(newbuf);
! 145: ms->flags |= MAGIC_COMPRESS;
! 146: return rv;
! 147: }
! 148: #endif
! 149:
! 150: /*
! 151: * `safe' write for sockets and pipes.
! 152: */
! 153: private ssize_t
! 154: swrite(int fd, const void *buf, size_t n)
! 155: {
! 156: int rv;
! 157: size_t rn = n;
! 158:
! 159: do
! 160: switch (rv = write(fd, buf, n)) {
! 161: case -1:
! 162: if (errno == EINTR)
! 163: continue;
! 164: return -1;
! 165: default:
! 166: n -= rv;
! 167: buf = ((const char *)buf) + rv;
! 168: break;
! 169: }
! 170: while (n > 0);
! 171: return rn;
! 172: }
! 173:
! 174:
! 175: /*
! 176: * `safe' read for sockets and pipes.
! 177: */
! 178: protected ssize_t
! 179: sread(int fd, void *buf, size_t n, int canbepipe)
! 180: {
! 181: int rv;
! 182: #ifdef FIONREAD
! 183: int t = 0;
! 184: #endif
! 185: size_t rn = n;
! 186:
! 187: if (fd == STDIN_FILENO)
! 188: goto nocheck;
! 189:
! 190: #ifdef FIONREAD
! 191: if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {
! 192: #ifdef FD_ZERO
! 193: int cnt;
! 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: int r, tfd;
! 249:
! 250: (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
! 251: #ifndef HAVE_MKSTEMP
! 252: {
! 253: char *ptr = mktemp(buf);
! 254: tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
! 255: r = errno;
! 256: (void)unlink(ptr);
! 257: errno = r;
! 258: }
! 259: #else
! 260: tfd = mkstemp(buf);
! 261: r = errno;
! 262: (void)unlink(buf);
! 263: errno = r;
! 264: #endif
! 265: if (tfd == -1) {
! 266: file_error(ms, errno,
! 267: "cannot create temporary file for pipe copy");
! 268: return -1;
! 269: }
! 270:
! 271: if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
! 272: r = 1;
! 273: else {
! 274: while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
! 275: if (swrite(tfd, buf, (size_t)r) != r)
! 276: break;
! 277: }
! 278:
! 279: switch (r) {
! 280: case -1:
! 281: file_error(ms, errno, "error copying from pipe to temp file");
! 282: return -1;
! 283: case 0:
! 284: break;
! 285: default:
! 286: file_error(ms, errno, "error while writing to temp file");
! 287: return -1;
! 288: }
! 289:
! 290: /*
! 291: * We duplicate the file descriptor, because fclose on a
! 292: * tmpfile will delete the file, but any open descriptors
! 293: * can still access the phantom inode.
! 294: */
! 295: if ((fd = dup2(tfd, fd)) == -1) {
! 296: file_error(ms, errno, "could not dup descriptor for temp file");
! 297: return -1;
! 298: }
! 299: (void)close(tfd);
! 300: if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
! 301: file_badseek(ms);
! 302: return -1;
! 303: }
! 304: return fd;
! 305: }
! 306:
! 307: #ifdef PHP_FILEINFO_UNCOMPRESS
! 308: #ifdef BUILTIN_DECOMPRESS
! 309:
! 310: #define FHCRC (1 << 1)
! 311: #define FEXTRA (1 << 2)
! 312: #define FNAME (1 << 3)
! 313: #define FCOMMENT (1 << 4)
! 314:
! 315:
! 316: private size_t
! 317: uncompressgzipped(struct magic_set *ms, const unsigned char *old,
! 318: unsigned char **newch, size_t n)
! 319: {
! 320: unsigned char flg = old[3];
! 321: size_t data_start = 10;
! 322: z_stream z;
! 323: int rc;
! 324:
! 325: if (flg & FEXTRA) {
! 326: if (data_start+1 >= n)
! 327: return 0;
! 328: data_start += 2 + old[data_start] + old[data_start + 1] * 256;
! 329: }
! 330: if (flg & FNAME) {
! 331: while(data_start < n && old[data_start])
! 332: data_start++;
! 333: data_start++;
! 334: }
! 335: if(flg & FCOMMENT) {
! 336: while(data_start < n && old[data_start])
! 337: data_start++;
! 338: data_start++;
! 339: }
! 340: if(flg & FHCRC)
! 341: data_start += 2;
! 342:
! 343: if (data_start >= n)
! 344: return 0;
! 345: *newch = (unsigned char *)emalloc(HOWMANY + 1));
! 346:
! 347: /* XXX: const castaway, via strchr */
! 348: z.next_in = (Bytef *)strchr((const char *)old + data_start,
! 349: old[data_start]);
! 350: z.avail_in = n - data_start;
! 351: z.next_out = *newch;
! 352: z.avail_out = HOWMANY;
! 353: z.zalloc = Z_NULL;
! 354: z.zfree = Z_NULL;
! 355: z.opaque = Z_NULL;
! 356:
! 357: rc = inflateInit2(&z, -15);
! 358: if (rc != Z_OK) {
! 359: file_error(ms, 0, "zlib: %s", z.msg);
! 360: return 0;
! 361: }
! 362:
! 363: rc = inflate(&z, Z_SYNC_FLUSH);
! 364: if (rc != Z_OK && rc != Z_STREAM_END) {
! 365: file_error(ms, 0, "zlib: %s", z.msg);
! 366: return 0;
! 367: }
! 368:
! 369: n = (size_t)z.total_out;
! 370: (void)inflateEnd(&z);
! 371:
! 372: /* let's keep the nul-terminate tradition */
! 373: (*newch)[n] = '\0';
! 374:
! 375: return n;
! 376: }
! 377: #endif
! 378:
! 379: private size_t
! 380: uncompressbuf(struct magic_set *ms, int fd, size_t method,
! 381: const unsigned char *old, unsigned char **newch, size_t n)
! 382: {
! 383: int fdin[2], fdout[2];
! 384: int r;
! 385:
! 386: #ifdef BUILTIN_DECOMPRESS
! 387: /* FIXME: This doesn't cope with bzip2 */
! 388: if (method == 2)
! 389: return uncompressgzipped(ms, old, newch, n);
! 390: #endif
! 391: (void)fflush(stdout);
! 392: (void)fflush(stderr);
! 393:
! 394: if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
! 395: file_error(ms, errno, "cannot create pipe");
! 396: return NODATA;
! 397: }
! 398: switch (fork()) {
! 399: case 0: /* child */
! 400: (void) close(0);
! 401: if (fd != -1) {
! 402: (void) dup(fd);
! 403: (void) lseek(0, (off_t)0, SEEK_SET);
! 404: } else {
! 405: (void) dup(fdin[0]);
! 406: (void) close(fdin[0]);
! 407: (void) close(fdin[1]);
! 408: }
! 409:
! 410: (void) close(1);
! 411: (void) dup(fdout[1]);
! 412: (void) close(fdout[0]);
! 413: (void) close(fdout[1]);
! 414: #ifndef DEBUG
! 415: if (compr[method].silent)
! 416: (void)close(2);
! 417: #endif
! 418:
! 419: (void)execvp(compr[method].argv[0],
! 420: (char *const *)(intptr_t)compr[method].argv);
! 421: #ifdef DEBUG
! 422: (void)fprintf(stderr, "exec `%s' failed (%s)\n",
! 423: compr[method].argv[0], strerror(errno));
! 424: #endif
! 425: exit(1);
! 426: /*NOTREACHED*/
! 427: case -1:
! 428: file_error(ms, errno, "could not fork");
! 429: return NODATA;
! 430:
! 431: default: /* parent */
! 432: (void) close(fdout[1]);
! 433: if (fd == -1) {
! 434: (void) close(fdin[0]);
! 435: /*
! 436: * fork again, to avoid blocking because both
! 437: * pipes filled
! 438: */
! 439: switch (fork()) {
! 440: case 0: /* child */
! 441: (void)close(fdout[0]);
! 442: if (swrite(fdin[1], old, n) != (ssize_t)n) {
! 443: #ifdef DEBUG
! 444: (void)fprintf(stderr,
! 445: "Write failed (%s)\n",
! 446: strerror(errno));
! 447: #endif
! 448: exit(1);
! 449: }
! 450: exit(0);
! 451: /*NOTREACHED*/
! 452:
! 453: case -1:
! 454: #ifdef DEBUG
! 455: (void)fprintf(stderr, "Fork failed (%s)\n",
! 456: strerror(errno));
! 457: #endif
! 458: exit(1);
! 459: /*NOTREACHED*/
! 460:
! 461: default: /* parent */
! 462: break;
! 463: }
! 464: (void) close(fdin[1]);
! 465: fdin[1] = -1;
! 466: }
! 467:
! 468: *newch = (unsigned char *) emalloc(HOWMANY + 1);
! 469:
! 470: if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
! 471: #ifdef DEBUG
! 472: (void)fprintf(stderr, "Read failed (%s)\n",
! 473: strerror(errno));
! 474: #endif
! 475: efree(*newch);
! 476: n = 0;
! 477: newch[0] = '\0';
! 478: goto err;
! 479: } else {
! 480: n = r;
! 481: }
! 482: /* NUL terminate, as every buffer is handled here. */
! 483: (*newch)[n] = '\0';
! 484: err:
! 485: if (fdin[1] != -1)
! 486: (void) close(fdin[1]);
! 487: (void) close(fdout[0]);
! 488: #ifdef WNOHANG
! 489: while (waitpid(-1, NULL, WNOHANG) != -1)
! 490: continue;
! 491: #else
! 492: (void)wait(NULL);
! 493: #endif
! 494: (void) close(fdin[0]);
! 495:
! 496: return n;
! 497: }
! 498: }
! 499: #endif /* if PHP_FILEINFO_UNCOMPRESS */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>