3 * zmagic() - returns 0 if not recognized, uncompresses and prints
4 * information if recognized
5 * uncompress(method, old, n, newch) - uncompress old into new,
6 * using method, return sizeof new
14 #ifdef HAVE_SYS_WAIT_H
22 FILE_RCSID("@(#)$Id: compress.c,v 1.25 2002/07/03 18:26:37 christos Exp $")
29 const char *const argv[3];
32 { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
33 /* Uncompress can get stuck; so use gzip first if we have it
34 * Idea from Damien Clark, thanks! */
35 { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
36 { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
37 { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
38 { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
39 /* the standard pack utilities do not accept standard input */
40 { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
41 { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
44 static int ncompr = sizeof(compr) / sizeof(compr[0]);
47 static int swrite(int, const void *, size_t);
48 static int sread(int, void *, size_t);
49 static int uncompressbuf(int, const unsigned char *, unsigned char **, int);
51 static int uncompressgzipped(const unsigned char *, unsigned char **, int);
55 zmagic(const char *fname, unsigned char *buf, int nbytes)
57 unsigned char *newbuf;
61 for (i = 0; i < ncompr; i++) {
62 if (nbytes < compr[i].maglen)
64 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
65 (newsize = uncompressbuf(i, buf, &newbuf, nbytes)) != 0) {
66 tryit(fname, newbuf, newsize, 1);
69 tryit(fname, buf, nbytes, 0);
82 * `safe' write for sockets and pipes.
85 swrite(int fd, const void *buf, size_t n)
91 switch (rv = write(fd, buf, n)) {
98 buf = ((const char *)buf) + rv;
107 * `safe' read for sockets and pipes.
110 sread(int fd, void *buf, size_t n)
116 switch (rv = read(fd, buf, n)) {
125 buf = ((char *)buf) + rv;
133 pipe2file(int fd, void *startbuf, size_t nbytes)
138 (void)strcpy(buf, "/tmp/file.XXXXXX");
141 char *ptr = mktemp(buf);
142 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
154 error("Can't create temporary file for pipe copy (%s)\n",
159 if (swrite(tfd, startbuf, nbytes) != nbytes)
162 while ((r = sread(fd, buf, sizeof(buf))) > 0)
163 if (swrite(tfd, buf, r) != r)
169 error("Error copying from pipe to temp file (%s)\n",
175 error("Error while writing to temp file (%s)\n",
181 * We duplicate the file descriptor, because fclose on a
182 * tmpfile will delete the file, but any open descriptors
183 * can still access the phantom inode.
185 if ((fd = dup2(tfd, fd)) == -1) {
186 error("Couldn't dup destcriptor for temp file(%s)\n",
191 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
192 error("Couldn't seek on temp file (%s)\n", strerror(errno));
200 #define FHCRC (1 << 1)
201 #define FEXTRA (1 << 2)
202 #define FNAME (1 << 3)
203 #define FCOMMENT (1 << 4)
206 uncompressgzipped(const unsigned char *old, unsigned char **newch, int n)
208 unsigned char flg = old[3];
214 data_start += 2 + old[data_start] + old[data_start + 1] * 256;
216 while(old[data_start])
221 while(old[data_start])
228 if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
232 z.next_in = (Bytef *)(old + data_start);
233 z.avail_in = n - data_start;
235 z.avail_out = HOWMANY;
240 rc = inflateInit2(&z, -15);
242 (void) fprintf(stderr,"%s: zlib: %s\n", progname, z.msg);
246 rc = inflate(&z, Z_SYNC_FLUSH);
247 if (rc != Z_OK && rc != Z_STREAM_END) {
248 fprintf(stderr,"%s: zlib: %s\n", progname, z.msg);
255 /* let's keep the nul-terminate tradition */
256 (*newch)[n++] = '\0';
263 uncompressbuf(int method, const unsigned char *old, unsigned char **newch,
266 int fdin[2], fdout[2];
268 /* The buffer is NUL terminated, and we don't need that. */
273 return uncompressgzipped(old,newch,n);
276 if (pipe(fdin) == -1 || pipe(fdout) == -1) {
277 error("cannot create pipe (%s).\n", strerror(errno));
284 (void) close(fdin[0]);
285 (void) close(fdin[1]);
288 (void) dup(fdout[1]);
289 (void) close(fdout[0]);
290 (void) close(fdout[1]);
291 if (compr[method].silent)
294 execvp(compr[method].argv[0],
295 (char *const *)compr[method].argv);
299 error("could not fork (%s).\n", strerror(errno));
302 default: /* parent */
303 (void) close(fdin[0]);
304 (void) close(fdout[1]);
305 if (swrite(fdin[1], old, n) != n) {
309 (void) close(fdin[1]);
311 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
315 if ((n = sread(fdout[0], *newch, HOWMANY)) <= 0) {
320 /* NUL terminate, as every buffer is handled here. */
321 (*newch)[n++] = '\0';
324 (void) close(fdin[1]);
325 (void) close(fdout[0]);