xref: /freebsd/contrib/file/src/compress.c (revision ae316d1d1cffd71ab7751f94e10118777a88e027)
1b6cee71dSXin LI /*
2b6cee71dSXin LI  * Copyright (c) Ian F. Darwin 1986-1995.
3b6cee71dSXin LI  * Software written by Ian F. Darwin and others;
4b6cee71dSXin LI  * maintained 1995-present by Christos Zoulas and others.
5b6cee71dSXin LI  *
6b6cee71dSXin LI  * Redistribution and use in source and binary forms, with or without
7b6cee71dSXin LI  * modification, are permitted provided that the following conditions
8b6cee71dSXin LI  * are met:
9b6cee71dSXin LI  * 1. Redistributions of source code must retain the above copyright
10b6cee71dSXin LI  *    notice immediately at the beginning of the file, without modification,
11b6cee71dSXin LI  *    this list of conditions, and the following disclaimer.
12b6cee71dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
13b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer in the
14b6cee71dSXin LI  *    documentation and/or other materials provided with the distribution.
15b6cee71dSXin LI  *
16b6cee71dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b6cee71dSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b6cee71dSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b6cee71dSXin LI  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20b6cee71dSXin LI  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b6cee71dSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b6cee71dSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b6cee71dSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b6cee71dSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b6cee71dSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b6cee71dSXin LI  * SUCH DAMAGE.
27b6cee71dSXin LI  */
28b6cee71dSXin LI /*
29b6cee71dSXin LI  * compress routines:
30b6cee71dSXin LI  *	zmagic() - returns 0 if not recognized, uncompresses and prints
31b6cee71dSXin LI  *		   information if recognized
32b6cee71dSXin LI  *	uncompress(method, old, n, newch) - uncompress old into new,
33b6cee71dSXin LI  *					    using method, return sizeof new
34b6cee71dSXin LI  */
35b6cee71dSXin LI #include "file.h"
36b6cee71dSXin LI 
37b6cee71dSXin LI #ifndef lint
38*ae316d1dSXin LI FILE_RCSID("@(#)$File: compress.c,v 1.158 2024/11/10 16:52:27 christos Exp $")
39b6cee71dSXin LI #endif
40b6cee71dSXin LI 
41b6cee71dSXin LI #include "magic.h"
42b6cee71dSXin LI #include <stdlib.h>
43b6cee71dSXin LI #ifdef HAVE_UNISTD_H
44b6cee71dSXin LI #include <unistd.h>
45b6cee71dSXin LI #endif
46a4d6d3b8SXin LI #ifdef HAVE_SPAWN_H
47a4d6d3b8SXin LI #include <spawn.h>
48a4d6d3b8SXin LI #endif
49*ae316d1dSXin LI #include <stdio.h>
50b6cee71dSXin LI #include <string.h>
51b6cee71dSXin LI #include <errno.h>
523e41d09dSXin LI #include <ctype.h>
533e41d09dSXin LI #include <stdarg.h>
544460e5b0SXin LI #include <signal.h>
555f0216bdSXin LI #ifndef HAVE_SIG_T
565f0216bdSXin LI typedef void (*sig_t)(int);
575f0216bdSXin LI #endif /* HAVE_SIG_T */
58a4d6d3b8SXin LI #ifdef HAVE_SYS_IOCTL_H
59b6cee71dSXin LI #include <sys/ioctl.h>
60b6cee71dSXin LI #endif
61b6cee71dSXin LI #ifdef HAVE_SYS_WAIT_H
62b6cee71dSXin LI #include <sys/wait.h>
63b6cee71dSXin LI #endif
64b6cee71dSXin LI #if defined(HAVE_SYS_TIME_H)
65b6cee71dSXin LI #include <sys/time.h>
66b6cee71dSXin LI #endif
6748c779cdSXin LI 
6840427ccaSGordon Tetlow #if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT)
69b6cee71dSXin LI #define BUILTIN_DECOMPRESS
70b6cee71dSXin LI #include <zlib.h>
713e41d09dSXin LI #endif
7248c779cdSXin LI 
732726a701SXin LI #if defined(HAVE_BZLIB_H) && defined(BZLIBSUPPORT)
7448c779cdSXin LI #define BUILTIN_BZLIB
7548c779cdSXin LI #include <bzlib.h>
7648c779cdSXin LI #endif
7748c779cdSXin LI 
7843a5ec4eSXin LI #if defined(HAVE_LZMA_H) && defined(XZLIBSUPPORT)
79d38c30c0SXin LI #define BUILTIN_XZLIB
80d38c30c0SXin LI #include <lzma.h>
81d38c30c0SXin LI #endif
82d38c30c0SXin LI 
83898496eeSXin LI #if defined(HAVE_ZSTD_H) && defined(ZSTDLIBSUPPORT)
84898496eeSXin LI #define BUILTIN_ZSTDLIB
85898496eeSXin LI #include <zstd.h>
86898496eeSXin LI #include <zstd_errors.h>
87898496eeSXin LI #endif
88898496eeSXin LI 
89898496eeSXin LI #if defined(HAVE_LZLIB_H) && defined(LZLIBSUPPORT)
90898496eeSXin LI #define BUILTIN_LZLIB
91898496eeSXin LI #include <lzlib.h>
92898496eeSXin LI #endif
93898496eeSXin LI 
94*ae316d1dSXin LI #ifdef notyet
95*ae316d1dSXin LI #if defined(HAVE_LRZIP_H) && defined(LRZIPLIBSUPPORT)
96*ae316d1dSXin LI #define BUILTIN_LRZIP
97*ae316d1dSXin LI #include <Lrzip.h>
98*ae316d1dSXin LI #endif
99*ae316d1dSXin LI #endif
100*ae316d1dSXin LI 
1013e41d09dSXin LI #ifdef DEBUG
1023e41d09dSXin LI int tty = -1;
1033e41d09dSXin LI #define DPRINTF(...)	do { \
1043e41d09dSXin LI 	if (tty == -1) \
1053e41d09dSXin LI 		tty = open("/dev/tty", O_RDWR); \
1063e41d09dSXin LI 	if (tty == -1) \
1073e41d09dSXin LI 		abort(); \
1083e41d09dSXin LI 	dprintf(tty, __VA_ARGS__); \
1093e41d09dSXin LI } while (/*CONSTCOND*/0)
1103e41d09dSXin LI #else
1113e41d09dSXin LI #define DPRINTF(...)
112b6cee71dSXin LI #endif
113b6cee71dSXin LI 
1143e41d09dSXin LI #ifdef ZLIBSUPPORT
1153e41d09dSXin LI /*
1163e41d09dSXin LI  * The following python code is not really used because ZLIBSUPPORT is only
1173e41d09dSXin LI  * defined if we have a built-in zlib, and the built-in zlib handles that.
11840427ccaSGordon Tetlow  * That is not true for android where we have zlib.h and not -lz.
1193e41d09dSXin LI  */
1203e41d09dSXin LI static const char zlibcode[] =
1213e41d09dSXin LI     "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
1223e41d09dSXin LI 
1233e41d09dSXin LI static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
1243e41d09dSXin LI 
1253e41d09dSXin LI static int
zlibcmp(const unsigned char * buf)1263e41d09dSXin LI zlibcmp(const unsigned char *buf)
1273e41d09dSXin LI {
1283e41d09dSXin LI 	unsigned short x = 1;
12940427ccaSGordon Tetlow 	unsigned char *s = CAST(unsigned char *, CAST(void *, &x));
1303e41d09dSXin LI 
1313e41d09dSXin LI 	if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
1323e41d09dSXin LI 		return 0;
1333e41d09dSXin LI 	if (s[0] != 1)	/* endianness test */
1343e41d09dSXin LI 		x = buf[0] | (buf[1] << 8);
1353e41d09dSXin LI 	else
1363e41d09dSXin LI 		x = buf[1] | (buf[0] << 8);
1373e41d09dSXin LI 	if (x % 31)
1383e41d09dSXin LI 		return 0;
1393e41d09dSXin LI 	return 1;
1403e41d09dSXin LI }
1413e41d09dSXin LI #endif
1423e41d09dSXin LI 
143d38c30c0SXin LI static int
lzmacmp(const unsigned char * buf)144d38c30c0SXin LI lzmacmp(const unsigned char *buf)
145d38c30c0SXin LI {
146d38c30c0SXin LI 	if (buf[0] != 0x5d || buf[1] || buf[2])
147d38c30c0SXin LI 		return 0;
148d38c30c0SXin LI 	if (buf[12] && buf[12] != 0xff)
149d38c30c0SXin LI 		return 0;
150d38c30c0SXin LI 	return 1;
151d38c30c0SXin LI }
152d38c30c0SXin LI 
1533e41d09dSXin LI #define gzip_flags "-cd"
1543e41d09dSXin LI #define lzip_flags gzip_flags
1553e41d09dSXin LI 
1563e41d09dSXin LI static const char *gzip_args[] = {
1573e41d09dSXin LI 	"gzip", gzip_flags, NULL
1583e41d09dSXin LI };
1593e41d09dSXin LI static const char *uncompress_args[] = {
1603e41d09dSXin LI 	"uncompress", "-c", NULL
1613e41d09dSXin LI };
1623e41d09dSXin LI static const char *bzip2_args[] = {
1633e41d09dSXin LI 	"bzip2", "-cd", NULL
1643e41d09dSXin LI };
1653e41d09dSXin LI static const char *lzip_args[] = {
1663e41d09dSXin LI 	"lzip", lzip_flags, NULL
1673e41d09dSXin LI };
1683e41d09dSXin LI static const char *xz_args[] = {
1693e41d09dSXin LI 	"xz", "-cd", NULL
1703e41d09dSXin LI };
1713e41d09dSXin LI static const char *lrzip_args[] = {
172898496eeSXin LI 	"lrzip", "-qdf", "-", NULL
1733e41d09dSXin LI };
1743e41d09dSXin LI static const char *lz4_args[] = {
1753e41d09dSXin LI 	"lz4", "-cd", NULL
176b6cee71dSXin LI };
177a5d223e6SXin LI static const char *zstd_args[] = {
178a5d223e6SXin LI 	"zstd", "-cd", NULL
179a5d223e6SXin LI };
180b6cee71dSXin LI 
18148c779cdSXin LI #define	do_zlib		NULL
18248c779cdSXin LI #define	do_bzlib	NULL
18348c779cdSXin LI 
184898496eeSXin LI file_private const struct {
1852726a701SXin LI 	union {
1862726a701SXin LI 		const char *magic;
1872726a701SXin LI 		int (*func)(const unsigned char *);
1882726a701SXin LI 	} u;
189d38c30c0SXin LI 	int maglen;
1903e41d09dSXin LI 	const char **argv;
19148c779cdSXin LI 	void *unused;
1923e41d09dSXin LI } compr[] = {
193d38c30c0SXin LI #define METH_FROZEN	2
194d38c30c0SXin LI #define METH_BZIP	7
195d38c30c0SXin LI #define METH_XZ		9
196898496eeSXin LI #define METH_LZIP	8
197*ae316d1dSXin LI #define METH_LRZIP	10
198898496eeSXin LI #define METH_ZSTD	12
199d38c30c0SXin LI #define METH_LZMA	13
200d38c30c0SXin LI #define METH_ZLIB	14
2012726a701SXin LI     { { .magic = "\037\235" },	2, gzip_args, NULL },	/* 0, compressed */
2023e41d09dSXin LI     /* Uncompress can get stuck; so use gzip first if we have it
2033e41d09dSXin LI      * Idea from Damien Clark, thanks! */
2042726a701SXin LI     { { .magic = "\037\235" },	2, uncompress_args, NULL },/* 1, compressed */
2052726a701SXin LI     { { .magic = "\037\213" },	2, gzip_args, do_zlib },/* 2, gzipped */
2062726a701SXin LI     { { .magic = "\037\236" },	2, gzip_args, NULL },	/* 3, frozen */
2072726a701SXin LI     { { .magic = "\037\240" },	2, gzip_args, NULL },	/* 4, SCO LZH */
2083e41d09dSXin LI     /* the standard pack utilities do not accept standard input */
2092726a701SXin LI     { { .magic = "\037\036" },	2, gzip_args, NULL },	/* 5, packed */
2102726a701SXin LI     { { .magic = "PK\3\4" },	4, gzip_args, NULL },	/* 6, pkziped */
2113e41d09dSXin LI     /* ...only first file examined */
2122726a701SXin LI     { { .magic = "BZh" },	3, bzip2_args, do_bzlib },/* 7, bzip2-ed */
2132726a701SXin LI     { { .magic = "LZIP" },	4, lzip_args, NULL },	/* 8, lzip-ed */
2142726a701SXin LI     { { .magic = "\3757zXZ\0" },6, xz_args, NULL },	/* 9, XZ Util */
2152726a701SXin LI     { { .magic = "LRZI" },	4, lrzip_args, NULL },	/* 10, LRZIP */
2162726a701SXin LI     { { .magic = "\004\"M\030" },4, lz4_args, NULL },	/* 11, LZ4 */
2172726a701SXin LI     { { .magic = "\x28\xB5\x2F\xFD" }, 4, zstd_args, NULL },/* 12, zstd */
2182726a701SXin LI     { { .func = lzmacmp },	-13, xz_args, NULL },	/* 13, lzma */
2193e41d09dSXin LI #ifdef ZLIBSUPPORT
2202726a701SXin LI     { { .func = zlibcmp },	-2, zlib_args, NULL },	/* 14, zlib */
2213e41d09dSXin LI #endif
2223e41d09dSXin LI };
2233e41d09dSXin LI 
2243e41d09dSXin LI #define OKDATA 	0
2253e41d09dSXin LI #define NODATA	1
2263e41d09dSXin LI #define ERRDATA	2
227b6cee71dSXin LI 
228898496eeSXin LI file_private ssize_t swrite(int, const void *, size_t);
229b6cee71dSXin LI #if HAVE_FORK
230898496eeSXin LI file_private size_t ncompr = __arraycount(compr);
231898496eeSXin LI file_private int uncompressbuf(int, size_t, size_t, int, const unsigned char *,
2323e41d09dSXin LI     unsigned char **, size_t *);
233b6cee71dSXin LI #ifdef BUILTIN_DECOMPRESS
234898496eeSXin LI file_private int uncompresszlib(const unsigned char *, unsigned char **, size_t,
2353e41d09dSXin LI     size_t *, int);
236898496eeSXin LI file_private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
237898496eeSXin LI     size_t *, int);
238b6cee71dSXin LI #endif
23948c779cdSXin LI #ifdef BUILTIN_BZLIB
240898496eeSXin LI file_private int uncompressbzlib(const unsigned char *, unsigned char **, size_t,
241898496eeSXin LI     size_t *, int);
242d38c30c0SXin LI #endif
243d38c30c0SXin LI #ifdef BUILTIN_XZLIB
244898496eeSXin LI file_private int uncompressxzlib(const unsigned char *, unsigned char **, size_t,
245898496eeSXin LI     size_t *, int);
246898496eeSXin LI #endif
247898496eeSXin LI #ifdef BUILTIN_ZSTDLIB
248898496eeSXin LI file_private int uncompresszstd(const unsigned char *, unsigned char **, size_t,
249898496eeSXin LI     size_t *, int);
250898496eeSXin LI #endif
251898496eeSXin LI #ifdef BUILTIN_LZLIB
252898496eeSXin LI file_private int uncompresslzlib(const unsigned char *, unsigned char **, size_t,
253898496eeSXin LI     size_t *, int);
25448c779cdSXin LI #endif
255*ae316d1dSXin LI #ifdef BUILTIN_LRZIP
256*ae316d1dSXin LI file_private int uncompresslrzip(const unsigned char *, unsigned char **, size_t,
257*ae316d1dSXin LI     size_t *, int);
258*ae316d1dSXin LI #endif
259*ae316d1dSXin LI 
26048c779cdSXin LI 
2613e41d09dSXin LI static int makeerror(unsigned char **, size_t *, const char *, ...)
2623e41d09dSXin LI     __attribute__((__format__(__printf__, 3, 4)));
263898496eeSXin LI file_private const char *methodname(size_t);
264b6cee71dSXin LI 
265898496eeSXin LI file_private int
format_decompression_error(struct magic_set * ms,size_t i,unsigned char * buf)2662dc4dbb9SEitan Adler format_decompression_error(struct magic_set *ms, size_t i, unsigned char *buf)
2672dc4dbb9SEitan Adler {
2682dc4dbb9SEitan Adler 	unsigned char *p;
2692dc4dbb9SEitan Adler 	int mime = ms->flags & MAGIC_MIME;
2702dc4dbb9SEitan Adler 
2712dc4dbb9SEitan Adler 	if (!mime)
2722dc4dbb9SEitan Adler 		return file_printf(ms, "ERROR:[%s: %s]", methodname(i), buf);
2732dc4dbb9SEitan Adler 
2742dc4dbb9SEitan Adler 	for (p = buf; *p; p++)
2752dc4dbb9SEitan Adler 		if (!isalnum(*p))
2762dc4dbb9SEitan Adler 			*p = '-';
2772dc4dbb9SEitan Adler 
2782dc4dbb9SEitan Adler 	return file_printf(ms, "application/x-decompression-error-%s-%s",
2792dc4dbb9SEitan Adler 	    methodname(i), buf);
2802dc4dbb9SEitan Adler }
2812dc4dbb9SEitan Adler 
282898496eeSXin LI file_protected int
file_zmagic(struct magic_set * ms,const struct buffer * b,const char * name)28358a0f0d0SEitan Adler file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name)
284b6cee71dSXin LI {
285b6cee71dSXin LI 	unsigned char *newbuf = NULL;
286b6cee71dSXin LI 	size_t i, nsz;
2873e41d09dSXin LI 	char *rbuf;
2883e41d09dSXin LI 	file_pushbuf_t *pb;
28920f8619dSXin LI 	int urv, prv, rv = 0;
290b6cee71dSXin LI 	int mime = ms->flags & MAGIC_MIME;
29158a0f0d0SEitan Adler 	int fd = b->fd;
29248c779cdSXin LI 	const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
29358a0f0d0SEitan Adler 	size_t nbytes = b->flen;
29448c779cdSXin LI 	int sa_saved = 0;
29548c779cdSXin LI 	struct sigaction sig_act;
296b6cee71dSXin LI 
297b6cee71dSXin LI 	if ((ms->flags & MAGIC_COMPRESS) == 0)
298b6cee71dSXin LI 		return 0;
299b6cee71dSXin LI 
300b6cee71dSXin LI 	for (i = 0; i < ncompr; i++) {
3013e41d09dSXin LI 		int zm;
302d38c30c0SXin LI 		if (nbytes < CAST(size_t, abs(compr[i].maglen)))
303b6cee71dSXin LI 			continue;
304d38c30c0SXin LI 		if (compr[i].maglen < 0) {
3052726a701SXin LI 			zm = (*compr[i].u.func)(buf);
306d38c30c0SXin LI 		} else {
3072726a701SXin LI 			zm = memcmp(buf, compr[i].u.magic,
308d38c30c0SXin LI 			    CAST(size_t, compr[i].maglen)) == 0;
309d38c30c0SXin LI 		}
310b6cee71dSXin LI 
3113e41d09dSXin LI 		if (!zm)
3123e41d09dSXin LI 			continue;
31348c779cdSXin LI 
31448c779cdSXin LI 		/* Prevent SIGPIPE death if child dies unexpectedly */
31548c779cdSXin LI 		if (!sa_saved) {
31648c779cdSXin LI 			//We can use sig_act for both new and old, but
31748c779cdSXin LI 			struct sigaction new_act;
31848c779cdSXin LI 			memset(&new_act, 0, sizeof(new_act));
31948c779cdSXin LI 			new_act.sa_handler = SIG_IGN;
32048c779cdSXin LI 			sa_saved = sigaction(SIGPIPE, &new_act, &sig_act) != -1;
32148c779cdSXin LI 		}
32248c779cdSXin LI 
3233e41d09dSXin LI 		nsz = nbytes;
324898496eeSXin LI 		free(newbuf);
325898496eeSXin LI 		urv = uncompressbuf(fd, ms->bytes_max, i,
326898496eeSXin LI 		    (ms->flags & MAGIC_NO_COMPRESS_FORK), buf, &newbuf, &nsz);
32748c779cdSXin LI 		DPRINTF("uncompressbuf = %d, %s, %" SIZE_T_FORMAT "u\n", urv,
32848c779cdSXin LI 		    (char *)newbuf, nsz);
32920f8619dSXin LI 		switch (urv) {
3303e41d09dSXin LI 		case OKDATA:
3313e41d09dSXin LI 		case ERRDATA:
3323e41d09dSXin LI 			ms->flags &= ~MAGIC_COMPRESS;
33320f8619dSXin LI 			if (urv == ERRDATA)
3342dc4dbb9SEitan Adler 				prv = format_decompression_error(ms, i, newbuf);
3353e41d09dSXin LI 			else
336898496eeSXin LI 				prv = file_buffer(ms, -1, NULL, name, newbuf,
337898496eeSXin LI 				    nsz);
33820f8619dSXin LI 			if (prv == -1)
3393e41d09dSXin LI 				goto error;
34020f8619dSXin LI 			rv = 1;
3413e41d09dSXin LI 			if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
3423e41d09dSXin LI 				goto out;
3433e41d09dSXin LI 			if (mime != MAGIC_MIME && mime != 0)
3443e41d09dSXin LI 				goto out;
3453e41d09dSXin LI 			if ((file_printf(ms,
3463e41d09dSXin LI 			    mime ? " compressed-encoding=" : " (")) == -1)
3473e41d09dSXin LI 				goto error;
3483e41d09dSXin LI 			if ((pb = file_push_buffer(ms)) == NULL)
349b6cee71dSXin LI 				goto error;
35020f8619dSXin LI 			/*
35120f8619dSXin LI 			 * XXX: If file_buffer fails here, we overwrite
35220f8619dSXin LI 			 * the compressed text. FIXME.
35320f8619dSXin LI 			 */
354898496eeSXin LI 			if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1)
355898496eeSXin LI 			{
35648c779cdSXin LI 				if (file_pop_buffer(ms, pb) != NULL)
35748c779cdSXin LI 					abort();
358b6cee71dSXin LI 				goto error;
35948c779cdSXin LI 			}
3603e41d09dSXin LI 			if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
3613e41d09dSXin LI 				if (file_printf(ms, "%s", rbuf) == -1) {
3623e41d09dSXin LI 					free(rbuf);
363b6cee71dSXin LI 					goto error;
364b6cee71dSXin LI 				}
3653e41d09dSXin LI 				free(rbuf);
3663e41d09dSXin LI 			}
3673e41d09dSXin LI 			if (!mime && file_printf(ms, ")") == -1)
3683e41d09dSXin LI 				goto error;
36920f8619dSXin LI 			/*FALLTHROUGH*/
3703e41d09dSXin LI 		case NODATA:
37120f8619dSXin LI 			break;
3723e41d09dSXin LI 		default:
3733e41d09dSXin LI 			abort();
37420f8619dSXin LI 			/*NOTREACHED*/
37520f8619dSXin LI 		error:
37620f8619dSXin LI 			rv = -1;
37720f8619dSXin LI 			break;
3783e41d09dSXin LI 		}
3793e41d09dSXin LI 	}
3803e41d09dSXin LI out:
38120f8619dSXin LI 	DPRINTF("rv = %d\n", rv);
38220f8619dSXin LI 
38348c779cdSXin LI 	if (sa_saved && sig_act.sa_handler != SIG_IGN)
38448c779cdSXin LI 		(void)sigaction(SIGPIPE, &sig_act, NULL);
38548c779cdSXin LI 
386b6cee71dSXin LI 	free(newbuf);
387b6cee71dSXin LI 	ms->flags |= MAGIC_COMPRESS;
3883e41d09dSXin LI 	DPRINTF("Zmagic returns %d\n", rv);
389b6cee71dSXin LI 	return rv;
390b6cee71dSXin LI }
391b6cee71dSXin LI #endif
392b6cee71dSXin LI /*
393b6cee71dSXin LI  * `safe' write for sockets and pipes.
394b6cee71dSXin LI  */
395898496eeSXin LI file_private ssize_t
swrite(int fd,const void * buf,size_t n)396b6cee71dSXin LI swrite(int fd, const void *buf, size_t n)
397b6cee71dSXin LI {
398b6cee71dSXin LI 	ssize_t rv;
399b6cee71dSXin LI 	size_t rn = n;
400b6cee71dSXin LI 
401b6cee71dSXin LI 	do
402b6cee71dSXin LI 		switch (rv = write(fd, buf, n)) {
403b6cee71dSXin LI 		case -1:
404b6cee71dSXin LI 			if (errno == EINTR)
405b6cee71dSXin LI 				continue;
406b6cee71dSXin LI 			return -1;
407b6cee71dSXin LI 		default:
408b6cee71dSXin LI 			n -= rv;
409b6cee71dSXin LI 			buf = CAST(const char *, buf) + rv;
410b6cee71dSXin LI 			break;
411b6cee71dSXin LI 		}
412b6cee71dSXin LI 	while (n > 0);
413b6cee71dSXin LI 	return rn;
414b6cee71dSXin LI }
415b6cee71dSXin LI 
416b6cee71dSXin LI 
417b6cee71dSXin LI /*
418b6cee71dSXin LI  * `safe' read for sockets and pipes.
419b6cee71dSXin LI  */
420898496eeSXin LI file_protected ssize_t
sread(int fd,void * buf,size_t n,int canbepipe)421b6cee71dSXin LI sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
422b6cee71dSXin LI {
423b6cee71dSXin LI 	ssize_t rv;
424898496eeSXin LI #if defined(FIONREAD) && !defined(__MINGW32__)
425b6cee71dSXin LI 	int t = 0;
426b6cee71dSXin LI #endif
427b6cee71dSXin LI 	size_t rn = n;
428b6cee71dSXin LI 
429b6cee71dSXin LI 	if (fd == STDIN_FILENO)
430b6cee71dSXin LI 		goto nocheck;
431b6cee71dSXin LI 
432898496eeSXin LI #if defined(FIONREAD) && !defined(__MINGW32__)
433b6cee71dSXin LI 	if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) {
434b6cee71dSXin LI #ifdef FD_ZERO
435b6cee71dSXin LI 		ssize_t cnt;
436b6cee71dSXin LI 		for (cnt = 0;; cnt++) {
437b6cee71dSXin LI 			fd_set check;
438b6cee71dSXin LI 			struct timeval tout = {0, 100 * 1000};
439b6cee71dSXin LI 			int selrv;
440b6cee71dSXin LI 
441b6cee71dSXin LI 			FD_ZERO(&check);
442b6cee71dSXin LI 			FD_SET(fd, &check);
443b6cee71dSXin LI 
444b6cee71dSXin LI 			/*
445b6cee71dSXin LI 			 * Avoid soft deadlock: do not read if there
446b6cee71dSXin LI 			 * is nothing to read from sockets and pipes.
447b6cee71dSXin LI 			 */
448b6cee71dSXin LI 			selrv = select(fd + 1, &check, NULL, NULL, &tout);
449b6cee71dSXin LI 			if (selrv == -1) {
450b6cee71dSXin LI 				if (errno == EINTR || errno == EAGAIN)
451b6cee71dSXin LI 					continue;
452b6cee71dSXin LI 			} else if (selrv == 0 && cnt >= 5) {
453b6cee71dSXin LI 				return 0;
454b6cee71dSXin LI 			} else
455b6cee71dSXin LI 				break;
456b6cee71dSXin LI 		}
457b6cee71dSXin LI #endif
458b6cee71dSXin LI 		(void)ioctl(fd, FIONREAD, &t);
459b6cee71dSXin LI 	}
460b6cee71dSXin LI 
46148c779cdSXin LI 	if (t > 0 && CAST(size_t, t) < n) {
462b6cee71dSXin LI 		n = t;
463b6cee71dSXin LI 		rn = n;
464b6cee71dSXin LI 	}
465b6cee71dSXin LI #endif
466b6cee71dSXin LI 
467b6cee71dSXin LI nocheck:
468b6cee71dSXin LI 	do
469b6cee71dSXin LI 		switch ((rv = read(fd, buf, n))) {
470b6cee71dSXin LI 		case -1:
471b6cee71dSXin LI 			if (errno == EINTR)
472b6cee71dSXin LI 				continue;
473b6cee71dSXin LI 			return -1;
474b6cee71dSXin LI 		case 0:
475b6cee71dSXin LI 			return rn - n;
476b6cee71dSXin LI 		default:
477b6cee71dSXin LI 			n -= rv;
478a5d223e6SXin LI 			buf = CAST(char *, CCAST(void *, buf)) + rv;
479b6cee71dSXin LI 			break;
480b6cee71dSXin LI 		}
481b6cee71dSXin LI 	while (n > 0);
482b6cee71dSXin LI 	return rn;
483b6cee71dSXin LI }
484b6cee71dSXin LI 
485898496eeSXin LI file_protected int
file_pipe2file(struct magic_set * ms,int fd,const void * startbuf,size_t nbytes)486b6cee71dSXin LI file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
487b6cee71dSXin LI     size_t nbytes)
488b6cee71dSXin LI {
489b6cee71dSXin LI 	char buf[4096];
490b6cee71dSXin LI 	ssize_t r;
491b6cee71dSXin LI 	int tfd;
492b6cee71dSXin LI 
493a4d6d3b8SXin LI #ifdef WIN32
494a4d6d3b8SXin LI 	const char *t;
495a4d6d3b8SXin LI 	buf[0] = '\0';
496a4d6d3b8SXin LI 	if ((t = getenv("TEMP")) != NULL)
497a4d6d3b8SXin LI 		(void)strlcpy(buf, t, sizeof(buf));
498a4d6d3b8SXin LI 	else if ((t = getenv("TMP")) != NULL)
499a4d6d3b8SXin LI 		(void)strlcpy(buf, t, sizeof(buf));
500a4d6d3b8SXin LI 	else if ((t = getenv("TMPDIR")) != NULL)
501a4d6d3b8SXin LI 		(void)strlcpy(buf, t, sizeof(buf));
502a4d6d3b8SXin LI 	if (buf[0] != '\0')
503a4d6d3b8SXin LI 		(void)strlcat(buf, "/", sizeof(buf));
504a4d6d3b8SXin LI 	(void)strlcat(buf, "file.XXXXXX", sizeof(buf));
505a4d6d3b8SXin LI #else
506a4d6d3b8SXin LI 	(void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof(buf));
507a4d6d3b8SXin LI #endif
508b6cee71dSXin LI #ifndef HAVE_MKSTEMP
509b6cee71dSXin LI 	{
510b6cee71dSXin LI 		char *ptr = mktemp(buf);
511b6cee71dSXin LI 		tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
512b6cee71dSXin LI 		r = errno;
513b6cee71dSXin LI 		(void)unlink(ptr);
514b6cee71dSXin LI 		errno = r;
515b6cee71dSXin LI 	}
516b6cee71dSXin LI #else
517b6cee71dSXin LI 	{
518b6cee71dSXin LI 		int te;
51948c779cdSXin LI 		mode_t ou = umask(0);
520b6cee71dSXin LI 		tfd = mkstemp(buf);
52148c779cdSXin LI 		(void)umask(ou);
522b6cee71dSXin LI 		te = errno;
523b6cee71dSXin LI 		(void)unlink(buf);
524b6cee71dSXin LI 		errno = te;
525b6cee71dSXin LI 	}
526b6cee71dSXin LI #endif
527b6cee71dSXin LI 	if (tfd == -1) {
528b6cee71dSXin LI 		file_error(ms, errno,
529b6cee71dSXin LI 		    "cannot create temporary file for pipe copy");
530b6cee71dSXin LI 		return -1;
531b6cee71dSXin LI 	}
532b6cee71dSXin LI 
53348c779cdSXin LI 	if (swrite(tfd, startbuf, nbytes) != CAST(ssize_t, nbytes))
534b6cee71dSXin LI 		r = 1;
535b6cee71dSXin LI 	else {
536b6cee71dSXin LI 		while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
53748c779cdSXin LI 			if (swrite(tfd, buf, CAST(size_t, r)) != r)
538b6cee71dSXin LI 				break;
539b6cee71dSXin LI 	}
540b6cee71dSXin LI 
541b6cee71dSXin LI 	switch (r) {
542b6cee71dSXin LI 	case -1:
543b6cee71dSXin LI 		file_error(ms, errno, "error copying from pipe to temp file");
544b6cee71dSXin LI 		return -1;
545b6cee71dSXin LI 	case 0:
546b6cee71dSXin LI 		break;
547b6cee71dSXin LI 	default:
548b6cee71dSXin LI 		file_error(ms, errno, "error while writing to temp file");
549b6cee71dSXin LI 		return -1;
550b6cee71dSXin LI 	}
551b6cee71dSXin LI 
552b6cee71dSXin LI 	/*
553b6cee71dSXin LI 	 * We duplicate the file descriptor, because fclose on a
554b6cee71dSXin LI 	 * tmpfile will delete the file, but any open descriptors
555b6cee71dSXin LI 	 * can still access the phantom inode.
556b6cee71dSXin LI 	 */
557b6cee71dSXin LI 	if ((fd = dup2(tfd, fd)) == -1) {
558b6cee71dSXin LI 		file_error(ms, errno, "could not dup descriptor for temp file");
559b6cee71dSXin LI 		return -1;
560b6cee71dSXin LI 	}
561b6cee71dSXin LI 	(void)close(tfd);
56248c779cdSXin LI 	if (lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1)) {
563b6cee71dSXin LI 		file_badseek(ms);
564b6cee71dSXin LI 		return -1;
565b6cee71dSXin LI 	}
566b6cee71dSXin LI 	return fd;
567b6cee71dSXin LI }
568b6cee71dSXin LI #if HAVE_FORK
569b6cee71dSXin LI #ifdef BUILTIN_DECOMPRESS
570b6cee71dSXin LI 
571b6cee71dSXin LI #define FHCRC		(1 << 1)
572b6cee71dSXin LI #define FEXTRA		(1 << 2)
573b6cee71dSXin LI #define FNAME		(1 << 3)
574b6cee71dSXin LI #define FCOMMENT	(1 << 4)
575b6cee71dSXin LI 
5763e41d09dSXin LI 
577898496eeSXin LI file_private int
uncompressgzipped(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)5783e41d09dSXin LI uncompressgzipped(const unsigned char *old, unsigned char **newch,
579898496eeSXin LI     size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
580b6cee71dSXin LI {
581898496eeSXin LI 	unsigned char flg;
582b6cee71dSXin LI 	size_t data_start = 10;
583b6cee71dSXin LI 
584898496eeSXin LI 	if (*n < 4) {
585898496eeSXin LI 		goto err;
586898496eeSXin LI 	}
587898496eeSXin LI 
588898496eeSXin LI 	flg = old[3];
589898496eeSXin LI 
590b6cee71dSXin LI 	if (flg & FEXTRA) {
5913e41d09dSXin LI 		if (data_start + 1 >= *n)
5923e41d09dSXin LI 			goto err;
593b6cee71dSXin LI 		data_start += 2 + old[data_start] + old[data_start + 1] * 256;
594b6cee71dSXin LI 	}
595b6cee71dSXin LI 	if (flg & FNAME) {
5963e41d09dSXin LI 		while(data_start < *n && old[data_start])
597b6cee71dSXin LI 			data_start++;
598b6cee71dSXin LI 		data_start++;
599b6cee71dSXin LI 	}
600b6cee71dSXin LI 	if (flg & FCOMMENT) {
6013e41d09dSXin LI 		while(data_start < *n && old[data_start])
602b6cee71dSXin LI 			data_start++;
603b6cee71dSXin LI 		data_start++;
604b6cee71dSXin LI 	}
605b6cee71dSXin LI 	if (flg & FHCRC)
606b6cee71dSXin LI 		data_start += 2;
607b6cee71dSXin LI 
6083e41d09dSXin LI 	if (data_start >= *n)
6093e41d09dSXin LI 		goto err;
6103e41d09dSXin LI 
6113e41d09dSXin LI 	*n -= data_start;
6123e41d09dSXin LI 	old += data_start;
6133e41d09dSXin LI 	return uncompresszlib(old, newch, bytes_max, n, 0);
6143e41d09dSXin LI err:
6153e41d09dSXin LI 	return makeerror(newch, n, "File too short");
616b6cee71dSXin LI }
617b6cee71dSXin LI 
618898496eeSXin LI file_private int
uncompresszlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int zlib)6193e41d09dSXin LI uncompresszlib(const unsigned char *old, unsigned char **newch,
6203e41d09dSXin LI     size_t bytes_max, size_t *n, int zlib)
6213e41d09dSXin LI {
6223e41d09dSXin LI 	int rc;
6233e41d09dSXin LI 	z_stream z;
6243e41d09dSXin LI 
625898496eeSXin LI 	DPRINTF("builtin zlib decompression\n");
6263e41d09dSXin LI 	z.next_in = CCAST(Bytef *, old);
6273e41d09dSXin LI 	z.avail_in = CAST(uint32_t, *n);
628b6cee71dSXin LI 	z.next_out = *newch;
62940427ccaSGordon Tetlow 	z.avail_out = CAST(unsigned int, bytes_max);
630b6cee71dSXin LI 	z.zalloc = Z_NULL;
631b6cee71dSXin LI 	z.zfree = Z_NULL;
632b6cee71dSXin LI 	z.opaque = Z_NULL;
633b6cee71dSXin LI 
634b6cee71dSXin LI 	/* LINTED bug in header macro */
6353e41d09dSXin LI 	rc = zlib ? inflateInit(&z) : inflateInit2(&z, -15);
6363e41d09dSXin LI 	if (rc != Z_OK)
6373e41d09dSXin LI 		goto err;
638b6cee71dSXin LI 
639b6cee71dSXin LI 	rc = inflate(&z, Z_SYNC_FLUSH);
640898496eeSXin LI 	if (rc != Z_OK && rc != Z_STREAM_END) {
641898496eeSXin LI 		inflateEnd(&z);
6423e41d09dSXin LI 		goto err;
643898496eeSXin LI 	}
644b6cee71dSXin LI 
64548c779cdSXin LI 	*n = CAST(size_t, z.total_out);
6463e41d09dSXin LI 	rc = inflateEnd(&z);
6473e41d09dSXin LI 	if (rc != Z_OK)
6483e41d09dSXin LI 		goto err;
649b6cee71dSXin LI 
650b6cee71dSXin LI 	/* let's keep the nul-terminate tradition */
6513e41d09dSXin LI 	(*newch)[*n] = '\0';
652b6cee71dSXin LI 
6533e41d09dSXin LI 	return OKDATA;
6543e41d09dSXin LI err:
655898496eeSXin LI 	return makeerror(newch, n, "%s", z.msg ? z.msg : zError(rc));
656b6cee71dSXin LI }
657b6cee71dSXin LI #endif
658b6cee71dSXin LI 
659d38c30c0SXin LI #ifdef BUILTIN_BZLIB
660898496eeSXin LI file_private int
uncompressbzlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)661d38c30c0SXin LI uncompressbzlib(const unsigned char *old, unsigned char **newch,
662898496eeSXin LI     size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
663d38c30c0SXin LI {
664d38c30c0SXin LI 	int rc;
665d38c30c0SXin LI 	bz_stream bz;
666d38c30c0SXin LI 
667898496eeSXin LI 	DPRINTF("builtin bzlib decompression\n");
668d38c30c0SXin LI 	memset(&bz, 0, sizeof(bz));
669d38c30c0SXin LI 	rc = BZ2_bzDecompressInit(&bz, 0, 0);
670d38c30c0SXin LI 	if (rc != BZ_OK)
671d38c30c0SXin LI 		goto err;
672d38c30c0SXin LI 
673d38c30c0SXin LI 	bz.next_in = CCAST(char *, RCAST(const char *, old));
674d38c30c0SXin LI 	bz.avail_in = CAST(uint32_t, *n);
675d38c30c0SXin LI 	bz.next_out = RCAST(char *, *newch);
676d38c30c0SXin LI 	bz.avail_out = CAST(unsigned int, bytes_max);
677d38c30c0SXin LI 
678d38c30c0SXin LI 	rc = BZ2_bzDecompress(&bz);
679898496eeSXin LI 	if (rc != BZ_OK && rc != BZ_STREAM_END) {
680898496eeSXin LI 		BZ2_bzDecompressEnd(&bz);
681d38c30c0SXin LI 		goto err;
682898496eeSXin LI 	}
683d38c30c0SXin LI 
684d38c30c0SXin LI 	/* Assume byte_max is within 32bit */
685d38c30c0SXin LI 	/* assert(bz.total_out_hi32 == 0); */
686d38c30c0SXin LI 	*n = CAST(size_t, bz.total_out_lo32);
687d38c30c0SXin LI 	rc = BZ2_bzDecompressEnd(&bz);
688d38c30c0SXin LI 	if (rc != BZ_OK)
689d38c30c0SXin LI 		goto err;
690d38c30c0SXin LI 
691d38c30c0SXin LI 	/* let's keep the nul-terminate tradition */
692d38c30c0SXin LI 	(*newch)[*n] = '\0';
693d38c30c0SXin LI 
694d38c30c0SXin LI 	return OKDATA;
695d38c30c0SXin LI err:
696898496eeSXin LI 	return makeerror(newch, n, "bunzip error %d", rc);
697d38c30c0SXin LI }
698d38c30c0SXin LI #endif
699d38c30c0SXin LI 
700d38c30c0SXin LI #ifdef BUILTIN_XZLIB
701898496eeSXin LI file_private int
uncompressxzlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)702d38c30c0SXin LI uncompressxzlib(const unsigned char *old, unsigned char **newch,
703898496eeSXin LI     size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
704d38c30c0SXin LI {
705d38c30c0SXin LI 	int rc;
706d38c30c0SXin LI 	lzma_stream xz;
707d38c30c0SXin LI 
708898496eeSXin LI 	DPRINTF("builtin xzlib decompression\n");
709d38c30c0SXin LI 	memset(&xz, 0, sizeof(xz));
710d38c30c0SXin LI 	rc = lzma_auto_decoder(&xz, UINT64_MAX, 0);
711d38c30c0SXin LI 	if (rc != LZMA_OK)
712d38c30c0SXin LI 		goto err;
713d38c30c0SXin LI 
714d38c30c0SXin LI 	xz.next_in = CCAST(const uint8_t *, old);
715d38c30c0SXin LI 	xz.avail_in = CAST(uint32_t, *n);
716d38c30c0SXin LI 	xz.next_out = RCAST(uint8_t *, *newch);
717d38c30c0SXin LI 	xz.avail_out = CAST(unsigned int, bytes_max);
718d38c30c0SXin LI 
719d38c30c0SXin LI 	rc = lzma_code(&xz, LZMA_RUN);
720898496eeSXin LI 	if (rc != LZMA_OK && rc != LZMA_STREAM_END) {
721898496eeSXin LI 		lzma_end(&xz);
722d38c30c0SXin LI 		goto err;
723898496eeSXin LI 	}
724d38c30c0SXin LI 
725d38c30c0SXin LI 	*n = CAST(size_t, xz.total_out);
726d38c30c0SXin LI 
727d38c30c0SXin LI 	lzma_end(&xz);
728d38c30c0SXin LI 
729d38c30c0SXin LI 	/* let's keep the nul-terminate tradition */
730d38c30c0SXin LI 	(*newch)[*n] = '\0';
731d38c30c0SXin LI 
732d38c30c0SXin LI 	return OKDATA;
733d38c30c0SXin LI err:
734898496eeSXin LI 	return makeerror(newch, n, "unxz error %d", rc);
735898496eeSXin LI }
736898496eeSXin LI #endif
737898496eeSXin LI 
738898496eeSXin LI #ifdef BUILTIN_ZSTDLIB
739898496eeSXin LI file_private int
uncompresszstd(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)740898496eeSXin LI uncompresszstd(const unsigned char *old, unsigned char **newch,
741898496eeSXin LI     size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
742898496eeSXin LI {
743898496eeSXin LI 	size_t rc;
744898496eeSXin LI 	ZSTD_DStream *zstd;
745898496eeSXin LI 	ZSTD_inBuffer in;
746898496eeSXin LI 	ZSTD_outBuffer out;
747898496eeSXin LI 
748898496eeSXin LI 	DPRINTF("builtin zstd decompression\n");
749898496eeSXin LI 	if ((zstd = ZSTD_createDStream()) == NULL) {
750898496eeSXin LI 		return makeerror(newch, n, "No ZSTD decompression stream, %s",
751898496eeSXin LI 		    strerror(errno));
752898496eeSXin LI 	}
753898496eeSXin LI 
754898496eeSXin LI 	rc = ZSTD_DCtx_reset(zstd, ZSTD_reset_session_only);
755898496eeSXin LI 	if (ZSTD_isError(rc))
756898496eeSXin LI 		goto err;
757898496eeSXin LI 
758898496eeSXin LI 	in.src = CCAST(const void *, old);
759898496eeSXin LI 	in.size = *n;
760898496eeSXin LI 	in.pos = 0;
761898496eeSXin LI 	out.dst = RCAST(void *, *newch);
762898496eeSXin LI 	out.size = bytes_max;
763898496eeSXin LI 	out.pos = 0;
764898496eeSXin LI 
765898496eeSXin LI 	rc = ZSTD_decompressStream(zstd, &out, &in);
766898496eeSXin LI 	if (ZSTD_isError(rc))
767898496eeSXin LI 		goto err;
768898496eeSXin LI 
769898496eeSXin LI 	*n = out.pos;
770898496eeSXin LI 
771898496eeSXin LI 	ZSTD_freeDStream(zstd);
772898496eeSXin LI 
773898496eeSXin LI 	/* let's keep the nul-terminate tradition */
774898496eeSXin LI 	(*newch)[*n] = '\0';
775898496eeSXin LI 
776898496eeSXin LI 	return OKDATA;
777898496eeSXin LI err:
778898496eeSXin LI 	ZSTD_freeDStream(zstd);
779898496eeSXin LI 	return makeerror(newch, n, "zstd error %d", ZSTD_getErrorCode(rc));
780898496eeSXin LI }
781898496eeSXin LI #endif
782898496eeSXin LI 
783898496eeSXin LI #ifdef BUILTIN_LZLIB
784898496eeSXin LI file_private int
uncompresslzlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)785898496eeSXin LI uncompresslzlib(const unsigned char *old, unsigned char **newch,
786898496eeSXin LI     size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
787898496eeSXin LI {
788898496eeSXin LI 	enum LZ_Errno err;
789898496eeSXin LI 	size_t old_remaining = *n;
790898496eeSXin LI 	size_t new_remaining = bytes_max;
791898496eeSXin LI 	size_t total_read = 0;
792898496eeSXin LI 	unsigned char *bufp;
793898496eeSXin LI 	struct LZ_Decoder *dec;
794898496eeSXin LI 
795898496eeSXin LI 	bufp = *newch;
796898496eeSXin LI 
797898496eeSXin LI 	DPRINTF("builtin lzlib decompression\n");
798898496eeSXin LI 	dec = LZ_decompress_open();
799898496eeSXin LI 	if (!dec) {
800898496eeSXin LI 		return makeerror(newch, n, "unable to allocate LZ_Decoder");
801898496eeSXin LI 	}
802898496eeSXin LI 	if (LZ_decompress_errno(dec) != LZ_ok)
803898496eeSXin LI 		goto err;
804898496eeSXin LI 
805898496eeSXin LI 	for (;;) {
806898496eeSXin LI 		// LZ_decompress_read() stops at member boundaries, so we may
807898496eeSXin LI 		// have more than one successful read after writing all data
808898496eeSXin LI 		// we have.
809898496eeSXin LI 		if (old_remaining > 0) {
810898496eeSXin LI 			int wr = LZ_decompress_write(dec, old, old_remaining);
811898496eeSXin LI 			if (wr < 0)
812898496eeSXin LI 				goto err;
813898496eeSXin LI 			old_remaining -= wr;
814898496eeSXin LI 			old += wr;
815898496eeSXin LI 		}
816898496eeSXin LI 
817898496eeSXin LI 		int rd = LZ_decompress_read(dec, bufp, new_remaining);
818898496eeSXin LI 		if (rd > 0) {
819898496eeSXin LI 			new_remaining -= rd;
820898496eeSXin LI 			bufp += rd;
821898496eeSXin LI 			total_read += rd;
822898496eeSXin LI 		}
823898496eeSXin LI 
824898496eeSXin LI 		if (rd < 0 || LZ_decompress_errno(dec) != LZ_ok)
825898496eeSXin LI 			goto err;
826898496eeSXin LI 		if (new_remaining == 0)
827898496eeSXin LI 			break;
828898496eeSXin LI 		if (old_remaining == 0 && rd == 0)
829898496eeSXin LI 			break;
830898496eeSXin LI 	}
831898496eeSXin LI 
832898496eeSXin LI 	LZ_decompress_close(dec);
833898496eeSXin LI 	*n = total_read;
834898496eeSXin LI 
835898496eeSXin LI 	/* let's keep the nul-terminate tradition */
836898496eeSXin LI 	*bufp = '\0';
837898496eeSXin LI 
838898496eeSXin LI 	return OKDATA;
839898496eeSXin LI err:
840898496eeSXin LI 	err = LZ_decompress_errno(dec);
841898496eeSXin LI 	LZ_decompress_close(dec);
842898496eeSXin LI 	return makeerror(newch, n, "lzlib error: %s", LZ_strerror(err));
843d38c30c0SXin LI }
844d38c30c0SXin LI #endif
845d38c30c0SXin LI 
846*ae316d1dSXin LI #ifdef BUILTIN_LRZIP
847*ae316d1dSXin LI file_private int
uncompresslrzip(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)848*ae316d1dSXin LI uncompresslrzip(const unsigned char *old, unsigned char **newch,
849*ae316d1dSXin LI     size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
850*ae316d1dSXin LI {
851*ae316d1dSXin LI 	Lrzip *lr;
852*ae316d1dSXin LI 	FILE *in, *out;
853*ae316d1dSXin LI 	int res = OKDATA;
854*ae316d1dSXin LI 
855*ae316d1dSXin LI 	DPRINTF("builtin rlzip decompression\n");
856*ae316d1dSXin LI 	lr = lrzip_new(LRZIP_MODE_DECOMPRESS);
857*ae316d1dSXin LI 	if (lr == NULL) {
858*ae316d1dSXin LI 		res = makeerror(newch, n, "unable to create an lrzip decoder");
859*ae316d1dSXin LI 		goto out0;
860*ae316d1dSXin LI 	}
861*ae316d1dSXin LI 	lrzip_config_env(lr);
862*ae316d1dSXin LI 	in = fmemopen(RCAST(void *, old), bytes_max, "r");
863*ae316d1dSXin LI 	if (in == NULL) {
864*ae316d1dSXin LI 		res = makeerror(newch, n, "unable to construct input file");
865*ae316d1dSXin LI 		goto out1;
866*ae316d1dSXin LI 	}
867*ae316d1dSXin LI 	if (!lrzip_file_add(lr, in)) {
868*ae316d1dSXin LI 		res = makeerror(newch, n, "unable to add input file");
869*ae316d1dSXin LI 		goto out2;
870*ae316d1dSXin LI 	}
871*ae316d1dSXin LI 	*newch = calloc(*n = 2 * bytes_max, 1);
872*ae316d1dSXin LI 	if (*newch == NULL) {
873*ae316d1dSXin LI 		res = makeerror(newch, n, "unable to allocate output buffer");
874*ae316d1dSXin LI 		goto out2;
875*ae316d1dSXin LI 	}
876*ae316d1dSXin LI 	out = fmemopen(*newch, *n, "w");
877*ae316d1dSXin LI 	if (out == NULL) {
878*ae316d1dSXin LI 		free(*newch);
879*ae316d1dSXin LI 		res = makeerror(newch, n, "unable to allocate output file");
880*ae316d1dSXin LI 		goto out2;
881*ae316d1dSXin LI 	}
882*ae316d1dSXin LI 	lrzip_outfile_set(lr, out);
883*ae316d1dSXin LI 	if (lrzip_run(lr)) {
884*ae316d1dSXin LI 		free(*newch);
885*ae316d1dSXin LI 		res = makeerror(newch, n, "unable to decompress file");
886*ae316d1dSXin LI 		goto out3;
887*ae316d1dSXin LI 	}
888*ae316d1dSXin LI 	*n = (size_t)ftell(out);
889*ae316d1dSXin LI out3:
890*ae316d1dSXin LI 	fclose(out);
891*ae316d1dSXin LI out2:
892*ae316d1dSXin LI 	fclose(in);
893*ae316d1dSXin LI out1:
894*ae316d1dSXin LI 	lrzip_free(lr);
895*ae316d1dSXin LI out0:
896*ae316d1dSXin LI 	return res;
897*ae316d1dSXin LI }
898*ae316d1dSXin LI #endif
899d38c30c0SXin LI 
9003e41d09dSXin LI static int
makeerror(unsigned char ** buf,size_t * len,const char * fmt,...)9013e41d09dSXin LI makeerror(unsigned char **buf, size_t *len, const char *fmt, ...)
902b6cee71dSXin LI {
9033e41d09dSXin LI 	char *msg;
9043e41d09dSXin LI 	va_list ap;
9053e41d09dSXin LI 	int rv;
906b6cee71dSXin LI 
907898496eeSXin LI 	DPRINTF("Makeerror %s\n", fmt);
908898496eeSXin LI 	free(*buf);
9093e41d09dSXin LI 	va_start(ap, fmt);
9103e41d09dSXin LI 	rv = vasprintf(&msg, fmt, ap);
9113e41d09dSXin LI 	va_end(ap);
9123e41d09dSXin LI 	if (rv < 0) {
913898496eeSXin LI 		DPRINTF("Makeerror failed");
9143e41d09dSXin LI 		*buf = NULL;
9153e41d09dSXin LI 		*len = 0;
916b6cee71dSXin LI 		return NODATA;
917b6cee71dSXin LI 	}
91848c779cdSXin LI 	*buf = RCAST(unsigned char *, msg);
9193e41d09dSXin LI 	*len = strlen(msg);
9203e41d09dSXin LI 	return ERRDATA;
921b6cee71dSXin LI }
922b6cee71dSXin LI 
9233e41d09dSXin LI static void
closefd(int * fd,size_t i)9243e41d09dSXin LI closefd(int *fd, size_t i)
9253e41d09dSXin LI {
9263e41d09dSXin LI 	if (fd[i] == -1)
9273e41d09dSXin LI 		return;
9283e41d09dSXin LI 	(void) close(fd[i]);
9293e41d09dSXin LI 	fd[i] = -1;
9303e41d09dSXin LI }
931b6cee71dSXin LI 
9323e41d09dSXin LI static void
closep(int * fd)9333e41d09dSXin LI closep(int *fd)
9343e41d09dSXin LI {
9353e41d09dSXin LI 	size_t i;
9363e41d09dSXin LI 	for (i = 0; i < 2; i++)
9373e41d09dSXin LI 		closefd(fd, i);
9383e41d09dSXin LI }
9393e41d09dSXin LI 
940a4d6d3b8SXin LI static void
movedesc(void * v,int i,int fd)941a4d6d3b8SXin LI movedesc(void *v, int i, int fd)
9423e41d09dSXin LI {
94348c779cdSXin LI 	if (fd == i)
944a4d6d3b8SXin LI 		return; /* "no dup was necessary" */
945a4d6d3b8SXin LI #ifdef HAVE_POSIX_SPAWNP
946a4d6d3b8SXin LI 	posix_spawn_file_actions_t *fa = RCAST(posix_spawn_file_actions_t *, v);
947a4d6d3b8SXin LI 	posix_spawn_file_actions_adddup2(fa, fd, i);
948a4d6d3b8SXin LI 	posix_spawn_file_actions_addclose(fa, fd);
949a4d6d3b8SXin LI #else
95048c779cdSXin LI 	if (dup2(fd, i) == -1) {
95148c779cdSXin LI 		DPRINTF("dup(%d, %d) failed (%s)\n", fd, i, strerror(errno));
952898496eeSXin LI 		exit(EXIT_FAILURE);
9533e41d09dSXin LI 	}
954a4d6d3b8SXin LI 	close(v ? fd : fd);
955a4d6d3b8SXin LI #endif
956a4d6d3b8SXin LI }
957a4d6d3b8SXin LI 
958a4d6d3b8SXin LI static void
closedesc(void * v,int fd)959a4d6d3b8SXin LI closedesc(void *v, int fd)
960a4d6d3b8SXin LI {
961a4d6d3b8SXin LI #ifdef HAVE_POSIX_SPAWNP
962a4d6d3b8SXin LI 	posix_spawn_file_actions_t *fa = RCAST(posix_spawn_file_actions_t *, v);
963a4d6d3b8SXin LI 	posix_spawn_file_actions_addclose(fa, fd);
964a4d6d3b8SXin LI #else
965a4d6d3b8SXin LI 	close(v ? fd : fd);
966a4d6d3b8SXin LI #endif
967a4d6d3b8SXin LI }
968a4d6d3b8SXin LI 
969a4d6d3b8SXin LI static void
handledesc(void * v,int fd,int fdp[3][2])970a4d6d3b8SXin LI handledesc(void *v, int fd, int fdp[3][2])
971a4d6d3b8SXin LI {
972a4d6d3b8SXin LI 	if (fd != -1) {
973a4d6d3b8SXin LI 		(void) lseek(fd, CAST(off_t, 0), SEEK_SET);
974a4d6d3b8SXin LI 		movedesc(v, STDIN_FILENO, fd);
975a4d6d3b8SXin LI 	} else {
976a4d6d3b8SXin LI 		movedesc(v, STDIN_FILENO, fdp[STDIN_FILENO][0]);
977a4d6d3b8SXin LI 		if (fdp[STDIN_FILENO][1] > 2)
978a4d6d3b8SXin LI 		    closedesc(v, fdp[STDIN_FILENO][1]);
979a4d6d3b8SXin LI 	}
980a4d6d3b8SXin LI 
981a4d6d3b8SXin LI 	file_clear_closexec(STDIN_FILENO);
982a4d6d3b8SXin LI 
983a4d6d3b8SXin LI ///FIXME: if one of the fdp[i][j] is 0 or 1, this can bomb spectacularly
984a4d6d3b8SXin LI 	movedesc(v, STDOUT_FILENO, fdp[STDOUT_FILENO][1]);
985a4d6d3b8SXin LI 	if (fdp[STDOUT_FILENO][0] > 2)
986a4d6d3b8SXin LI 		closedesc(v, fdp[STDOUT_FILENO][0]);
987a4d6d3b8SXin LI 
988a4d6d3b8SXin LI 	file_clear_closexec(STDOUT_FILENO);
989a4d6d3b8SXin LI 
990a4d6d3b8SXin LI 	movedesc(v, STDERR_FILENO, fdp[STDERR_FILENO][1]);
991a4d6d3b8SXin LI 	if (fdp[STDERR_FILENO][0] > 2)
992a4d6d3b8SXin LI 		closedesc(v, fdp[STDERR_FILENO][0]);
993a4d6d3b8SXin LI 
994a4d6d3b8SXin LI 	file_clear_closexec(STDERR_FILENO);
9953e41d09dSXin LI }
996b6cee71dSXin LI 
99748c779cdSXin LI static pid_t
writechild(int fd,const void * old,size_t n)99848c779cdSXin LI writechild(int fd, const void *old, size_t n)
9993e41d09dSXin LI {
100048c779cdSXin LI 	pid_t pid;
10013e41d09dSXin LI 
1002b6cee71dSXin LI 	/*
1003b6cee71dSXin LI 	 * fork again, to avoid blocking because both
1004b6cee71dSXin LI 	 * pipes filled
1005b6cee71dSXin LI 	 */
100648c779cdSXin LI 	pid = fork();
100748c779cdSXin LI 	if (pid == -1) {
100848c779cdSXin LI 		DPRINTF("Fork failed (%s)\n", strerror(errno));
1009898496eeSXin LI 		return -1;
101048c779cdSXin LI 	}
101148c779cdSXin LI 	if (pid == 0) {
101248c779cdSXin LI 		/* child */
101348c779cdSXin LI 		if (swrite(fd, old, n) != CAST(ssize_t, n)) {
10143e41d09dSXin LI 			DPRINTF("Write failed (%s)\n", strerror(errno));
1015898496eeSXin LI 			exit(EXIT_FAILURE);
1016b6cee71dSXin LI 		}
1017898496eeSXin LI 		exit(EXIT_SUCCESS);
1018c2931133SXin LI 	}
101948c779cdSXin LI 	/* parent */
102048c779cdSXin LI 	return pid;
1021b6cee71dSXin LI }
1022b6cee71dSXin LI 
10233e41d09dSXin LI static ssize_t
filter_error(unsigned char * ubuf,ssize_t n)10243e41d09dSXin LI filter_error(unsigned char *ubuf, ssize_t n)
10253e41d09dSXin LI {
10263e41d09dSXin LI 	char *p;
10273e41d09dSXin LI 	char *buf;
1028c2931133SXin LI 
10293e41d09dSXin LI 	ubuf[n] = '\0';
103048c779cdSXin LI 	buf = RCAST(char *, ubuf);
103148c779cdSXin LI 	while (isspace(CAST(unsigned char, *buf)))
10323e41d09dSXin LI 		buf++;
10333e41d09dSXin LI 	DPRINTF("Filter error[[[%s]]]\n", buf);
103448c779cdSXin LI 	if ((p = strchr(CAST(char *, buf), '\n')) != NULL)
10353e41d09dSXin LI 		*p = '\0';
103648c779cdSXin LI 	if ((p = strchr(CAST(char *, buf), ';')) != NULL)
10373e41d09dSXin LI 		*p = '\0';
103848c779cdSXin LI 	if ((p = strrchr(CAST(char *, buf), ':')) != NULL) {
10393e41d09dSXin LI 		++p;
104048c779cdSXin LI 		while (isspace(CAST(unsigned char, *p)))
10413e41d09dSXin LI 			p++;
10423e41d09dSXin LI 		n = strlen(p);
104340427ccaSGordon Tetlow 		memmove(ubuf, p, CAST(size_t, n + 1));
10443e41d09dSXin LI 	}
10453e41d09dSXin LI 	DPRINTF("Filter error after[[[%s]]]\n", (char *)ubuf);
10463e41d09dSXin LI 	if (islower(*ubuf))
10473e41d09dSXin LI 		*ubuf = toupper(*ubuf);
1048b6cee71dSXin LI 	return n;
1049b6cee71dSXin LI }
10503e41d09dSXin LI 
1051898496eeSXin LI file_private const char *
methodname(size_t method)10523e41d09dSXin LI methodname(size_t method)
10533e41d09dSXin LI {
1054d38c30c0SXin LI 	switch (method) {
10553e41d09dSXin LI #ifdef BUILTIN_DECOMPRESS
1056d38c30c0SXin LI 	case METH_FROZEN:
1057d38c30c0SXin LI 	case METH_ZLIB:
10583e41d09dSXin LI 		return "zlib";
10593e41d09dSXin LI #endif
1060d38c30c0SXin LI #ifdef BUILTIN_BZLIB
1061d38c30c0SXin LI 	case METH_BZIP:
1062d38c30c0SXin LI 		return "bzlib";
1063d38c30c0SXin LI #endif
1064d38c30c0SXin LI #ifdef BUILTIN_XZLIB
1065d38c30c0SXin LI 	case METH_XZ:
1066d38c30c0SXin LI 	case METH_LZMA:
1067d38c30c0SXin LI 		return "xzlib";
1068d38c30c0SXin LI #endif
1069898496eeSXin LI #ifdef BUILTIN_ZSTDLIB
1070898496eeSXin LI 	case METH_ZSTD:
1071898496eeSXin LI 		return "zstd";
1072898496eeSXin LI #endif
1073898496eeSXin LI #ifdef BUILTIN_LZLIB
1074898496eeSXin LI 	case METH_LZIP:
1075898496eeSXin LI 		return "lzlib";
1076898496eeSXin LI #endif
1077*ae316d1dSXin LI #ifdef BUILTIN_LRZIP
1078*ae316d1dSXin LI 	case METH_LRZIP:
1079*ae316d1dSXin LI 		return "lrzip";
1080*ae316d1dSXin LI #endif
1081d38c30c0SXin LI 	default:
10823e41d09dSXin LI 		return compr[method].argv[0];
10833e41d09dSXin LI 	}
1084d38c30c0SXin LI }
10853e41d09dSXin LI 
1086898496eeSXin LI file_private int (*
getdecompressor(size_t method)1087898496eeSXin LI getdecompressor(size_t method))(const unsigned char *, unsigned char **, size_t,
1088898496eeSXin LI     size_t *, int)
1089898496eeSXin LI {
1090898496eeSXin LI 	switch (method) {
1091898496eeSXin LI #ifdef BUILTIN_DECOMPRESS
1092898496eeSXin LI 	case METH_FROZEN:
1093898496eeSXin LI 		return uncompressgzipped;
1094898496eeSXin LI 	case METH_ZLIB:
1095898496eeSXin LI 		return uncompresszlib;
1096898496eeSXin LI #endif
1097898496eeSXin LI #ifdef BUILTIN_BZLIB
1098898496eeSXin LI 	case METH_BZIP:
1099898496eeSXin LI 		return uncompressbzlib;
1100898496eeSXin LI #endif
1101898496eeSXin LI #ifdef BUILTIN_XZLIB
1102898496eeSXin LI 	case METH_XZ:
1103898496eeSXin LI 	case METH_LZMA:
1104898496eeSXin LI 		return uncompressxzlib;
1105898496eeSXin LI #endif
1106898496eeSXin LI #ifdef BUILTIN_ZSTDLIB
1107898496eeSXin LI 	case METH_ZSTD:
1108898496eeSXin LI 		return uncompresszstd;
1109898496eeSXin LI #endif
1110898496eeSXin LI #ifdef BUILTIN_LZLIB
1111898496eeSXin LI 	case METH_LZIP:
1112898496eeSXin LI 		return uncompresslzlib;
1113898496eeSXin LI #endif
1114*ae316d1dSXin LI #ifdef BUILTIN_LRZIP
1115*ae316d1dSXin LI 	case METH_LRZIP:
1116*ae316d1dSXin LI 		return uncompresslrzip;
1117*ae316d1dSXin LI #endif
1118898496eeSXin LI 	default:
1119898496eeSXin LI 		return NULL;
1120898496eeSXin LI 	}
1121898496eeSXin LI }
1122898496eeSXin LI 
1123898496eeSXin LI file_private int
uncompressbuf(int fd,size_t bytes_max,size_t method,int nofork,const unsigned char * old,unsigned char ** newch,size_t * n)1124898496eeSXin LI uncompressbuf(int fd, size_t bytes_max, size_t method, int nofork,
1125898496eeSXin LI     const unsigned char *old, unsigned char **newch, size_t* n)
11263e41d09dSXin LI {
11273e41d09dSXin LI 	int fdp[3][2];
112848c779cdSXin LI 	int status, rv, w;
112948c779cdSXin LI 	pid_t pid;
113048c779cdSXin LI 	pid_t writepid = -1;
11313e41d09dSXin LI 	size_t i;
1132898496eeSXin LI 	ssize_t r, re;
1133a4d6d3b8SXin LI 	char *const *args;
1134a4d6d3b8SXin LI #ifdef HAVE_POSIX_SPAWNP
1135a4d6d3b8SXin LI 	posix_spawn_file_actions_t fa;
1136a4d6d3b8SXin LI #endif
1137898496eeSXin LI 	int (*decompress)(const unsigned char *, unsigned char **,
1138898496eeSXin LI 	    size_t, size_t *, int) = getdecompressor(method);
11393e41d09dSXin LI 
1140898496eeSXin LI 	*newch = CAST(unsigned char *, malloc(bytes_max + 1));
1141898496eeSXin LI 	if (*newch == NULL)
1142898496eeSXin LI 		return makeerror(newch, n, "No buffer, %s", strerror(errno));
1143898496eeSXin LI 
1144898496eeSXin LI 	if (decompress) {
1145898496eeSXin LI 		if (nofork) {
1146898496eeSXin LI 			return makeerror(newch, n,
1147898496eeSXin LI 			    "Fork is required to uncompress, but disabled");
1148898496eeSXin LI 		}
1149898496eeSXin LI 		return (*decompress)(old, newch, bytes_max, n, 1);
1150d38c30c0SXin LI 	}
1151d38c30c0SXin LI 
11523e41d09dSXin LI 	(void)fflush(stdout);
11533e41d09dSXin LI 	(void)fflush(stderr);
11543e41d09dSXin LI 
11553e41d09dSXin LI 	for (i = 0; i < __arraycount(fdp); i++)
11563e41d09dSXin LI 		fdp[i][0] = fdp[i][1] = -1;
11573e41d09dSXin LI 
115843a5ec4eSXin LI 	/*
115943a5ec4eSXin LI 	 * There are multithreaded users who run magic_file()
116043a5ec4eSXin LI 	 * from dozens of threads. If two parallel magic_file() calls
116143a5ec4eSXin LI 	 * analyze two large compressed files, both will spawn
116243a5ec4eSXin LI 	 * an uncompressing child here, which writes out uncompressed data.
116343a5ec4eSXin LI 	 * We read some portion, then close the pipe, then waitpid() the child.
1164898496eeSXin LI 	 * If uncompressed data is larger, child should get EPIPE and exit.
116543a5ec4eSXin LI 	 * However, with *parallel* calls OTHER child may unintentionally
116643a5ec4eSXin LI 	 * inherit pipe fds, thus keeping pipe open and making writes in
116743a5ec4eSXin LI 	 * our child block instead of failing with EPIPE!
116843a5ec4eSXin LI 	 * (For the bug to occur, two threads must mutually inherit their pipes,
116943a5ec4eSXin LI 	 * and both must have large outputs. Thus it happens not that often).
117043a5ec4eSXin LI 	 * To avoid this, be sure to create pipes with O_CLOEXEC.
117143a5ec4eSXin LI 	 */
117243a5ec4eSXin LI 	if ((fd == -1 && file_pipe_closexec(fdp[STDIN_FILENO]) == -1) ||
117343a5ec4eSXin LI 	    file_pipe_closexec(fdp[STDOUT_FILENO]) == -1 ||
117443a5ec4eSXin LI 	    file_pipe_closexec(fdp[STDERR_FILENO]) == -1) {
11753e41d09dSXin LI 		closep(fdp[STDIN_FILENO]);
11763e41d09dSXin LI 		closep(fdp[STDOUT_FILENO]);
11773e41d09dSXin LI 		return makeerror(newch, n, "Cannot create pipe, %s",
11783e41d09dSXin LI 		    strerror(errno));
11793e41d09dSXin LI 	}
11803e41d09dSXin LI 
1181a4d6d3b8SXin LI 	args = RCAST(char *const *, RCAST(intptr_t, compr[method].argv));
1182a4d6d3b8SXin LI #ifdef HAVE_POSIX_SPAWNP
1183a4d6d3b8SXin LI 	posix_spawn_file_actions_init(&fa);
1184a4d6d3b8SXin LI 
1185a4d6d3b8SXin LI 	handledesc(&fa, fd, fdp);
1186a4d6d3b8SXin LI 
1187898496eeSXin LI 	DPRINTF("Executing %s\n", compr[method].argv[0]);
1188a4d6d3b8SXin LI 	status = posix_spawnp(&pid, compr[method].argv[0], &fa, NULL,
1189a4d6d3b8SXin LI 	    args, NULL);
1190a4d6d3b8SXin LI 
1191a4d6d3b8SXin LI 	posix_spawn_file_actions_destroy(&fa);
1192a4d6d3b8SXin LI 
1193a4d6d3b8SXin LI 	if (status == -1) {
1194a4d6d3b8SXin LI 		return makeerror(newch, n, "Cannot posix_spawn `%s', %s",
1195a4d6d3b8SXin LI 		    compr[method].argv[0], strerror(errno));
1196a4d6d3b8SXin LI 	}
1197a4d6d3b8SXin LI #else
119848c779cdSXin LI 	/* For processes with large mapped virtual sizes, vfork
119948c779cdSXin LI 	 * may be _much_ faster (10-100 times) than fork.
120048c779cdSXin LI 	 */
120148c779cdSXin LI 	pid = vfork();
120248c779cdSXin LI 	if (pid == -1) {
120348c779cdSXin LI 		return makeerror(newch, n, "Cannot vfork, %s",
120448c779cdSXin LI 		    strerror(errno));
120548c779cdSXin LI 	}
120648c779cdSXin LI 	if (pid == 0) {
120748c779cdSXin LI 		/* child */
120848c779cdSXin LI 		/* Note: we are after vfork, do not modify memory
120948c779cdSXin LI 		 * in a way which confuses parent. In particular,
121048c779cdSXin LI 		 * do not modify fdp[i][j].
121148c779cdSXin LI 		 */
1212a4d6d3b8SXin LI 		handledesc(NULL, fd, fdp);
1213898496eeSXin LI 		DPRINTF("Executing %s\n", compr[method].argv[0]);
121443a5ec4eSXin LI 
1215a4d6d3b8SXin LI 		(void)execvp(compr[method].argv[0], args);
12163e41d09dSXin LI 		dprintf(STDERR_FILENO, "exec `%s' failed, %s",
12173e41d09dSXin LI 		    compr[method].argv[0], strerror(errno));
1218898496eeSXin LI 		_exit(EXIT_FAILURE); /* _exit(), not exit(), because of vfork */
121948c779cdSXin LI 	}
1220a4d6d3b8SXin LI #endif
122148c779cdSXin LI 	/* parent */
122248c779cdSXin LI 	/* Close write sides of child stdout/err pipes */
12233e41d09dSXin LI 	for (i = 1; i < __arraycount(fdp); i++)
12243e41d09dSXin LI 		closefd(fdp[i], 1);
122548c779cdSXin LI 	/* Write the buffer data to child stdin, if we don't have fd */
122648c779cdSXin LI 	if (fd == -1) {
122748c779cdSXin LI 		closefd(fdp[STDIN_FILENO], 0);
122848c779cdSXin LI 		writepid = writechild(fdp[STDIN_FILENO][1], old, *n);
1229898496eeSXin LI 		if (writepid == (pid_t)-1) {
1230898496eeSXin LI 			rv = makeerror(newch, n, "Write to child failed, %s",
1231898496eeSXin LI 			    strerror(errno));
1232898496eeSXin LI 			DPRINTF("Write to child failed\n");
1233898496eeSXin LI 			goto err;
1234898496eeSXin LI 		}
123548c779cdSXin LI 		closefd(fdp[STDIN_FILENO], 1);
123648c779cdSXin LI 	}
12373e41d09dSXin LI 
1238898496eeSXin LI 	rv = OKDATA;
1239898496eeSXin LI 	r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0);
1240898496eeSXin LI 	DPRINTF("read got %zd\n", r);
1241898496eeSXin LI 	if (r < 0) {
1242898496eeSXin LI 		rv = ERRDATA;
1243898496eeSXin LI 		DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0],
12443e41d09dSXin LI 		        strerror(errno));
12453e41d09dSXin LI 		goto err;
12463e41d09dSXin LI 	}
1247898496eeSXin LI 	if (CAST(size_t, r) == bytes_max) {
1248898496eeSXin LI 		/*
1249898496eeSXin LI 		 * close fd so that the child exits with sigpipe and ignore
1250898496eeSXin LI 		 * errors, otherwise we risk the child blocking and never
1251898496eeSXin LI 		 * exiting.
1252898496eeSXin LI 		 */
1253898496eeSXin LI 		DPRINTF("Closing stdout for bytes_max\n");
1254898496eeSXin LI 		closefd(fdp[STDOUT_FILENO], 0);
1255a2dfb722SXin LI 		goto ok;
1256898496eeSXin LI 	}
1257898496eeSXin LI 	if ((re = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0) {
1258898496eeSXin LI 		DPRINTF("Got stuff from stderr %s\n", *newch);
12593e41d09dSXin LI 		rv = ERRDATA;
12603e41d09dSXin LI 		r = filter_error(*newch, r);
126148c779cdSXin LI 		goto ok;
12623e41d09dSXin LI 	}
1263898496eeSXin LI 	if  (re == 0)
1264898496eeSXin LI 		goto ok;
1265898496eeSXin LI 	rv = makeerror(newch, n, "Read stderr failed, %s",
12663e41d09dSXin LI 	    strerror(errno));
12673e41d09dSXin LI 	goto err;
126848c779cdSXin LI ok:
12693e41d09dSXin LI 	*n = r;
12703e41d09dSXin LI 	/* NUL terminate, as every buffer is handled here. */
12713e41d09dSXin LI 	(*newch)[*n] = '\0';
12723e41d09dSXin LI err:
12733e41d09dSXin LI 	closefd(fdp[STDIN_FILENO], 1);
12743e41d09dSXin LI 	closefd(fdp[STDOUT_FILENO], 0);
12753e41d09dSXin LI 	closefd(fdp[STDERR_FILENO], 0);
127648c779cdSXin LI 
127748c779cdSXin LI 	w = waitpid(pid, &status, 0);
127848c779cdSXin LI wait_err:
127948c779cdSXin LI 	if (w == -1) {
12803e41d09dSXin LI 		rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
12813e41d09dSXin LI 		DPRINTF("Child wait return %#x\n", status);
12823e41d09dSXin LI 	} else if (!WIFEXITED(status)) {
128340427ccaSGordon Tetlow 		DPRINTF("Child not exited (%#x)\n", status);
12843e41d09dSXin LI 	} else if (WEXITSTATUS(status) != 0) {
128540427ccaSGordon Tetlow 		DPRINTF("Child exited (%#x)\n", WEXITSTATUS(status));
12863e41d09dSXin LI 	}
128748c779cdSXin LI 	if (writepid > 0) {
128848c779cdSXin LI 		/* _After_ we know decompressor has exited, our input writer
128948c779cdSXin LI 		 * definitely will exit now (at worst, writing fails in it,
129048c779cdSXin LI 		 * since output fd is closed now on the reading size).
129148c779cdSXin LI 		 */
129248c779cdSXin LI 		w = waitpid(writepid, &status, 0);
129348c779cdSXin LI 		writepid = -1;
129448c779cdSXin LI 		goto wait_err;
129548c779cdSXin LI 	}
12963e41d09dSXin LI 
129748c779cdSXin LI 	closefd(fdp[STDIN_FILENO], 0); //why? it is already closed here!
129848c779cdSXin LI 	DPRINTF("Returning %p n=%" SIZE_T_FORMAT "u rv=%d\n", *newch, *n, rv);
12993e41d09dSXin LI 
13003e41d09dSXin LI 	return rv;
1301b6cee71dSXin LI }
1302b6cee71dSXin LI #endif
1303