xref: /netbsd-src/external/bsd/file/dist/src/compress.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: compress.c,v 1.20 2020/06/15 00:37:24 christos Exp $	*/
2 
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.
17  *
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.
29  */
30 /*
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
36  */
37 #include "file.h"
38 
39 #ifndef lint
40 #if 0
41 FILE_RCSID("@(#)$File: compress.c,v 1.127 2020/05/31 00:11:06 christos Exp $")
42 #else
43 __RCSID("$NetBSD: compress.c,v 1.20 2020/06/15 00:37:24 christos Exp $");
44 #endif
45 #endif
46 
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 <ctype.h>
55 #include <stdarg.h>
56 #include <signal.h>
57 #ifndef HAVE_SIG_T
58 typedef void (*sig_t)(int);
59 #endif /* HAVE_SIG_T */
60 #if !defined(__MINGW32__) && !defined(WIN32) && !defined(__MINGW64__)
61 #include <sys/ioctl.h>
62 #endif
63 #ifdef HAVE_SYS_WAIT_H
64 #include <sys/wait.h>
65 #endif
66 #if defined(HAVE_SYS_TIME_H)
67 #include <sys/time.h>
68 #endif
69 
70 #if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT)
71 #define BUILTIN_DECOMPRESS
72 #include <zlib.h>
73 #endif
74 
75 #if defined(HAVE_BZLIB_H) && defined(BZLIBSUPPORT)
76 #define BUILTIN_BZLIB
77 #include <bzlib.h>
78 #endif
79 
80 #if defined(HAVE_XZLIB_H) && defined(XZLIBSUPPORT)
81 #define BUILTIN_XZLIB
82 #include <lzma.h>
83 #endif
84 
85 #ifdef DEBUG
86 int tty = -1;
87 #define DPRINTF(...)	do { \
88 	if (tty == -1) \
89 		tty = open("/dev/tty", O_RDWR); \
90 	if (tty == -1) \
91 		abort(); \
92 	dprintf(tty, __VA_ARGS__); \
93 } while (/*CONSTCOND*/0)
94 #else
95 #define DPRINTF(...)
96 #endif
97 
98 #ifdef ZLIBSUPPORT
99 /*
100  * The following python code is not really used because ZLIBSUPPORT is only
101  * defined if we have a built-in zlib, and the built-in zlib handles that.
102  * That is not true for android where we have zlib.h and not -lz.
103  */
104 static const char zlibcode[] =
105     "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
106 
107 static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
108 
109 static int
110 zlibcmp(const unsigned char *buf)
111 {
112 	unsigned short x = 1;
113 	unsigned char *s = CAST(unsigned char *, CAST(void *, &x));
114 
115 	if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
116 		return 0;
117 	if (s[0] != 1)	/* endianness test */
118 		x = buf[0] | (buf[1] << 8);
119 	else
120 		x = buf[1] | (buf[0] << 8);
121 	if (x % 31)
122 		return 0;
123 	return 1;
124 }
125 #endif
126 
127 static int
128 lzmacmp(const unsigned char *buf)
129 {
130 	if (buf[0] != 0x5d || buf[1] || buf[2])
131 		return 0;
132 	if (buf[12] && buf[12] != 0xff)
133 		return 0;
134 	return 1;
135 }
136 
137 #define gzip_flags "-cd"
138 #define lrzip_flags "-do"
139 #define lzip_flags gzip_flags
140 
141 static const char *gzip_args[] = {
142 	"gzip", gzip_flags, NULL
143 };
144 static const char *uncompress_args[] = {
145 	"uncompress", "-c", NULL
146 };
147 static const char *bzip2_args[] = {
148 	"bzip2", "-cd", NULL
149 };
150 static const char *lzip_args[] = {
151 	"lzip", lzip_flags, NULL
152 };
153 static const char *xz_args[] = {
154 	"xz", "-cd", NULL
155 };
156 static const char *lrzip_args[] = {
157 	"lrzip", lrzip_flags, NULL
158 };
159 static const char *lz4_args[] = {
160 	"lz4", "-cd", NULL
161 };
162 static const char *zstd_args[] = {
163 	"zstd", "-cd", NULL
164 };
165 
166 #define	do_zlib		NULL
167 #define	do_bzlib	NULL
168 
169 private const struct {
170 	union {
171 		const char *magic;
172 		int (*func)(const unsigned char *);
173 	} u;
174 	int maglen;
175 	const char **argv;
176 	void *unused;
177 } compr[] = {
178 #define METH_FROZEN	2
179 #define METH_BZIP	7
180 #define METH_XZ		9
181 #define METH_LZMA	13
182 #define METH_ZLIB	14
183     { { .magic = "\037\235" },	2, gzip_args, NULL },	/* 0, compressed */
184     /* Uncompress can get stuck; so use gzip first if we have it
185      * Idea from Damien Clark, thanks! */
186     { { .magic = "\037\235" },	2, uncompress_args, NULL },/* 1, compressed */
187     { { .magic = "\037\213" },	2, gzip_args, do_zlib },/* 2, gzipped */
188     { { .magic = "\037\236" },	2, gzip_args, NULL },	/* 3, frozen */
189     { { .magic = "\037\240" },	2, gzip_args, NULL },	/* 4, SCO LZH */
190     /* the standard pack utilities do not accept standard input */
191     { { .magic = "\037\036" },	2, gzip_args, NULL },	/* 5, packed */
192     { { .magic = "PK\3\4" },	4, gzip_args, NULL },	/* 6, pkziped */
193     /* ...only first file examined */
194     { { .magic = "BZh" },	3, bzip2_args, do_bzlib },/* 7, bzip2-ed */
195     { { .magic = "LZIP" },	4, lzip_args, NULL },	/* 8, lzip-ed */
196     { { .magic = "\3757zXZ\0" },6, xz_args, NULL },	/* 9, XZ Util */
197     { { .magic = "LRZI" },	4, lrzip_args, NULL },	/* 10, LRZIP */
198     { { .magic = "\004\"M\030" },4, lz4_args, NULL },	/* 11, LZ4 */
199     { { .magic = "\x28\xB5\x2F\xFD" }, 4, zstd_args, NULL },/* 12, zstd */
200     { { .func = lzmacmp },	-13, xz_args, NULL },	/* 13, lzma */
201 #ifdef ZLIBSUPPORT
202     { { .func = zlibcmp },	-2, zlib_args, NULL },	/* 14, zlib */
203 #endif
204 };
205 
206 #define OKDATA 	0
207 #define NODATA	1
208 #define ERRDATA	2
209 
210 private ssize_t swrite(int, const void *, size_t);
211 #if HAVE_FORK
212 private size_t ncompr = __arraycount(compr);
213 private int uncompressbuf(int, size_t, size_t, const unsigned char *,
214     unsigned char **, size_t *);
215 #ifdef BUILTIN_DECOMPRESS
216 private int uncompresszlib(const unsigned char *, unsigned char **, size_t,
217     size_t *, int);
218 private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
219     size_t *);
220 #endif
221 #ifdef BUILTIN_BZLIB
222 private int uncompressbzlib(const unsigned char *, unsigned char **, size_t,
223     size_t *);
224 #endif
225 #ifdef BUILTIN_XZLIB
226 private int uncompressxzlib(const unsigned char *, unsigned char **, size_t,
227     size_t *);
228 #endif
229 
230 static int makeerror(unsigned char **, size_t *, const char *, ...)
231     __attribute__((__format__(__printf__, 3, 4)));
232 private const char *methodname(size_t);
233 
234 private int
235 format_decompression_error(struct magic_set *ms, size_t i, unsigned char *buf)
236 {
237 	unsigned char *p;
238 	int mime = ms->flags & MAGIC_MIME;
239 
240 	if (!mime)
241 		return file_printf(ms, "ERROR:[%s: %s]", methodname(i), buf);
242 
243 	for (p = buf; *p; p++)
244 		if (!isalnum(*p))
245 			*p = '-';
246 
247 	return file_printf(ms, "application/x-decompression-error-%s-%s",
248 	    methodname(i), buf);
249 }
250 
251 protected int
252 file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name)
253 {
254 	unsigned char *newbuf = NULL;
255 	size_t i, nsz;
256 	char *rbuf;
257 	file_pushbuf_t *pb;
258 	int urv, prv, rv = 0;
259 	int mime = ms->flags & MAGIC_MIME;
260 	int fd = b->fd;
261 	const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
262 	size_t nbytes = b->flen;
263 	int sa_saved = 0;
264 	struct sigaction sig_act;
265 
266 	if ((ms->flags & MAGIC_COMPRESS) == 0)
267 		return 0;
268 
269 	for (i = 0; i < ncompr; i++) {
270 		int zm;
271 		if (nbytes < CAST(size_t, abs(compr[i].maglen)))
272 			continue;
273 		if (compr[i].maglen < 0) {
274 			zm = (*compr[i].u.func)(buf);
275 		} else {
276 			zm = memcmp(buf, compr[i].u.magic,
277 			    CAST(size_t, compr[i].maglen)) == 0;
278 		}
279 
280 		if (!zm)
281 			continue;
282 
283 		/* Prevent SIGPIPE death if child dies unexpectedly */
284 		if (!sa_saved) {
285 			//We can use sig_act for both new and old, but
286 			struct sigaction new_act;
287 			memset(&new_act, 0, sizeof(new_act));
288 			new_act.sa_handler = SIG_IGN;
289 			sa_saved = sigaction(SIGPIPE, &new_act, &sig_act) != -1;
290 		}
291 
292 		nsz = nbytes;
293 		urv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz);
294 		DPRINTF("uncompressbuf = %d, %s, %" SIZE_T_FORMAT "u\n", urv,
295 		    (char *)newbuf, nsz);
296 		switch (urv) {
297 		case OKDATA:
298 		case ERRDATA:
299 			ms->flags &= ~MAGIC_COMPRESS;
300 			if (urv == ERRDATA)
301 				prv = format_decompression_error(ms, i, newbuf);
302 			else
303 				prv = file_buffer(ms, -1, NULL, name, newbuf, nsz);
304 			if (prv == -1)
305 				goto error;
306 			rv = 1;
307 			if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
308 				goto out;
309 			if (mime != MAGIC_MIME && mime != 0)
310 				goto out;
311 			if ((file_printf(ms,
312 			    mime ? " compressed-encoding=" : " (")) == -1)
313 				goto error;
314 			if ((pb = file_push_buffer(ms)) == NULL)
315 				goto error;
316 			/*
317 			 * XXX: If file_buffer fails here, we overwrite
318 			 * the compressed text. FIXME.
319 			 */
320 			if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1) {
321 				if (file_pop_buffer(ms, pb) != NULL)
322 					abort();
323 				goto error;
324 			}
325 			if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
326 				if (file_printf(ms, "%s", rbuf) == -1) {
327 					free(rbuf);
328 					goto error;
329 				}
330 				free(rbuf);
331 			}
332 			if (!mime && file_printf(ms, ")") == -1)
333 				goto error;
334 			/*FALLTHROUGH*/
335 		case NODATA:
336 			break;
337 		default:
338 			abort();
339 			/*NOTREACHED*/
340 		error:
341 			rv = -1;
342 			break;
343 		}
344 	}
345 out:
346 	DPRINTF("rv = %d\n", rv);
347 
348 	if (sa_saved && sig_act.sa_handler != SIG_IGN)
349 		(void)sigaction(SIGPIPE, &sig_act, NULL);
350 
351 	free(newbuf);
352 	ms->flags |= MAGIC_COMPRESS;
353 	DPRINTF("Zmagic returns %d\n", rv);
354 	return rv;
355 }
356 #endif
357 /*
358  * `safe' write for sockets and pipes.
359  */
360 private ssize_t
361 swrite(int fd, const void *buf, size_t n)
362 {
363 	ssize_t rv;
364 	size_t rn = n;
365 
366 	do
367 		switch (rv = write(fd, buf, n)) {
368 		case -1:
369 			if (errno == EINTR)
370 				continue;
371 			return -1;
372 		default:
373 			n -= rv;
374 			buf = CAST(const char *, buf) + rv;
375 			break;
376 		}
377 	while (n > 0);
378 	return rn;
379 }
380 
381 
382 /*
383  * `safe' read for sockets and pipes.
384  */
385 protected ssize_t
386 sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
387 {
388 	ssize_t rv;
389 #ifdef FIONREAD
390 	int t = 0;
391 #endif
392 	size_t rn = n;
393 
394 	if (fd == STDIN_FILENO)
395 		goto nocheck;
396 
397 #ifdef FIONREAD
398 	if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) {
399 #ifdef FD_ZERO
400 		ssize_t cnt;
401 		for (cnt = 0;; cnt++) {
402 			fd_set check;
403 			struct timeval tout = {0, 100 * 1000};
404 			int selrv;
405 
406 			FD_ZERO(&check);
407 			FD_SET(fd, &check);
408 
409 			/*
410 			 * Avoid soft deadlock: do not read if there
411 			 * is nothing to read from sockets and pipes.
412 			 */
413 			selrv = select(fd + 1, &check, NULL, NULL, &tout);
414 			if (selrv == -1) {
415 				if (errno == EINTR || errno == EAGAIN)
416 					continue;
417 			} else if (selrv == 0 && cnt >= 5) {
418 				return 0;
419 			} else
420 				break;
421 		}
422 #endif
423 		(void)ioctl(fd, FIONREAD, &t);
424 	}
425 
426 	if (t > 0 && CAST(size_t, t) < n) {
427 		n = t;
428 		rn = n;
429 	}
430 #endif
431 
432 nocheck:
433 	do
434 		switch ((rv = read(fd, buf, n))) {
435 		case -1:
436 			if (errno == EINTR)
437 				continue;
438 			return -1;
439 		case 0:
440 			return rn - n;
441 		default:
442 			n -= rv;
443 			buf = CAST(char *, CCAST(void *, buf)) + rv;
444 			break;
445 		}
446 	while (n > 0);
447 	return rn;
448 }
449 
450 protected int
451 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
452     size_t nbytes)
453 {
454 	char buf[4096];
455 	ssize_t r;
456 	int tfd;
457 
458 	(void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
459 #ifndef HAVE_MKSTEMP
460 	{
461 		char *ptr = mktemp(buf);
462 		tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
463 		r = errno;
464 		(void)unlink(ptr);
465 		errno = r;
466 	}
467 #else
468 	{
469 		int te;
470 		mode_t ou = umask(0);
471 		tfd = mkstemp(buf);
472 		(void)umask(ou);
473 		te = errno;
474 		(void)unlink(buf);
475 		errno = te;
476 	}
477 #endif
478 	if (tfd == -1) {
479 		file_error(ms, errno,
480 		    "cannot create temporary file for pipe copy");
481 		return -1;
482 	}
483 
484 	if (swrite(tfd, startbuf, nbytes) != CAST(ssize_t, nbytes))
485 		r = 1;
486 	else {
487 		while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
488 			if (swrite(tfd, buf, CAST(size_t, r)) != r)
489 				break;
490 	}
491 
492 	switch (r) {
493 	case -1:
494 		file_error(ms, errno, "error copying from pipe to temp file");
495 		return -1;
496 	case 0:
497 		break;
498 	default:
499 		file_error(ms, errno, "error while writing to temp file");
500 		return -1;
501 	}
502 
503 	/*
504 	 * We duplicate the file descriptor, because fclose on a
505 	 * tmpfile will delete the file, but any open descriptors
506 	 * can still access the phantom inode.
507 	 */
508 	if ((fd = dup2(tfd, fd)) == -1) {
509 		file_error(ms, errno, "could not dup descriptor for temp file");
510 		return -1;
511 	}
512 	(void)close(tfd);
513 	if (lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1)) {
514 		file_badseek(ms);
515 		return -1;
516 	}
517 	return fd;
518 }
519 #if HAVE_FORK
520 #ifdef BUILTIN_DECOMPRESS
521 
522 #define FHCRC		(1 << 1)
523 #define FEXTRA		(1 << 2)
524 #define FNAME		(1 << 3)
525 #define FCOMMENT	(1 << 4)
526 
527 
528 private int
529 uncompressgzipped(const unsigned char *old, unsigned char **newch,
530     size_t bytes_max, size_t *n)
531 {
532 	unsigned char flg = old[3];
533 	size_t data_start = 10;
534 
535 	if (flg & FEXTRA) {
536 		if (data_start + 1 >= *n)
537 			goto err;
538 		data_start += 2 + old[data_start] + old[data_start + 1] * 256;
539 	}
540 	if (flg & FNAME) {
541 		while(data_start < *n && old[data_start])
542 			data_start++;
543 		data_start++;
544 	}
545 	if (flg & FCOMMENT) {
546 		while(data_start < *n && old[data_start])
547 			data_start++;
548 		data_start++;
549 	}
550 	if (flg & FHCRC)
551 		data_start += 2;
552 
553 	if (data_start >= *n)
554 		goto err;
555 
556 	*n -= data_start;
557 	old += data_start;
558 	return uncompresszlib(old, newch, bytes_max, n, 0);
559 err:
560 	return makeerror(newch, n, "File too short");
561 }
562 
563 private int
564 uncompresszlib(const unsigned char *old, unsigned char **newch,
565     size_t bytes_max, size_t *n, int zlib)
566 {
567 	int rc;
568 	z_stream z;
569 
570 	if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
571 		return makeerror(newch, n, "No buffer, %s", strerror(errno));
572 
573 	z.next_in = CCAST(Bytef *, old);
574 	z.avail_in = CAST(uint32_t, *n);
575 	z.next_out = *newch;
576 	z.avail_out = CAST(unsigned int, bytes_max);
577 	z.zalloc = Z_NULL;
578 	z.zfree = Z_NULL;
579 	z.opaque = Z_NULL;
580 
581 	/* LINTED bug in header macro */
582 	rc = zlib ? inflateInit(&z) : inflateInit2(&z, -15);
583 	if (rc != Z_OK)
584 		goto err;
585 
586 	rc = inflate(&z, Z_SYNC_FLUSH);
587 	if (rc != Z_OK && rc != Z_STREAM_END)
588 		goto err;
589 
590 	*n = CAST(size_t, z.total_out);
591 	rc = inflateEnd(&z);
592 	if (rc != Z_OK)
593 		goto err;
594 
595 	/* let's keep the nul-terminate tradition */
596 	(*newch)[*n] = '\0';
597 
598 	return OKDATA;
599 err:
600 	strlcpy(RCAST(char *, *newch), z.msg ? z.msg : zError(rc), bytes_max);
601 	*n = strlen(RCAST(char *, *newch));
602 	return ERRDATA;
603 }
604 #endif
605 
606 #ifdef BUILTIN_BZLIB
607 private int
608 uncompressbzlib(const unsigned char *old, unsigned char **newch,
609     size_t bytes_max, size_t *n)
610 {
611 	int rc;
612 	bz_stream bz;
613 
614 	memset(&bz, 0, sizeof(bz));
615 	rc = BZ2_bzDecompressInit(&bz, 0, 0);
616 	if (rc != BZ_OK)
617 		goto err;
618 
619 	if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
620 		return makeerror(newch, n, "No buffer, %s", strerror(errno));
621 
622 	bz.next_in = CCAST(char *, RCAST(const char *, old));
623 	bz.avail_in = CAST(uint32_t, *n);
624 	bz.next_out = RCAST(char *, *newch);
625 	bz.avail_out = CAST(unsigned int, bytes_max);
626 
627 	rc = BZ2_bzDecompress(&bz);
628 	if (rc != BZ_OK && rc != BZ_STREAM_END)
629 		goto err;
630 
631 	/* Assume byte_max is within 32bit */
632 	/* assert(bz.total_out_hi32 == 0); */
633 	*n = CAST(size_t, bz.total_out_lo32);
634 	rc = BZ2_bzDecompressEnd(&bz);
635 	if (rc != BZ_OK)
636 		goto err;
637 
638 	/* let's keep the nul-terminate tradition */
639 	(*newch)[*n] = '\0';
640 
641 	return OKDATA;
642 err:
643 	snprintf(RCAST(char *, *newch), bytes_max, "bunzip error %d", rc);
644 	*n = strlen(RCAST(char *, *newch));
645 	return ERRDATA;
646 }
647 #endif
648 
649 #ifdef BUILTIN_XZLIB
650 private int
651 uncompressxzlib(const unsigned char *old, unsigned char **newch,
652     size_t bytes_max, size_t *n)
653 {
654 	int rc;
655 	lzma_stream xz;
656 
657 	memset(&xz, 0, sizeof(xz));
658 	rc = lzma_auto_decoder(&xz, UINT64_MAX, 0);
659 	if (rc != LZMA_OK)
660 		goto err;
661 
662 	if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
663 		return makeerror(newch, n, "No buffer, %s", strerror(errno));
664 
665 	xz.next_in = CCAST(const uint8_t *, old);
666 	xz.avail_in = CAST(uint32_t, *n);
667 	xz.next_out = RCAST(uint8_t *, *newch);
668 	xz.avail_out = CAST(unsigned int, bytes_max);
669 
670 	rc = lzma_code(&xz, LZMA_RUN);
671 	if (rc != LZMA_OK && rc != LZMA_STREAM_END)
672 		goto err;
673 
674 	*n = CAST(size_t, xz.total_out);
675 
676 	lzma_end(&xz);
677 
678 	/* let's keep the nul-terminate tradition */
679 	(*newch)[*n] = '\0';
680 
681 	return OKDATA;
682 err:
683 	snprintf(RCAST(char *, *newch), bytes_max, "unxz error %d", rc);
684 	*n = strlen(RCAST(char *, *newch));
685 	return ERRDATA;
686 }
687 #endif
688 
689 
690 static int
691 makeerror(unsigned char **buf, size_t *len, const char *fmt, ...)
692 {
693 	char *msg;
694 	va_list ap;
695 	int rv;
696 
697 	va_start(ap, fmt);
698 	rv = vasprintf(&msg, fmt, ap);
699 	va_end(ap);
700 	if (rv < 0) {
701 		*buf = NULL;
702 		*len = 0;
703 		return NODATA;
704 	}
705 	*buf = RCAST(unsigned char *, msg);
706 	*len = strlen(msg);
707 	return ERRDATA;
708 }
709 
710 static void
711 closefd(int *fd, size_t i)
712 {
713 	if (fd[i] == -1)
714 		return;
715 	(void) close(fd[i]);
716 	fd[i] = -1;
717 }
718 
719 static void
720 closep(int *fd)
721 {
722 	size_t i;
723 	for (i = 0; i < 2; i++)
724 		closefd(fd, i);
725 }
726 
727 static int
728 copydesc(int i, int fd)
729 {
730 	if (fd == i)
731 		return 0; /* "no dup was necessary" */
732 	if (dup2(fd, i) == -1) {
733 		DPRINTF("dup(%d, %d) failed (%s)\n", fd, i, strerror(errno));
734 		exit(1);
735 	}
736 	return 1;
737 }
738 
739 static pid_t
740 writechild(int fd, const void *old, size_t n)
741 {
742 	pid_t pid;
743 
744 	/*
745 	 * fork again, to avoid blocking because both
746 	 * pipes filled
747 	 */
748 	pid = fork();
749 	if (pid == -1) {
750 		DPRINTF("Fork failed (%s)\n", strerror(errno));
751 		exit(1);
752 	}
753 	if (pid == 0) {
754 		/* child */
755 		if (swrite(fd, old, n) != CAST(ssize_t, n)) {
756 			DPRINTF("Write failed (%s)\n", strerror(errno));
757 			exit(1);
758 		}
759 		exit(0);
760 	}
761 	/* parent */
762 	return pid;
763 }
764 
765 static ssize_t
766 filter_error(unsigned char *ubuf, ssize_t n)
767 {
768 	char *p;
769 	char *buf;
770 
771 	ubuf[n] = '\0';
772 	buf = RCAST(char *, ubuf);
773 	while (isspace(CAST(unsigned char, *buf)))
774 		buf++;
775 	DPRINTF("Filter error[[[%s]]]\n", buf);
776 	if ((p = strchr(CAST(char *, buf), '\n')) != NULL)
777 		*p = '\0';
778 	if ((p = strchr(CAST(char *, buf), ';')) != NULL)
779 		*p = '\0';
780 	if ((p = strrchr(CAST(char *, buf), ':')) != NULL) {
781 		++p;
782 		while (isspace(CAST(unsigned char, *p)))
783 			p++;
784 		n = strlen(p);
785 		memmove(ubuf, p, CAST(size_t, n + 1));
786 	}
787 	DPRINTF("Filter error after[[[%s]]]\n", (char *)ubuf);
788 	if (islower(*ubuf))
789 		*ubuf = toupper(*ubuf);
790 	return n;
791 }
792 
793 private const char *
794 methodname(size_t method)
795 {
796 	switch (method) {
797 #ifdef BUILTIN_DECOMPRESS
798 	case METH_FROZEN:
799 	case METH_ZLIB:
800 		return "zlib";
801 #endif
802 #ifdef BUILTIN_BZLIB
803 	case METH_BZIP:
804 		return "bzlib";
805 #endif
806 #ifdef BUILTIN_XZLIB
807 	case METH_XZ:
808 	case METH_LZMA:
809 		return "xzlib";
810 #endif
811 	default:
812 		return compr[method].argv[0];
813 	}
814 }
815 
816 private int
817 uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old,
818     unsigned char **newch, size_t* n)
819 {
820 	int fdp[3][2];
821 	int status, rv, w;
822 	pid_t pid;
823 	pid_t writepid = -1;
824 	size_t i;
825 	ssize_t r;
826 
827 	switch (method) {
828 #ifdef BUILTIN_DECOMPRESS
829 	case METH_FROZEN:
830 		return uncompressgzipped(old, newch, bytes_max, n);
831 	case METH_ZLIB:
832 		return uncompresszlib(old, newch, bytes_max, n, 1);
833 #endif
834 #ifdef BUILTIN_BZLIB
835 	case METH_BZIP:
836 		return uncompressbzlib(old, newch, bytes_max, n);
837 #endif
838 #ifdef BUILTIN_XZLIB
839 	case METH_XZ:
840 	case METH_LZMA:
841 		return uncompressxzlib(old, newch, bytes_max, n);
842 #endif
843 	default:
844 		break;
845 	}
846 
847 	(void)fflush(stdout);
848 	(void)fflush(stderr);
849 
850 	for (i = 0; i < __arraycount(fdp); i++)
851 		fdp[i][0] = fdp[i][1] = -1;
852 
853 	if ((fd == -1 && pipe(fdp[STDIN_FILENO]) == -1) ||
854 	    pipe(fdp[STDOUT_FILENO]) == -1 || pipe(fdp[STDERR_FILENO]) == -1) {
855 		closep(fdp[STDIN_FILENO]);
856 		closep(fdp[STDOUT_FILENO]);
857 		return makeerror(newch, n, "Cannot create pipe, %s",
858 		    strerror(errno));
859 	}
860 
861 	/* For processes with large mapped virtual sizes, vfork
862 	 * may be _much_ faster (10-100 times) than fork.
863 	 */
864 	pid = vfork();
865 	if (pid == -1) {
866 		return makeerror(newch, n, "Cannot vfork, %s",
867 		    strerror(errno));
868 	}
869 	if (pid == 0) {
870 		/* child */
871 		/* Note: we are after vfork, do not modify memory
872 		 * in a way which confuses parent. In particular,
873 		 * do not modify fdp[i][j].
874 		 */
875 		if (fd != -1) {
876 			(void) lseek(fd, CAST(off_t, 0), SEEK_SET);
877 			if (copydesc(STDIN_FILENO, fd))
878 				(void) close(fd);
879 		} else {
880 			if (copydesc(STDIN_FILENO, fdp[STDIN_FILENO][0]))
881 				(void) close(fdp[STDIN_FILENO][0]);
882 			if (fdp[STDIN_FILENO][1] > 2)
883 				(void) close(fdp[STDIN_FILENO][1]);
884 		}
885 ///FIXME: if one of the fdp[i][j] is 0 or 1, this can bomb spectacularly
886 		if (copydesc(STDOUT_FILENO, fdp[STDOUT_FILENO][1]))
887 			(void) close(fdp[STDOUT_FILENO][1]);
888 		if (fdp[STDOUT_FILENO][0] > 2)
889 			(void) close(fdp[STDOUT_FILENO][0]);
890 
891 		if (copydesc(STDERR_FILENO, fdp[STDERR_FILENO][1]))
892 			(void) close(fdp[STDERR_FILENO][1]);
893 		if (fdp[STDERR_FILENO][0] > 2)
894 			(void) close(fdp[STDERR_FILENO][0]);
895 
896 		(void)execvp(compr[method].argv[0],
897 		    RCAST(char *const *, RCAST(intptr_t, compr[method].argv)));
898 		dprintf(STDERR_FILENO, "exec `%s' failed, %s",
899 		    compr[method].argv[0], strerror(errno));
900 		_exit(1); /* _exit(), not exit(), because of vfork */
901 	}
902 	/* parent */
903 	/* Close write sides of child stdout/err pipes */
904 	for (i = 1; i < __arraycount(fdp); i++)
905 		closefd(fdp[i], 1);
906 	/* Write the buffer data to child stdin, if we don't have fd */
907 	if (fd == -1) {
908 		closefd(fdp[STDIN_FILENO], 0);
909 		writepid = writechild(fdp[STDIN_FILENO][1], old, *n);
910 		closefd(fdp[STDIN_FILENO], 1);
911 	}
912 
913 	*newch = CAST(unsigned char *, malloc(bytes_max + 1));
914 	if (*newch == NULL) {
915 		rv = makeerror(newch, n, "No buffer, %s",
916 		    strerror(errno));
917 		goto err;
918 	}
919 	rv = OKDATA;
920 	r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0);
921 	if (r <= 0) {
922 		DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0],
923 		    r != -1 ? strerror(errno) : "no data");
924 
925 		rv = ERRDATA;
926 		if (r == 0 &&
927 		    (r = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0)
928 		{
929 			r = filter_error(*newch, r);
930 			goto ok;
931 		}
932 		free(*newch);
933 		if  (r == 0)
934 			rv = makeerror(newch, n, "Read failed, %s",
935 			    strerror(errno));
936 		else
937 			rv = makeerror(newch, n, "No data");
938 		goto err;
939 	}
940 ok:
941 	*n = r;
942 	/* NUL terminate, as every buffer is handled here. */
943 	(*newch)[*n] = '\0';
944 err:
945 	closefd(fdp[STDIN_FILENO], 1);
946 	closefd(fdp[STDOUT_FILENO], 0);
947 	closefd(fdp[STDERR_FILENO], 0);
948 
949 	w = waitpid(pid, &status, 0);
950 wait_err:
951 	if (w == -1) {
952 		free(*newch);
953 		rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
954 		DPRINTF("Child wait return %#x\n", status);
955 	} else if (!WIFEXITED(status)) {
956 		DPRINTF("Child not exited (%#x)\n", status);
957 	} else if (WEXITSTATUS(status) != 0) {
958 		DPRINTF("Child exited (%#x)\n", WEXITSTATUS(status));
959 	}
960 	if (writepid > 0) {
961 		/* _After_ we know decompressor has exited, our input writer
962 		 * definitely will exit now (at worst, writing fails in it,
963 		 * since output fd is closed now on the reading size).
964 		 */
965 		w = waitpid(writepid, &status, 0);
966 		writepid = -1;
967 		goto wait_err;
968 	}
969 
970 	closefd(fdp[STDIN_FILENO], 0); //why? it is already closed here!
971 	DPRINTF("Returning %p n=%" SIZE_T_FORMAT "u rv=%d\n", *newch, *n, rv);
972 
973 	return rv;
974 }
975 #endif
976