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