Annotation of src/usr.bin/file/compress.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: compress.c,v 1.13 2009/04/24 18:54:34 chl 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: #ifndef HAVE_MKSTEMP
239: {
240: char *ptr = mktemp(buf);
241: tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
242: r = errno;
243: (void)unlink(ptr);
244: errno = r;
245: }
246: #else
247: tfd = mkstemp(buf);
248: r = errno;
249: (void)unlink(buf);
250: errno = r;
251: #endif
252: if (tfd == -1) {
1.11 tedu 253: file_error(ms, errno,
254: "cannot create temporary file for pipe copy");
255: return -1;
1.6 ian 256: }
257:
1.11 tedu 258: if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
1.6 ian 259: r = 1;
260: else {
1.12 chl 261: while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
1.11 tedu 262: if (swrite(tfd, buf, (size_t)r) != r)
1.6 ian 263: break;
264: }
265:
266: switch (r) {
267: case -1:
1.11 tedu 268: file_error(ms, errno, "error copying from pipe to temp file");
269: return -1;
1.6 ian 270: case 0:
271: break;
272: default:
1.11 tedu 273: file_error(ms, errno, "error while writing to temp file");
274: return -1;
1.6 ian 275: }
276:
277: /*
278: * We duplicate the file descriptor, because fclose on a
279: * tmpfile will delete the file, but any open descriptors
280: * can still access the phantom inode.
281: */
282: if ((fd = dup2(tfd, fd)) == -1) {
1.11 tedu 283: file_error(ms, errno, "could not dup descriptor for temp file");
284: return -1;
1.6 ian 285: }
286: (void)close(tfd);
287: if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1.11 tedu 288: file_badseek(ms);
289: return -1;
1.6 ian 290: }
291: return fd;
292: }
1.1 deraadt 293:
1.13 chl 294: #ifdef BUILTIN_DECOMPRESS
1.11 tedu 295:
296: #define FHCRC (1 << 1)
297: #define FEXTRA (1 << 2)
298: #define FNAME (1 << 3)
299: #define FCOMMENT (1 << 4)
300:
301: private size_t
302: uncompressgzipped(struct magic_set *ms, const unsigned char *old,
303: unsigned char **newch, size_t n)
304: {
305: unsigned char flg = old[3];
306: size_t data_start = 10;
307: z_stream z;
308: int rc;
309:
310: if (flg & FEXTRA) {
311: if (data_start+1 >= n)
312: return 0;
313: data_start += 2 + old[data_start] + old[data_start + 1] * 256;
314: }
315: if (flg & FNAME) {
316: while(data_start < n && old[data_start])
317: data_start++;
318: data_start++;
319: }
320: if(flg & FCOMMENT) {
321: while(data_start < n && old[data_start])
322: data_start++;
323: data_start++;
324: }
325: if(flg & FHCRC)
326: data_start += 2;
327:
328: if (data_start >= n)
329: return 0;
330: if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
331: return 0;
332: }
333:
334: /* XXX: const castaway, via strchr */
335: z.next_in = (Bytef *)strchr((const char *)old + data_start,
336: old[data_start]);
337: z.avail_in = n - data_start;
338: z.next_out = *newch;
339: z.avail_out = HOWMANY;
340: z.zalloc = Z_NULL;
341: z.zfree = Z_NULL;
342: z.opaque = Z_NULL;
343:
344: rc = inflateInit2(&z, -15);
345: if (rc != Z_OK) {
346: file_error(ms, 0, "zlib: %s", z.msg);
347: return 0;
348: }
349:
350: rc = inflate(&z, Z_SYNC_FLUSH);
351: if (rc != Z_OK && rc != Z_STREAM_END) {
352: file_error(ms, 0, "zlib: %s", z.msg);
353: return 0;
354: }
355:
356: n = (size_t)z.total_out;
1.12 chl 357: (void)inflateEnd(&z);
1.11 tedu 358:
359: /* let's keep the nul-terminate tradition */
1.12 chl 360: (*newch)[n] = '\0';
1.11 tedu 361:
362: return n;
363: }
364: #endif
365:
366: private size_t
1.12 chl 367: uncompressbuf(struct magic_set *ms, int fd, size_t method,
368: const unsigned char *old, unsigned char **newch, size_t n)
1.1 deraadt 369: {
370: int fdin[2], fdout[2];
1.11 tedu 371: int r;
372:
1.13 chl 373: #ifdef BUILTIN_DECOMPRESS
374: /* FIXME: This doesn't cope with bzip2 */
1.11 tedu 375: if (method == 2)
376: return uncompressgzipped(ms, old, newch, n);
377: #endif
1.12 chl 378: (void)fflush(stdout);
379: (void)fflush(stderr);
1.1 deraadt 380:
1.12 chl 381: if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
1.11 tedu 382: file_error(ms, errno, "cannot create pipe");
1.12 chl 383: return NODATA;
1.1 deraadt 384: }
385: switch (fork()) {
386: case 0: /* child */
387: (void) close(0);
1.12 chl 388: if (fd != -1) {
389: (void) dup(fd);
390: (void) lseek(0, (off_t)0, SEEK_SET);
391: } else {
392: (void) dup(fdin[0]);
393: (void) close(fdin[0]);
394: (void) close(fdin[1]);
395: }
1.1 deraadt 396:
397: (void) close(1);
398: (void) dup(fdout[1]);
399: (void) close(fdout[0]);
400: (void) close(fdout[1]);
1.12 chl 401: #ifndef DEBUG
1.1 deraadt 402: if (compr[method].silent)
1.12 chl 403: (void)close(2);
404: #endif
1.1 deraadt 405:
1.12 chl 406: (void)execvp(compr[method].argv[0],
407: (char *const *)(intptr_t)compr[method].argv);
408: #ifdef DEBUG
409: (void)fprintf(stderr, "exec `%s' failed (%s)\n",
410: compr[method].argv[0], strerror(errno));
411: #endif
1.11 tedu 412: exit(1);
1.1 deraadt 413: /*NOTREACHED*/
414: case -1:
1.11 tedu 415: file_error(ms, errno, "could not fork");
1.12 chl 416: return NODATA;
1.1 deraadt 417:
418: default: /* parent */
419: (void) close(fdout[1]);
1.12 chl 420: if (fd == -1) {
421: (void) close(fdin[0]);
422: /*
423: * fork again, to avoid blocking because both
424: * pipes filled
425: */
426: switch (fork()) {
427: case 0: /* child */
428: (void)close(fdout[0]);
429: if (swrite(fdin[1], old, n) != (ssize_t)n) {
430: #ifdef DEBUG
431: (void)fprintf(stderr,
432: "Write failed (%s)\n",
433: strerror(errno));
434: #endif
435: exit(1);
436: }
437: exit(0);
438: /*NOTREACHED*/
439:
440: case -1:
441: #ifdef DEBUG
442: (void)fprintf(stderr, "Fork failed (%s)\n",
443: strerror(errno));
444: #endif
1.11 tedu 445: exit(1);
1.12 chl 446: /*NOTREACHED*/
1.11 tedu 447:
1.12 chl 448: default: /* parent */
449: break;
450: }
451: (void) close(fdin[1]);
452: fdin[1] = -1;
453: }
1.11 tedu 454:
455: if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
1.12 chl 456: #ifdef DEBUG
457: (void)fprintf(stderr, "Malloc failed (%s)\n",
458: strerror(errno));
459: #endif
1.11 tedu 460: n = 0;
461: goto err;
1.1 deraadt 462: }
1.12 chl 463: if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
464: #ifdef DEBUG
465: (void)fprintf(stderr, "Read failed (%s)\n",
466: strerror(errno));
467: #endif
1.1 deraadt 468: free(*newch);
1.11 tedu 469: n = 0;
470: newch[0] = '\0';
471: goto err;
472: } else {
473: n = r;
1.1 deraadt 474: }
1.11 tedu 475: /* NUL terminate, as every buffer is handled here. */
1.12 chl 476: (*newch)[n] = '\0';
1.11 tedu 477: err:
478: if (fdin[1] != -1)
479: (void) close(fdin[1]);
1.1 deraadt 480: (void) close(fdout[0]);
1.11 tedu 481: #ifdef WNOHANG
482: while (waitpid(-1, NULL, WNOHANG) != -1)
483: continue;
484: #else
485: (void)wait(NULL);
486: #endif
1.1 deraadt 487: return n;
488: }
489: }