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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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
38 FILE_RCSID("@(#)$File: compress.c,v 1.68 2011/12/08 12:38:24 rrt Exp $")
49 #include <sys/ioctl.h>
51 #ifdef HAVE_SYS_WAIT_H
54 #if defined(HAVE_SYS_TIME_H)
57 #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
58 #define BUILTIN_DECOMPRESS
62 private const struct {
68 { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
69 /* Uncompress can get stuck; so use gzip first if we have it
70 * Idea from Damien Clark, thanks! */
71 { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
72 { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
73 { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
74 { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
75 /* the standard pack utilities do not accept standard input */
76 { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
77 { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */
78 /* ...only first file examined */
79 { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
80 { "LZIP", 4, { "lzip", "-cdq", NULL }, 1 },
81 { "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */
82 { "LRZI", 4, { "lrzip", "-dqo-", NULL }, 1 }, /* LRZIP */
85 #define NODATA ((size_t)~0)
87 private ssize_t swrite(int, const void *, size_t);
89 private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
90 private size_t uncompressbuf(struct magic_set *, int, size_t,
91 const unsigned char *, unsigned char **, size_t);
92 #ifdef BUILTIN_DECOMPRESS
93 private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
94 unsigned char **, size_t);
98 file_zmagic(struct magic_set *ms, int fd, const char *name,
99 const unsigned char *buf, size_t nbytes)
101 unsigned char *newbuf = NULL;
104 int mime = ms->flags & MAGIC_MIME;
106 if ((ms->flags & MAGIC_COMPRESS) == 0)
109 for (i = 0; i < ncompr; i++) {
110 if (nbytes < compr[i].maglen)
112 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
113 (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
114 nbytes)) != NODATA) {
115 ms->flags &= ~MAGIC_COMPRESS;
117 if (file_buffer(ms, -1, name, newbuf, nsz) == -1)
120 if (mime == MAGIC_MIME || mime == 0) {
121 if (file_printf(ms, mime ?
122 " compressed-encoding=" : " (") == -1)
126 if ((mime == 0 || mime & MAGIC_MIME_ENCODING) &&
127 file_buffer(ms, -1, NULL, buf, nbytes) == -1)
130 if (!mime && file_printf(ms, ")") == -1)
138 ms->flags |= MAGIC_COMPRESS;
143 * `safe' write for sockets and pipes.
146 swrite(int fd, const void *buf, size_t n)
152 switch (rv = write(fd, buf, n)) {
159 buf = CAST(const char *, buf) + rv;
168 * `safe' read for sockets and pipes.
171 sread(int fd, void *buf, size_t n, int canbepipe __attribute__ ((unused)))
182 if (fd == STDIN_FILENO)
186 if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {
188 for (cnt = 0;; cnt++) {
190 struct timeval tout = {0, 100 * 1000};
197 * Avoid soft deadlock: do not read if there
198 * is nothing to read from sockets and pipes.
200 selrv = select(fd + 1, &check, NULL, NULL, &tout);
202 if (errno == EINTR || errno == EAGAIN)
204 } else if (selrv == 0 && cnt >= 5) {
210 (void)ioctl(fd, FIONREAD, &t);
213 if (t > 0 && (size_t)t < n) {
221 switch ((rv = read(fd, buf, n))) {
230 buf = ((char *)buf) + rv;
238 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
248 (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
251 char *ptr = mktemp(buf);
252 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
264 file_error(ms, errno,
265 "cannot create temporary file for pipe copy");
269 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
272 while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
273 if (swrite(tfd, buf, (size_t)r) != r)
279 file_error(ms, errno, "error copying from pipe to temp file");
284 file_error(ms, errno, "error while writing to temp file");
289 * We duplicate the file descriptor, because fclose on a
290 * tmpfile will delete the file, but any open descriptors
291 * can still access the phantom inode.
293 if ((fd = dup2(tfd, fd)) == -1) {
294 file_error(ms, errno, "could not dup descriptor for temp file");
298 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
305 #ifdef BUILTIN_DECOMPRESS
307 #define FHCRC (1 << 1)
308 #define FEXTRA (1 << 2)
309 #define FNAME (1 << 3)
310 #define FCOMMENT (1 << 4)
313 uncompressgzipped(struct magic_set *ms, const unsigned char *old,
314 unsigned char **newch, size_t n)
316 unsigned char flg = old[3];
317 size_t data_start = 10;
322 if (data_start+1 >= n)
324 data_start += 2 + old[data_start] + old[data_start + 1] * 256;
327 while(data_start < n && old[data_start])
332 while(data_start < n && old[data_start])
341 if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
345 /* XXX: const castaway, via strchr */
346 z.next_in = (Bytef *)strchr((const char *)old + data_start,
348 z.avail_in = CAST(uint32_t, (n - data_start));
350 z.avail_out = HOWMANY;
355 /* LINTED bug in header macro */
356 rc = inflateInit2(&z, -15);
358 file_error(ms, 0, "zlib: %s", z.msg);
362 rc = inflate(&z, Z_SYNC_FLUSH);
363 if (rc != Z_OK && rc != Z_STREAM_END) {
364 file_error(ms, 0, "zlib: %s", z.msg);
368 n = (size_t)z.total_out;
369 (void)inflateEnd(&z);
371 /* let's keep the nul-terminate tradition */
379 uncompressbuf(struct magic_set *ms, int fd, size_t method,
380 const unsigned char *old, unsigned char **newch, size_t n)
382 int fdin[2], fdout[2];
386 #ifdef BUILTIN_DECOMPRESS
387 /* FIXME: This doesn't cope with bzip2 */
389 return uncompressgzipped(ms, old, newch, n);
391 (void)fflush(stdout);
392 (void)fflush(stderr);
394 if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
395 file_error(ms, errno, "cannot create pipe");
398 switch (pid = fork()) {
403 (void) lseek(0, (off_t)0, SEEK_SET);
406 (void) close(fdin[0]);
407 (void) close(fdin[1]);
411 (void) dup(fdout[1]);
412 (void) close(fdout[0]);
413 (void) close(fdout[1]);
415 if (compr[method].silent)
419 (void)execvp(compr[method].argv[0],
420 (char *const *)(intptr_t)compr[method].argv);
422 (void)fprintf(stderr, "exec `%s' failed (%s)\n",
423 compr[method].argv[0], strerror(errno));
428 file_error(ms, errno, "could not fork");
431 default: /* parent */
432 (void) close(fdout[1]);
434 (void) close(fdin[0]);
436 * fork again, to avoid blocking because both
441 (void)close(fdout[0]);
442 if (swrite(fdin[1], old, n) != (ssize_t)n) {
444 (void)fprintf(stderr,
445 "Write failed (%s)\n",
455 (void)fprintf(stderr, "Fork failed (%s)\n",
461 default: /* parent */
464 (void) close(fdin[1]);
468 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
470 (void)fprintf(stderr, "Malloc failed (%s)\n",
476 if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
478 (void)fprintf(stderr, "Read failed (%s)\n",
488 /* NUL terminate, as every buffer is handled here. */
492 (void) close(fdin[1]);
493 (void) close(fdout[0]);
495 while (waitpid(pid, NULL, WNOHANG) != -1)
500 (void) close(fdin[0]);