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"
|
1994-09-03 19:16:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#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>
|
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
|
|
|
|
#ifndef lint
|
2001-07-30 03:09:46 +00:00
|
|
|
FILE_RCSID("@(#)$Id: compress.c,v 1.20 2001/07/22 21:04:15 christos Exp $")
|
2000-11-05 08:33:55 +00:00
|
|
|
#endif
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
static struct {
|
2000-11-05 08:33:55 +00:00
|
|
|
const char *magic;
|
|
|
|
int maglen;
|
|
|
|
const char *const argv[3];
|
|
|
|
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 */
|
2001-07-30 03:09:46 +00:00
|
|
|
{ "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
|
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));
|
2001-04-25 07:41:21 +00:00
|
|
|
static int swrite __P((int, const void *, size_t));
|
|
|
|
static int sread __P((int, void *, size_t));
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
zmagic(buf, nbytes)
|
2000-11-05 08:33:55 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
int nbytes;
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
|
|
|
unsigned char *newbuf;
|
|
|
|
int newsize;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
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 &&
|
|
|
|
(newsize = uncompress(i, buf, &newbuf, nbytes)) != 0) {
|
|
|
|
tryit(newbuf, newsize, 1);
|
|
|
|
free(newbuf);
|
|
|
|
printf(" (");
|
|
|
|
tryit(buf, nbytes, 0);
|
|
|
|
printf(")");
|
|
|
|
return 1;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i == ncompr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-04-25 07:41:21 +00:00
|
|
|
/*
|
|
|
|
* `safe' write for sockets and pipes.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
swrite(fd, buf, n)
|
|
|
|
int fd;
|
|
|
|
const void *buf;
|
|
|
|
size_t n;
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
size_t rn = n;
|
|
|
|
|
|
|
|
do
|
|
|
|
switch (rv = write(fd, buf, n)) {
|
|
|
|
case -1:
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
n -= rv;
|
|
|
|
buf = ((char *)buf) + rv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (n > 0);
|
|
|
|
return rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* `safe' read for sockets and pipes.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sread(fd, buf, n)
|
|
|
|
int fd;
|
|
|
|
void *buf;
|
|
|
|
size_t n;
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
size_t rn = n;
|
|
|
|
|
|
|
|
do
|
|
|
|
switch (rv = read(fd, buf, n)) {
|
|
|
|
case -1:
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
n -= rv;
|
|
|
|
buf = ((char *)buf) + rv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (n > 0);
|
|
|
|
return rn;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
uncompress(method, old, newch, n)
|
2000-11-05 08:33:55 +00:00
|
|
|
int method;
|
|
|
|
const unsigned char *old;
|
|
|
|
unsigned char **newch;
|
|
|
|
int n;
|
1994-09-03 19:16:28 +00:00
|
|
|
{
|
|
|
|
int fdin[2], fdout[2];
|
|
|
|
|
|
|
|
if (pipe(fdin) == -1 || pipe(fdout) == -1) {
|
|
|
|
error("cannot create pipe (%s).\n", strerror(errno));
|
|
|
|
/*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)
|
2000-11-05 08:33:55 +00:00
|
|
|
(void) close(2);
|
1994-09-03 19:16:28 +00:00
|
|
|
|
2000-11-05 08:33:55 +00:00
|
|
|
execvp(compr[method].argv[0],
|
|
|
|
(char *const *)compr[method].argv);
|
|
|
|
exit(1);
|
1994-09-03 19:16:28 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
case -1:
|
|
|
|
error("could not fork (%s).\n", strerror(errno));
|
|
|
|
/*NOTREACHED*/
|
|
|
|
|
|
|
|
default: /* parent */
|
|
|
|
(void) close(fdin[0]);
|
|
|
|
(void) close(fdout[1]);
|
2001-04-25 07:41:21 +00:00
|
|
|
if (swrite(fdin[1], old, n) != n) {
|
|
|
|
n = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
1994-09-03 19:16:28 +00:00
|
|
|
(void) close(fdin[1]);
|
2001-04-25 07:41:21 +00:00
|
|
|
fdin[1] = -1;
|
|
|
|
if ((*newch = (unsigned char *) malloc(n)) == NULL) {
|
|
|
|
n = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ((n = sread(fdout[0], *newch, n)) <= 0) {
|
1994-09-03 19:16:28 +00:00
|
|
|
free(*newch);
|
2001-04-25 07:41:21 +00:00
|
|
|
n = 0;
|
|
|
|
goto err;
|
1994-09-03 19:16:28 +00:00
|
|
|
}
|
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]);
|
|
|
|
(void) wait(NULL);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|