1994-09-03 19:16:28 +00:00
|
|
|
/*
|
|
|
|
* compress routines:
|
|
|
|
* zmagic() - returns 0 if not recognized, uncompresses and prints
|
|
|
|
* information if recognized
|
1995-05-30 06:41:30 +00:00
|
|
|
* uncompress(method, old, n, newch) - uncompress old into new,
|
1994-09-03 19:16:28 +00:00
|
|
|
* using method, return sizeof new
|
|
|
|
*/
|
1998-01-28 07:36:25 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1998-01-28 07:36:25 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <err.h>
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
1998-01-28 07:36:25 +00:00
|
|
|
#include <unistd.h>
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
char *magic;
|
|
|
|
int maglen;
|
|
|
|
char *argv[3];
|
|
|
|
int silent;
|
|
|
|
} compr[] = {
|
1996-01-23 12:40:36 +00:00
|
|
|
{ "\037\235", 2, { "uncompress", "-c", NULL }, 0 }, /* compressed */
|
|
|
|
{ "\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 */
|
1994-09-03 19:16:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int ncompr = sizeof(compr) / sizeof(compr[0]);
|
|
|
|
|
|
|
|
|
|
|
|
static int uncompress __P((int, const unsigned char *, unsigned char **, int));
|
|
|
|
|
|
|
|
int
|
|
|
|
zmagic(buf, nbytes)
|
|
|
|
unsigned char *buf;
|
|
|
|
int nbytes;
|
|
|
|
{
|
|
|
|
unsigned char *newbuf;
|
|
|
|
int newsize;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ncompr; i++) {
|
|
|
|
if (nbytes < compr[i].maglen)
|
|
|
|
continue;
|
|
|
|
if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == ncompr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((newsize = uncompress(i, buf, &newbuf, nbytes)) != 0) {
|
|
|
|
tryit(newbuf, newsize, 1);
|
|
|
|
free(newbuf);
|
|
|
|
printf(" (");
|
|
|
|
tryit(buf, nbytes, 0);
|
|
|
|
printf(")");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
uncompress(method, old, newch, n)
|
|
|
|
int method;
|
|
|
|
const unsigned char *old;
|
|
|
|
unsigned char **newch;
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
int fdin[2], fdout[2];
|
|
|
|
|
|
|
|
if (pipe(fdin) == -1 || pipe(fdout) == -1) {
|
1998-01-28 07:36:25 +00:00
|
|
|
err(1, "cannot create pipe");
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
switch (fork()) {
|
|
|
|
case 0: /* child */
|
|
|
|
(void) close(0);
|
|
|
|
(void) dup(fdin[0]);
|
|
|
|
(void) close(fdin[0]);
|
|
|
|
(void) close(fdin[1]);
|
|
|
|
|
|
|
|
(void) close(1);
|
|
|
|
(void) dup(fdout[1]);
|
|
|
|
(void) close(fdout[0]);
|
|
|
|
(void) close(fdout[1]);
|
|
|
|
if (compr[method].silent)
|
|
|
|
(void) close(2);
|
|
|
|
|
|
|
|
execvp(compr[method].argv[0], compr[method].argv);
|
1998-01-28 07:36:25 +00:00
|
|
|
err(1, "could not execute `%s'", compr[method].argv[0]);
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
case -1:
|
1998-01-28 07:36:25 +00:00
|
|
|
err(1, "could not fork");
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
|
|
|
|
default: /* parent */
|
|
|
|
(void) close(fdin[0]);
|
|
|
|
(void) close(fdout[1]);
|
|
|
|
if (write(fdin[1], old, n) != n) {
|
1998-01-28 07:36:25 +00:00
|
|
|
err(1, "write failed");
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
(void) close(fdin[1]);
|
|
|
|
if ((*newch = (unsigned char *) malloc(n)) == NULL) {
|
1998-01-28 07:36:25 +00:00
|
|
|
errx(1, "out of memory");
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if ((n = read(fdout[0], *newch, n)) <= 0) {
|
|
|
|
free(*newch);
|
1998-01-28 07:36:25 +00:00
|
|
|
err(1, "read failed");
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
(void) close(fdout[0]);
|
|
|
|
(void) wait(NULL);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|