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