compress.c revision 191736
1/*
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.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28/*
29 * compress routines:
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
34 */
35#include "file.h"
36
37#ifndef lint
38FILE_RCSID("@(#)$File: compress.c,v 1.61 2009/02/03 20:27:51 christos Exp $")
39#endif
40
41#include "magic.h"
42#include <stdlib.h>
43#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46#include <string.h>
47#include <errno.h>
48#include <sys/ioctl.h>
49#ifdef HAVE_SYS_WAIT_H
50#include <sys/wait.h>
51#endif
52#if defined(HAVE_SYS_TIME_H)
53#include <sys/time.h>
54#endif
55#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
56#define BUILTIN_DECOMPRESS
57#include <zlib.h>
58#endif
59
60private const struct {
61	const char magic[8];
62	size_t maglen;
63	const char *argv[3];
64	int silent;
65} compr[] = {
66	{ "\037\235", 2, { "gzip", "-cdq", NULL }, 1 },		/* compressed */
67	/* Uncompress can get stuck; so use gzip first if we have it
68	 * Idea from Damien Clark, thanks! */
69	{ "\037\235", 2, { "uncompress", "-c", NULL }, 1 },	/* compressed */
70	{ "\037\213", 2, { "gzip", "-cdq", NULL }, 1 },		/* gzipped */
71	{ "\037\236", 2, { "gzip", "-cdq", NULL }, 1 },		/* frozen */
72	{ "\037\240", 2, { "gzip", "-cdq", NULL }, 1 },		/* SCO LZH */
73	/* the standard pack utilities do not accept standard input */
74	{ "\037\036", 2, { "gzip", "-cdq", NULL }, 0 },		/* packed */
75	{ "PK\3\4",   4, { "gzip", "-cdq", NULL }, 1 },		/* pkzipped, */
76					    /* ...only first file examined */
77	{ "BZh",      3, { "bzip2", "-cd", NULL }, 1 },		/* bzip2-ed */
78	{ "LZIP",     4, { "lzip", "-cdq", NULL }, 1 },
79};
80
81private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
82
83#define NODATA ((size_t)~0)
84
85
86private ssize_t swrite(int, const void *, size_t);
87private size_t uncompressbuf(struct magic_set *, int, size_t,
88    const unsigned char *, unsigned char **, size_t);
89#ifdef BUILTIN_DECOMPRESS
90private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
91    unsigned char **, size_t);
92#endif
93
94protected int
95file_zmagic(struct magic_set *ms, int fd, const char *name,
96    const unsigned char *buf, size_t nbytes)
97{
98	unsigned char *newbuf = NULL;
99	size_t i, nsz;
100	int rv = 0;
101	int mime = ms->flags & MAGIC_MIME;
102
103	if ((ms->flags & MAGIC_COMPRESS) == 0)
104		return 0;
105
106	for (i = 0; i < ncompr; i++) {
107		if (nbytes < compr[i].maglen)
108			continue;
109		if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
110		    (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
111		    nbytes)) != NODATA) {
112			ms->flags &= ~MAGIC_COMPRESS;
113			rv = -1;
114			if (file_buffer(ms, -1, name, newbuf, nsz) == -1)
115				goto error;
116
117			if (mime == MAGIC_MIME || mime == 0) {
118				if (file_printf(ms, mime ?
119				    " compressed-encoding=" : " (") == -1)
120					goto error;
121			}
122
123			if ((mime == 0 || mime & MAGIC_MIME_ENCODING) &&
124			    file_buffer(ms, -1, NULL, buf, nbytes) == -1)
125				goto error;
126
127			if (!mime && file_printf(ms, ")") == -1)
128				goto error;
129			rv = 1;
130			break;
131		}
132	}
133error:
134	if (newbuf)
135		free(newbuf);
136	ms->flags |= MAGIC_COMPRESS;
137	return rv;
138}
139
140/*
141 * `safe' write for sockets and pipes.
142 */
143private ssize_t
144swrite(int fd, const void *buf, size_t n)
145{
146	int rv;
147	size_t rn = n;
148
149	do
150		switch (rv = write(fd, buf, n)) {
151		case -1:
152			if (errno == EINTR)
153				continue;
154			return -1;
155		default:
156			n -= rv;
157			buf = ((const char *)buf) + rv;
158			break;
159		}
160	while (n > 0);
161	return rn;
162}
163
164
165/*
166 * `safe' read for sockets and pipes.
167 */
168protected ssize_t
169sread(int fd, void *buf, size_t n, int canbepipe)
170{
171	int rv, cnt;
172#ifdef FIONREAD
173	int t = 0;
174#endif
175	size_t rn = n;
176
177	if (fd == STDIN_FILENO)
178		goto nocheck;
179
180#ifdef FIONREAD
181	if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {
182#ifdef FD_ZERO
183		for (cnt = 0;; cnt++) {
184			fd_set check;
185			struct timeval tout = {0, 100 * 1000};
186			int selrv;
187
188			FD_ZERO(&check);
189			FD_SET(fd, &check);
190
191			/*
192			 * Avoid soft deadlock: do not read if there
193			 * is nothing to read from sockets and pipes.
194			 */
195			selrv = select(fd + 1, &check, NULL, NULL, &tout);
196			if (selrv == -1) {
197				if (errno == EINTR || errno == EAGAIN)
198					continue;
199			} else if (selrv == 0 && cnt >= 5) {
200				return 0;
201			} else
202				break;
203		}
204#endif
205		(void)ioctl(fd, FIONREAD, &t);
206	}
207
208	if (t > 0 && (size_t)t < n) {
209		n = t;
210		rn = n;
211	}
212#endif
213
214nocheck:
215	do
216		switch ((rv = read(fd, buf, n))) {
217		case -1:
218			if (errno == EINTR)
219				continue;
220			return -1;
221		case 0:
222			return rn - n;
223		default:
224			n -= rv;
225			buf = ((char *)buf) + rv;
226			break;
227		}
228	while (n > 0);
229	return rn;
230}
231
232protected int
233file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
234    size_t nbytes)
235{
236	char buf[4096];
237	int r, tfd;
238
239	(void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
240#ifndef HAVE_MKSTEMP
241	{
242		char *ptr = mktemp(buf);
243		tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
244		r = errno;
245		(void)unlink(ptr);
246		errno = r;
247	}
248#else
249	tfd = mkstemp(buf);
250	r = errno;
251	(void)unlink(buf);
252	errno = r;
253#endif
254	if (tfd == -1) {
255		file_error(ms, errno,
256		    "cannot create temporary file for pipe copy");
257		return -1;
258	}
259
260	if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
261		r = 1;
262	else {
263		while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
264			if (swrite(tfd, buf, (size_t)r) != r)
265				break;
266	}
267
268	switch (r) {
269	case -1:
270		file_error(ms, errno, "error copying from pipe to temp file");
271		return -1;
272	case 0:
273		break;
274	default:
275		file_error(ms, errno, "error while writing to temp file");
276		return -1;
277	}
278
279	/*
280	 * We duplicate the file descriptor, because fclose on a
281	 * tmpfile will delete the file, but any open descriptors
282	 * can still access the phantom inode.
283	 */
284	if ((fd = dup2(tfd, fd)) == -1) {
285		file_error(ms, errno, "could not dup descriptor for temp file");
286		return -1;
287	}
288	(void)close(tfd);
289	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
290		file_badseek(ms);
291		return -1;
292	}
293	return fd;
294}
295
296#ifdef BUILTIN_DECOMPRESS
297
298#define FHCRC		(1 << 1)
299#define FEXTRA		(1 << 2)
300#define FNAME		(1 << 3)
301#define FCOMMENT	(1 << 4)
302
303private size_t
304uncompressgzipped(struct magic_set *ms, const unsigned char *old,
305    unsigned char **newch, size_t n)
306{
307	unsigned char flg = old[3];
308	size_t data_start = 10;
309	z_stream z;
310	int rc;
311
312	if (flg & FEXTRA) {
313		if (data_start+1 >= n)
314			return 0;
315		data_start += 2 + old[data_start] + old[data_start + 1] * 256;
316	}
317	if (flg & FNAME) {
318		while(data_start < n && old[data_start])
319			data_start++;
320		data_start++;
321	}
322	if(flg & FCOMMENT) {
323		while(data_start < n && old[data_start])
324			data_start++;
325		data_start++;
326	}
327	if(flg & FHCRC)
328		data_start += 2;
329
330	if (data_start >= n)
331		return 0;
332	if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
333		return 0;
334	}
335
336	/* XXX: const castaway, via strchr */
337	z.next_in = (Bytef *)strchr((const char *)old + data_start,
338	    old[data_start]);
339	z.avail_in = n - data_start;
340	z.next_out = *newch;
341	z.avail_out = HOWMANY;
342	z.zalloc = Z_NULL;
343	z.zfree = Z_NULL;
344	z.opaque = Z_NULL;
345
346	rc = inflateInit2(&z, -15);
347	if (rc != Z_OK) {
348		file_error(ms, 0, "zlib: %s", z.msg);
349		return 0;
350	}
351
352	rc = inflate(&z, Z_SYNC_FLUSH);
353	if (rc != Z_OK && rc != Z_STREAM_END) {
354		file_error(ms, 0, "zlib: %s", z.msg);
355		return 0;
356	}
357
358	n = (size_t)z.total_out;
359	(void)inflateEnd(&z);
360
361	/* let's keep the nul-terminate tradition */
362	(*newch)[n] = '\0';
363
364	return n;
365}
366#endif
367
368private size_t
369uncompressbuf(struct magic_set *ms, int fd, size_t method,
370    const unsigned char *old, unsigned char **newch, size_t n)
371{
372	int fdin[2], fdout[2];
373	int r;
374
375#ifdef BUILTIN_DECOMPRESS
376        /* FIXME: This doesn't cope with bzip2 */
377	if (method == 2)
378		return uncompressgzipped(ms, old, newch, n);
379#endif
380	(void)fflush(stdout);
381	(void)fflush(stderr);
382
383	if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
384		file_error(ms, errno, "cannot create pipe");
385		return NODATA;
386	}
387	switch (fork()) {
388	case 0:	/* child */
389		(void) close(0);
390		if (fd != -1) {
391		    (void) dup(fd);
392		    (void) lseek(0, (off_t)0, SEEK_SET);
393		} else {
394		    (void) dup(fdin[0]);
395		    (void) close(fdin[0]);
396		    (void) close(fdin[1]);
397		}
398
399		(void) close(1);
400		(void) dup(fdout[1]);
401		(void) close(fdout[0]);
402		(void) close(fdout[1]);
403#ifndef DEBUG
404		if (compr[method].silent)
405			(void)close(2);
406#endif
407
408		(void)execvp(compr[method].argv[0],
409		    (char *const *)(intptr_t)compr[method].argv);
410#ifdef DEBUG
411		(void)fprintf(stderr, "exec `%s' failed (%s)\n",
412		    compr[method].argv[0], strerror(errno));
413#endif
414		exit(1);
415		/*NOTREACHED*/
416	case -1:
417		file_error(ms, errno, "could not fork");
418		return NODATA;
419
420	default: /* parent */
421		(void) close(fdout[1]);
422		if (fd == -1) {
423			(void) close(fdin[0]);
424			/*
425			 * fork again, to avoid blocking because both
426			 * pipes filled
427			 */
428			switch (fork()) {
429			case 0: /* child */
430				(void)close(fdout[0]);
431				if (swrite(fdin[1], old, n) != (ssize_t)n) {
432#ifdef DEBUG
433					(void)fprintf(stderr,
434					    "Write failed (%s)\n",
435					    strerror(errno));
436#endif
437					exit(1);
438				}
439				exit(0);
440				/*NOTREACHED*/
441
442			case -1:
443#ifdef DEBUG
444				(void)fprintf(stderr, "Fork failed (%s)\n",
445				    strerror(errno));
446#endif
447				exit(1);
448				/*NOTREACHED*/
449
450			default:  /* parent */
451				break;
452			}
453			(void) close(fdin[1]);
454			fdin[1] = -1;
455		}
456
457		if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
458#ifdef DEBUG
459			(void)fprintf(stderr, "Malloc failed (%s)\n",
460			    strerror(errno));
461#endif
462			n = 0;
463			goto err;
464		}
465		if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
466#ifdef DEBUG
467			(void)fprintf(stderr, "Read failed (%s)\n",
468			    strerror(errno));
469#endif
470			free(*newch);
471			n = 0;
472			newch[0] = '\0';
473			goto err;
474		} else {
475			n = r;
476		}
477 		/* NUL terminate, as every buffer is handled here. */
478 		(*newch)[n] = '\0';
479err:
480		if (fdin[1] != -1)
481			(void) close(fdin[1]);
482		(void) close(fdout[0]);
483#ifdef WNOHANG
484		while (waitpid(-1, NULL, WNOHANG) != -1)
485			continue;
486#else
487		(void)wait(NULL);
488#endif
489		return n;
490	}
491}
492