Home | History | Annotate | Line # | Download | only in src
      1 /*	$NetBSD: magic.c,v 1.19 2026/06/10 20:54:16 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) Christos Zoulas 2003.
      5  * All Rights Reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice immediately at the beginning of the file, without modification,
     12  *    this list of conditions, and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #ifdef WIN32
     31 #include <windows.h>
     32 #include <shlwapi.h>
     33 #endif
     34 
     35 #include "file.h"
     36 
     37 #ifndef	lint
     38 #if 0
     39 FILE_RCSID("@(#)$File: magic.c,v 1.128 2026/05/09 22:34:30 christos Exp $")
     40 #else
     41 __RCSID("$NetBSD: magic.c,v 1.19 2026/06/10 20:54:16 christos Exp $");
     42 #endif
     43 #endif	/* lint */
     44 
     45 #include "magic.h"
     46 
     47 #include <stdlib.h>
     48 #include <unistd.h>
     49 #include <string.h>
     50 #ifdef QUICK
     51 #include <sys/mman.h>
     52 #endif
     53 #include <limits.h>	/* for PIPE_BUF */
     54 
     55 #if defined(HAVE_UTIMES)
     56 # include <sys/time.h>
     57 #elif defined(HAVE_UTIME)
     58 # if defined(HAVE_SYS_UTIME_H)
     59 #  include <sys/utime.h>
     60 # elif defined(HAVE_UTIME_H)
     61 #  include <utime.h>
     62 # endif
     63 #endif
     64 
     65 #ifdef HAVE_UNISTD_H
     66 #include <unistd.h>	/* for read() */
     67 #endif
     68 
     69 #ifndef PIPE_BUF
     70 /* Get the PIPE_BUF from pathconf */
     71 #ifdef _PC_PIPE_BUF
     72 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
     73 #else
     74 #define PIPE_BUF 512
     75 #endif
     76 #endif
     77 
     78 file_private void close_and_restore(const struct magic_set *, const char *, int,
     79     const struct stat *);
     80 file_private int unreadable_info(struct magic_set *, mode_t, const char *);
     81 file_private const char *get_default_magic(void);
     82 #ifndef COMPILE_ONLY
     83 file_private const char *file_or_fd(struct magic_set *, const char *, int);
     84 #endif
     85 
     86 #ifndef	STDIN_FILENO
     87 #define	STDIN_FILENO	0
     88 #endif
     89 
     90 #ifdef WIN32
     91 /* HINSTANCE of this shared library. Needed for get_default_magic() */
     92 static HINSTANCE _w32_dll_instance = NULL;
     93 
     94 static void
     95 _w32_append_path(char **hmagicpath, const char *fmt, ...)
     96 {
     97 	char *tmppath;
     98         char *newpath;
     99 	va_list ap;
    100 
    101 	va_start(ap, fmt);
    102 	if (vasprintf(&tmppath, fmt, ap) < 0) {
    103 		va_end(ap);
    104 		return;
    105 	}
    106 	va_end(ap);
    107 
    108 	if (access(tmppath, R_OK) == -1)
    109 		goto out;
    110 
    111 	if (*hmagicpath == NULL) {
    112 		*hmagicpath = tmppath;
    113 		return;
    114 	}
    115 
    116 	if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
    117 		goto out;
    118 
    119 	free(*hmagicpath);
    120 	free(tmppath);
    121 	*hmagicpath = newpath;
    122 	return;
    123 out:
    124 	free(tmppath);
    125 }
    126 
    127 static void
    128 _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
    129 {
    130 	static const char *trypaths[] = {
    131 		"%s/share/misc/magic.mgc",
    132 		"%s/magic.mgc",
    133 	};
    134 	LPSTR dllpath;
    135 	size_t sp;
    136 
    137 	dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
    138 
    139 	if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
    140 		goto out;
    141 
    142 	PathRemoveFileSpecA(dllpath);
    143 
    144 	if (module) {
    145 		char exepath[MAX_PATH];
    146 		GetModuleFileNameA(NULL, exepath, MAX_PATH);
    147 		PathRemoveFileSpecA(exepath);
    148 		if (stricmp(exepath, dllpath) == 0)
    149 			goto out;
    150 	}
    151 
    152 	sp = strlen(dllpath);
    153 	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
    154 		_w32_append_path(hmagicpath,
    155 		    "%s/../share/misc/magic.mgc", dllpath);
    156 		goto out;
    157 	}
    158 
    159 	for (sp = 0; sp < __arraycount(trypaths); sp++)
    160 		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
    161 out:
    162 	free(dllpath);
    163 }
    164 
    165 #ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY
    166 /* Placate GCC by offering a sacrificial previous prototype */
    167 BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
    168 
    169 BOOL WINAPI
    170 DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
    171     LPVOID lpvReserved __attribute__((__unused__)))
    172 {
    173 	if (fdwReason == DLL_PROCESS_ATTACH)
    174 		_w32_dll_instance = hinstDLL;
    175 	return 1;
    176 }
    177 #endif
    178 #endif
    179 
    180 file_private const char *
    181 get_default_magic(void)
    182 {
    183 	static const char hmagic[] = "/.magic/magic.mgc";
    184 	static char *default_magic;
    185 	char *home, *hmagicpath, *tmp_magic;
    186 
    187 #ifndef WIN32
    188 	struct stat st;
    189 
    190 	if ((home = getenv("HOME")) == NULL)
    191 		return MAGIC;
    192 
    193 	if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
    194 		return MAGIC;
    195 	if (stat(hmagicpath, &st) == -1) {
    196 		free(hmagicpath);
    197 		if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
    198 			return MAGIC;
    199 		if (stat(hmagicpath, &st) == -1)
    200 			goto out;
    201 		if (S_ISDIR(st.st_mode)) {
    202 			free(hmagicpath);
    203 			if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
    204 				return MAGIC;
    205 			if (access(hmagicpath, R_OK) == -1)
    206 				goto out;
    207 		}
    208 	}
    209 
    210 	if (asprintf(&tmp_magic, "%s%c%s", hmagicpath, PATHSEP, MAGIC) < 0)
    211 		goto out;
    212 	free(hmagicpath);
    213 	hmagicpath = default_magic;
    214 	default_magic = tmp_magic;
    215 	free(hmagicpath);
    216 	return default_magic;
    217 out:
    218 	default_magic = NULL;
    219 	free(hmagicpath);
    220 	return MAGIC;
    221 #else
    222 	hmagicpath = NULL;
    223 
    224 	if (default_magic) {
    225 		free(default_magic);
    226 		default_magic = NULL;
    227 	}
    228 
    229 	/* Before anything else, try to get a magic file from user HOME */
    230 	if ((home = getenv("HOME")) != NULL)
    231 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
    232 
    233 	/* First, try to get a magic file from user-application data */
    234 	if ((home = getenv("LOCALAPPDATA")) != NULL)
    235 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
    236 
    237 	/* Second, try to get a magic file from the user profile data */
    238 	if ((home = getenv("USERPROFILE")) != NULL)
    239 		_w32_append_path(&hmagicpath,
    240 		    "%s/Local Settings/Application Data%s", home, hmagic);
    241 
    242 	/* Third, try to get a magic file from Common Files */
    243 	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
    244 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
    245 
    246 	/* Fourth, try to get magic file relative to exe location */
    247         _w32_get_magic_relative_to(&hmagicpath, NULL);
    248 
    249 	/* Fifth, try to get magic file relative to dll location */
    250         _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
    251 
    252 	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
    253 	tmp_magic = default_magic;
    254 	default_magic = hmagicpath;
    255 	free(tmp_magic);
    256 	return default_magic;
    257 #endif
    258 }
    259 
    260 file_public const char *
    261 magic_getpath(const char *magicfile, int action)
    262 {
    263 	if (magicfile != NULL)
    264 		return magicfile;
    265 
    266 	magicfile = getenv("MAGIC");
    267 	if (magicfile != NULL)
    268 		return magicfile;
    269 
    270 	return action == FILE_LOAD ? get_default_magic() : MAGIC;
    271 }
    272 
    273 file_public struct magic_set *
    274 magic_open(int flags)
    275 {
    276 	return file_ms_alloc(flags);
    277 }
    278 
    279 file_private int
    280 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
    281 {
    282 	if (file) {
    283 		/* We cannot open it, but we were able to stat it. */
    284 		if (access(file, W_OK) == 0)
    285 			if (file_printf(ms, "writable, ") == -1)
    286 				return -1;
    287 #ifndef WIN32
    288 		if (access(file, X_OK) == 0)
    289 			if (file_printf(ms, "executable, ") == -1)
    290 				return -1;
    291 #else
    292 		/* X_OK doesn't work well on MS-Windows */
    293 		{
    294 			const char *p = strrchr(file, '.');
    295 			if (p && (stricmp(p, ".exe")
    296 				  || stricmp(p, ".dll")
    297 				  || stricmp(p, ".bat")
    298 				  || stricmp(p, ".cmd")))
    299 				if (file_printf(ms, "writable, ") == -1)
    300 					return -1;
    301 		}
    302 #endif
    303 	}
    304 	if (S_ISREG(md))
    305 		if (file_printf(ms, "regular file, ") == -1)
    306 			return -1;
    307 	if (file_printf(ms, "no read permission") == -1)
    308 		return -1;
    309 	return 0;
    310 }
    311 
    312 file_public void
    313 magic_close(struct magic_set *ms)
    314 {
    315 	if (ms == NULL)
    316 		return;
    317 	file_ms_free(ms);
    318 }
    319 
    320 /*
    321  * load a magic file
    322  */
    323 file_public int
    324 magic_load(struct magic_set *ms, const char *magicfile)
    325 {
    326 	if (ms == NULL)
    327 		return -1;
    328 	return file_apprentice(ms, magicfile, FILE_LOAD);
    329 }
    330 
    331 #ifndef COMPILE_ONLY
    332 /*
    333  * Install a set of compiled magic buffers.
    334  */
    335 file_public int
    336 magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
    337     size_t nbufs)
    338 {
    339 	if (ms == NULL)
    340 		return -1;
    341 	return buffer_apprentice(ms, RCAST(struct magic **, bufs),
    342 	    sizes, nbufs);
    343 }
    344 #endif
    345 
    346 file_public int
    347 magic_compile(struct magic_set *ms, const char *magicfile)
    348 {
    349 	if (ms == NULL)
    350 		return -1;
    351 	return file_apprentice(ms, magicfile, FILE_COMPILE);
    352 }
    353 
    354 file_public int
    355 magic_check(struct magic_set *ms, const char *magicfile)
    356 {
    357 	if (ms == NULL)
    358 		return -1;
    359 	return file_apprentice(ms, magicfile, FILE_CHECK);
    360 }
    361 
    362 file_public int
    363 magic_list(struct magic_set *ms, const char *magicfile)
    364 {
    365 	if (ms == NULL)
    366 		return -1;
    367 	return file_apprentice(ms, magicfile, FILE_LIST);
    368 }
    369 
    370 file_private void
    371 close_and_restore(const struct magic_set *ms, const char *name, int fd,
    372     const struct stat *sb)
    373 {
    374 	if (fd == STDIN_FILENO || name == NULL)
    375 		return;
    376 	(void) close(fd);
    377 
    378 	if (sb == NULL || (ms->flags & MAGIC_PRESERVE_ATIME) == 0)
    379 		return;
    380 
    381 	/*
    382 	 * Try to restore access, modification times if read it.
    383 	 * This is really *bad* because it will modify the status
    384 	 * time of the file... And of course this will affect
    385 	 * backup programs
    386 	 */
    387 #ifdef HAVE_UTIMES
    388 	struct timeval  utsbuf[2];
    389 	(void)memset(utsbuf, 0, sizeof(utsbuf));
    390 	utsbuf[0].tv_sec = sb->st_atime;
    391 	utsbuf[1].tv_sec = sb->st_mtime;
    392 
    393 	(void) utimes(name, utsbuf); /* don't care if loses */
    394 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
    395 	struct utimbuf  utbuf;
    396 
    397 	(void)memset(&utbuf, 0, sizeof(utbuf));
    398 	utbuf.actime = sb->st_atime;
    399 	utbuf.modtime = sb->st_mtime;
    400 	(void) utime(name, &utbuf); /* don't care if loses */
    401 #endif
    402 }
    403 
    404 #ifndef COMPILE_ONLY
    405 
    406 /*
    407  * find type of descriptor
    408  */
    409 file_public const char *
    410 magic_descriptor(struct magic_set *ms, int fd)
    411 {
    412 	if (ms == NULL)
    413 		return NULL;
    414 	return file_or_fd(ms, NULL, fd);
    415 }
    416 
    417 /*
    418  * find type of named file
    419  */
    420 file_public const char *
    421 magic_file(struct magic_set *ms, const char *inname)
    422 {
    423 	if (ms == NULL)
    424 		return NULL;
    425 	return file_or_fd(ms, inname, STDIN_FILENO);
    426 }
    427 
    428 file_private const char *
    429 file_or_fd(struct magic_set *ms, const char *inname, int fd)
    430 {
    431 	int	rv = -1;
    432 	unsigned char *buf;
    433 	struct stat	sb;
    434 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
    435 	int	ispipe = 0;
    436 	int	okstat = 0;
    437 	off_t	pos = CAST(off_t, -1);
    438 
    439 	if (file_reset(ms, 1) == -1)
    440 		goto out;
    441 
    442 	/*
    443 	 * one extra for terminating '\0', and
    444 	 * some overlapping space for matches near EOF
    445 	 */
    446 #define SLOP (1 + sizeof(union VALUETYPE))
    447 	if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
    448 		return NULL;
    449 
    450 	switch (file_fsmagic(ms, inname, &sb)) {
    451 	case -1:		/* error */
    452 		goto done;
    453 	case 0:			/* nothing found */
    454 		break;
    455 	default:		/* matched it and printed type */
    456 		rv = 0;
    457 		goto done;
    458 	}
    459 
    460 #ifdef WIN32
    461 	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
    462 	if (fd == STDIN_FILENO)
    463 		_setmode(STDIN_FILENO, O_BINARY);
    464 #endif
    465 	if (inname != NULL) {
    466 		int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC;
    467 		errno = 0;
    468 		if ((fd = open(inname, flags)) < 0) {
    469 			okstat = stat(inname, &sb) == 0;
    470 #ifdef WIN32
    471 			/*
    472 			 * Can't stat, can't open.  It may have been opened in
    473 			 * fsmagic, so if the user doesn't have read permission,
    474 			 * allow it to say so; otherwise an error was probably
    475 			 * displayed in fsmagic.
    476 			 */
    477 			if (!okstat && errno == EACCES) {
    478 				sb.st_mode = S_IFBLK;
    479 				okstat = 1;
    480 			}
    481 #endif
    482 			if (okstat &&
    483 			    unreadable_info(ms, sb.st_mode, inname) == -1)
    484 				goto done;
    485 			rv = 0;
    486 			goto done;
    487 		}
    488 #if O_CLOEXEC == 0 && defined(F_SETFD)
    489 		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    490 #endif
    491 	}
    492 
    493 	if (fd != -1) {
    494 		okstat = fstat(fd, &sb) == 0;
    495 		if (okstat && S_ISFIFO(sb.st_mode))
    496 			ispipe = 1;
    497 		if (inname == NULL)
    498 			pos = lseek(fd, CAST(off_t, 0), SEEK_CUR);
    499 	}
    500 
    501 	/*
    502 	 * try looking at the first ms->bytes_max bytes
    503 	 */
    504 	if (ispipe) {
    505 		if (fd != -1) {
    506 			ssize_t r = 0;
    507 
    508 			while ((r = sread(fd, RCAST(void *, &buf[nbytes]),
    509 			    CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) {
    510 				nbytes += r;
    511 				if (r < PIPE_BUF) break;
    512 			}
    513 		}
    514 
    515 		if (nbytes == 0 && inname) {
    516 			/* We can not read it, but we were able to stat it. */
    517 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
    518 				goto done;
    519 			rv = 0;
    520 			goto done;
    521 		}
    522 
    523 	} else if (fd != -1) {
    524 		/* Windows refuses to read from a big console buffer. */
    525 		size_t howmany =
    526 #ifdef WIN32
    527 		    _isatty(fd) ? 8 * 1024 :
    528 #endif
    529 		    ms->bytes_max;
    530 		if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) {
    531 			if (inname == NULL && fd != STDIN_FILENO)
    532 				file_error(ms, errno, "cannot read fd %d", fd);
    533 			else
    534 				file_error(ms, errno, "cannot read `%s'",
    535 				    inname == NULL ? "/dev/stdin" : inname);
    536 			goto done;
    537 		}
    538 	}
    539 
    540 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
    541 	if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf,
    542 	    CAST(size_t, nbytes)) == -1)
    543 		goto done;
    544 	rv = 0;
    545 done:
    546 	free(buf);
    547 	if (fd != -1) {
    548 		if (pos != CAST(off_t, -1))
    549 			(void)lseek(fd, pos, SEEK_SET);
    550 		close_and_restore(ms, inname, fd, okstat ? &sb : NULL);
    551 	}
    552 out:
    553 	return rv == 0 ? file_getbuffer(ms) : NULL;
    554 }
    555 
    556 
    557 file_public const char *
    558 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
    559 {
    560 	if (ms == NULL)
    561 		return NULL;
    562 	if (file_reset(ms, 1) == -1)
    563 		return NULL;
    564 	/*
    565 	 * The main work is done here!
    566 	 * We have the file name and/or the data buffer to be identified.
    567 	 */
    568 	if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) {
    569 		return NULL;
    570 	}
    571 	return file_getbuffer(ms);
    572 }
    573 #endif
    574 
    575 file_public const char *
    576 magic_error(struct magic_set *ms)
    577 {
    578 	if (ms == NULL)
    579 		return "Magic database is not open";
    580 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
    581 }
    582 
    583 file_public int
    584 magic_errno(struct magic_set *ms)
    585 {
    586 	if (ms == NULL)
    587 		return EINVAL;
    588 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
    589 }
    590 
    591 file_public int
    592 magic_getflags(struct magic_set *ms)
    593 {
    594 	if (ms == NULL)
    595 		return -1;
    596 
    597 	return ms->flags;
    598 }
    599 
    600 file_public int
    601 magic_setflags(struct magic_set *ms, int flags)
    602 {
    603 	if (ms == NULL)
    604 		return -1;
    605 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
    606 	if (flags & MAGIC_PRESERVE_ATIME)
    607 		return -1;
    608 #endif
    609 	ms->flags = flags;
    610 	return 0;
    611 }
    612 
    613 file_public int
    614 magic_version(void)
    615 {
    616 	return MAGIC_VERSION;
    617 }
    618 
    619 file_public ssize_t
    620 magic_getmaxparam(int param)
    621 {
    622 	switch (param) {
    623 	case MAGIC_PARAM_INDIR_MAX:
    624 	case MAGIC_PARAM_NAME_MAX:
    625 	case MAGIC_PARAM_ELF_PHNUM_MAX:
    626 	case MAGIC_PARAM_ELF_SHNUM_MAX:
    627 	case MAGIC_PARAM_ELF_SHSIZE_MAX:
    628 	case MAGIC_PARAM_ELF_NOTES_MAX:
    629 	case MAGIC_PARAM_REGEX_MAX:
    630 		return 0xffff;
    631 	case MAGIC_PARAM_BYTES_MAX:
    632 	case MAGIC_PARAM_ENCODING_MAX:
    633 	case MAGIC_PARAM_MAGWARN_MAX:
    634 		return 0x7fffffff;
    635 	default:
    636 		errno = EINVAL;
    637 		return -1;
    638 	}
    639 }
    640 
    641 file_public int
    642 magic_setparam(struct magic_set *ms, int param, const void *val)
    643 {
    644 	if (ms == NULL || val == NULL) {
    645 		errno = EFAULT;
    646 		return -1;
    647 	}
    648 	const size_t v = *CAST(const size_t *, val);
    649 	switch (param) {
    650 	case MAGIC_PARAM_INDIR_MAX:
    651 		ms->indir_max = CAST(uint16_t, v);
    652 		return 0;
    653 	case MAGIC_PARAM_NAME_MAX:
    654 		ms->name_max = CAST(uint16_t, v);
    655 		return 0;
    656 	case MAGIC_PARAM_ELF_PHNUM_MAX:
    657 		ms->elf_phnum_max = CAST(uint16_t, v);
    658 		return 0;
    659 	case MAGIC_PARAM_ELF_SHNUM_MAX:
    660 		ms->elf_shnum_max = CAST(uint16_t, v);
    661 		return 0;
    662 	case MAGIC_PARAM_ELF_SHSIZE_MAX:
    663 		ms->elf_shsize_max = v;
    664 		return 0;
    665 	case MAGIC_PARAM_ELF_NOTES_MAX:
    666 		ms->elf_notes_max = CAST(uint16_t, v);
    667 		return 0;
    668 	case MAGIC_PARAM_REGEX_MAX:
    669 		ms->regex_max = CAST(uint16_t, v);
    670 		return 0;
    671 	case MAGIC_PARAM_BYTES_MAX:
    672 		ms->bytes_max = v;
    673 		return 0;
    674 	case MAGIC_PARAM_ENCODING_MAX:
    675 		ms->encoding_max = v;
    676 		return 0;
    677 	case MAGIC_PARAM_MAGWARN_MAX:
    678 		ms->magwarn_max = v;
    679 		return 0;
    680 	default:
    681 		errno = EINVAL;
    682 		return -1;
    683 	}
    684 }
    685 
    686 file_public int
    687 magic_getparam(struct magic_set *ms, int param, void *val)
    688 {
    689 	if (ms == NULL || val == NULL) {
    690 		errno = EFAULT;
    691 		return -1;
    692 	}
    693 	switch (param) {
    694 	case MAGIC_PARAM_INDIR_MAX:
    695 		*CAST(size_t *, val) = ms->indir_max;
    696 		return 0;
    697 	case MAGIC_PARAM_NAME_MAX:
    698 		*CAST(size_t *, val) = ms->name_max;
    699 		return 0;
    700 	case MAGIC_PARAM_ELF_PHNUM_MAX:
    701 		*CAST(size_t *, val) = ms->elf_phnum_max;
    702 		return 0;
    703 	case MAGIC_PARAM_ELF_SHNUM_MAX:
    704 		*CAST(size_t *, val) = ms->elf_shnum_max;
    705 		return 0;
    706 	case MAGIC_PARAM_ELF_SHSIZE_MAX:
    707 		*CAST(size_t *, val) = ms->elf_shsize_max;
    708 		return 0;
    709 	case MAGIC_PARAM_ELF_NOTES_MAX:
    710 		*CAST(size_t *, val) = ms->elf_notes_max;
    711 		return 0;
    712 	case MAGIC_PARAM_REGEX_MAX:
    713 		*CAST(size_t *, val) = ms->regex_max;
    714 		return 0;
    715 	case MAGIC_PARAM_BYTES_MAX:
    716 		*CAST(size_t *, val) = ms->bytes_max;
    717 		return 0;
    718 	case MAGIC_PARAM_ENCODING_MAX:
    719 		*CAST(size_t *, val) = ms->encoding_max;
    720 		return 0;
    721 	case MAGIC_PARAM_MAGWARN_MAX:
    722 		*CAST(size_t *, val) = ms->magwarn_max;
    723 		return 0;
    724 	default:
    725 		errno = EINVAL;
    726 		return -1;
    727 	}
    728 }
    729