2004-08-09 08:45:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Ian F. Darwin 1986-1995.
|
|
|
|
* Software written by Ian F. Darwin and others;
|
|
|
|
* maintained 1995-present by Christos Zoulas and others.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice immediately at the beginning of the file, without modification,
|
|
|
|
* this list of conditions, and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* compress routines:
|
|
|
|
* zmagic() - returns 0 if not recognized, uncompresses and prints
|
|
|
|
* information if recognized
|
|
|
|
* uncompress(method, old, n, newch) - uncompress old into new,
|
|
|
|
* using method, return sizeof new
|
|
|
|
*/
|
2000-11-05 08:33:55 +00:00
|
|
|
#include "file.h"
|
2009-05-02 06:25:51 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
2011-10-06 06:01:12 +00:00
|
|
|
FILE_RCSID("@(#)$File: compress.c,v 1.67 2011/09/01 12:12:37 christos Exp $")
|
2009-05-02 06:25:51 +00:00
|
|
|
#endif
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
#include "magic.h"
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <stdlib.h>
|
2000-11-05 08:33:55 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <unistd.h>
|
2000-11-05 08:33:55 +00:00
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <string.h>
|
2004-08-09 08:45:41 +00:00
|
|
|
#include <errno.h>
|
2011-10-06 06:01:12 +00:00
|
|
|
#ifndef __MINGW32__
|
2007-05-24 16:12:23 +00:00
|
|
|
#include <sys/ioctl.h>
|
2011-10-06 06:01:12 +00:00
|
|
|
#endif
|
2000-11-05 08:33:55 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <sys/wait.h>
|
2000-11-05 08:33:55 +00:00
|
|
|
#endif
|
2007-05-24 21:59:38 +00:00
|
|
|
#if defined(HAVE_SYS_TIME_H)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2008-01-13 20:35:16 +00:00
|
|
|
#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
|
|
|
|
#define BUILTIN_DECOMPRESS
|
2002-09-16 05:54:26 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-02 02:57:16 +00:00
|
|
|
private const struct {
|
|
|
|
const char magic[8];
|
2004-08-09 08:45:41 +00:00
|
|
|
size_t maglen;
|
2009-01-02 02:57:16 +00:00
|
|
|
const char *argv[3];
|
2004-08-09 08:45:41 +00:00
|
|
|
int silent;
|
1994-09-03 19:16:28 +00:00
|
|
|
} compr[] = {
|
2000-11-05 08:33:55 +00:00
|
|
|
{ "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
|
2001-04-25 07:41:21 +00:00
|
|
|
/* Uncompress can get stuck; so use gzip first if we have it
|
|
|
|
* Idea from Damien Clark, thanks! */
|
|
|
|
{ "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
|
2000-11-05 08:33:55 +00:00
|
|
|
{ "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
|
|
|
|
{ "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
|
|
|
|
{ "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
|
|
|
|
/* the standard pack utilities do not accept standard input */
|
|
|
|
{ "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
|
2006-06-19 07:52:15 +00:00
|
|
|
{ "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */
|
|
|
|
/* ...only first file examined */
|
2001-07-30 03:09:46 +00:00
|
|
|
{ "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
|
2009-05-02 06:25:51 +00:00
|
|
|
{ "LZIP", 4, { "lzip", "-cdq", NULL }, 1 },
|
2009-05-18 22:27:42 +00:00
|
|
|
{ "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */
|
2011-10-06 06:01:12 +00:00
|
|
|
{ "LRZI", 4, { "lrzip", "-dqo-", NULL }, 1 }, /* LRZIP */
|
1994-09-03 19:16:28 +00:00
|
|
|
};
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
#define NODATA ((size_t)~0)
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private ssize_t swrite(int, const void *, size_t);
|
2011-10-06 06:01:12 +00:00
|
|
|
#if HAVE_FORK
|
|
|
|
private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
|
2006-06-19 07:52:15 +00:00
|
|
|
private size_t uncompressbuf(struct magic_set *, int, size_t,
|
|
|
|
const unsigned char *, unsigned char **, size_t);
|
2008-01-13 20:35:16 +00:00
|
|
|
#ifdef BUILTIN_DECOMPRESS
|
2004-08-09 08:45:41 +00:00
|
|
|
private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
|
|
|
|
unsigned char **, size_t);
|
2002-09-16 05:54:26 +00:00
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
protected int
|
2007-05-24 21:59:38 +00:00
|
|
|
file_zmagic(struct magic_set *ms, int fd, const char *name,
|
|
|
|
const unsigned char *buf, size_t nbytes)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
2004-08-09 08:45:41 +00:00
|
|
|
unsigned char *newbuf = NULL;
|
|
|
|
size_t i, nsz;
|
|
|
|
int rv = 0;
|
2008-01-13 20:35:16 +00:00
|
|
|
int mime = ms->flags & MAGIC_MIME;
|
2004-08-09 08:45:41 +00:00
|
|
|
|
|
|
|
if ((ms->flags & MAGIC_COMPRESS) == 0)
|
|
|
|
return 0;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ncompr; i++) {
|
|
|
|
if (nbytes < compr[i].maglen)
|
|
|
|
continue;
|
2000-11-05 08:33:55 +00:00
|
|
|
if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
|
2006-06-19 07:52:15 +00:00
|
|
|
(nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
|
2007-05-24 16:12:23 +00:00
|
|
|
nbytes)) != NODATA) {
|
2004-08-09 08:45:41 +00:00
|
|
|
ms->flags &= ~MAGIC_COMPRESS;
|
|
|
|
rv = -1;
|
2007-05-24 21:59:38 +00:00
|
|
|
if (file_buffer(ms, -1, name, newbuf, nsz) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
goto error;
|
2008-01-13 20:35:16 +00:00
|
|
|
|
|
|
|
if (mime == MAGIC_MIME || mime == 0) {
|
|
|
|
if (file_printf(ms, mime ?
|
|
|
|
" compressed-encoding=" : " (") == -1)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mime == 0 || mime & MAGIC_MIME_ENCODING) &&
|
|
|
|
file_buffer(ms, -1, NULL, buf, nbytes) == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
goto error;
|
2008-01-13 20:35:16 +00:00
|
|
|
|
|
|
|
if (!mime && file_printf(ms, ")") == -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
goto error;
|
|
|
|
rv = 1;
|
|
|
|
break;
|
2000-11-05 08:33:55 +00:00
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2004-08-09 08:45:41 +00:00
|
|
|
error:
|
|
|
|
if (newbuf)
|
|
|
|
free(newbuf);
|
|
|
|
ms->flags |= MAGIC_COMPRESS;
|
|
|
|
return rv;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2011-10-06 06:01:12 +00:00
|
|
|
#endif
|
2001-04-25 07:41:21 +00:00
|
|
|
/*
|
|
|
|
* `safe' write for sockets and pipes.
|
|
|
|
*/
|
2004-08-09 08:45:41 +00:00
|
|
|
private ssize_t
|
2002-09-16 05:54:26 +00:00
|
|
|
swrite(int fd, const void *buf, size_t n)
|
2001-04-25 07:41:21 +00:00
|
|
|
{
|
2011-10-06 06:01:12 +00:00
|
|
|
ssize_t rv;
|
2001-04-25 07:41:21 +00:00
|
|
|
size_t rn = n;
|
|
|
|
|
|
|
|
do
|
|
|
|
switch (rv = write(fd, buf, n)) {
|
|
|
|
case -1:
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
n -= rv;
|
2011-10-06 06:01:12 +00:00
|
|
|
buf = CAST(const char *, buf) + rv;
|
2001-04-25 07:41:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (n > 0);
|
|
|
|
return rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* `safe' read for sockets and pipes.
|
|
|
|
*/
|
2007-05-24 16:12:23 +00:00
|
|
|
protected ssize_t
|
2011-10-06 06:01:12 +00:00
|
|
|
sread(int fd, void *buf, size_t n, int canbepipe __attribute__ ((unused)))
|
2001-04-25 07:41:21 +00:00
|
|
|
{
|
2011-10-06 06:01:12 +00:00
|
|
|
ssize_t rv;
|
|
|
|
#ifdef FD_ZERO
|
|
|
|
ssize_t cnt;
|
|
|
|
#endif
|
2007-05-24 16:12:23 +00:00
|
|
|
#ifdef FIONREAD
|
|
|
|
int t = 0;
|
|
|
|
#endif
|
2001-04-25 07:41:21 +00:00
|
|
|
size_t rn = n;
|
|
|
|
|
2007-05-24 16:12:23 +00:00
|
|
|
if (fd == STDIN_FILENO)
|
|
|
|
goto nocheck;
|
|
|
|
|
|
|
|
#ifdef FIONREAD
|
2007-05-24 21:59:38 +00:00
|
|
|
if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {
|
2007-05-24 16:12:23 +00:00
|
|
|
#ifdef FD_ZERO
|
2007-05-24 21:59:38 +00:00
|
|
|
for (cnt = 0;; cnt++) {
|
2007-05-24 16:12:23 +00:00
|
|
|
fd_set check;
|
|
|
|
struct timeval tout = {0, 100 * 1000};
|
2007-05-24 21:59:38 +00:00
|
|
|
int selrv;
|
2007-05-24 16:12:23 +00:00
|
|
|
|
|
|
|
FD_ZERO(&check);
|
|
|
|
FD_SET(fd, &check);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid soft deadlock: do not read if there
|
|
|
|
* is nothing to read from sockets and pipes.
|
|
|
|
*/
|
2007-05-24 21:59:38 +00:00
|
|
|
selrv = select(fd + 1, &check, NULL, NULL, &tout);
|
|
|
|
if (selrv == -1) {
|
2007-05-24 16:12:23 +00:00
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
continue;
|
2007-05-24 21:59:38 +00:00
|
|
|
} else if (selrv == 0 && cnt >= 5) {
|
2007-05-24 16:12:23 +00:00
|
|
|
return 0;
|
2007-05-24 21:59:38 +00:00
|
|
|
} else
|
|
|
|
break;
|
2007-05-24 16:12:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
(void)ioctl(fd, FIONREAD, &t);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t > 0 && (size_t)t < n) {
|
|
|
|
n = t;
|
|
|
|
rn = n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nocheck:
|
2001-04-25 07:41:21 +00:00
|
|
|
do
|
2007-05-24 16:12:23 +00:00
|
|
|
switch ((rv = read(fd, buf, n))) {
|
2001-04-25 07:41:21 +00:00
|
|
|
case -1:
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
2002-09-16 05:54:26 +00:00
|
|
|
case 0:
|
|
|
|
return rn - n;
|
2001-04-25 07:41:21 +00:00
|
|
|
default:
|
|
|
|
n -= rv;
|
|
|
|
buf = ((char *)buf) + rv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (n > 0);
|
|
|
|
return rn;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
protected int
|
|
|
|
file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
|
|
|
|
size_t nbytes)
|
2002-09-16 05:54:26 +00:00
|
|
|
{
|
|
|
|
char buf[4096];
|
2011-10-06 06:01:12 +00:00
|
|
|
ssize_t r;
|
|
|
|
int tfd;
|
|
|
|
#ifdef HAVE_MKSTEMP
|
|
|
|
int te;
|
|
|
|
#endif
|
2002-09-16 05:54:26 +00:00
|
|
|
|
2009-05-02 06:25:51 +00:00
|
|
|
(void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
|
2002-09-16 05:54:26 +00:00
|
|
|
#ifndef HAVE_MKSTEMP
|
|
|
|
{
|
|
|
|
char *ptr = mktemp(buf);
|
|
|
|
tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
|
|
|
|
r = errno;
|
|
|
|
(void)unlink(ptr);
|
|
|
|
errno = r;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
tfd = mkstemp(buf);
|
2011-10-06 06:01:12 +00:00
|
|
|
te = errno;
|
2002-09-16 05:54:26 +00:00
|
|
|
(void)unlink(buf);
|
2011-10-06 06:01:12 +00:00
|
|
|
errno = te;
|
2002-09-16 05:54:26 +00:00
|
|
|
#endif
|
|
|
|
if (tfd == -1) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno,
|
|
|
|
"cannot create temporary file for pipe copy");
|
|
|
|
return -1;
|
2002-09-16 05:54:26 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
|
2002-09-16 05:54:26 +00:00
|
|
|
r = 1;
|
|
|
|
else {
|
2007-05-24 21:59:38 +00:00
|
|
|
while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
|
2004-08-09 08:45:41 +00:00
|
|
|
if (swrite(tfd, buf, (size_t)r) != r)
|
2002-09-16 05:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case -1:
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "error copying from pipe to temp file");
|
|
|
|
return -1;
|
2002-09-16 05:54:26 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "error while writing to temp file");
|
|
|
|
return -1;
|
2002-09-16 05:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We duplicate the file descriptor, because fclose on a
|
|
|
|
* tmpfile will delete the file, but any open descriptors
|
|
|
|
* can still access the phantom inode.
|
|
|
|
*/
|
|
|
|
if ((fd = dup2(tfd, fd)) == -1) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "could not dup descriptor for temp file");
|
|
|
|
return -1;
|
2002-09-16 05:54:26 +00:00
|
|
|
}
|
|
|
|
(void)close(tfd);
|
|
|
|
if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_badseek(ms);
|
|
|
|
return -1;
|
2002-09-16 05:54:26 +00:00
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2011-10-06 06:01:12 +00:00
|
|
|
#if HAVE_FORK
|
2008-01-13 20:35:16 +00:00
|
|
|
#ifdef BUILTIN_DECOMPRESS
|
2002-09-16 05:54:26 +00:00
|
|
|
|
|
|
|
#define FHCRC (1 << 1)
|
|
|
|
#define FEXTRA (1 << 2)
|
|
|
|
#define FNAME (1 << 3)
|
|
|
|
#define FCOMMENT (1 << 4)
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private size_t
|
|
|
|
uncompressgzipped(struct magic_set *ms, const unsigned char *old,
|
|
|
|
unsigned char **newch, size_t n)
|
2002-09-16 05:54:26 +00:00
|
|
|
{
|
|
|
|
unsigned char flg = old[3];
|
2004-08-09 08:45:41 +00:00
|
|
|
size_t data_start = 10;
|
2002-09-16 05:54:26 +00:00
|
|
|
z_stream z;
|
|
|
|
int rc;
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (flg & FEXTRA) {
|
|
|
|
if (data_start+1 >= n)
|
|
|
|
return 0;
|
2002-09-16 05:54:26 +00:00
|
|
|
data_start += 2 + old[data_start] + old[data_start + 1] * 256;
|
2004-08-09 08:45:41 +00:00
|
|
|
}
|
2002-09-16 05:54:26 +00:00
|
|
|
if (flg & FNAME) {
|
2004-08-09 08:45:41 +00:00
|
|
|
while(data_start < n && old[data_start])
|
2002-09-16 05:54:26 +00:00
|
|
|
data_start++;
|
|
|
|
data_start++;
|
|
|
|
}
|
|
|
|
if(flg & FCOMMENT) {
|
2004-08-09 08:45:41 +00:00
|
|
|
while(data_start < n && old[data_start])
|
2002-09-16 05:54:26 +00:00
|
|
|
data_start++;
|
|
|
|
data_start++;
|
|
|
|
}
|
|
|
|
if(flg & FHCRC)
|
|
|
|
data_start += 2;
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
if (data_start >= n)
|
|
|
|
return 0;
|
2009-01-02 02:57:16 +00:00
|
|
|
if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
|
2002-09-16 05:54:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
/* XXX: const castaway, via strchr */
|
|
|
|
z.next_in = (Bytef *)strchr((const char *)old + data_start,
|
|
|
|
old[data_start]);
|
2011-10-06 06:01:12 +00:00
|
|
|
z.avail_in = CAST(uint32_t, (n - data_start));
|
2002-09-16 05:54:26 +00:00
|
|
|
z.next_out = *newch;
|
|
|
|
z.avail_out = HOWMANY;
|
|
|
|
z.zalloc = Z_NULL;
|
|
|
|
z.zfree = Z_NULL;
|
|
|
|
z.opaque = Z_NULL;
|
|
|
|
|
2011-10-06 06:01:12 +00:00
|
|
|
/* LINTED bug in header macro */
|
2002-09-16 05:54:26 +00:00
|
|
|
rc = inflateInit2(&z, -15);
|
|
|
|
if (rc != Z_OK) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, 0, "zlib: %s", z.msg);
|
2002-09-16 05:54:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = inflate(&z, Z_SYNC_FLUSH);
|
|
|
|
if (rc != Z_OK && rc != Z_STREAM_END) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, 0, "zlib: %s", z.msg);
|
2002-09-16 05:54:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
n = (size_t)z.total_out;
|
2007-05-24 21:59:38 +00:00
|
|
|
(void)inflateEnd(&z);
|
2002-09-16 05:54:26 +00:00
|
|
|
|
|
|
|
/* let's keep the nul-terminate tradition */
|
2007-05-24 16:12:23 +00:00
|
|
|
(*newch)[n] = '\0';
|
2002-09-16 05:54:26 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-09 08:45:41 +00:00
|
|
|
private size_t
|
2006-06-19 07:52:15 +00:00
|
|
|
uncompressbuf(struct magic_set *ms, int fd, size_t method,
|
|
|
|
const unsigned char *old, unsigned char **newch, size_t n)
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
|
|
|
int fdin[2], fdout[2];
|
2011-10-06 06:01:12 +00:00
|
|
|
ssize_t r;
|
|
|
|
pid_t pid;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2008-01-13 20:35:16 +00:00
|
|
|
#ifdef BUILTIN_DECOMPRESS
|
2009-01-02 02:57:16 +00:00
|
|
|
/* FIXME: This doesn't cope with bzip2 */
|
2002-09-16 05:54:26 +00:00
|
|
|
if (method == 2)
|
2004-08-09 08:45:41 +00:00
|
|
|
return uncompressgzipped(ms, old, newch, n);
|
2002-09-16 05:54:26 +00:00
|
|
|
#endif
|
2006-06-19 07:52:15 +00:00
|
|
|
(void)fflush(stdout);
|
|
|
|
(void)fflush(stderr);
|
2002-09-16 05:54:26 +00:00
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "cannot create pipe");
|
2007-05-24 16:12:23 +00:00
|
|
|
return NODATA;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2011-10-06 06:01:12 +00:00
|
|
|
switch (pid = fork()) {
|
1994-09-03 19:16:28 +00:00
|
|
|
case 0: /* child */
|
|
|
|
(void) close(0);
|
2006-06-19 07:52:15 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
(void) dup(fd);
|
|
|
|
(void) lseek(0, (off_t)0, SEEK_SET);
|
|
|
|
} else {
|
|
|
|
(void) dup(fdin[0]);
|
|
|
|
(void) close(fdin[0]);
|
|
|
|
(void) close(fdin[1]);
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
(void) close(1);
|
|
|
|
(void) dup(fdout[1]);
|
|
|
|
(void) close(fdout[0]);
|
|
|
|
(void) close(fdout[1]);
|
2006-06-19 07:52:15 +00:00
|
|
|
#ifndef DEBUG
|
1994-09-03 19:16:28 +00:00
|
|
|
if (compr[method].silent)
|
2006-06-19 07:52:15 +00:00
|
|
|
(void)close(2);
|
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2007-05-24 21:59:38 +00:00
|
|
|
(void)execvp(compr[method].argv[0],
|
|
|
|
(char *const *)(intptr_t)compr[method].argv);
|
2006-06-19 07:52:15 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
(void)fprintf(stderr, "exec `%s' failed (%s)\n",
|
|
|
|
compr[method].argv[0], strerror(errno));
|
|
|
|
#endif
|
2000-11-05 08:33:55 +00:00
|
|
|
exit(1);
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
case -1:
|
2004-08-09 08:45:41 +00:00
|
|
|
file_error(ms, errno, "could not fork");
|
2007-05-24 16:12:23 +00:00
|
|
|
return NODATA;
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
default: /* parent */
|
|
|
|
(void) close(fdout[1]);
|
2006-06-19 07:52:15 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
(void) close(fdin[0]);
|
|
|
|
/*
|
|
|
|
* fork again, to avoid blocking because both
|
|
|
|
* pipes filled
|
|
|
|
*/
|
|
|
|
switch (fork()) {
|
|
|
|
case 0: /* child */
|
|
|
|
(void)close(fdout[0]);
|
2007-05-24 21:59:38 +00:00
|
|
|
if (swrite(fdin[1], old, n) != (ssize_t)n) {
|
2006-06-19 07:52:15 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Write failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
#endif
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
#ifdef DEBUG
|
|
|
|
(void)fprintf(stderr, "Fork failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
#endif
|
2004-08-09 08:45:41 +00:00
|
|
|
exit(1);
|
2006-06-19 07:52:15 +00:00
|
|
|
/*NOTREACHED*/
|
2004-08-09 08:45:41 +00:00
|
|
|
|
2006-06-19 07:52:15 +00:00
|
|
|
default: /* parent */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) close(fdin[1]);
|
|
|
|
fdin[1] = -1;
|
2001-04-25 07:41:21 +00:00
|
|
|
}
|
2006-06-19 07:52:15 +00:00
|
|
|
|
2002-09-16 05:54:26 +00:00
|
|
|
if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
|
2006-06-19 07:52:15 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
(void)fprintf(stderr, "Malloc failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
#endif
|
2001-04-25 07:41:21 +00:00
|
|
|
n = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
2007-05-24 21:59:38 +00:00
|
|
|
if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
|
2006-06-19 07:52:15 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
(void)fprintf(stderr, "Read failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
free(*newch);
|
2001-04-25 07:41:21 +00:00
|
|
|
n = 0;
|
2004-08-09 08:45:41 +00:00
|
|
|
newch[0] = '\0';
|
2001-04-25 07:41:21 +00:00
|
|
|
goto err;
|
2004-08-09 08:45:41 +00:00
|
|
|
} else {
|
|
|
|
n = r;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
2002-09-16 05:54:26 +00:00
|
|
|
/* NUL terminate, as every buffer is handled here. */
|
2007-05-24 16:12:23 +00:00
|
|
|
(*newch)[n] = '\0';
|
2001-04-25 07:41:21 +00:00
|
|
|
err:
|
|
|
|
if (fdin[1] != -1)
|
|
|
|
(void) close(fdin[1]);
|
1994-09-03 19:16:28 +00:00
|
|
|
(void) close(fdout[0]);
|
2004-08-09 08:45:41 +00:00
|
|
|
#ifdef WNOHANG
|
2011-10-06 06:01:12 +00:00
|
|
|
while (waitpid(pid, NULL, WNOHANG) != -1)
|
2004-08-09 08:45:41 +00:00
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
(void)wait(NULL);
|
|
|
|
#endif
|
2009-05-18 22:27:42 +00:00
|
|
|
(void) close(fdin[0]);
|
|
|
|
|
1994-09-03 19:16:28 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
2011-10-06 06:01:12 +00:00
|
|
|
#endif
|