No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / file / dist / src / compress.c
blob6f32c2dd97291c1bcdcec637600b965a87f1b07b
1 /* $NetBSD: compress.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $ */
3 /*
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
31 * compress routines:
32 * zmagic() - returns 0 if not recognized, uncompresses and prints
33 * information if recognized
34 * uncompress(method, old, n, newch) - uncompress old into new,
35 * using method, return sizeof new
37 #include "file.h"
39 #ifndef lint
40 #if 0
41 FILE_RCSID("@(#)$File: compress.c,v 1.63 2009/03/23 14:21:51 christos Exp $")
42 #else
43 __RCSID("$NetBSD: compress.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $");
44 #endif
45 #endif
47 #include "magic.h"
48 #include <stdlib.h>
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52 #include <string.h>
53 #include <errno.h>
54 #include <sys/ioctl.h>
55 #ifdef HAVE_SYS_WAIT_H
56 #include <sys/wait.h>
57 #endif
58 #if defined(HAVE_SYS_TIME_H)
59 #include <sys/time.h>
60 #endif
61 #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
62 #define BUILTIN_DECOMPRESS
63 #include <zlib.h>
64 #endif
66 private const struct {
67 const char magic[8];
68 size_t maglen;
69 const char *argv[3];
70 int silent;
71 } compr[] = {
72 { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
73 /* Uncompress can get stuck; so use gzip first if we have it
74 * Idea from Damien Clark, thanks! */
75 { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
76 { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
77 { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
78 { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
79 /* the standard pack utilities do not accept standard input */
80 { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
81 { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */
82 /* ...only first file examined */
83 { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
84 { "LZIP", 4, { "lzip", "-cdq", NULL }, 1 },
85 { "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */
88 private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
90 #define NODATA ((size_t)~0)
93 private ssize_t swrite(int, const void *, size_t);
94 private size_t uncompressbuf(struct magic_set *, int, size_t,
95 const unsigned char *, unsigned char **, size_t);
96 #ifdef BUILTIN_DECOMPRESS
97 private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
98 unsigned char **, size_t);
99 #endif
101 protected int
102 file_zmagic(struct magic_set *ms, int fd, const char *name,
103 const unsigned char *buf, size_t nbytes)
105 unsigned char *newbuf = NULL;
106 size_t i, nsz;
107 int rv = 0;
108 int mime = ms->flags & MAGIC_MIME;
110 if ((ms->flags & MAGIC_COMPRESS) == 0)
111 return 0;
113 for (i = 0; i < ncompr; i++) {
114 if (nbytes < compr[i].maglen)
115 continue;
116 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
117 (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
118 nbytes)) != NODATA) {
119 ms->flags &= ~MAGIC_COMPRESS;
120 rv = -1;
121 if (file_buffer(ms, -1, name, newbuf, nsz) == -1)
122 goto error;
124 if (mime == MAGIC_MIME || mime == 0) {
125 if (file_printf(ms, mime ?
126 " compressed-encoding=" : " (") == -1)
127 goto error;
130 if ((mime == 0 || mime & MAGIC_MIME_ENCODING) &&
131 file_buffer(ms, -1, NULL, buf, nbytes) == -1)
132 goto error;
134 if (!mime && file_printf(ms, ")") == -1)
135 goto error;
136 rv = 1;
137 break;
140 error:
141 if (newbuf)
142 free(newbuf);
143 ms->flags |= MAGIC_COMPRESS;
144 return rv;
148 * `safe' write for sockets and pipes.
150 private ssize_t
151 swrite(int fd, const void *buf, size_t n)
153 ssize_t rv;
154 size_t rn = n;
157 switch (rv = write(fd, buf, n)) {
158 case -1:
159 if (errno == EINTR)
160 continue;
161 return -1;
162 default:
163 n -= rv;
164 buf = CAST(const char *, buf) + rv;
165 break;
167 while (n > 0);
168 return rn;
173 * `safe' read for sockets and pipes.
175 protected ssize_t
176 sread(int fd, void *buf, size_t n, int canbepipe)
178 ssize_t rv, cnt;
179 #ifdef FIONREAD
180 int t = 0;
181 #endif
182 size_t rn = n;
184 if (fd == STDIN_FILENO)
185 goto nocheck;
187 #ifdef FIONREAD
188 if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {
189 #ifdef FD_ZERO
190 for (cnt = 0;; cnt++) {
191 fd_set check;
192 struct timeval tout = {0, 100 * 1000};
193 int selrv;
195 FD_ZERO(&check);
196 FD_SET(fd, &check);
199 * Avoid soft deadlock: do not read if there
200 * is nothing to read from sockets and pipes.
202 selrv = select(fd + 1, &check, NULL, NULL, &tout);
203 if (selrv == -1) {
204 if (errno == EINTR || errno == EAGAIN)
205 continue;
206 } else if (selrv == 0 && cnt >= 5) {
207 return 0;
208 } else
209 break;
211 #endif
212 (void)ioctl(fd, FIONREAD, &t);
215 if (t > 0 && (size_t)t < n) {
216 n = t;
217 rn = n;
219 #endif
221 nocheck:
223 switch ((rv = read(fd, buf, n))) {
224 case -1:
225 if (errno == EINTR)
226 continue;
227 return -1;
228 case 0:
229 return rn - n;
230 default:
231 n -= rv;
232 buf = ((char *)buf) + rv;
233 break;
235 while (n > 0);
236 return rn;
239 protected int
240 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
241 size_t nbytes)
243 char buf[4096];
244 ssize_t r;
245 int tfd, te;
247 (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
248 #ifndef HAVE_MKSTEMP
250 char *ptr = mktemp(buf);
251 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
252 r = errno;
253 (void)unlink(ptr);
254 errno = r;
256 #else
257 tfd = mkstemp(buf);
258 te = errno;
259 (void)unlink(buf);
260 errno = te;
261 #endif
262 if (tfd == -1) {
263 file_error(ms, errno,
264 "cannot create temporary file for pipe copy");
265 return -1;
268 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
269 r = 1;
270 else {
271 while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
272 if (swrite(tfd, buf, (size_t)r) != r)
273 break;
276 switch (r) {
277 case -1:
278 file_error(ms, errno, "error copying from pipe to temp file");
279 return -1;
280 case 0:
281 break;
282 default:
283 file_error(ms, errno, "error while writing to temp file");
284 return -1;
288 * We duplicate the file descriptor, because fclose on a
289 * tmpfile will delete the file, but any open descriptors
290 * can still access the phantom inode.
292 if ((fd = dup2(tfd, fd)) == -1) {
293 file_error(ms, errno, "could not dup descriptor for temp file");
294 return -1;
296 (void)close(tfd);
297 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
298 file_badseek(ms);
299 return -1;
301 return fd;
304 #ifdef BUILTIN_DECOMPRESS
306 #define FHCRC (1 << 1)
307 #define FEXTRA (1 << 2)
308 #define FNAME (1 << 3)
309 #define FCOMMENT (1 << 4)
311 private size_t
312 uncompressgzipped(struct magic_set *ms, const unsigned char *old,
313 unsigned char **newch, size_t n)
315 unsigned char flg = old[3];
316 size_t data_start = 10;
317 z_stream z;
318 int rc;
320 if (flg & FEXTRA) {
321 if (data_start+1 >= n)
322 return 0;
323 data_start += 2 + old[data_start] + old[data_start + 1] * 256;
325 if (flg & FNAME) {
326 while(data_start < n && old[data_start])
327 data_start++;
328 data_start++;
330 if(flg & FCOMMENT) {
331 while(data_start < n && old[data_start])
332 data_start++;
333 data_start++;
335 if(flg & FHCRC)
336 data_start += 2;
338 if (data_start >= n)
339 return 0;
340 if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
341 return 0;
344 /* XXX: const castaway, via strchr */
345 z.next_in = (Bytef *)strchr((const char *)old + data_start,
346 old[data_start]);
347 z.avail_in = CAST(uint32_t, (n - data_start));
348 z.next_out = *newch;
349 z.avail_out = HOWMANY;
350 z.zalloc = Z_NULL;
351 z.zfree = Z_NULL;
352 z.opaque = Z_NULL;
354 /* LINTED bug in header macro */
355 rc = inflateInit2(&z, -15);
356 if (rc != Z_OK) {
357 file_error(ms, 0, "zlib: %s", z.msg);
358 return 0;
361 rc = inflate(&z, Z_SYNC_FLUSH);
362 if (rc != Z_OK && rc != Z_STREAM_END) {
363 file_error(ms, 0, "zlib: %s", z.msg);
364 return 0;
367 n = (size_t)z.total_out;
368 (void)inflateEnd(&z);
370 /* let's keep the nul-terminate tradition */
371 (*newch)[n] = '\0';
373 return n;
375 #endif
377 private size_t
378 uncompressbuf(struct magic_set *ms, int fd, size_t method,
379 const unsigned char *old, unsigned char **newch, size_t n)
381 int fdin[2], fdout[2];
382 ssize_t r;
384 #ifdef BUILTIN_DECOMPRESS
385 /* FIXME: This doesn't cope with bzip2 */
386 if (method == 2)
387 return uncompressgzipped(ms, old, newch, n);
388 #endif
389 (void)fflush(stdout);
390 (void)fflush(stderr);
392 if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
393 file_error(ms, errno, "cannot create pipe");
394 return NODATA;
396 switch (fork()) {
397 case 0: /* child */
398 (void) close(0);
399 if (fd != -1) {
400 (void) dup(fd);
401 (void) lseek(0, (off_t)0, SEEK_SET);
402 } else {
403 (void) dup(fdin[0]);
404 (void) close(fdin[0]);
405 (void) close(fdin[1]);
408 (void) close(1);
409 (void) dup(fdout[1]);
410 (void) close(fdout[0]);
411 (void) close(fdout[1]);
412 #ifndef DEBUG
413 if (compr[method].silent)
414 (void)close(2);
415 #endif
417 (void)execvp(compr[method].argv[0],
418 (char *const *)(intptr_t)compr[method].argv);
419 #ifdef DEBUG
420 (void)fprintf(stderr, "exec `%s' failed (%s)\n",
421 compr[method].argv[0], strerror(errno));
422 #endif
423 exit(1);
424 /*NOTREACHED*/
425 case -1:
426 file_error(ms, errno, "could not fork");
427 return NODATA;
429 default: /* parent */
430 (void) close(fdout[1]);
431 if (fd == -1) {
432 (void) close(fdin[0]);
434 * fork again, to avoid blocking because both
435 * pipes filled
437 switch (fork()) {
438 case 0: /* child */
439 (void)close(fdout[0]);
440 if (swrite(fdin[1], old, n) != (ssize_t)n) {
441 #ifdef DEBUG
442 (void)fprintf(stderr,
443 "Write failed (%s)\n",
444 strerror(errno));
445 #endif
446 exit(1);
448 exit(0);
449 /*NOTREACHED*/
451 case -1:
452 #ifdef DEBUG
453 (void)fprintf(stderr, "Fork failed (%s)\n",
454 strerror(errno));
455 #endif
456 exit(1);
457 /*NOTREACHED*/
459 default: /* parent */
460 break;
462 (void) close(fdin[1]);
463 fdin[1] = -1;
466 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
467 #ifdef DEBUG
468 (void)fprintf(stderr, "Malloc failed (%s)\n",
469 strerror(errno));
470 #endif
471 n = 0;
472 goto err;
474 if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
475 #ifdef DEBUG
476 (void)fprintf(stderr, "Read failed (%s)\n",
477 strerror(errno));
478 #endif
479 free(*newch);
480 n = 0;
481 newch[0] = '\0';
482 goto err;
483 } else {
484 n = r;
486 /* NUL terminate, as every buffer is handled here. */
487 (*newch)[n] = '\0';
488 err:
489 if (fdin[1] != -1)
490 (void) close(fdin[1]);
491 (void) close(fdout[0]);
492 #ifdef WNOHANG
493 while (waitpid(-1, NULL, WNOHANG) != -1)
494 continue;
495 #else
496 (void)wait(NULL);
497 #endif
498 (void) close(fdin[0]);
500 return n;