1*ddb17682Schristos /* $NetBSD: compress.c,v 1.23 2023/08/18 19:00:11 christos Exp $ */
2fa9ee498Schristos
31b108b8bSchristos /*
41b108b8bSchristos * Copyright (c) Ian F. Darwin 1986-1995.
51b108b8bSchristos * Software written by Ian F. Darwin and others;
61b108b8bSchristos * maintained 1995-present by Christos Zoulas and others.
71b108b8bSchristos *
81b108b8bSchristos * Redistribution and use in source and binary forms, with or without
91b108b8bSchristos * modification, are permitted provided that the following conditions
101b108b8bSchristos * are met:
111b108b8bSchristos * 1. Redistributions of source code must retain the above copyright
121b108b8bSchristos * notice immediately at the beginning of the file, without modification,
131b108b8bSchristos * this list of conditions, and the following disclaimer.
141b108b8bSchristos * 2. Redistributions in binary form must reproduce the above copyright
151b108b8bSchristos * notice, this list of conditions and the following disclaimer in the
161b108b8bSchristos * documentation and/or other materials provided with the distribution.
171b108b8bSchristos *
181b108b8bSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191b108b8bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201b108b8bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211b108b8bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
221b108b8bSchristos * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231b108b8bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241b108b8bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251b108b8bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261b108b8bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271b108b8bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281b108b8bSchristos * SUCH DAMAGE.
291b108b8bSchristos */
301b108b8bSchristos /*
311b108b8bSchristos * compress routines:
321b108b8bSchristos * zmagic() - returns 0 if not recognized, uncompresses and prints
331b108b8bSchristos * information if recognized
341b108b8bSchristos * uncompress(method, old, n, newch) - uncompress old into new,
351b108b8bSchristos * using method, return sizeof new
361b108b8bSchristos */
371b108b8bSchristos #include "file.h"
381b108b8bSchristos
391b108b8bSchristos #ifndef lint
401b108b8bSchristos #if 0
41*ddb17682Schristos FILE_RCSID("@(#)$File: compress.c,v 1.157 2023/05/21 15:59:58 christos Exp $")
421b108b8bSchristos #else
43*ddb17682Schristos __RCSID("$NetBSD: compress.c,v 1.23 2023/08/18 19:00:11 christos Exp $");
441b108b8bSchristos #endif
451b108b8bSchristos #endif
461b108b8bSchristos
471b108b8bSchristos #include "magic.h"
481b108b8bSchristos #include <stdlib.h>
491b108b8bSchristos #ifdef HAVE_UNISTD_H
501b108b8bSchristos #include <unistd.h>
511b108b8bSchristos #endif
521d4cb158Schristos #ifdef HAVE_SPAWN_H
531d4cb158Schristos #include <spawn.h>
541d4cb158Schristos #endif
551b108b8bSchristos #include <string.h>
561b108b8bSchristos #include <errno.h>
5774db5203Schristos #include <ctype.h>
5874db5203Schristos #include <stdarg.h>
59fa9ee498Schristos #include <signal.h>
6074db5203Schristos #ifndef HAVE_SIG_T
6174db5203Schristos typedef void (*sig_t)(int);
6274db5203Schristos #endif /* HAVE_SIG_T */
631d4cb158Schristos #ifdef HAVE_SYS_IOCTL_H
641b108b8bSchristos #include <sys/ioctl.h>
652344ff98Schristos #endif
661b108b8bSchristos #ifdef HAVE_SYS_WAIT_H
671b108b8bSchristos #include <sys/wait.h>
681b108b8bSchristos #endif
691b108b8bSchristos #if defined(HAVE_SYS_TIME_H)
701b108b8bSchristos #include <sys/time.h>
711b108b8bSchristos #endif
72c02f7f97Schristos
7374db5203Schristos #if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT)
741b108b8bSchristos #define BUILTIN_DECOMPRESS
751b108b8bSchristos #include <zlib.h>
761b108b8bSchristos #endif
77c02f7f97Schristos
7829faeba7Schristos #if defined(HAVE_BZLIB_H) && defined(BZLIBSUPPORT)
79c02f7f97Schristos #define BUILTIN_BZLIB
80c02f7f97Schristos #include <bzlib.h>
81c02f7f97Schristos #endif
82c02f7f97Schristos
83d87b0039Schristos #if defined(HAVE_LZMA_H) && defined(XZLIBSUPPORT)
8478a23c3aSchristos #define BUILTIN_XZLIB
8578a23c3aSchristos #include <lzma.h>
8678a23c3aSchristos #endif
8778a23c3aSchristos
881d4cb158Schristos #if defined(HAVE_ZSTD_H) && defined(ZSTDLIBSUPPORT)
891d4cb158Schristos #define BUILTIN_ZSTDLIB
901d4cb158Schristos #include <zstd.h>
911d4cb158Schristos #include <zstd_errors.h>
921d4cb158Schristos #endif
931d4cb158Schristos
941d4cb158Schristos #if defined(HAVE_LZLIB_H) && defined(LZLIBSUPPORT)
951d4cb158Schristos #define BUILTIN_LZLIB
961d4cb158Schristos #include <lzlib.h>
971d4cb158Schristos #endif
981d4cb158Schristos
9974db5203Schristos #ifdef DEBUG
10074db5203Schristos int tty = -1;
10174db5203Schristos #define DPRINTF(...) do { \
10274db5203Schristos if (tty == -1) \
10374db5203Schristos tty = open("/dev/tty", O_RDWR); \
10474db5203Schristos if (tty == -1) \
10574db5203Schristos abort(); \
10674db5203Schristos dprintf(tty, __VA_ARGS__); \
10774db5203Schristos } while (/*CONSTCOND*/0)
10874db5203Schristos #else
10974db5203Schristos #define DPRINTF(...)
11074db5203Schristos #endif
1111b108b8bSchristos
11274db5203Schristos #ifdef ZLIBSUPPORT
11374db5203Schristos /*
11474db5203Schristos * The following python code is not really used because ZLIBSUPPORT is only
11574db5203Schristos * defined if we have a built-in zlib, and the built-in zlib handles that.
11674db5203Schristos * That is not true for android where we have zlib.h and not -lz.
11774db5203Schristos */
11874db5203Schristos static const char zlibcode[] =
11974db5203Schristos "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
12074db5203Schristos
12174db5203Schristos static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
12274db5203Schristos
12374db5203Schristos static int
zlibcmp(const unsigned char * buf)12474db5203Schristos zlibcmp(const unsigned char *buf)
12574db5203Schristos {
12674db5203Schristos unsigned short x = 1;
127e2725312Schristos unsigned char *s = CAST(unsigned char *, CAST(void *, &x));
12874db5203Schristos
12974db5203Schristos if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
13074db5203Schristos return 0;
13174db5203Schristos if (s[0] != 1) /* endianness test */
13274db5203Schristos x = buf[0] | (buf[1] << 8);
13374db5203Schristos else
13474db5203Schristos x = buf[1] | (buf[0] << 8);
13574db5203Schristos if (x % 31)
13674db5203Schristos return 0;
13774db5203Schristos return 1;
13874db5203Schristos }
13974db5203Schristos #endif
14074db5203Schristos
14178a23c3aSchristos static int
lzmacmp(const unsigned char * buf)14278a23c3aSchristos lzmacmp(const unsigned char *buf)
14378a23c3aSchristos {
14478a23c3aSchristos if (buf[0] != 0x5d || buf[1] || buf[2])
14578a23c3aSchristos return 0;
14678a23c3aSchristos if (buf[12] && buf[12] != 0xff)
14778a23c3aSchristos return 0;
14878a23c3aSchristos return 1;
14978a23c3aSchristos }
15078a23c3aSchristos
15174db5203Schristos #define gzip_flags "-cd"
15274db5203Schristos #define lzip_flags gzip_flags
15374db5203Schristos
15474db5203Schristos static const char *gzip_args[] = {
15574db5203Schristos "gzip", gzip_flags, NULL
15674db5203Schristos };
15774db5203Schristos static const char *uncompress_args[] = {
15874db5203Schristos "uncompress", "-c", NULL
15974db5203Schristos };
16074db5203Schristos static const char *bzip2_args[] = {
16174db5203Schristos "bzip2", "-cd", NULL
16274db5203Schristos };
16374db5203Schristos static const char *lzip_args[] = {
16474db5203Schristos "lzip", lzip_flags, NULL
16574db5203Schristos };
16674db5203Schristos static const char *xz_args[] = {
16774db5203Schristos "xz", "-cd", NULL
16874db5203Schristos };
16974db5203Schristos static const char *lrzip_args[] = {
170*ddb17682Schristos "lrzip", "-qdf", "-", NULL
17174db5203Schristos };
17274db5203Schristos static const char *lz4_args[] = {
17374db5203Schristos "lz4", "-cd", NULL
17474db5203Schristos };
17574db5203Schristos static const char *zstd_args[] = {
17674db5203Schristos "zstd", "-cd", NULL
1771b108b8bSchristos };
1781b108b8bSchristos
179c02f7f97Schristos #define do_zlib NULL
180c02f7f97Schristos #define do_bzlib NULL
181c02f7f97Schristos
182*ddb17682Schristos file_private const struct {
18329faeba7Schristos union {
18429faeba7Schristos const char *magic;
18529faeba7Schristos int (*func)(const unsigned char *);
18629faeba7Schristos } u;
18778a23c3aSchristos int maglen;
18874db5203Schristos const char **argv;
189c02f7f97Schristos void *unused;
19074db5203Schristos } compr[] = {
19178a23c3aSchristos #define METH_FROZEN 2
19278a23c3aSchristos #define METH_BZIP 7
19378a23c3aSchristos #define METH_XZ 9
1941d4cb158Schristos #define METH_LZIP 8
1951d4cb158Schristos #define METH_ZSTD 12
19678a23c3aSchristos #define METH_LZMA 13
19778a23c3aSchristos #define METH_ZLIB 14
19829faeba7Schristos { { .magic = "\037\235" }, 2, gzip_args, NULL }, /* 0, compressed */
19974db5203Schristos /* Uncompress can get stuck; so use gzip first if we have it
20074db5203Schristos * Idea from Damien Clark, thanks! */
20129faeba7Schristos { { .magic = "\037\235" }, 2, uncompress_args, NULL },/* 1, compressed */
20229faeba7Schristos { { .magic = "\037\213" }, 2, gzip_args, do_zlib },/* 2, gzipped */
20329faeba7Schristos { { .magic = "\037\236" }, 2, gzip_args, NULL }, /* 3, frozen */
20429faeba7Schristos { { .magic = "\037\240" }, 2, gzip_args, NULL }, /* 4, SCO LZH */
20574db5203Schristos /* the standard pack utilities do not accept standard input */
20629faeba7Schristos { { .magic = "\037\036" }, 2, gzip_args, NULL }, /* 5, packed */
20729faeba7Schristos { { .magic = "PK\3\4" }, 4, gzip_args, NULL }, /* 6, pkziped */
20874db5203Schristos /* ...only first file examined */
20929faeba7Schristos { { .magic = "BZh" }, 3, bzip2_args, do_bzlib },/* 7, bzip2-ed */
21029faeba7Schristos { { .magic = "LZIP" }, 4, lzip_args, NULL }, /* 8, lzip-ed */
21129faeba7Schristos { { .magic = "\3757zXZ\0" },6, xz_args, NULL }, /* 9, XZ Util */
21229faeba7Schristos { { .magic = "LRZI" }, 4, lrzip_args, NULL }, /* 10, LRZIP */
21329faeba7Schristos { { .magic = "\004\"M\030" },4, lz4_args, NULL }, /* 11, LZ4 */
21429faeba7Schristos { { .magic = "\x28\xB5\x2F\xFD" }, 4, zstd_args, NULL },/* 12, zstd */
21529faeba7Schristos { { .func = lzmacmp }, -13, xz_args, NULL }, /* 13, lzma */
21674db5203Schristos #ifdef ZLIBSUPPORT
21729faeba7Schristos { { .func = zlibcmp }, -2, zlib_args, NULL }, /* 14, zlib */
21874db5203Schristos #endif
21974db5203Schristos };
22074db5203Schristos
22174db5203Schristos #define OKDATA 0
22274db5203Schristos #define NODATA 1
22374db5203Schristos #define ERRDATA 2
2241b108b8bSchristos
225*ddb17682Schristos file_private ssize_t swrite(int, const void *, size_t);
2262344ff98Schristos #if HAVE_FORK
227*ddb17682Schristos file_private size_t ncompr = __arraycount(compr);
228*ddb17682Schristos file_private int uncompressbuf(int, size_t, size_t, int, const unsigned char *,
22974db5203Schristos unsigned char **, size_t *);
2301b108b8bSchristos #ifdef BUILTIN_DECOMPRESS
231*ddb17682Schristos file_private int uncompresszlib(const unsigned char *, unsigned char **, size_t,
23274db5203Schristos size_t *, int);
233*ddb17682Schristos file_private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
2341d4cb158Schristos size_t *, int);
2351b108b8bSchristos #endif
236c02f7f97Schristos #ifdef BUILTIN_BZLIB
237*ddb17682Schristos file_private int uncompressbzlib(const unsigned char *, unsigned char **, size_t,
2381d4cb158Schristos size_t *, int);
23978a23c3aSchristos #endif
24078a23c3aSchristos #ifdef BUILTIN_XZLIB
241*ddb17682Schristos file_private int uncompressxzlib(const unsigned char *, unsigned char **, size_t,
2421d4cb158Schristos size_t *, int);
2431d4cb158Schristos #endif
2441d4cb158Schristos #ifdef BUILTIN_ZSTDLIB
245*ddb17682Schristos file_private int uncompresszstd(const unsigned char *, unsigned char **, size_t,
2461d4cb158Schristos size_t *, int);
2471d4cb158Schristos #endif
2481d4cb158Schristos #ifdef BUILTIN_LZLIB
249*ddb17682Schristos file_private int uncompresslzlib(const unsigned char *, unsigned char **, size_t,
2501d4cb158Schristos size_t *, int);
251c02f7f97Schristos #endif
252c02f7f97Schristos
25374db5203Schristos static int makeerror(unsigned char **, size_t *, const char *, ...)
25474db5203Schristos __attribute__((__format__(__printf__, 3, 4)));
255*ddb17682Schristos file_private const char *methodname(size_t);
2561b108b8bSchristos
257*ddb17682Schristos file_private int
format_decompression_error(struct magic_set * ms,size_t i,unsigned char * buf)258c02f7f97Schristos format_decompression_error(struct magic_set *ms, size_t i, unsigned char *buf)
259c02f7f97Schristos {
260c02f7f97Schristos unsigned char *p;
261c02f7f97Schristos int mime = ms->flags & MAGIC_MIME;
262c02f7f97Schristos
263c02f7f97Schristos if (!mime)
264c02f7f97Schristos return file_printf(ms, "ERROR:[%s: %s]", methodname(i), buf);
265c02f7f97Schristos
266c02f7f97Schristos for (p = buf; *p; p++)
267c02f7f97Schristos if (!isalnum(*p))
268c02f7f97Schristos *p = '-';
269c02f7f97Schristos
270c02f7f97Schristos return file_printf(ms, "application/x-decompression-error-%s-%s",
271c02f7f97Schristos methodname(i), buf);
272c02f7f97Schristos }
273c02f7f97Schristos
274*ddb17682Schristos file_protected int
file_zmagic(struct magic_set * ms,const struct buffer * b,const char * name)2755efe63deSchristos file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name)
2761b108b8bSchristos {
2771b108b8bSchristos unsigned char *newbuf = NULL;
2781b108b8bSchristos size_t i, nsz;
27974db5203Schristos char *rbuf;
28074db5203Schristos file_pushbuf_t *pb;
28174db5203Schristos int urv, prv, rv = 0;
2821b108b8bSchristos int mime = ms->flags & MAGIC_MIME;
2835efe63deSchristos int fd = b->fd;
284c02f7f97Schristos const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
2855efe63deSchristos size_t nbytes = b->flen;
286d0c65b7bSchristos int sa_saved = 0;
287d0c65b7bSchristos struct sigaction sig_act;
2881b108b8bSchristos
2891b108b8bSchristos if ((ms->flags & MAGIC_COMPRESS) == 0)
2901b108b8bSchristos return 0;
2911b108b8bSchristos
2921b108b8bSchristos for (i = 0; i < ncompr; i++) {
29374db5203Schristos int zm;
29478a23c3aSchristos if (nbytes < CAST(size_t, abs(compr[i].maglen)))
2951b108b8bSchristos continue;
29678a23c3aSchristos if (compr[i].maglen < 0) {
29729faeba7Schristos zm = (*compr[i].u.func)(buf);
29878a23c3aSchristos } else {
29929faeba7Schristos zm = memcmp(buf, compr[i].u.magic,
30078a23c3aSchristos CAST(size_t, compr[i].maglen)) == 0;
30178a23c3aSchristos }
3021b108b8bSchristos
30374db5203Schristos if (!zm)
30474db5203Schristos continue;
305d0c65b7bSchristos
306d0c65b7bSchristos /* Prevent SIGPIPE death if child dies unexpectedly */
307d0c65b7bSchristos if (!sa_saved) {
308d0c65b7bSchristos //We can use sig_act for both new and old, but
309d0c65b7bSchristos struct sigaction new_act;
310d0c65b7bSchristos memset(&new_act, 0, sizeof(new_act));
311d0c65b7bSchristos new_act.sa_handler = SIG_IGN;
312d0c65b7bSchristos sa_saved = sigaction(SIGPIPE, &new_act, &sig_act) != -1;
313d0c65b7bSchristos }
314d0c65b7bSchristos
31574db5203Schristos nsz = nbytes;
316*ddb17682Schristos free(newbuf);
3171d4cb158Schristos urv = uncompressbuf(fd, ms->bytes_max, i,
3181d4cb158Schristos (ms->flags & MAGIC_NO_COMPRESS_FORK), buf, &newbuf, &nsz);
319c02f7f97Schristos DPRINTF("uncompressbuf = %d, %s, %" SIZE_T_FORMAT "u\n", urv,
320c02f7f97Schristos (char *)newbuf, nsz);
32174db5203Schristos switch (urv) {
32274db5203Schristos case OKDATA:
32374db5203Schristos case ERRDATA:
32474db5203Schristos ms->flags &= ~MAGIC_COMPRESS;
32574db5203Schristos if (urv == ERRDATA)
326c02f7f97Schristos prv = format_decompression_error(ms, i, newbuf);
32774db5203Schristos else
3281d4cb158Schristos prv = file_buffer(ms, -1, NULL, name, newbuf,
3291d4cb158Schristos nsz);
33074db5203Schristos if (prv == -1)
3311b108b8bSchristos goto error;
33274db5203Schristos rv = 1;
33374db5203Schristos if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
33474db5203Schristos goto out;
33574db5203Schristos if (mime != MAGIC_MIME && mime != 0)
33674db5203Schristos goto out;
33774db5203Schristos if ((file_printf(ms,
33874db5203Schristos mime ? " compressed-encoding=" : " (")) == -1)
33974db5203Schristos goto error;
34074db5203Schristos if ((pb = file_push_buffer(ms)) == NULL)
34174db5203Schristos goto error;
34274db5203Schristos /*
34374db5203Schristos * XXX: If file_buffer fails here, we overwrite
34474db5203Schristos * the compressed text. FIXME.
34574db5203Schristos */
3461d4cb158Schristos if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1)
3471d4cb158Schristos {
348c02f7f97Schristos if (file_pop_buffer(ms, pb) != NULL)
349c02f7f97Schristos abort();
3501b108b8bSchristos goto error;
351c02f7f97Schristos }
35274db5203Schristos if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
35374db5203Schristos if (file_printf(ms, "%s", rbuf) == -1) {
35474db5203Schristos free(rbuf);
3551b108b8bSchristos goto error;
3568dd459ccSchristos }
35774db5203Schristos free(rbuf);
35874db5203Schristos }
35974db5203Schristos if (!mime && file_printf(ms, ")") == -1)
36074db5203Schristos goto error;
36174db5203Schristos /*FALLTHROUGH*/
36274db5203Schristos case NODATA:
36374db5203Schristos break;
36474db5203Schristos default:
36574db5203Schristos abort();
36674db5203Schristos /*NOTREACHED*/
36774db5203Schristos error:
36874db5203Schristos rv = -1;
3691b108b8bSchristos break;
3701b108b8bSchristos }
3711b108b8bSchristos }
37274db5203Schristos out:
37374db5203Schristos DPRINTF("rv = %d\n", rv);
37474db5203Schristos
375d0c65b7bSchristos if (sa_saved && sig_act.sa_handler != SIG_IGN)
376d0c65b7bSchristos (void)sigaction(SIGPIPE, &sig_act, NULL);
377d0c65b7bSchristos
3781b108b8bSchristos free(newbuf);
3791b108b8bSchristos ms->flags |= MAGIC_COMPRESS;
38074db5203Schristos DPRINTF("Zmagic returns %d\n", rv);
3811b108b8bSchristos return rv;
3821b108b8bSchristos }
3832344ff98Schristos #endif
3841b108b8bSchristos /*
3851b108b8bSchristos * `safe' write for sockets and pipes.
3861b108b8bSchristos */
387*ddb17682Schristos file_private ssize_t
swrite(int fd,const void * buf,size_t n)3881b108b8bSchristos swrite(int fd, const void *buf, size_t n)
3891b108b8bSchristos {
3907c25ef23Schristos ssize_t rv;
3911b108b8bSchristos size_t rn = n;
3921b108b8bSchristos
3931b108b8bSchristos do
3941b108b8bSchristos switch (rv = write(fd, buf, n)) {
3951b108b8bSchristos case -1:
3961b108b8bSchristos if (errno == EINTR)
3971b108b8bSchristos continue;
3981b108b8bSchristos return -1;
3991b108b8bSchristos default:
4001b108b8bSchristos n -= rv;
4017c25ef23Schristos buf = CAST(const char *, buf) + rv;
4021b108b8bSchristos break;
4031b108b8bSchristos }
4041b108b8bSchristos while (n > 0);
4051b108b8bSchristos return rn;
4061b108b8bSchristos }
4071b108b8bSchristos
4081b108b8bSchristos
4091b108b8bSchristos /*
4101b108b8bSchristos * `safe' read for sockets and pipes.
4111b108b8bSchristos */
412*ddb17682Schristos file_protected ssize_t
sread(int fd,void * buf,size_t n,int canbepipe)41320d96732Schristos sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
4141b108b8bSchristos {
4152344ff98Schristos ssize_t rv;
416*ddb17682Schristos #if defined(FIONREAD) && !defined(__MINGW32__)
4171b108b8bSchristos int t = 0;
4181b108b8bSchristos #endif
4191b108b8bSchristos size_t rn = n;
4201b108b8bSchristos
4211b108b8bSchristos if (fd == STDIN_FILENO)
4221b108b8bSchristos goto nocheck;
4231b108b8bSchristos
424*ddb17682Schristos #if defined(FIONREAD) && !defined(__MINGW32__)
42520d96732Schristos if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) {
4261b108b8bSchristos #ifdef FD_ZERO
42720d96732Schristos ssize_t cnt;
4281b108b8bSchristos for (cnt = 0;; cnt++) {
4291b108b8bSchristos fd_set check;
4301b108b8bSchristos struct timeval tout = {0, 100 * 1000};
4311b108b8bSchristos int selrv;
4321b108b8bSchristos
4331b108b8bSchristos FD_ZERO(&check);
4341b108b8bSchristos FD_SET(fd, &check);
4351b108b8bSchristos
4361b108b8bSchristos /*
4371b108b8bSchristos * Avoid soft deadlock: do not read if there
4381b108b8bSchristos * is nothing to read from sockets and pipes.
4391b108b8bSchristos */
4401b108b8bSchristos selrv = select(fd + 1, &check, NULL, NULL, &tout);
4411b108b8bSchristos if (selrv == -1) {
4421b108b8bSchristos if (errno == EINTR || errno == EAGAIN)
4431b108b8bSchristos continue;
4441b108b8bSchristos } else if (selrv == 0 && cnt >= 5) {
4451b108b8bSchristos return 0;
4461b108b8bSchristos } else
4471b108b8bSchristos break;
4481b108b8bSchristos }
4491b108b8bSchristos #endif
4501b108b8bSchristos (void)ioctl(fd, FIONREAD, &t);
4511b108b8bSchristos }
4521b108b8bSchristos
453d0c65b7bSchristos if (t > 0 && CAST(size_t, t) < n) {
4541b108b8bSchristos n = t;
4551b108b8bSchristos rn = n;
4561b108b8bSchristos }
4571b108b8bSchristos #endif
4581b108b8bSchristos
4591b108b8bSchristos nocheck:
4601b108b8bSchristos do
4611b108b8bSchristos switch ((rv = read(fd, buf, n))) {
4621b108b8bSchristos case -1:
4631b108b8bSchristos if (errno == EINTR)
4641b108b8bSchristos continue;
4651b108b8bSchristos return -1;
4661b108b8bSchristos case 0:
4671b108b8bSchristos return rn - n;
4681b108b8bSchristos default:
4691b108b8bSchristos n -= rv;
47074db5203Schristos buf = CAST(char *, CCAST(void *, buf)) + rv;
4711b108b8bSchristos break;
4721b108b8bSchristos }
4731b108b8bSchristos while (n > 0);
4741b108b8bSchristos return rn;
4751b108b8bSchristos }
4761b108b8bSchristos
477*ddb17682Schristos file_protected int
file_pipe2file(struct magic_set * ms,int fd,const void * startbuf,size_t nbytes)4781b108b8bSchristos file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
4791b108b8bSchristos size_t nbytes)
4801b108b8bSchristos {
4811b108b8bSchristos char buf[4096];
4827c25ef23Schristos ssize_t r;
4832344ff98Schristos int tfd;
4841b108b8bSchristos
4851d4cb158Schristos #ifdef WIN32
4861d4cb158Schristos const char *t;
4871d4cb158Schristos buf[0] = '\0';
4881d4cb158Schristos if ((t = getenv("TEMP")) != NULL)
4891d4cb158Schristos (void)strlcpy(buf, t, sizeof(buf));
4901d4cb158Schristos else if ((t = getenv("TMP")) != NULL)
4911d4cb158Schristos (void)strlcpy(buf, t, sizeof(buf));
4921d4cb158Schristos else if ((t = getenv("TMPDIR")) != NULL)
4931d4cb158Schristos (void)strlcpy(buf, t, sizeof(buf));
4941d4cb158Schristos if (buf[0] != '\0')
4951d4cb158Schristos (void)strlcat(buf, "/", sizeof(buf));
4961d4cb158Schristos (void)strlcat(buf, "file.XXXXXX", sizeof(buf));
4971d4cb158Schristos #else
4981d4cb158Schristos (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof(buf));
4991d4cb158Schristos #endif
5001b108b8bSchristos #ifndef HAVE_MKSTEMP
5011b108b8bSchristos {
5021b108b8bSchristos char *ptr = mktemp(buf);
5031b108b8bSchristos tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
5041b108b8bSchristos r = errno;
5051b108b8bSchristos (void)unlink(ptr);
5061b108b8bSchristos errno = r;
5071b108b8bSchristos }
5081b108b8bSchristos #else
50920d96732Schristos {
51020d96732Schristos int te;
511bdab9e2dSchristos mode_t ou = umask(0);
5121b108b8bSchristos tfd = mkstemp(buf);
513c02f7f97Schristos (void)umask(ou);
5147c25ef23Schristos te = errno;
5151b108b8bSchristos (void)unlink(buf);
5167c25ef23Schristos errno = te;
51720d96732Schristos }
5181b108b8bSchristos #endif
5191b108b8bSchristos if (tfd == -1) {
5201b108b8bSchristos file_error(ms, errno,
5211b108b8bSchristos "cannot create temporary file for pipe copy");
5221b108b8bSchristos return -1;
5231b108b8bSchristos }
5241b108b8bSchristos
525d0c65b7bSchristos if (swrite(tfd, startbuf, nbytes) != CAST(ssize_t, nbytes))
5261b108b8bSchristos r = 1;
5271b108b8bSchristos else {
5281b108b8bSchristos while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
529d0c65b7bSchristos if (swrite(tfd, buf, CAST(size_t, r)) != r)
5301b108b8bSchristos break;
5311b108b8bSchristos }
5321b108b8bSchristos
5331b108b8bSchristos switch (r) {
5341b108b8bSchristos case -1:
5351b108b8bSchristos file_error(ms, errno, "error copying from pipe to temp file");
5361b108b8bSchristos return -1;
5371b108b8bSchristos case 0:
5381b108b8bSchristos break;
5391b108b8bSchristos default:
5401b108b8bSchristos file_error(ms, errno, "error while writing to temp file");
5411b108b8bSchristos return -1;
5421b108b8bSchristos }
5431b108b8bSchristos
5441b108b8bSchristos /*
5451b108b8bSchristos * We duplicate the file descriptor, because fclose on a
5461b108b8bSchristos * tmpfile will delete the file, but any open descriptors
5471b108b8bSchristos * can still access the phantom inode.
5481b108b8bSchristos */
5491b108b8bSchristos if ((fd = dup2(tfd, fd)) == -1) {
5501b108b8bSchristos file_error(ms, errno, "could not dup descriptor for temp file");
5511b108b8bSchristos return -1;
5521b108b8bSchristos }
5531b108b8bSchristos (void)close(tfd);
554d0c65b7bSchristos if (lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1)) {
5551b108b8bSchristos file_badseek(ms);
5561b108b8bSchristos return -1;
5571b108b8bSchristos }
5581b108b8bSchristos return fd;
5591b108b8bSchristos }
5602344ff98Schristos #if HAVE_FORK
5611b108b8bSchristos #ifdef BUILTIN_DECOMPRESS
5621b108b8bSchristos
5631b108b8bSchristos #define FHCRC (1 << 1)
5641b108b8bSchristos #define FEXTRA (1 << 2)
5651b108b8bSchristos #define FNAME (1 << 3)
5661b108b8bSchristos #define FCOMMENT (1 << 4)
5671b108b8bSchristos
56874db5203Schristos
569*ddb17682Schristos file_private int
uncompressgzipped(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)57074db5203Schristos uncompressgzipped(const unsigned char *old, unsigned char **newch,
5711d4cb158Schristos size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
5721b108b8bSchristos {
5731d4cb158Schristos unsigned char flg;
5741b108b8bSchristos size_t data_start = 10;
5751b108b8bSchristos
5761d4cb158Schristos if (*n < 4) {
5771d4cb158Schristos goto err;
5781d4cb158Schristos }
5791d4cb158Schristos
5801d4cb158Schristos flg = old[3];
5811d4cb158Schristos
5821b108b8bSchristos if (flg & FEXTRA) {
58374db5203Schristos if (data_start + 1 >= *n)
58474db5203Schristos goto err;
5851b108b8bSchristos data_start += 2 + old[data_start] + old[data_start + 1] * 256;
5861b108b8bSchristos }
5871b108b8bSchristos if (flg & FNAME) {
58874db5203Schristos while(data_start < *n && old[data_start])
5891b108b8bSchristos data_start++;
5901b108b8bSchristos data_start++;
5911b108b8bSchristos }
5921b108b8bSchristos if (flg & FCOMMENT) {
59374db5203Schristos while(data_start < *n && old[data_start])
5941b108b8bSchristos data_start++;
5951b108b8bSchristos data_start++;
5961b108b8bSchristos }
5971b108b8bSchristos if (flg & FHCRC)
5981b108b8bSchristos data_start += 2;
5991b108b8bSchristos
60074db5203Schristos if (data_start >= *n)
60174db5203Schristos goto err;
60274db5203Schristos
60374db5203Schristos *n -= data_start;
60474db5203Schristos old += data_start;
60574db5203Schristos return uncompresszlib(old, newch, bytes_max, n, 0);
60674db5203Schristos err:
60774db5203Schristos return makeerror(newch, n, "File too short");
6081b108b8bSchristos }
6091b108b8bSchristos
610*ddb17682Schristos file_private int
uncompresszlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int zlib)61174db5203Schristos uncompresszlib(const unsigned char *old, unsigned char **newch,
61274db5203Schristos size_t bytes_max, size_t *n, int zlib)
61374db5203Schristos {
61474db5203Schristos int rc;
61574db5203Schristos z_stream z;
61674db5203Schristos
617*ddb17682Schristos DPRINTF("builtin zlib decompression\n");
61874db5203Schristos z.next_in = CCAST(Bytef *, old);
61974db5203Schristos z.avail_in = CAST(uint32_t, *n);
6201b108b8bSchristos z.next_out = *newch;
62144ddf42aSchristos z.avail_out = CAST(unsigned int, bytes_max);
6221b108b8bSchristos z.zalloc = Z_NULL;
6231b108b8bSchristos z.zfree = Z_NULL;
6241b108b8bSchristos z.opaque = Z_NULL;
6251b108b8bSchristos
6267c25ef23Schristos /* LINTED bug in header macro */
62774db5203Schristos rc = zlib ? inflateInit(&z) : inflateInit2(&z, -15);
62874db5203Schristos if (rc != Z_OK)
62974db5203Schristos goto err;
6301b108b8bSchristos
6311b108b8bSchristos rc = inflate(&z, Z_SYNC_FLUSH);
6321d4cb158Schristos if (rc != Z_OK && rc != Z_STREAM_END) {
6331d4cb158Schristos inflateEnd(&z);
63474db5203Schristos goto err;
6351d4cb158Schristos }
6361b108b8bSchristos
637d0c65b7bSchristos *n = CAST(size_t, z.total_out);
63874db5203Schristos rc = inflateEnd(&z);
63974db5203Schristos if (rc != Z_OK)
64074db5203Schristos goto err;
6411b108b8bSchristos
6421b108b8bSchristos /* let's keep the nul-terminate tradition */
64374db5203Schristos (*newch)[*n] = '\0';
6441b108b8bSchristos
64574db5203Schristos return OKDATA;
64674db5203Schristos err:
6471d4cb158Schristos return makeerror(newch, n, "%s", z.msg ? z.msg : zError(rc));
6481b108b8bSchristos }
6491b108b8bSchristos #endif
6501b108b8bSchristos
65178a23c3aSchristos #ifdef BUILTIN_BZLIB
652*ddb17682Schristos file_private int
uncompressbzlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)65378a23c3aSchristos uncompressbzlib(const unsigned char *old, unsigned char **newch,
6541d4cb158Schristos size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
65578a23c3aSchristos {
65678a23c3aSchristos int rc;
65778a23c3aSchristos bz_stream bz;
65878a23c3aSchristos
659*ddb17682Schristos DPRINTF("builtin bzlib decompression\n");
66078a23c3aSchristos memset(&bz, 0, sizeof(bz));
66178a23c3aSchristos rc = BZ2_bzDecompressInit(&bz, 0, 0);
66278a23c3aSchristos if (rc != BZ_OK)
66378a23c3aSchristos goto err;
66478a23c3aSchristos
66578a23c3aSchristos bz.next_in = CCAST(char *, RCAST(const char *, old));
66678a23c3aSchristos bz.avail_in = CAST(uint32_t, *n);
66778a23c3aSchristos bz.next_out = RCAST(char *, *newch);
66878a23c3aSchristos bz.avail_out = CAST(unsigned int, bytes_max);
66978a23c3aSchristos
67078a23c3aSchristos rc = BZ2_bzDecompress(&bz);
6711d4cb158Schristos if (rc != BZ_OK && rc != BZ_STREAM_END) {
6721d4cb158Schristos BZ2_bzDecompressEnd(&bz);
67378a23c3aSchristos goto err;
6741d4cb158Schristos }
67578a23c3aSchristos
67678a23c3aSchristos /* Assume byte_max is within 32bit */
67778a23c3aSchristos /* assert(bz.total_out_hi32 == 0); */
67878a23c3aSchristos *n = CAST(size_t, bz.total_out_lo32);
67978a23c3aSchristos rc = BZ2_bzDecompressEnd(&bz);
68078a23c3aSchristos if (rc != BZ_OK)
68178a23c3aSchristos goto err;
68278a23c3aSchristos
68378a23c3aSchristos /* let's keep the nul-terminate tradition */
68478a23c3aSchristos (*newch)[*n] = '\0';
68578a23c3aSchristos
68678a23c3aSchristos return OKDATA;
68778a23c3aSchristos err:
6881d4cb158Schristos return makeerror(newch, n, "bunzip error %d", rc);
68978a23c3aSchristos }
69078a23c3aSchristos #endif
69178a23c3aSchristos
69278a23c3aSchristos #ifdef BUILTIN_XZLIB
693*ddb17682Schristos file_private int
uncompressxzlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)69478a23c3aSchristos uncompressxzlib(const unsigned char *old, unsigned char **newch,
6951d4cb158Schristos size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
69678a23c3aSchristos {
69778a23c3aSchristos int rc;
69878a23c3aSchristos lzma_stream xz;
69978a23c3aSchristos
700*ddb17682Schristos DPRINTF("builtin xzlib decompression\n");
70178a23c3aSchristos memset(&xz, 0, sizeof(xz));
70278a23c3aSchristos rc = lzma_auto_decoder(&xz, UINT64_MAX, 0);
70378a23c3aSchristos if (rc != LZMA_OK)
70478a23c3aSchristos goto err;
70578a23c3aSchristos
70678a23c3aSchristos xz.next_in = CCAST(const uint8_t *, old);
70778a23c3aSchristos xz.avail_in = CAST(uint32_t, *n);
70878a23c3aSchristos xz.next_out = RCAST(uint8_t *, *newch);
70978a23c3aSchristos xz.avail_out = CAST(unsigned int, bytes_max);
71078a23c3aSchristos
71178a23c3aSchristos rc = lzma_code(&xz, LZMA_RUN);
7121d4cb158Schristos if (rc != LZMA_OK && rc != LZMA_STREAM_END) {
7131d4cb158Schristos lzma_end(&xz);
71478a23c3aSchristos goto err;
7151d4cb158Schristos }
71678a23c3aSchristos
71778a23c3aSchristos *n = CAST(size_t, xz.total_out);
71878a23c3aSchristos
71978a23c3aSchristos lzma_end(&xz);
72078a23c3aSchristos
72178a23c3aSchristos /* let's keep the nul-terminate tradition */
72278a23c3aSchristos (*newch)[*n] = '\0';
72378a23c3aSchristos
72478a23c3aSchristos return OKDATA;
72578a23c3aSchristos err:
7261d4cb158Schristos return makeerror(newch, n, "unxz error %d", rc);
7271d4cb158Schristos }
7281d4cb158Schristos #endif
7291d4cb158Schristos
7301d4cb158Schristos #ifdef BUILTIN_ZSTDLIB
731*ddb17682Schristos file_private int
uncompresszstd(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)7321d4cb158Schristos uncompresszstd(const unsigned char *old, unsigned char **newch,
7331d4cb158Schristos size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
7341d4cb158Schristos {
7351d4cb158Schristos size_t rc;
7361d4cb158Schristos ZSTD_DStream *zstd;
7371d4cb158Schristos ZSTD_inBuffer in;
7381d4cb158Schristos ZSTD_outBuffer out;
7391d4cb158Schristos
740*ddb17682Schristos DPRINTF("builtin zstd decompression\n");
7411d4cb158Schristos if ((zstd = ZSTD_createDStream()) == NULL) {
7421d4cb158Schristos return makeerror(newch, n, "No ZSTD decompression stream, %s",
7431d4cb158Schristos strerror(errno));
7441d4cb158Schristos }
7451d4cb158Schristos
7461d4cb158Schristos rc = ZSTD_DCtx_reset(zstd, ZSTD_reset_session_only);
7471d4cb158Schristos if (ZSTD_isError(rc))
7481d4cb158Schristos goto err;
7491d4cb158Schristos
7501d4cb158Schristos in.src = CCAST(const void *, old);
7511d4cb158Schristos in.size = *n;
7521d4cb158Schristos in.pos = 0;
7531d4cb158Schristos out.dst = RCAST(void *, *newch);
7541d4cb158Schristos out.size = bytes_max;
7551d4cb158Schristos out.pos = 0;
7561d4cb158Schristos
7571d4cb158Schristos rc = ZSTD_decompressStream(zstd, &out, &in);
7581d4cb158Schristos if (ZSTD_isError(rc))
7591d4cb158Schristos goto err;
7601d4cb158Schristos
7611d4cb158Schristos *n = out.pos;
7621d4cb158Schristos
7631d4cb158Schristos ZSTD_freeDStream(zstd);
7641d4cb158Schristos
7651d4cb158Schristos /* let's keep the nul-terminate tradition */
7661d4cb158Schristos (*newch)[*n] = '\0';
7671d4cb158Schristos
7681d4cb158Schristos return OKDATA;
7691d4cb158Schristos err:
7701d4cb158Schristos ZSTD_freeDStream(zstd);
7711d4cb158Schristos return makeerror(newch, n, "zstd error %d", ZSTD_getErrorCode(rc));
7721d4cb158Schristos }
7731d4cb158Schristos #endif
7741d4cb158Schristos
7751d4cb158Schristos #ifdef BUILTIN_LZLIB
776*ddb17682Schristos file_private int
uncompresslzlib(const unsigned char * old,unsigned char ** newch,size_t bytes_max,size_t * n,int extra)7771d4cb158Schristos uncompresslzlib(const unsigned char *old, unsigned char **newch,
7781d4cb158Schristos size_t bytes_max, size_t *n, int extra __attribute__((__unused__)))
7791d4cb158Schristos {
7801d4cb158Schristos enum LZ_Errno err;
7811d4cb158Schristos size_t old_remaining = *n;
7821d4cb158Schristos size_t new_remaining = bytes_max;
7831d4cb158Schristos size_t total_read = 0;
7841d4cb158Schristos unsigned char *bufp;
7851d4cb158Schristos struct LZ_Decoder *dec;
7861d4cb158Schristos
7871d4cb158Schristos bufp = *newch;
7881d4cb158Schristos
789*ddb17682Schristos DPRINTF("builtin lzlib decompression\n");
7901d4cb158Schristos dec = LZ_decompress_open();
7911d4cb158Schristos if (!dec) {
7921d4cb158Schristos return makeerror(newch, n, "unable to allocate LZ_Decoder");
7931d4cb158Schristos }
7941d4cb158Schristos if (LZ_decompress_errno(dec) != LZ_ok)
7951d4cb158Schristos goto err;
7961d4cb158Schristos
7971d4cb158Schristos for (;;) {
7981d4cb158Schristos // LZ_decompress_read() stops at member boundaries, so we may
7991d4cb158Schristos // have more than one successful read after writing all data
8001d4cb158Schristos // we have.
8011d4cb158Schristos if (old_remaining > 0) {
8021d4cb158Schristos int wr = LZ_decompress_write(dec, old, old_remaining);
8031d4cb158Schristos if (wr < 0)
8041d4cb158Schristos goto err;
8051d4cb158Schristos old_remaining -= wr;
8061d4cb158Schristos old += wr;
8071d4cb158Schristos }
8081d4cb158Schristos
8091d4cb158Schristos int rd = LZ_decompress_read(dec, bufp, new_remaining);
8101d4cb158Schristos if (rd > 0) {
8111d4cb158Schristos new_remaining -= rd;
8121d4cb158Schristos bufp += rd;
8131d4cb158Schristos total_read += rd;
8141d4cb158Schristos }
8151d4cb158Schristos
8161d4cb158Schristos if (rd < 0 || LZ_decompress_errno(dec) != LZ_ok)
8171d4cb158Schristos goto err;
8181d4cb158Schristos if (new_remaining == 0)
8191d4cb158Schristos break;
8201d4cb158Schristos if (old_remaining == 0 && rd == 0)
8211d4cb158Schristos break;
8221d4cb158Schristos }
8231d4cb158Schristos
8241d4cb158Schristos LZ_decompress_close(dec);
8251d4cb158Schristos *n = total_read;
8261d4cb158Schristos
8271d4cb158Schristos /* let's keep the nul-terminate tradition */
8281d4cb158Schristos *bufp = '\0';
8291d4cb158Schristos
8301d4cb158Schristos return OKDATA;
8311d4cb158Schristos err:
8321d4cb158Schristos err = LZ_decompress_errno(dec);
8331d4cb158Schristos LZ_decompress_close(dec);
8341d4cb158Schristos return makeerror(newch, n, "lzlib error: %s", LZ_strerror(err));
83578a23c3aSchristos }
83678a23c3aSchristos #endif
83778a23c3aSchristos
83878a23c3aSchristos
83974db5203Schristos static int
makeerror(unsigned char ** buf,size_t * len,const char * fmt,...)84074db5203Schristos makeerror(unsigned char **buf, size_t *len, const char *fmt, ...)
8411b108b8bSchristos {
84274db5203Schristos char *msg;
84374db5203Schristos va_list ap;
84474db5203Schristos int rv;
8451b108b8bSchristos
846*ddb17682Schristos DPRINTF("Makeerror %s\n", fmt);
8471d4cb158Schristos free(*buf);
84874db5203Schristos va_start(ap, fmt);
84974db5203Schristos rv = vasprintf(&msg, fmt, ap);
85074db5203Schristos va_end(ap);
85174db5203Schristos if (rv < 0) {
852*ddb17682Schristos DPRINTF("Makeerror failed");
85374db5203Schristos *buf = NULL;
85474db5203Schristos *len = 0;
8551b108b8bSchristos return NODATA;
8561b108b8bSchristos }
857d0c65b7bSchristos *buf = RCAST(unsigned char *, msg);
85874db5203Schristos *len = strlen(msg);
85974db5203Schristos return ERRDATA;
8601b108b8bSchristos }
8611b108b8bSchristos
86274db5203Schristos static void
closefd(int * fd,size_t i)86374db5203Schristos closefd(int *fd, size_t i)
86474db5203Schristos {
86574db5203Schristos if (fd[i] == -1)
86674db5203Schristos return;
86774db5203Schristos (void) close(fd[i]);
86874db5203Schristos fd[i] = -1;
86974db5203Schristos }
8701b108b8bSchristos
87174db5203Schristos static void
closep(int * fd)87274db5203Schristos closep(int *fd)
87374db5203Schristos {
87474db5203Schristos size_t i;
87574db5203Schristos for (i = 0; i < 2; i++)
87674db5203Schristos closefd(fd, i);
87774db5203Schristos }
87874db5203Schristos
8791d4cb158Schristos static void
movedesc(void * v,int i,int fd)8801d4cb158Schristos movedesc(void *v, int i, int fd)
88174db5203Schristos {
882d0c65b7bSchristos if (fd == i)
8831d4cb158Schristos return; /* "no dup was necessary" */
8841d4cb158Schristos #ifdef HAVE_POSIX_SPAWNP
8851d4cb158Schristos posix_spawn_file_actions_t *fa = RCAST(posix_spawn_file_actions_t *, v);
8861d4cb158Schristos posix_spawn_file_actions_adddup2(fa, fd, i);
8871d4cb158Schristos posix_spawn_file_actions_addclose(fa, fd);
8881d4cb158Schristos #else
889d0c65b7bSchristos if (dup2(fd, i) == -1) {
890d0c65b7bSchristos DPRINTF("dup(%d, %d) failed (%s)\n", fd, i, strerror(errno));
8911d4cb158Schristos exit(EXIT_FAILURE);
89274db5203Schristos }
8931d4cb158Schristos close(v ? fd : fd);
8941d4cb158Schristos #endif
8951d4cb158Schristos }
8961d4cb158Schristos
8971d4cb158Schristos static void
closedesc(void * v,int fd)8981d4cb158Schristos closedesc(void *v, int fd)
8991d4cb158Schristos {
9001d4cb158Schristos #ifdef HAVE_POSIX_SPAWNP
9011d4cb158Schristos posix_spawn_file_actions_t *fa = RCAST(posix_spawn_file_actions_t *, v);
9021d4cb158Schristos posix_spawn_file_actions_addclose(fa, fd);
9031d4cb158Schristos #else
9041d4cb158Schristos close(v ? fd : fd);
9051d4cb158Schristos #endif
9061d4cb158Schristos }
9071d4cb158Schristos
9081d4cb158Schristos static void
handledesc(void * v,int fd,int fdp[3][2])9091d4cb158Schristos handledesc(void *v, int fd, int fdp[3][2])
9101d4cb158Schristos {
9111d4cb158Schristos if (fd != -1) {
9121d4cb158Schristos (void) lseek(fd, CAST(off_t, 0), SEEK_SET);
9131d4cb158Schristos movedesc(v, STDIN_FILENO, fd);
9141d4cb158Schristos } else {
9151d4cb158Schristos movedesc(v, STDIN_FILENO, fdp[STDIN_FILENO][0]);
9161d4cb158Schristos if (fdp[STDIN_FILENO][1] > 2)
9171d4cb158Schristos closedesc(v, fdp[STDIN_FILENO][1]);
9181d4cb158Schristos }
9191d4cb158Schristos
9201d4cb158Schristos file_clear_closexec(STDIN_FILENO);
9211d4cb158Schristos
9221d4cb158Schristos ///FIXME: if one of the fdp[i][j] is 0 or 1, this can bomb spectacularly
9231d4cb158Schristos movedesc(v, STDOUT_FILENO, fdp[STDOUT_FILENO][1]);
9241d4cb158Schristos if (fdp[STDOUT_FILENO][0] > 2)
9251d4cb158Schristos closedesc(v, fdp[STDOUT_FILENO][0]);
9261d4cb158Schristos
9271d4cb158Schristos file_clear_closexec(STDOUT_FILENO);
9281d4cb158Schristos
9291d4cb158Schristos movedesc(v, STDERR_FILENO, fdp[STDERR_FILENO][1]);
9301d4cb158Schristos if (fdp[STDERR_FILENO][0] > 2)
9311d4cb158Schristos closedesc(v, fdp[STDERR_FILENO][0]);
9321d4cb158Schristos
9331d4cb158Schristos file_clear_closexec(STDERR_FILENO);
93474db5203Schristos }
9351b108b8bSchristos
936d0c65b7bSchristos static pid_t
writechild(int fd,const void * old,size_t n)937d0c65b7bSchristos writechild(int fd, const void *old, size_t n)
93874db5203Schristos {
939d0c65b7bSchristos pid_t pid;
94074db5203Schristos
9411b108b8bSchristos /*
9421b108b8bSchristos * fork again, to avoid blocking because both
9431b108b8bSchristos * pipes filled
9441b108b8bSchristos */
945d0c65b7bSchristos pid = fork();
946d0c65b7bSchristos if (pid == -1) {
947d0c65b7bSchristos DPRINTF("Fork failed (%s)\n", strerror(errno));
9481d4cb158Schristos return -1;
949d0c65b7bSchristos }
950d0c65b7bSchristos if (pid == 0) {
951d0c65b7bSchristos /* child */
952d0c65b7bSchristos if (swrite(fd, old, n) != CAST(ssize_t, n)) {
95374db5203Schristos DPRINTF("Write failed (%s)\n", strerror(errno));
9541d4cb158Schristos exit(EXIT_FAILURE);
9551b108b8bSchristos }
9561d4cb158Schristos exit(EXIT_SUCCESS);
957fa9ee498Schristos }
958d0c65b7bSchristos /* parent */
959d0c65b7bSchristos return pid;
9601b108b8bSchristos }
9611b108b8bSchristos
96274db5203Schristos static ssize_t
filter_error(unsigned char * ubuf,ssize_t n)96374db5203Schristos filter_error(unsigned char *ubuf, ssize_t n)
96474db5203Schristos {
96574db5203Schristos char *p;
96674db5203Schristos char *buf;
967fa9ee498Schristos
96874db5203Schristos ubuf[n] = '\0';
969d0c65b7bSchristos buf = RCAST(char *, ubuf);
970d0c65b7bSchristos while (isspace(CAST(unsigned char, *buf)))
97174db5203Schristos buf++;
97274db5203Schristos DPRINTF("Filter error[[[%s]]]\n", buf);
973d0c65b7bSchristos if ((p = strchr(CAST(char *, buf), '\n')) != NULL)
97474db5203Schristos *p = '\0';
975d0c65b7bSchristos if ((p = strchr(CAST(char *, buf), ';')) != NULL)
97674db5203Schristos *p = '\0';
977d0c65b7bSchristos if ((p = strrchr(CAST(char *, buf), ':')) != NULL) {
97874db5203Schristos ++p;
979d0c65b7bSchristos while (isspace(CAST(unsigned char, *p)))
98074db5203Schristos p++;
98174db5203Schristos n = strlen(p);
98244ddf42aSchristos memmove(ubuf, p, CAST(size_t, n + 1));
98374db5203Schristos }
98474db5203Schristos DPRINTF("Filter error after[[[%s]]]\n", (char *)ubuf);
98574db5203Schristos if (islower(*ubuf))
98674db5203Schristos *ubuf = toupper(*ubuf);
9871b108b8bSchristos return n;
9881b108b8bSchristos }
98974db5203Schristos
990*ddb17682Schristos file_private const char *
methodname(size_t method)99174db5203Schristos methodname(size_t method)
99274db5203Schristos {
99378a23c3aSchristos switch (method) {
99474db5203Schristos #ifdef BUILTIN_DECOMPRESS
99578a23c3aSchristos case METH_FROZEN:
99678a23c3aSchristos case METH_ZLIB:
99774db5203Schristos return "zlib";
99874db5203Schristos #endif
99978a23c3aSchristos #ifdef BUILTIN_BZLIB
100078a23c3aSchristos case METH_BZIP:
100178a23c3aSchristos return "bzlib";
100278a23c3aSchristos #endif
100378a23c3aSchristos #ifdef BUILTIN_XZLIB
100478a23c3aSchristos case METH_XZ:
100578a23c3aSchristos case METH_LZMA:
100678a23c3aSchristos return "xzlib";
100778a23c3aSchristos #endif
10081d4cb158Schristos #ifdef BUILTIN_ZSTDLIB
10091d4cb158Schristos case METH_ZSTD:
10101d4cb158Schristos return "zstd";
10111d4cb158Schristos #endif
10121d4cb158Schristos #ifdef BUILTIN_LZLIB
10131d4cb158Schristos case METH_LZIP:
10141d4cb158Schristos return "lzlib";
10151d4cb158Schristos #endif
101678a23c3aSchristos default:
101774db5203Schristos return compr[method].argv[0];
101874db5203Schristos }
101978a23c3aSchristos }
102074db5203Schristos
1021*ddb17682Schristos file_private int (*
getdecompressor(size_t method)1022*ddb17682Schristos getdecompressor(size_t method))(const unsigned char *, unsigned char **, size_t,
10231d4cb158Schristos size_t *, int)
10241d4cb158Schristos {
10251d4cb158Schristos switch (method) {
10261d4cb158Schristos #ifdef BUILTIN_DECOMPRESS
10271d4cb158Schristos case METH_FROZEN:
10281d4cb158Schristos return uncompressgzipped;
10291d4cb158Schristos case METH_ZLIB:
10301d4cb158Schristos return uncompresszlib;
10311d4cb158Schristos #endif
10321d4cb158Schristos #ifdef BUILTIN_BZLIB
10331d4cb158Schristos case METH_BZIP:
10341d4cb158Schristos return uncompressbzlib;
10351d4cb158Schristos #endif
10361d4cb158Schristos #ifdef BUILTIN_XZLIB
10371d4cb158Schristos case METH_XZ:
10381d4cb158Schristos case METH_LZMA:
10391d4cb158Schristos return uncompressxzlib;
10401d4cb158Schristos #endif
10411d4cb158Schristos #ifdef BUILTIN_ZSTDLIB
10421d4cb158Schristos case METH_ZSTD:
10431d4cb158Schristos return uncompresszstd;
10441d4cb158Schristos #endif
10451d4cb158Schristos #ifdef BUILTIN_LZLIB
10461d4cb158Schristos case METH_LZIP:
10471d4cb158Schristos return uncompresslzlib;
10481d4cb158Schristos #endif
10491d4cb158Schristos default:
10501d4cb158Schristos return NULL;
10511d4cb158Schristos }
10521d4cb158Schristos }
10531d4cb158Schristos
1054*ddb17682Schristos 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)10551d4cb158Schristos uncompressbuf(int fd, size_t bytes_max, size_t method, int nofork,
10561d4cb158Schristos const unsigned char *old, unsigned char **newch, size_t* n)
105774db5203Schristos {
105874db5203Schristos int fdp[3][2];
1059d0c65b7bSchristos int status, rv, w;
1060d0c65b7bSchristos pid_t pid;
1061d0c65b7bSchristos pid_t writepid = -1;
106274db5203Schristos size_t i;
1063*ddb17682Schristos ssize_t r, re;
10641d4cb158Schristos char *const *args;
10651d4cb158Schristos #ifdef HAVE_POSIX_SPAWNP
10661d4cb158Schristos posix_spawn_file_actions_t fa;
10671d4cb158Schristos #endif
10681d4cb158Schristos int (*decompress)(const unsigned char *, unsigned char **,
10691d4cb158Schristos size_t, size_t *, int) = getdecompressor(method);
107074db5203Schristos
10711d4cb158Schristos *newch = CAST(unsigned char *, malloc(bytes_max + 1));
10721d4cb158Schristos if (*newch == NULL)
10731d4cb158Schristos return makeerror(newch, n, "No buffer, %s", strerror(errno));
10741d4cb158Schristos
10751d4cb158Schristos if (decompress) {
10761d4cb158Schristos if (nofork) {
10771d4cb158Schristos return makeerror(newch, n,
10781d4cb158Schristos "Fork is required to uncompress, but disabled");
10791d4cb158Schristos }
10801d4cb158Schristos return (*decompress)(old, newch, bytes_max, n, 1);
108178a23c3aSchristos }
108278a23c3aSchristos
108374db5203Schristos (void)fflush(stdout);
108474db5203Schristos (void)fflush(stderr);
108574db5203Schristos
108674db5203Schristos for (i = 0; i < __arraycount(fdp); i++)
108774db5203Schristos fdp[i][0] = fdp[i][1] = -1;
108874db5203Schristos
1089d87b0039Schristos /*
1090d87b0039Schristos * There are multithreaded users who run magic_file()
1091d87b0039Schristos * from dozens of threads. If two parallel magic_file() calls
1092d87b0039Schristos * analyze two large compressed files, both will spawn
1093d87b0039Schristos * an uncompressing child here, which writes out uncompressed data.
1094d87b0039Schristos * We read some portion, then close the pipe, then waitpid() the child.
1095*ddb17682Schristos * If uncompressed data is larger, child should get EPIPE and exit.
1096d87b0039Schristos * However, with *parallel* calls OTHER child may unintentionally
1097d87b0039Schristos * inherit pipe fds, thus keeping pipe open and making writes in
1098d87b0039Schristos * our child block instead of failing with EPIPE!
1099d87b0039Schristos * (For the bug to occur, two threads must mutually inherit their pipes,
1100d87b0039Schristos * and both must have large outputs. Thus it happens not that often).
1101d87b0039Schristos * To avoid this, be sure to create pipes with O_CLOEXEC.
1102d87b0039Schristos */
1103d87b0039Schristos if ((fd == -1 && file_pipe_closexec(fdp[STDIN_FILENO]) == -1) ||
1104d87b0039Schristos file_pipe_closexec(fdp[STDOUT_FILENO]) == -1 ||
1105d87b0039Schristos file_pipe_closexec(fdp[STDERR_FILENO]) == -1) {
110674db5203Schristos closep(fdp[STDIN_FILENO]);
110774db5203Schristos closep(fdp[STDOUT_FILENO]);
110874db5203Schristos return makeerror(newch, n, "Cannot create pipe, %s",
110974db5203Schristos strerror(errno));
111074db5203Schristos }
111174db5203Schristos
11121d4cb158Schristos args = RCAST(char *const *, RCAST(intptr_t, compr[method].argv));
11131d4cb158Schristos #ifdef HAVE_POSIX_SPAWNP
11141d4cb158Schristos posix_spawn_file_actions_init(&fa);
11151d4cb158Schristos
11161d4cb158Schristos handledesc(&fa, fd, fdp);
11171d4cb158Schristos
1118*ddb17682Schristos DPRINTF("Executing %s\n", compr[method].argv[0]);
11191d4cb158Schristos status = posix_spawnp(&pid, compr[method].argv[0], &fa, NULL,
11201d4cb158Schristos args, NULL);
11211d4cb158Schristos
11221d4cb158Schristos posix_spawn_file_actions_destroy(&fa);
11231d4cb158Schristos
11241d4cb158Schristos if (status == -1) {
11251d4cb158Schristos return makeerror(newch, n, "Cannot posix_spawn `%s', %s",
11261d4cb158Schristos compr[method].argv[0], strerror(errno));
11271d4cb158Schristos }
11281d4cb158Schristos #else
1129d0c65b7bSchristos /* For processes with large mapped virtual sizes, vfork
1130d0c65b7bSchristos * may be _much_ faster (10-100 times) than fork.
1131d0c65b7bSchristos */
1132d0c65b7bSchristos pid = vfork();
1133d0c65b7bSchristos if (pid == -1) {
1134d0c65b7bSchristos return makeerror(newch, n, "Cannot vfork, %s",
1135d0c65b7bSchristos strerror(errno));
1136d0c65b7bSchristos }
1137d0c65b7bSchristos if (pid == 0) {
1138d0c65b7bSchristos /* child */
1139d0c65b7bSchristos /* Note: we are after vfork, do not modify memory
1140d0c65b7bSchristos * in a way which confuses parent. In particular,
1141d0c65b7bSchristos * do not modify fdp[i][j].
1142d0c65b7bSchristos */
11431d4cb158Schristos handledesc(NULL, fd, fdp);
1144*ddb17682Schristos DPRINTF("Executing %s\n", compr[method].argv[0]);
1145d87b0039Schristos
11461d4cb158Schristos (void)execvp(compr[method].argv[0], args);
114774db5203Schristos dprintf(STDERR_FILENO, "exec `%s' failed, %s",
114874db5203Schristos compr[method].argv[0], strerror(errno));
11491d4cb158Schristos _exit(EXIT_FAILURE); /* _exit(), not exit(), because of vfork */
1150d0c65b7bSchristos }
11511d4cb158Schristos #endif
1152d0c65b7bSchristos /* parent */
1153d0c65b7bSchristos /* Close write sides of child stdout/err pipes */
115474db5203Schristos for (i = 1; i < __arraycount(fdp); i++)
115574db5203Schristos closefd(fdp[i], 1);
1156d0c65b7bSchristos /* Write the buffer data to child stdin, if we don't have fd */
1157d0c65b7bSchristos if (fd == -1) {
1158d0c65b7bSchristos closefd(fdp[STDIN_FILENO], 0);
1159d0c65b7bSchristos writepid = writechild(fdp[STDIN_FILENO][1], old, *n);
11601d4cb158Schristos if (writepid == (pid_t)-1) {
11611d4cb158Schristos rv = makeerror(newch, n, "Write to child failed, %s",
11621d4cb158Schristos strerror(errno));
1163*ddb17682Schristos DPRINTF("Write to child failed\n");
11641d4cb158Schristos goto err;
11651d4cb158Schristos }
1166d0c65b7bSchristos closefd(fdp[STDIN_FILENO], 1);
1167d0c65b7bSchristos }
116874db5203Schristos
116974db5203Schristos rv = OKDATA;
1170d0c65b7bSchristos r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0);
1171*ddb17682Schristos DPRINTF("read got %zd\n", r);
11721d4cb158Schristos if (r < 0) {
117374db5203Schristos rv = ERRDATA;
11741d4cb158Schristos DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0],
11751d4cb158Schristos strerror(errno));
11761d4cb158Schristos goto err;
1177*ddb17682Schristos }
1178*ddb17682Schristos if (CAST(size_t, r) == bytes_max) {
1179*ddb17682Schristos /*
1180*ddb17682Schristos * close fd so that the child exits with sigpipe and ignore
1181*ddb17682Schristos * errors, otherwise we risk the child blocking and never
1182*ddb17682Schristos * exiting.
1183*ddb17682Schristos */
1184*ddb17682Schristos DPRINTF("Closing stdout for bytes_max\n");
1185*ddb17682Schristos closefd(fdp[STDOUT_FILENO], 0);
1186*ddb17682Schristos goto ok;
1187*ddb17682Schristos }
1188*ddb17682Schristos if ((re = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0) {
1189*ddb17682Schristos DPRINTF("Got stuff from stderr %s\n", *newch);
11901d4cb158Schristos rv = ERRDATA;
119174db5203Schristos r = filter_error(*newch, r);
1192d0c65b7bSchristos goto ok;
119374db5203Schristos }
1194*ddb17682Schristos if (re == 0)
11951d4cb158Schristos goto ok;
11961d4cb158Schristos rv = makeerror(newch, n, "Read stderr failed, %s",
119774db5203Schristos strerror(errno));
119874db5203Schristos goto err;
1199d0c65b7bSchristos ok:
120074db5203Schristos *n = r;
120174db5203Schristos /* NUL terminate, as every buffer is handled here. */
120274db5203Schristos (*newch)[*n] = '\0';
120374db5203Schristos err:
120474db5203Schristos closefd(fdp[STDIN_FILENO], 1);
120574db5203Schristos closefd(fdp[STDOUT_FILENO], 0);
120674db5203Schristos closefd(fdp[STDERR_FILENO], 0);
1207d0c65b7bSchristos
1208d0c65b7bSchristos w = waitpid(pid, &status, 0);
1209d0c65b7bSchristos wait_err:
1210d0c65b7bSchristos if (w == -1) {
121174db5203Schristos rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
121274db5203Schristos DPRINTF("Child wait return %#x\n", status);
121374db5203Schristos } else if (!WIFEXITED(status)) {
1214e2725312Schristos DPRINTF("Child not exited (%#x)\n", status);
121574db5203Schristos } else if (WEXITSTATUS(status) != 0) {
1216e2725312Schristos DPRINTF("Child exited (%#x)\n", WEXITSTATUS(status));
121774db5203Schristos }
1218d0c65b7bSchristos if (writepid > 0) {
1219d0c65b7bSchristos /* _After_ we know decompressor has exited, our input writer
1220d0c65b7bSchristos * definitely will exit now (at worst, writing fails in it,
1221d0c65b7bSchristos * since output fd is closed now on the reading size).
1222d0c65b7bSchristos */
1223d0c65b7bSchristos w = waitpid(writepid, &status, 0);
1224d0c65b7bSchristos writepid = -1;
1225d0c65b7bSchristos goto wait_err;
1226d0c65b7bSchristos }
122774db5203Schristos
1228d0c65b7bSchristos closefd(fdp[STDIN_FILENO], 0); //why? it is already closed here!
1229c02f7f97Schristos DPRINTF("Returning %p n=%" SIZE_T_FORMAT "u rv=%d\n", *newch, *n, rv);
123074db5203Schristos
123174db5203Schristos return rv;
12321b108b8bSchristos }
12332344ff98Schristos #endif
1234