Update to file 4.13. Put the contrib files into contrib/file-4 instead
[dragonfly.git] / contrib / file-4 / src / compress.c
bloba176b8e2a2ac3180e34c4aacc3aa02116e0b4483
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.
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.
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
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>
45 #ifdef HAVE_SYS_WAIT_H
46 #include <sys/wait.h>
47 #endif
48 #ifdef HAVE_LIBZ
49 #include <zlib.h>
50 #endif
52 #ifndef lint
53 FILE_RCSID("@(#)$Id: compress.c,v 1.41 2005/01/07 19:17:26 christos Exp $")
54 #endif
57 private 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 */
72 { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
75 private int ncompr = sizeof(compr) / sizeof(compr[0]);
78 private ssize_t swrite(int, const void *, size_t);
79 private ssize_t sread(int, void *, size_t);
80 private size_t uncompressbuf(struct magic_set *, int, size_t,
81 const unsigned char *, unsigned char **, size_t);
82 #ifdef HAVE_LIBZ
83 private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
84 unsigned char **, size_t);
85 #endif
87 protected int
88 file_zmagic(struct magic_set *ms, int fd, const unsigned char *buf,
89 size_t nbytes)
91 unsigned char *newbuf = NULL;
92 size_t i, nsz;
93 int rv = 0;
95 if ((ms->flags & MAGIC_COMPRESS) == 0)
96 return 0;
98 for (i = 0; i < ncompr; i++) {
99 if (nbytes < compr[i].maglen)
100 continue;
101 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
102 (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
103 nbytes)) != 0) {
104 ms->flags &= ~MAGIC_COMPRESS;
105 rv = -1;
106 if (file_buffer(ms, -1, newbuf, nsz) == -1)
107 goto error;
108 if (file_printf(ms, " (") == -1)
109 goto error;
110 if (file_buffer(ms, -1, buf, nbytes) == -1)
111 goto error;
112 if (file_printf(ms, ")") == -1)
113 goto error;
114 rv = 1;
115 break;
118 error:
119 if (newbuf)
120 free(newbuf);
121 ms->flags |= MAGIC_COMPRESS;
122 return rv;
126 * `safe' write for sockets and pipes.
128 private ssize_t
129 swrite(int fd, const void *buf, size_t n)
131 int rv;
132 size_t rn = n;
135 switch (rv = write(fd, buf, n)) {
136 case -1:
137 if (errno == EINTR)
138 continue;
139 return -1;
140 default:
141 n -= rv;
142 buf = ((const char *)buf) + rv;
143 break;
145 while (n > 0);
146 return rn;
151 * `safe' read for sockets and pipes.
153 private ssize_t
154 sread(int fd, void *buf, size_t n)
156 int rv;
157 size_t rn = n;
160 switch (rv = read(fd, buf, n)) {
161 case -1:
162 if (errno == EINTR)
163 continue;
164 return -1;
165 case 0:
166 return rn - n;
167 default:
168 n -= rv;
169 buf = ((char *)buf) + rv;
170 break;
172 while (n > 0);
173 return rn;
176 protected int
177 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
178 size_t nbytes)
180 char buf[4096];
181 int r, tfd;
183 (void)strcpy(buf, "/tmp/file.XXXXXX");
184 #ifndef HAVE_MKSTEMP
186 char *ptr = mktemp(buf);
187 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
188 r = errno;
189 (void)unlink(ptr);
190 errno = r;
192 #else
193 tfd = mkstemp(buf);
194 r = errno;
195 (void)unlink(buf);
196 errno = r;
197 #endif
198 if (tfd == -1) {
199 file_error(ms, errno,
200 "cannot create temporary file for pipe copy");
201 return -1;
204 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
205 r = 1;
206 else {
207 while ((r = sread(fd, buf, sizeof(buf))) > 0)
208 if (swrite(tfd, buf, (size_t)r) != r)
209 break;
212 switch (r) {
213 case -1:
214 file_error(ms, errno, "error copying from pipe to temp file");
215 return -1;
216 case 0:
217 break;
218 default:
219 file_error(ms, errno, "error while writing to temp file");
220 return -1;
224 * We duplicate the file descriptor, because fclose on a
225 * tmpfile will delete the file, but any open descriptors
226 * can still access the phantom inode.
228 if ((fd = dup2(tfd, fd)) == -1) {
229 file_error(ms, errno, "could not dup descriptor for temp file");
230 return -1;
232 (void)close(tfd);
233 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
234 file_badseek(ms);
235 return -1;
237 return fd;
240 #ifdef HAVE_LIBZ
242 #define FHCRC (1 << 1)
243 #define FEXTRA (1 << 2)
244 #define FNAME (1 << 3)
245 #define FCOMMENT (1 << 4)
247 private size_t
248 uncompressgzipped(struct magic_set *ms, const unsigned char *old,
249 unsigned char **newch, size_t n)
251 unsigned char flg = old[3];
252 size_t data_start = 10;
253 z_stream z;
254 int rc;
256 if (flg & FEXTRA) {
257 if (data_start+1 >= n)
258 return 0;
259 data_start += 2 + old[data_start] + old[data_start + 1] * 256;
261 if (flg & FNAME) {
262 while(data_start < n && old[data_start])
263 data_start++;
264 data_start++;
266 if(flg & FCOMMENT) {
267 while(data_start < n && old[data_start])
268 data_start++;
269 data_start++;
271 if(flg & FHCRC)
272 data_start += 2;
274 if (data_start >= n)
275 return 0;
276 if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
277 return 0;
280 /* XXX: const castaway, via strchr */
281 z.next_in = (Bytef *)strchr((const char *)old + data_start,
282 old[data_start]);
283 z.avail_in = n - data_start;
284 z.next_out = *newch;
285 z.avail_out = HOWMANY;
286 z.zalloc = Z_NULL;
287 z.zfree = Z_NULL;
288 z.opaque = Z_NULL;
290 rc = inflateInit2(&z, -15);
291 if (rc != Z_OK) {
292 file_error(ms, 0, "zlib: %s", z.msg);
293 return 0;
296 rc = inflate(&z, Z_SYNC_FLUSH);
297 if (rc != Z_OK && rc != Z_STREAM_END) {
298 file_error(ms, 0, "zlib: %s", z.msg);
299 return 0;
302 n = (size_t)z.total_out;
303 inflateEnd(&z);
305 /* let's keep the nul-terminate tradition */
306 (*newch)[n++] = '\0';
308 return n;
310 #endif
312 private size_t
313 uncompressbuf(struct magic_set *ms, int fd, size_t method,
314 const unsigned char *old, unsigned char **newch, size_t n)
316 int fdin[2], fdout[2];
317 int r;
319 #ifdef HAVE_LIBZ
320 if (method == 2)
321 return uncompressgzipped(ms, old, newch, n);
322 #endif
323 (void)fflush(stdout);
324 (void)fflush(stderr);
326 if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
327 file_error(ms, errno, "cannot create pipe");
328 return 0;
330 switch (fork()) {
331 case 0: /* child */
332 (void) close(0);
333 if (fd != -1) {
334 (void) dup(fd);
335 (void) lseek(0, (off_t)0, SEEK_SET);
336 } else {
337 (void) dup(fdin[0]);
338 (void) close(fdin[0]);
339 (void) close(fdin[1]);
342 (void) close(1);
343 (void) dup(fdout[1]);
344 (void) close(fdout[0]);
345 (void) close(fdout[1]);
346 #ifndef DEBUG
347 if (compr[method].silent)
348 (void)close(2);
349 #endif
351 execvp(compr[method].argv[0],
352 (char *const *)compr[method].argv);
353 #ifdef DEBUG
354 (void)fprintf(stderr, "exec `%s' failed (%s)\n",
355 compr[method].argv[0], strerror(errno));
356 #endif
357 exit(1);
358 /*NOTREACHED*/
359 case -1:
360 file_error(ms, errno, "could not fork");
361 return 0;
363 default: /* parent */
364 (void) close(fdout[1]);
365 if (fd == -1) {
366 (void) close(fdin[0]);
368 * fork again, to avoid blocking because both
369 * pipes filled
371 switch (fork()) {
372 case 0: /* child */
373 (void)close(fdout[0]);
374 if (swrite(fdin[1], old, n) != n) {
375 #ifdef DEBUG
376 (void)fprintf(stderr,
377 "Write failed (%s)\n",
378 strerror(errno));
379 #endif
380 exit(1);
382 exit(0);
383 /*NOTREACHED*/
385 case -1:
386 #ifdef DEBUG
387 (void)fprintf(stderr, "Fork failed (%s)\n",
388 strerror(errno));
389 #endif
390 exit(1);
391 /*NOTREACHED*/
393 default: /* parent */
394 break;
396 (void) close(fdin[1]);
397 fdin[1] = -1;
400 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
401 #ifdef DEBUG
402 (void)fprintf(stderr, "Malloc failed (%s)\n",
403 strerror(errno));
404 #endif
405 n = 0;
406 goto err;
408 if ((r = sread(fdout[0], *newch, HOWMANY)) <= 0) {
409 #ifdef DEBUG
410 (void)fprintf(stderr, "Read failed (%s)\n",
411 strerror(errno));
412 #endif
413 free(*newch);
414 n = 0;
415 newch[0] = '\0';
416 goto err;
417 } else {
418 n = r;
420 /* NUL terminate, as every buffer is handled here. */
421 (*newch)[n++] = '\0';
422 err:
423 if (fdin[1] != -1)
424 (void) close(fdin[1]);
425 (void) close(fdout[0]);
426 #ifdef WNOHANG
427 while (waitpid(-1, NULL, WNOHANG) != -1)
428 continue;
429 #else
430 (void)wait(NULL);
431 #endif
432 return n;