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
44 #include <sys/types.h>
45 #ifdef HAVE_SYS_WAIT_H
53 FILE_RCSID("@(#)$Id: compress.c,v 1.41 2005/01/07 19:17:26 christos Exp $")
60 const char *const argv
[3];
63 { "\037\235", 2, { "gzip", "-cdq", NULL
}, 1 }, /* compressed */
64 /* Uncompress can get stuck; so use gzip first if we have it
65 * Idea from Damien Clark, thanks! */
66 { "\037\235", 2, { "uncompress", "-c", NULL
}, 1 }, /* compressed */
67 { "\037\213", 2, { "gzip", "-cdq", NULL
}, 1 }, /* gzipped */
68 { "\037\236", 2, { "gzip", "-cdq", NULL
}, 1 }, /* frozen */
69 { "\037\240", 2, { "gzip", "-cdq", NULL
}, 1 }, /* SCO LZH */
70 /* the standard pack utilities do not accept standard input */
71 { "\037\036", 2, { "gzip", "-cdq", NULL
}, 0 }, /* packed */
72 { "BZh", 3, { "bzip2", "-cd", NULL
}, 1 }, /* bzip2-ed */
75 private int ncompr
= sizeof(compr
) / sizeof(compr
[0]);
78 private ssize_t
swrite(int, const void *, size_t);
79 private ssize_t
sread(int, void *, size_t);
80 private size_t uncompressbuf(struct magic_set
*, int, size_t,
81 const unsigned char *, unsigned char **, size_t);
83 private size_t uncompressgzipped(struct magic_set
*, const unsigned char *,
84 unsigned char **, size_t);
88 file_zmagic(struct magic_set
*ms
, int fd
, const unsigned char *buf
,
91 unsigned char *newbuf
= NULL
;
95 if ((ms
->flags
& MAGIC_COMPRESS
) == 0)
98 for (i
= 0; i
< ncompr
; i
++) {
99 if (nbytes
< compr
[i
].maglen
)
101 if (memcmp(buf
, compr
[i
].magic
, compr
[i
].maglen
) == 0 &&
102 (nsz
= uncompressbuf(ms
, fd
, i
, buf
, &newbuf
,
104 ms
->flags
&= ~MAGIC_COMPRESS
;
106 if (file_buffer(ms
, -1, newbuf
, nsz
) == -1)
108 if (file_printf(ms
, " (") == -1)
110 if (file_buffer(ms
, -1, buf
, nbytes
) == -1)
112 if (file_printf(ms
, ")") == -1)
121 ms
->flags
|= MAGIC_COMPRESS
;
126 * `safe' write for sockets and pipes.
129 swrite(int fd
, const void *buf
, size_t n
)
135 switch (rv
= write(fd
, buf
, n
)) {
142 buf
= ((const char *)buf
) + rv
;
151 * `safe' read for sockets and pipes.
154 sread(int fd
, void *buf
, size_t n
)
160 switch (rv
= read(fd
, buf
, n
)) {
169 buf
= ((char *)buf
) + rv
;
177 file_pipe2file(struct magic_set
*ms
, int fd
, const void *startbuf
,
183 (void)strcpy(buf
, "/tmp/file.XXXXXX");
186 char *ptr
= mktemp(buf
);
187 tfd
= open(ptr
, O_RDWR
|O_TRUNC
|O_EXCL
|O_CREAT
, 0600);
199 file_error(ms
, errno
,
200 "cannot create temporary file for pipe copy");
204 if (swrite(tfd
, startbuf
, nbytes
) != (ssize_t
)nbytes
)
207 while ((r
= sread(fd
, buf
, sizeof(buf
))) > 0)
208 if (swrite(tfd
, buf
, (size_t)r
) != r
)
214 file_error(ms
, errno
, "error copying from pipe to temp file");
219 file_error(ms
, errno
, "error while writing to temp file");
224 * We duplicate the file descriptor, because fclose on a
225 * tmpfile will delete the file, but any open descriptors
226 * can still access the phantom inode.
228 if ((fd
= dup2(tfd
, fd
)) == -1) {
229 file_error(ms
, errno
, "could not dup descriptor for temp file");
233 if (lseek(fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1) {
242 #define FHCRC (1 << 1)
243 #define FEXTRA (1 << 2)
244 #define FNAME (1 << 3)
245 #define FCOMMENT (1 << 4)
248 uncompressgzipped(struct magic_set
*ms
, const unsigned char *old
,
249 unsigned char **newch
, size_t n
)
251 unsigned char flg
= old
[3];
252 size_t data_start
= 10;
257 if (data_start
+1 >= n
)
259 data_start
+= 2 + old
[data_start
] + old
[data_start
+ 1] * 256;
262 while(data_start
< n
&& old
[data_start
])
267 while(data_start
< n
&& old
[data_start
])
276 if ((*newch
= (unsigned char *)malloc(HOWMANY
+ 1)) == NULL
) {
280 /* XXX: const castaway, via strchr */
281 z
.next_in
= (Bytef
*)strchr((const char *)old
+ data_start
,
283 z
.avail_in
= n
- data_start
;
285 z
.avail_out
= HOWMANY
;
290 rc
= inflateInit2(&z
, -15);
292 file_error(ms
, 0, "zlib: %s", z
.msg
);
296 rc
= inflate(&z
, Z_SYNC_FLUSH
);
297 if (rc
!= Z_OK
&& rc
!= Z_STREAM_END
) {
298 file_error(ms
, 0, "zlib: %s", z
.msg
);
302 n
= (size_t)z
.total_out
;
305 /* let's keep the nul-terminate tradition */
306 (*newch
)[n
++] = '\0';
313 uncompressbuf(struct magic_set
*ms
, int fd
, size_t method
,
314 const unsigned char *old
, unsigned char **newch
, size_t n
)
316 int fdin
[2], fdout
[2];
321 return uncompressgzipped(ms
, old
, newch
, n
);
323 (void)fflush(stdout
);
324 (void)fflush(stderr
);
326 if ((fd
!= -1 && pipe(fdin
) == -1) || pipe(fdout
) == -1) {
327 file_error(ms
, errno
, "cannot create pipe");
335 (void) lseek(0, (off_t
)0, SEEK_SET
);
338 (void) close(fdin
[0]);
339 (void) close(fdin
[1]);
343 (void) dup(fdout
[1]);
344 (void) close(fdout
[0]);
345 (void) close(fdout
[1]);
347 if (compr
[method
].silent
)
351 execvp(compr
[method
].argv
[0],
352 (char *const *)compr
[method
].argv
);
354 (void)fprintf(stderr
, "exec `%s' failed (%s)\n",
355 compr
[method
].argv
[0], strerror(errno
));
360 file_error(ms
, errno
, "could not fork");
363 default: /* parent */
364 (void) close(fdout
[1]);
366 (void) close(fdin
[0]);
368 * fork again, to avoid blocking because both
373 (void)close(fdout
[0]);
374 if (swrite(fdin
[1], old
, n
) != n
) {
376 (void)fprintf(stderr
,
377 "Write failed (%s)\n",
387 (void)fprintf(stderr
, "Fork failed (%s)\n",
393 default: /* parent */
396 (void) close(fdin
[1]);
400 if ((*newch
= (unsigned char *) malloc(HOWMANY
+ 1)) == NULL
) {
402 (void)fprintf(stderr
, "Malloc failed (%s)\n",
408 if ((r
= sread(fdout
[0], *newch
, HOWMANY
)) <= 0) {
410 (void)fprintf(stderr
, "Read failed (%s)\n",
420 /* NUL terminate, as every buffer is handled here. */
421 (*newch
)[n
++] = '\0';
424 (void) close(fdin
[1]);
425 (void) close(fdout
[0]);
427 while (waitpid(-1, NULL
, WNOHANG
) != -1)