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