1 /* $NetBSD: compress.c,v 1.4 2011/09/16 21:06:26 christos Exp $ */
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * zmagic() - returns 0 if not recognized, uncompresses and prints
33 * information if recognized
34 * uncompress(method, old, n, newch) - uncompress old into new,
35 * using method, return sizeof new
41 FILE_RCSID("@(#)$File: compress.c,v 1.67 2011/09/01 12:12:37 christos Exp $")
43 __RCSID("$NetBSD: compress.c,v 1.4 2011/09/16 21:06:26 christos Exp $");
55 #include <sys/ioctl.h>
57 #ifdef HAVE_SYS_WAIT_H
60 #if defined(HAVE_SYS_TIME_H)
63 #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
64 #define BUILTIN_DECOMPRESS
68 private const struct {
74 { "\037\235", 2, { "gzip", "-cdq", NULL
}, 1 }, /* compressed */
75 /* Uncompress can get stuck; so use gzip first if we have it
76 * Idea from Damien Clark, thanks! */
77 { "\037\235", 2, { "uncompress", "-c", NULL
}, 1 }, /* compressed */
78 { "\037\213", 2, { "gzip", "-cdq", NULL
}, 1 }, /* gzipped */
79 { "\037\236", 2, { "gzip", "-cdq", NULL
}, 1 }, /* frozen */
80 { "\037\240", 2, { "gzip", "-cdq", NULL
}, 1 }, /* SCO LZH */
81 /* the standard pack utilities do not accept standard input */
82 { "\037\036", 2, { "gzip", "-cdq", NULL
}, 0 }, /* packed */
83 { "PK\3\4", 4, { "gzip", "-cdq", NULL
}, 1 }, /* pkzipped, */
84 /* ...only first file examined */
85 { "BZh", 3, { "bzip2", "-cd", NULL
}, 1 }, /* bzip2-ed */
86 { "LZIP", 4, { "lzip", "-cdq", NULL
}, 1 },
87 { "\3757zXZ\0",6,{ "xz", "-cd", NULL
}, 1 }, /* XZ Utils */
88 { "LRZI", 4, { "lrzip", "-dqo-", NULL
}, 1 }, /* LRZIP */
91 #define NODATA ((size_t)~0)
93 private ssize_t
swrite(int, const void *, size_t);
95 private size_t ncompr
= sizeof(compr
) / sizeof(compr
[0]);
96 private size_t uncompressbuf(struct magic_set
*, int, size_t,
97 const unsigned char *, unsigned char **, size_t);
98 #ifdef BUILTIN_DECOMPRESS
99 private size_t uncompressgzipped(struct magic_set
*, const unsigned char *,
100 unsigned char **, size_t);
104 file_zmagic(struct magic_set
*ms
, int fd
, const char *name
,
105 const unsigned char *buf
, size_t nbytes
)
107 unsigned char *newbuf
= NULL
;
110 int mime
= ms
->flags
& MAGIC_MIME
;
112 if ((ms
->flags
& MAGIC_COMPRESS
) == 0)
115 for (i
= 0; i
< ncompr
; i
++) {
116 if (nbytes
< compr
[i
].maglen
)
118 if (memcmp(buf
, compr
[i
].magic
, compr
[i
].maglen
) == 0 &&
119 (nsz
= uncompressbuf(ms
, fd
, i
, buf
, &newbuf
,
120 nbytes
)) != NODATA
) {
121 ms
->flags
&= ~MAGIC_COMPRESS
;
123 if (file_buffer(ms
, -1, name
, newbuf
, nsz
) == -1)
126 if (mime
== MAGIC_MIME
|| mime
== 0) {
127 if (file_printf(ms
, mime
?
128 " compressed-encoding=" : " (") == -1)
132 if ((mime
== 0 || mime
& MAGIC_MIME_ENCODING
) &&
133 file_buffer(ms
, -1, NULL
, buf
, nbytes
) == -1)
136 if (!mime
&& file_printf(ms
, ")") == -1)
145 ms
->flags
|= MAGIC_COMPRESS
;
150 * `safe' write for sockets and pipes.
153 swrite(int fd
, const void *buf
, size_t n
)
159 switch (rv
= write(fd
, buf
, n
)) {
166 buf
= CAST(const char *, buf
) + rv
;
175 * `safe' read for sockets and pipes.
178 sread(int fd
, void *buf
, size_t n
, int canbepipe
__attribute__ ((unused
)))
189 if (fd
== STDIN_FILENO
)
193 if ((canbepipe
&& (ioctl(fd
, FIONREAD
, &t
) == -1)) || (t
== 0)) {
195 for (cnt
= 0;; cnt
++) {
197 struct timeval tout
= {0, 100 * 1000};
204 * Avoid soft deadlock: do not read if there
205 * is nothing to read from sockets and pipes.
207 selrv
= select(fd
+ 1, &check
, NULL
, NULL
, &tout
);
209 if (errno
== EINTR
|| errno
== EAGAIN
)
211 } else if (selrv
== 0 && cnt
>= 5) {
217 (void)ioctl(fd
, FIONREAD
, &t
);
220 if (t
> 0 && (size_t)t
< n
) {
228 switch ((rv
= read(fd
, buf
, n
))) {
237 buf
= ((char *)buf
) + rv
;
245 file_pipe2file(struct magic_set
*ms
, int fd
, const void *startbuf
,
255 (void)strlcpy(buf
, "/tmp/file.XXXXXX", sizeof buf
);
258 char *ptr
= mktemp(buf
);
259 tfd
= open(ptr
, O_RDWR
|O_TRUNC
|O_EXCL
|O_CREAT
, 0600);
271 file_error(ms
, errno
,
272 "cannot create temporary file for pipe copy");
276 if (swrite(tfd
, startbuf
, nbytes
) != (ssize_t
)nbytes
)
279 while ((r
= sread(fd
, buf
, sizeof(buf
), 1)) > 0)
280 if (swrite(tfd
, buf
, (size_t)r
) != r
)
286 file_error(ms
, errno
, "error copying from pipe to temp file");
291 file_error(ms
, errno
, "error while writing to temp file");
296 * We duplicate the file descriptor, because fclose on a
297 * tmpfile will delete the file, but any open descriptors
298 * can still access the phantom inode.
300 if ((fd
= dup2(tfd
, fd
)) == -1) {
301 file_error(ms
, errno
, "could not dup descriptor for temp file");
305 if (lseek(fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1) {
312 #ifdef BUILTIN_DECOMPRESS
314 #define FHCRC (1 << 1)
315 #define FEXTRA (1 << 2)
316 #define FNAME (1 << 3)
317 #define FCOMMENT (1 << 4)
320 uncompressgzipped(struct magic_set
*ms
, const unsigned char *old
,
321 unsigned char **newch
, size_t n
)
323 unsigned char flg
= old
[3];
324 size_t data_start
= 10;
329 if (data_start
+1 >= n
)
331 data_start
+= 2 + old
[data_start
] + old
[data_start
+ 1] * 256;
334 while(data_start
< n
&& old
[data_start
])
339 while(data_start
< n
&& old
[data_start
])
348 if ((*newch
= CAST(unsigned char *, malloc(HOWMANY
+ 1))) == NULL
) {
352 /* XXX: const castaway, via strchr */
353 z
.next_in
= (Bytef
*)strchr((const char *)old
+ data_start
,
355 z
.avail_in
= CAST(uint32_t, (n
- data_start
));
357 z
.avail_out
= HOWMANY
;
362 /* LINTED bug in header macro */
363 rc
= inflateInit2(&z
, -15);
365 file_error(ms
, 0, "zlib: %s", z
.msg
);
369 rc
= inflate(&z
, Z_SYNC_FLUSH
);
370 if (rc
!= Z_OK
&& rc
!= Z_STREAM_END
) {
371 file_error(ms
, 0, "zlib: %s", z
.msg
);
375 n
= (size_t)z
.total_out
;
376 (void)inflateEnd(&z
);
378 /* let's keep the nul-terminate tradition */
386 uncompressbuf(struct magic_set
*ms
, int fd
, size_t method
,
387 const unsigned char *old
, unsigned char **newch
, size_t n
)
389 int fdin
[2], fdout
[2];
393 #ifdef BUILTIN_DECOMPRESS
394 /* FIXME: This doesn't cope with bzip2 */
396 return uncompressgzipped(ms
, old
, newch
, n
);
398 (void)fflush(stdout
);
399 (void)fflush(stderr
);
401 if ((fd
!= -1 && pipe(fdin
) == -1) || pipe(fdout
) == -1) {
402 file_error(ms
, errno
, "cannot create pipe");
405 switch (pid
= fork()) {
410 (void) lseek(0, (off_t
)0, SEEK_SET
);
413 (void) close(fdin
[0]);
414 (void) close(fdin
[1]);
418 (void) dup(fdout
[1]);
419 (void) close(fdout
[0]);
420 (void) close(fdout
[1]);
422 if (compr
[method
].silent
)
426 (void)execvp(compr
[method
].argv
[0],
427 (char *const *)(intptr_t)compr
[method
].argv
);
429 (void)fprintf(stderr
, "exec `%s' failed (%s)\n",
430 compr
[method
].argv
[0], strerror(errno
));
435 file_error(ms
, errno
, "could not fork");
438 default: /* parent */
439 (void) close(fdout
[1]);
441 (void) close(fdin
[0]);
443 * fork again, to avoid blocking because both
448 (void)close(fdout
[0]);
449 if (swrite(fdin
[1], old
, n
) != (ssize_t
)n
) {
451 (void)fprintf(stderr
,
452 "Write failed (%s)\n",
462 (void)fprintf(stderr
, "Fork failed (%s)\n",
468 default: /* parent */
471 (void) close(fdin
[1]);
475 if ((*newch
= (unsigned char *) malloc(HOWMANY
+ 1)) == NULL
) {
477 (void)fprintf(stderr
, "Malloc failed (%s)\n",
483 if ((r
= sread(fdout
[0], *newch
, HOWMANY
, 0)) <= 0) {
485 (void)fprintf(stderr
, "Read failed (%s)\n",
495 /* NUL terminate, as every buffer is handled here. */
499 (void) close(fdin
[1]);
500 (void) close(fdout
[0]);
502 while (waitpid(pid
, NULL
, WNOHANG
) != -1)
507 (void) close(fdin
[0]);