summaryrefslogtreecommitdiffstats
path: root/usr.bin/file/compress.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/file/compress.c')
-rw-r--r--usr.bin/file/compress.c36
1 files changed, 23 insertions, 13 deletions
diff --git a/usr.bin/file/compress.c b/usr.bin/file/compress.c
index a17a1d32861..6da92b83202 100644
--- a/usr.bin/file/compress.c
+++ b/usr.bin/file/compress.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: compress.c,v 1.12 2008/05/08 01:40:56 chl Exp $ */
+/* $OpenBSD: compress.c,v 1.13 2009/04/24 18:54:34 chl Exp $ */
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
@@ -50,19 +50,20 @@
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
-#ifdef HAVE_LIBZ
+#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
+#define BUILTIN_DECOMPRESS
#include <zlib.h>
#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: compress.c,v 1.12 2008/05/08 01:40:56 chl Exp $")
+FILE_RCSID("@(#)$Id: compress.c,v 1.13 2009/04/24 18:54:34 chl Exp $")
#endif
-private struct {
- const char *magic;
+private const struct {
+ const char magic[8];
size_t maglen;
- const char *const argv[3];
+ const char *argv[3];
int silent;
} compr[] = {
{ "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
@@ -87,7 +88,7 @@ private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
private ssize_t swrite(int, const void *, size_t);
private size_t uncompressbuf(struct magic_set *, int, size_t,
const unsigned char *, unsigned char **, size_t);
-#ifdef HAVE_LIBZ
+#ifdef BUILTIN_DECOMPRESS
private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
unsigned char **, size_t);
#endif
@@ -99,6 +100,7 @@ file_zmagic(struct magic_set *ms, int fd, const char *name,
unsigned char *newbuf = NULL;
size_t i, nsz;
int rv = 0;
+ int mime = ms->flags & MAGIC_MIME;
if ((ms->flags & MAGIC_COMPRESS) == 0)
return 0;
@@ -113,11 +115,18 @@ file_zmagic(struct magic_set *ms, int fd, const char *name,
rv = -1;
if (file_buffer(ms, -1, name, newbuf, nsz) == -1)
goto error;
- if (file_printf(ms, " (") == -1)
- goto error;
- if (file_buffer(ms, -1, NULL, buf, nbytes) == -1)
+
+ if (mime == MAGIC_MIME || mime == 0) {
+ if (file_printf(ms, mime ?
+ " compressed-encoding=" : " (") == -1)
+ goto error;
+ }
+
+ if ((mime == 0 || mime & MAGIC_MIME_ENCODING) &&
+ file_buffer(ms, -1, NULL, buf, nbytes) == -1)
goto error;
- if (file_printf(ms, ")") == -1)
+
+ if (!mime && file_printf(ms, ")") == -1)
goto error;
rv = 1;
break;
@@ -286,7 +295,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
return fd;
}
-#ifdef HAVE_LIBZ
+#ifdef BUILTIN_DECOMPRESS
#define FHCRC (1 << 1)
#define FEXTRA (1 << 2)
@@ -365,7 +374,8 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method,
int fdin[2], fdout[2];
int r;
-#ifdef HAVE_LIBZ
+#ifdef BUILTIN_DECOMPRESS
+ /* FIXME: This doesn't cope with bzip2 */
if (method == 2)
return uncompressgzipped(ms, old, newch, n);
#endif