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.68 2011/12/08 12:38:24 rrt 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 int 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 #ifdef HAVE_MKSTEMP
251 int te;
252 #endif
253
254 (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
255 #ifndef HAVE_MKSTEMP
256 {
257 char *ptr = mktemp(buf);
258 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
259 r = errno;
260 (void)unlink(ptr);
261 errno = r;
262 }
263 #else
264 tfd = mkstemp(buf);
265 te = errno;
266 (void)unlink(buf);
267 errno = te;
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 *newch = (unsigned char *)emalloc(HOWMANY + 1));
349
350 /* XXX: const castaway, via strchr */
351 z.next_in = (Bytef *)strchr((const char *)old + data_start,
352 old[data_start]);
353 z.avail_in = CAST(uint32_t, (n - data_start));
354 z.next_out = *newch;
355 z.avail_out = HOWMANY;
356 z.zalloc = Z_NULL;
357 z.zfree = Z_NULL;
358 z.opaque = Z_NULL;
359
360 /* LINTED bug in header macro */
361 rc = inflateInit2(&z, -15);
362 if (rc != Z_OK) {
363 file_error(ms, 0, "zlib: %s", z.msg);
364 return 0;
365 }
366
367 rc = inflate(&z, Z_SYNC_FLUSH);
368 if (rc != Z_OK && rc != Z_STREAM_END) {
369 file_error(ms, 0, "zlib: %s", z.msg);
370 return 0;
371 }
372
373 n = (size_t)z.total_out;
374 (void)inflateEnd(&z);
375
376 /* let's keep the nul-terminate tradition */
377 (*newch)[n] = '\0';
378
379 return n;
380 }
381 #endif
382
383 private size_t
uncompressbuf(struct magic_set * ms,int fd,size_t method,const unsigned char * old,unsigned char ** newch,size_t n)384 uncompressbuf(struct magic_set *ms, int fd, size_t method,
385 const unsigned char *old, unsigned char **newch, size_t n)
386 {
387 int fdin[2], fdout[2];
388 ssize_t r;
389 pid_t pid;
390
391 #ifdef BUILTIN_DECOMPRESS
392 /* FIXME: This doesn't cope with bzip2 */
393 if (method == 2)
394 return uncompressgzipped(ms, old, newch, n);
395 #endif
396 (void)fflush(stdout);
397 (void)fflush(stderr);
398
399 if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
400 file_error(ms, errno, "cannot create pipe");
401 return NODATA;
402 }
403 switch (pid = fork()) {
404 case 0: /* child */
405 (void) close(0);
406 if (fd != -1) {
407 (void) dup(fd);
408 (void) FINFO_LSEEK_FUNC(0, (off_t)0, SEEK_SET);
409 } else {
410 (void) dup(fdin[0]);
411 (void) close(fdin[0]);
412 (void) close(fdin[1]);
413 }
414
415 (void) close(1);
416 (void) dup(fdout[1]);
417 (void) close(fdout[0]);
418 (void) close(fdout[1]);
419 #ifndef DEBUG
420 if (compr[method].silent)
421 (void)close(2);
422 #endif
423
424 (void)execvp(compr[method].argv[0],
425 (char *const *)(intptr_t)compr[method].argv);
426 #ifdef DEBUG
427 (void)fprintf(stderr, "exec `%s' failed (%s)\n",
428 compr[method].argv[0], strerror(errno));
429 #endif
430 exit(1);
431 /*NOTREACHED*/
432 case -1:
433 file_error(ms, errno, "could not fork");
434 return NODATA;
435
436 default: /* parent */
437 (void) close(fdout[1]);
438 if (fd == -1) {
439 (void) close(fdin[0]);
440 /*
441 * fork again, to avoid blocking because both
442 * pipes filled
443 */
444 switch (fork()) {
445 case 0: /* child */
446 (void)close(fdout[0]);
447 if (swrite(fdin[1], old, n) != (ssize_t)n) {
448 #ifdef DEBUG
449 (void)fprintf(stderr,
450 "Write failed (%s)\n",
451 strerror(errno));
452 #endif
453 exit(1);
454 }
455 exit(0);
456 /*NOTREACHED*/
457
458 case -1:
459 #ifdef DEBUG
460 (void)fprintf(stderr, "Fork failed (%s)\n",
461 strerror(errno));
462 #endif
463 exit(1);
464 /*NOTREACHED*/
465
466 default: /* parent */
467 break;
468 }
469 (void) close(fdin[1]);
470 fdin[1] = -1;
471 }
472
473 *newch = (unsigned char *) emalloc(HOWMANY + 1);
474
475 if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
476 #ifdef DEBUG
477 (void)fprintf(stderr, "Read failed (%s)\n",
478 strerror(errno));
479 #endif
480 efree(*newch);
481 n = 0;
482 newch[0] = '\0';
483 goto err;
484 } else {
485 n = r;
486 }
487 /* NUL terminate, as every buffer is handled here. */
488 (*newch)[n] = '\0';
489 err:
490 if (fdin[1] != -1)
491 (void) close(fdin[1]);
492 (void) close(fdout[0]);
493 #ifdef WNOHANG
494 while (waitpid(pid, NULL, WNOHANG) != -1)
495 continue;
496 #else
497 (void)wait(NULL);
498 #endif
499 (void) close(fdin[0]);
500
501 return n;
502 }
503 }
504 #endif /* if PHP_FILEINFO_UNCOMPRESS */
505