Import file-4.23.
[dragonfly.git] / contrib / file-4.12 / src / compress.c
blobb76edb2f117e9805a1481e52e33fd4f53de39191
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.38 2004/09/11 19:15:57 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 *, size_t, const unsigned char *,
81 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, const unsigned char *buf, size_t nbytes)
90 unsigned char *newbuf = NULL;
91 size_t i, nsz;
92 int rv = 0;
94 if ((ms->flags & MAGIC_COMPRESS) == 0)
95 return 0;
97 for (i = 0; i < ncompr; i++) {
98 if (nbytes < compr[i].maglen)
99 continue;
100 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
101 (nsz = uncompressbuf(ms, i, buf, &newbuf, nbytes)) != 0) {
102 ms->flags &= ~MAGIC_COMPRESS;
103 rv = -1;
104 if (file_buffer(ms, newbuf, nsz) == -1)
105 goto error;
106 if (file_printf(ms, " (") == -1)
107 goto error;
108 if (file_buffer(ms, buf, nbytes) == -1)
109 goto error;
110 if (file_printf(ms, ")") == -1)
111 goto error;
112 rv = 1;
113 break;
116 error:
117 if (newbuf)
118 free(newbuf);
119 ms->flags |= MAGIC_COMPRESS;
120 return rv;
124 * `safe' write for sockets and pipes.
126 private ssize_t
127 swrite(int fd, const void *buf, size_t n)
129 int rv;
130 size_t rn = n;
133 switch (rv = write(fd, buf, n)) {
134 case -1:
135 if (errno == EINTR)
136 continue;
137 return -1;
138 default:
139 n -= rv;
140 buf = ((const char *)buf) + rv;
141 break;
143 while (n > 0);
144 return rn;
149 * `safe' read for sockets and pipes.
151 private ssize_t
152 sread(int fd, void *buf, size_t n)
154 int rv;
155 size_t rn = n;
158 switch (rv = read(fd, buf, n)) {
159 case -1:
160 if (errno == EINTR)
161 continue;
162 return -1;
163 case 0:
164 return rn - n;
165 default:
166 n -= rv;
167 buf = ((char *)buf) + rv;
168 break;
170 while (n > 0);
171 return rn;
174 protected int
175 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
176 size_t nbytes)
178 char buf[4096];
179 int r, tfd;
181 (void)strcpy(buf, "/tmp/file.XXXXXX");
182 #ifndef HAVE_MKSTEMP
184 char *ptr = mktemp(buf);
185 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
186 r = errno;
187 (void)unlink(ptr);
188 errno = r;
190 #else
191 tfd = mkstemp(buf);
192 r = errno;
193 (void)unlink(buf);
194 errno = r;
195 #endif
196 if (tfd == -1) {
197 file_error(ms, errno,
198 "cannot create temporary file for pipe copy");
199 return -1;
202 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
203 r = 1;
204 else {
205 while ((r = sread(fd, buf, sizeof(buf))) > 0)
206 if (swrite(tfd, buf, (size_t)r) != r)
207 break;
210 switch (r) {
211 case -1:
212 file_error(ms, errno, "error copying from pipe to temp file");
213 return -1;
214 case 0:
215 break;
216 default:
217 file_error(ms, errno, "error while writing to temp file");
218 return -1;
222 * We duplicate the file descriptor, because fclose on a
223 * tmpfile will delete the file, but any open descriptors
224 * can still access the phantom inode.
226 if ((fd = dup2(tfd, fd)) == -1) {
227 file_error(ms, errno, "could not dup descriptor for temp file");
228 return -1;
230 (void)close(tfd);
231 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
232 file_badseek(ms);
233 return -1;
235 return fd;
238 #ifdef HAVE_LIBZ
240 #define FHCRC (1 << 1)
241 #define FEXTRA (1 << 2)
242 #define FNAME (1 << 3)
243 #define FCOMMENT (1 << 4)
245 private size_t
246 uncompressgzipped(struct magic_set *ms, const unsigned char *old,
247 unsigned char **newch, size_t n)
249 unsigned char flg = old[3];
250 size_t data_start = 10;
251 z_stream z;
252 int rc;
254 if (flg & FEXTRA) {
255 if (data_start+1 >= n)
256 return 0;
257 data_start += 2 + old[data_start] + old[data_start + 1] * 256;
259 if (flg & FNAME) {
260 while(data_start < n && old[data_start])
261 data_start++;
262 data_start++;
264 if(flg & FCOMMENT) {
265 while(data_start < n && old[data_start])
266 data_start++;
267 data_start++;
269 if(flg & FHCRC)
270 data_start += 2;
272 if (data_start >= n)
273 return 0;
274 if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
275 return 0;
278 /* XXX: const castaway, via strchr */
279 z.next_in = (Bytef *)strchr((const char *)old + data_start,
280 old[data_start]);
281 z.avail_in = n - data_start;
282 z.next_out = *newch;
283 z.avail_out = HOWMANY;
284 z.zalloc = Z_NULL;
285 z.zfree = Z_NULL;
286 z.opaque = Z_NULL;
288 rc = inflateInit2(&z, -15);
289 if (rc != Z_OK) {
290 file_error(ms, 0, "zlib: %s", z.msg);
291 return 0;
294 rc = inflate(&z, Z_SYNC_FLUSH);
295 if (rc != Z_OK && rc != Z_STREAM_END) {
296 file_error(ms, 0, "zlib: %s", z.msg);
297 return 0;
300 n = (size_t)z.total_out;
301 inflateEnd(&z);
303 /* let's keep the nul-terminate tradition */
304 (*newch)[n++] = '\0';
306 return n;
308 #endif
310 private size_t
311 uncompressbuf(struct magic_set *ms, size_t method, const unsigned char *old,
312 unsigned char **newch, size_t n)
314 int fdin[2], fdout[2];
315 int r;
317 /* The buffer is NUL terminated, and we don't need that. */
318 n--;
320 #ifdef HAVE_LIBZ
321 if (method == 2)
322 return uncompressgzipped(ms, old, newch, n);
323 #endif
325 if (pipe(fdin) == -1 || pipe(fdout) == -1) {
326 file_error(ms, errno, "cannot create pipe");
327 return 0;
329 switch (fork()) {
330 case 0: /* child */
331 (void) close(0);
332 (void) dup(fdin[0]);
333 (void) close(fdin[0]);
334 (void) close(fdin[1]);
336 (void) close(1);
337 (void) dup(fdout[1]);
338 (void) close(fdout[0]);
339 (void) close(fdout[1]);
340 if (compr[method].silent)
341 (void) close(2);
343 execvp(compr[method].argv[0],
344 (char *const *)compr[method].argv);
345 exit(1);
346 /*NOTREACHED*/
347 case -1:
348 file_error(ms, errno, "could not fork");
349 return 0;
351 default: /* parent */
352 (void) close(fdin[0]);
353 (void) close(fdout[1]);
354 /* fork again, to avoid blocking because both pipes filled */
355 switch (fork()) {
356 case 0: /* child */
357 (void)close(fdout[0]);
358 if (swrite(fdin[1], old, n) != n)
359 exit(1);
360 exit(0);
361 /*NOTREACHED*/
363 case -1:
364 exit(1);
365 /*NOTREACHED*/
367 default: /* parent */
368 break;
370 (void) close(fdin[1]);
371 fdin[1] = -1;
372 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
373 n = 0;
374 goto err;
376 if ((r = sread(fdout[0], *newch, HOWMANY)) <= 0) {
377 free(*newch);
378 n = 0;
379 newch[0] = '\0';
380 goto err;
381 } else {
382 n = r;
384 /* NUL terminate, as every buffer is handled here. */
385 (*newch)[n++] = '\0';
386 err:
387 if (fdin[1] != -1)
388 (void) close(fdin[1]);
389 (void) close(fdout[0]);
390 #ifdef WNOHANG
391 while (waitpid(-1, NULL, WNOHANG) != -1)
392 continue;
393 #else
394 (void)wait(NULL);
395 #endif
396 return n;