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