[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.5

1.5     ! millert     1: /*     $OpenBSD: compress.c,v 1.4 1998/07/10 15:05:19 mickey Exp $     */
1.3       millert     2:
1.1       deraadt     3: /*
                      4:  * compress routines:
                      5:  *     zmagic() - returns 0 if not recognized, uncompresses and prints
                      6:  *                information if recognized
                      7:  *     uncompress(method, old, n, newch) - uncompress old into new,
                      8:  *                                         using method, return sizeof new
                      9:  */
1.4       mickey     10: #include <sys/wait.h>
1.1       deraadt    11: #include <stdio.h>
                     12: #include <stdlib.h>
                     13: #include <unistd.h>
                     14: #include <string.h>
1.4       mickey     15: #include <err.h>
1.1       deraadt    16:
                     17: #include "file.h"
                     18:
                     19: static struct {
                     20:    char *magic;
                     21:    int   maglen;
                     22:    char *argv[3];
                     23:    int  silent;
                     24: } compr[] = {
                     25:     { "\037\235", 2, { "uncompress", "-c", NULL }, 0 },        /* compressed */
                     26:     { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 },    /* gzipped */
                     27:     { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 },    /* frozen */
                     28:     { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 },    /* SCO LZH */
                     29:     /* the standard pack utilities do not accept standard input */
                     30:     { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 },    /* packed */
                     31: };
                     32:
                     33: static int ncompr = sizeof(compr) / sizeof(compr[0]);
                     34:
                     35:
1.5     ! millert    36: static int uncompress(int, const unsigned char *, unsigned char **, int);
1.1       deraadt    37:
                     38: int
                     39: zmagic(buf, nbytes)
                     40: unsigned char *buf;
                     41: int nbytes;
                     42: {
                     43:        unsigned char *newbuf;
                     44:        int newsize;
                     45:        int i;
                     46:
                     47:        for (i = 0; i < ncompr; i++) {
                     48:                if (nbytes < compr[i].maglen)
                     49:                        continue;
                     50:                if (memcmp(buf, compr[i].magic,  compr[i].maglen) == 0)
                     51:                        break;
                     52:        }
                     53:
                     54:        if (i == ncompr)
                     55:                return 0;
                     56:
                     57:        if ((newsize = uncompress(i, buf, &newbuf, nbytes)) != 0) {
                     58:                tryit(newbuf, newsize, 1);
                     59:                free(newbuf);
                     60:                printf(" (");
                     61:                tryit(buf, nbytes, 0);
                     62:                printf(")");
                     63:        }
                     64:        return 1;
                     65: }
                     66:
                     67:
                     68: static int
                     69: uncompress(method, old, newch, n)
                     70: int method;
                     71: const unsigned char *old;
                     72: unsigned char **newch;
                     73: int n;
                     74: {
                     75:        int fdin[2], fdout[2];
                     76:
                     77:        if (pipe(fdin) == -1 || pipe(fdout) == -1) {
1.4       mickey     78:                err(1, "cannot create pipe");
1.1       deraadt    79:                /*NOTREACHED*/
                     80:        }
                     81:        switch (fork()) {
                     82:        case 0: /* child */
                     83:                (void) close(0);
                     84:                (void) dup(fdin[0]);
                     85:                (void) close(fdin[0]);
                     86:                (void) close(fdin[1]);
                     87:
                     88:                (void) close(1);
                     89:                (void) dup(fdout[1]);
                     90:                (void) close(fdout[0]);
                     91:                (void) close(fdout[1]);
                     92:                if (compr[method].silent)
                     93:                    (void) close(2);
                     94:
                     95:                execvp(compr[method].argv[0], compr[method].argv);
1.4       mickey     96:                err(1, "could not execute `%s'", compr[method].argv[0]);
1.1       deraadt    97:                /*NOTREACHED*/
                     98:        case -1:
1.4       mickey     99:                err(1, "could not fork");
1.1       deraadt   100:                /*NOTREACHED*/
                    101:
                    102:        default: /* parent */
                    103:                (void) close(fdin[0]);
                    104:                (void) close(fdout[1]);
                    105:                if (write(fdin[1], old, n) != n) {
1.4       mickey    106:                        err(1, "write failed");
1.1       deraadt   107:                        /*NOTREACHED*/
                    108:                }
                    109:                (void) close(fdin[1]);
                    110:                if ((*newch = (unsigned char *) malloc(n)) == NULL) {
1.4       mickey    111:                        err(1, "malloc");
1.1       deraadt   112:                        /*NOTREACHED*/
                    113:                }
                    114:                if ((n = read(fdout[0], *newch, n)) <= 0) {
                    115:                        free(*newch);
1.4       mickey    116:                        err(1, "read failed");
1.1       deraadt   117:                        /*NOTREACHED*/
                    118:                }
                    119:                (void) close(fdout[0]);
                    120:                (void) wait(NULL);
                    121:                return n;
                    122:        }
                    123: }