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