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