[BACK]Return to compress.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/compress.c, Revision 1.15

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