xref: /freebsd/contrib/file/src/magic.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1b6cee71dSXin LI /*
2b6cee71dSXin LI  * Copyright (c) Christos Zoulas 2003.
3b6cee71dSXin LI  * All Rights Reserved.
4b6cee71dSXin LI  *
5b6cee71dSXin LI  * Redistribution and use in source and binary forms, with or without
6b6cee71dSXin LI  * modification, are permitted provided that the following conditions
7b6cee71dSXin LI  * are met:
8b6cee71dSXin LI  * 1. Redistributions of source code must retain the above copyright
9b6cee71dSXin LI  *    notice immediately at the beginning of the file, without modification,
10b6cee71dSXin LI  *    this list of conditions, and the following disclaimer.
11b6cee71dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer in the
13b6cee71dSXin LI  *    documentation and/or other materials provided with the distribution.
14b6cee71dSXin LI  *
15b6cee71dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b6cee71dSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b6cee71dSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b6cee71dSXin LI  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19b6cee71dSXin LI  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b6cee71dSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b6cee71dSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b6cee71dSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b6cee71dSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b6cee71dSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b6cee71dSXin LI  * SUCH DAMAGE.
26b6cee71dSXin LI  */
27b6cee71dSXin LI 
28b6cee71dSXin LI #ifdef WIN32
29b6cee71dSXin LI #include <windows.h>
30b6cee71dSXin LI #include <shlwapi.h>
31b6cee71dSXin LI #endif
32b6cee71dSXin LI 
33b6cee71dSXin LI #include "file.h"
34b6cee71dSXin LI 
35b6cee71dSXin LI #ifndef	lint
36*43a5ec4eSXin LI FILE_RCSID("@(#)$File: magic.c,v 1.115 2021/09/20 17:45:41 christos Exp $")
37b6cee71dSXin LI #endif	/* lint */
38b6cee71dSXin LI 
39b6cee71dSXin LI #include "magic.h"
40b6cee71dSXin LI 
41b6cee71dSXin LI #include <stdlib.h>
42b6cee71dSXin LI #include <unistd.h>
43b6cee71dSXin LI #include <string.h>
44b6cee71dSXin LI #ifdef QUICK
45b6cee71dSXin LI #include <sys/mman.h>
46b6cee71dSXin LI #endif
47b6cee71dSXin LI #include <limits.h>	/* for PIPE_BUF */
48b6cee71dSXin LI 
49b6cee71dSXin LI #if defined(HAVE_UTIMES)
50b6cee71dSXin LI # include <sys/time.h>
51b6cee71dSXin LI #elif defined(HAVE_UTIME)
52b6cee71dSXin LI # if defined(HAVE_SYS_UTIME_H)
53b6cee71dSXin LI #  include <sys/utime.h>
54b6cee71dSXin LI # elif defined(HAVE_UTIME_H)
55b6cee71dSXin LI #  include <utime.h>
56b6cee71dSXin LI # endif
57b6cee71dSXin LI #endif
58b6cee71dSXin LI 
59b6cee71dSXin LI #ifdef HAVE_UNISTD_H
60b6cee71dSXin LI #include <unistd.h>	/* for read() */
61b6cee71dSXin LI #endif
62b6cee71dSXin LI 
63b6cee71dSXin LI #ifndef PIPE_BUF
64b6cee71dSXin LI /* Get the PIPE_BUF from pathconf */
65b6cee71dSXin LI #ifdef _PC_PIPE_BUF
66b6cee71dSXin LI #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
67b6cee71dSXin LI #else
68b6cee71dSXin LI #define PIPE_BUF 512
69b6cee71dSXin LI #endif
70b6cee71dSXin LI #endif
71b6cee71dSXin LI 
72b6cee71dSXin LI private void close_and_restore(const struct magic_set *, const char *, int,
73b6cee71dSXin LI     const struct stat *);
74b6cee71dSXin LI private int unreadable_info(struct magic_set *, mode_t, const char *);
75b6cee71dSXin LI private const char* get_default_magic(void);
76b6cee71dSXin LI #ifndef COMPILE_ONLY
77b6cee71dSXin LI private const char *file_or_fd(struct magic_set *, const char *, int);
78b6cee71dSXin LI #endif
79b6cee71dSXin LI 
80b6cee71dSXin LI #ifndef	STDIN_FILENO
81b6cee71dSXin LI #define	STDIN_FILENO	0
82b6cee71dSXin LI #endif
83b6cee71dSXin LI 
845f0216bdSXin LI #ifdef WIN32
855f0216bdSXin LI /* HINSTANCE of this shared library. Needed for get_default_magic() */
865f0216bdSXin LI static HINSTANCE _w32_dll_instance = NULL;
875f0216bdSXin LI 
885f0216bdSXin LI static void
895f0216bdSXin LI _w32_append_path(char **hmagicpath, const char *fmt, ...)
905f0216bdSXin LI {
915f0216bdSXin LI 	char *tmppath;
925f0216bdSXin LI         char *newpath;
935f0216bdSXin LI 	va_list ap;
945f0216bdSXin LI 
955f0216bdSXin LI 	va_start(ap, fmt);
965f0216bdSXin LI 	if (vasprintf(&tmppath, fmt, ap) < 0) {
975f0216bdSXin LI 		va_end(ap);
985f0216bdSXin LI 		return;
995f0216bdSXin LI 	}
1005f0216bdSXin LI 	va_end(ap);
1015f0216bdSXin LI 
1025f0216bdSXin LI 	if (access(tmppath, R_OK) == -1)
1035f0216bdSXin LI 		goto out;
1045f0216bdSXin LI 
1055f0216bdSXin LI 	if (*hmagicpath == NULL) {
1065f0216bdSXin LI 		*hmagicpath = tmppath;
1075f0216bdSXin LI 		return;
1085f0216bdSXin LI 	}
1095f0216bdSXin LI 
1105f0216bdSXin LI 	if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
1115f0216bdSXin LI 		goto out;
1125f0216bdSXin LI 
1135f0216bdSXin LI 	free(*hmagicpath);
1145f0216bdSXin LI 	free(tmppath);
1155f0216bdSXin LI 	*hmagicpath = newpath;
1165f0216bdSXin LI 	return;
1175f0216bdSXin LI out:
1185f0216bdSXin LI 	free(tmppath);
1195f0216bdSXin LI }
1205f0216bdSXin LI 
1215f0216bdSXin LI static void
1225f0216bdSXin LI _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
1235f0216bdSXin LI {
1245f0216bdSXin LI 	static const char *trypaths[] = {
1255f0216bdSXin LI 		"%s/share/misc/magic.mgc",
1265f0216bdSXin LI 		"%s/magic.mgc",
1275f0216bdSXin LI 	};
1285f0216bdSXin LI 	LPSTR dllpath;
1295f0216bdSXin LI 	size_t sp;
1305f0216bdSXin LI 
1315f0216bdSXin LI 	dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
1325f0216bdSXin LI 
1335f0216bdSXin LI 	if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
1345f0216bdSXin LI 		goto out;
1355f0216bdSXin LI 
1365f0216bdSXin LI 	PathRemoveFileSpecA(dllpath);
1375f0216bdSXin LI 
1389ce06829SXin LI 	if (module) {
1399ce06829SXin LI 		char exepath[MAX_PATH];
1409ce06829SXin LI 		GetModuleFileNameA(NULL, exepath, MAX_PATH);
1419ce06829SXin LI 		PathRemoveFileSpecA(exepath);
1429ce06829SXin LI 		if (stricmp(exepath, dllpath) == 0)
1439ce06829SXin LI 			goto out;
1449ce06829SXin LI 	}
1459ce06829SXin LI 
1465f0216bdSXin LI 	sp = strlen(dllpath);
1475f0216bdSXin LI 	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
1485f0216bdSXin LI 		_w32_append_path(hmagicpath,
1495f0216bdSXin LI 		    "%s/../share/misc/magic.mgc", dllpath);
1505f0216bdSXin LI 		goto out;
1515f0216bdSXin LI 	}
1525f0216bdSXin LI 
1535f0216bdSXin LI 	for (sp = 0; sp < __arraycount(trypaths); sp++)
1545f0216bdSXin LI 		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
1555f0216bdSXin LI out:
1565f0216bdSXin LI 	free(dllpath);
1575f0216bdSXin LI }
1585f0216bdSXin LI 
159*43a5ec4eSXin LI #ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY
1605f0216bdSXin LI /* Placate GCC by offering a sacrificial previous prototype */
1615f0216bdSXin LI BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
1625f0216bdSXin LI 
1635f0216bdSXin LI BOOL WINAPI
1645f0216bdSXin LI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
1655f0216bdSXin LI     LPVOID lpvReserved __attribute__((__unused__)))
1665f0216bdSXin LI {
1675f0216bdSXin LI 	if (fdwReason == DLL_PROCESS_ATTACH)
1685f0216bdSXin LI 		_w32_dll_instance = hinstDLL;
16940427ccaSGordon Tetlow 	return 1;
1705f0216bdSXin LI }
1715f0216bdSXin LI #endif
172*43a5ec4eSXin LI #endif
1735f0216bdSXin LI 
174b6cee71dSXin LI private const char *
175b6cee71dSXin LI get_default_magic(void)
176b6cee71dSXin LI {
177b6cee71dSXin LI 	static const char hmagic[] = "/.magic/magic.mgc";
178b6cee71dSXin LI 	static char *default_magic;
179b6cee71dSXin LI 	char *home, *hmagicpath;
180b6cee71dSXin LI 
181b6cee71dSXin LI #ifndef WIN32
182b6cee71dSXin LI 	struct stat st;
183b6cee71dSXin LI 
184b6cee71dSXin LI 	if (default_magic) {
185b6cee71dSXin LI 		free(default_magic);
186b6cee71dSXin LI 		default_magic = NULL;
187b6cee71dSXin LI 	}
188b6cee71dSXin LI 	if ((home = getenv("HOME")) == NULL)
189b6cee71dSXin LI 		return MAGIC;
190b6cee71dSXin LI 
191b6cee71dSXin LI 	if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
192b6cee71dSXin LI 		return MAGIC;
193b6cee71dSXin LI 	if (stat(hmagicpath, &st) == -1) {
194b6cee71dSXin LI 		free(hmagicpath);
195b6cee71dSXin LI 		if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
196b6cee71dSXin LI 			return MAGIC;
197b6cee71dSXin LI 		if (stat(hmagicpath, &st) == -1)
198b6cee71dSXin LI 			goto out;
199b6cee71dSXin LI 		if (S_ISDIR(st.st_mode)) {
200b6cee71dSXin LI 			free(hmagicpath);
201b6cee71dSXin LI 			if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
202b6cee71dSXin LI 				return MAGIC;
203b6cee71dSXin LI 			if (access(hmagicpath, R_OK) == -1)
204b6cee71dSXin LI 				goto out;
205b6cee71dSXin LI 		}
206b6cee71dSXin LI 	}
207b6cee71dSXin LI 
208b6cee71dSXin LI 	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
209b6cee71dSXin LI 		goto out;
210b6cee71dSXin LI 	free(hmagicpath);
211b6cee71dSXin LI 	return default_magic;
212b6cee71dSXin LI out:
213b6cee71dSXin LI 	default_magic = NULL;
214b6cee71dSXin LI 	free(hmagicpath);
215b6cee71dSXin LI 	return MAGIC;
216b6cee71dSXin LI #else
217b6cee71dSXin LI 	hmagicpath = NULL;
218b6cee71dSXin LI 
219b6cee71dSXin LI 	if (default_magic) {
220b6cee71dSXin LI 		free(default_magic);
221b6cee71dSXin LI 		default_magic = NULL;
222b6cee71dSXin LI 	}
223b6cee71dSXin LI 
2245f0216bdSXin LI 	/* First, try to get a magic file from user-application data */
2255f0216bdSXin LI 	if ((home = getenv("LOCALAPPDATA")) != NULL)
2265f0216bdSXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
2275f0216bdSXin LI 
2285f0216bdSXin LI 	/* Second, try to get a magic file from the user profile data */
229b6cee71dSXin LI 	if ((home = getenv("USERPROFILE")) != NULL)
2305f0216bdSXin LI 		_w32_append_path(&hmagicpath,
2315f0216bdSXin LI 		    "%s/Local Settings/Application Data%s", home, hmagic);
232b6cee71dSXin LI 
2335f0216bdSXin LI 	/* Third, try to get a magic file from Common Files */
2345f0216bdSXin LI 	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
2355f0216bdSXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
236b6cee71dSXin LI 
2375f0216bdSXin LI 	/* Fourth, try to get magic file relative to exe location */
2385f0216bdSXin LI         _w32_get_magic_relative_to(&hmagicpath, NULL);
239b6cee71dSXin LI 
2405f0216bdSXin LI 	/* Fifth, try to get magic file relative to dll location */
2415f0216bdSXin LI         _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
242b6cee71dSXin LI 
2435f0216bdSXin LI 	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
244b6cee71dSXin LI 	default_magic = hmagicpath;
245b6cee71dSXin LI 	return default_magic;
246b6cee71dSXin LI #endif
247b6cee71dSXin LI }
248b6cee71dSXin LI 
249b6cee71dSXin LI public const char *
250b6cee71dSXin LI magic_getpath(const char *magicfile, int action)
251b6cee71dSXin LI {
252b6cee71dSXin LI 	if (magicfile != NULL)
253b6cee71dSXin LI 		return magicfile;
254b6cee71dSXin LI 
255b6cee71dSXin LI 	magicfile = getenv("MAGIC");
256b6cee71dSXin LI 	if (magicfile != NULL)
257b6cee71dSXin LI 		return magicfile;
258b6cee71dSXin LI 
259b6cee71dSXin LI 	return action == FILE_LOAD ? get_default_magic() : MAGIC;
260b6cee71dSXin LI }
261b6cee71dSXin LI 
262b6cee71dSXin LI public struct magic_set *
263b6cee71dSXin LI magic_open(int flags)
264b6cee71dSXin LI {
265b6cee71dSXin LI 	return file_ms_alloc(flags);
266b6cee71dSXin LI }
267b6cee71dSXin LI 
268b6cee71dSXin LI private int
269b6cee71dSXin LI unreadable_info(struct magic_set *ms, mode_t md, const char *file)
270b6cee71dSXin LI {
271b6cee71dSXin LI 	if (file) {
272b6cee71dSXin LI 		/* We cannot open it, but we were able to stat it. */
273b6cee71dSXin LI 		if (access(file, W_OK) == 0)
274b6cee71dSXin LI 			if (file_printf(ms, "writable, ") == -1)
275b6cee71dSXin LI 				return -1;
276b6cee71dSXin LI 		if (access(file, X_OK) == 0)
277b6cee71dSXin LI 			if (file_printf(ms, "executable, ") == -1)
278b6cee71dSXin LI 				return -1;
279b6cee71dSXin LI 	}
280b6cee71dSXin LI 	if (S_ISREG(md))
281b6cee71dSXin LI 		if (file_printf(ms, "regular file, ") == -1)
282b6cee71dSXin LI 			return -1;
283b6cee71dSXin LI 	if (file_printf(ms, "no read permission") == -1)
284b6cee71dSXin LI 		return -1;
285b6cee71dSXin LI 	return 0;
286b6cee71dSXin LI }
287b6cee71dSXin LI 
288b6cee71dSXin LI public void
289b6cee71dSXin LI magic_close(struct magic_set *ms)
290b6cee71dSXin LI {
291b6cee71dSXin LI 	if (ms == NULL)
292b6cee71dSXin LI 		return;
293b6cee71dSXin LI 	file_ms_free(ms);
294b6cee71dSXin LI }
295b6cee71dSXin LI 
296b6cee71dSXin LI /*
297b6cee71dSXin LI  * load a magic file
298b6cee71dSXin LI  */
299b6cee71dSXin LI public int
300b6cee71dSXin LI magic_load(struct magic_set *ms, const char *magicfile)
301b6cee71dSXin LI {
302b6cee71dSXin LI 	if (ms == NULL)
303b6cee71dSXin LI 		return -1;
304b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_LOAD);
305b6cee71dSXin LI }
306b6cee71dSXin LI 
307c2931133SXin LI #ifndef COMPILE_ONLY
308c2931133SXin LI /*
309c2931133SXin LI  * Install a set of compiled magic buffers.
310c2931133SXin LI  */
311c2931133SXin LI public int
312c2931133SXin LI magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
313c2931133SXin LI     size_t nbufs)
314c2931133SXin LI {
315c2931133SXin LI 	if (ms == NULL)
316c2931133SXin LI 		return -1;
31748c779cdSXin LI 	return buffer_apprentice(ms, RCAST(struct magic **, bufs),
31848c779cdSXin LI 	    sizes, nbufs);
319c2931133SXin LI }
320c2931133SXin LI #endif
321c2931133SXin LI 
322b6cee71dSXin LI public int
323b6cee71dSXin LI magic_compile(struct magic_set *ms, const char *magicfile)
324b6cee71dSXin LI {
325b6cee71dSXin LI 	if (ms == NULL)
326b6cee71dSXin LI 		return -1;
327b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_COMPILE);
328b6cee71dSXin LI }
329b6cee71dSXin LI 
330b6cee71dSXin LI public int
331b6cee71dSXin LI magic_check(struct magic_set *ms, const char *magicfile)
332b6cee71dSXin LI {
333b6cee71dSXin LI 	if (ms == NULL)
334b6cee71dSXin LI 		return -1;
335b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_CHECK);
336b6cee71dSXin LI }
337b6cee71dSXin LI 
338b6cee71dSXin LI public int
339b6cee71dSXin LI magic_list(struct magic_set *ms, const char *magicfile)
340b6cee71dSXin LI {
341b6cee71dSXin LI 	if (ms == NULL)
342b6cee71dSXin LI 		return -1;
343b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_LIST);
344b6cee71dSXin LI }
345b6cee71dSXin LI 
346b6cee71dSXin LI private void
347b6cee71dSXin LI close_and_restore(const struct magic_set *ms, const char *name, int fd,
348b6cee71dSXin LI     const struct stat *sb)
349b6cee71dSXin LI {
3507206c4fcSXin LI 	if (fd == STDIN_FILENO || name == NULL)
351b6cee71dSXin LI 		return;
352b6cee71dSXin LI 	(void) close(fd);
353b6cee71dSXin LI 
354b6cee71dSXin LI 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
355b6cee71dSXin LI 		/*
356b6cee71dSXin LI 		 * Try to restore access, modification times if read it.
357b6cee71dSXin LI 		 * This is really *bad* because it will modify the status
358b6cee71dSXin LI 		 * time of the file... And of course this will affect
359b6cee71dSXin LI 		 * backup programs
360b6cee71dSXin LI 		 */
361b6cee71dSXin LI #ifdef HAVE_UTIMES
362b6cee71dSXin LI 		struct timeval  utsbuf[2];
363b6cee71dSXin LI 		(void)memset(utsbuf, 0, sizeof(utsbuf));
364b6cee71dSXin LI 		utsbuf[0].tv_sec = sb->st_atime;
365b6cee71dSXin LI 		utsbuf[1].tv_sec = sb->st_mtime;
366b6cee71dSXin LI 
367b6cee71dSXin LI 		(void) utimes(name, utsbuf); /* don't care if loses */
368b6cee71dSXin LI #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
369b6cee71dSXin LI 		struct utimbuf  utbuf;
370b6cee71dSXin LI 
371b6cee71dSXin LI 		(void)memset(&utbuf, 0, sizeof(utbuf));
372b6cee71dSXin LI 		utbuf.actime = sb->st_atime;
373b6cee71dSXin LI 		utbuf.modtime = sb->st_mtime;
374b6cee71dSXin LI 		(void) utime(name, &utbuf); /* don't care if loses */
375b6cee71dSXin LI #endif
376b6cee71dSXin LI 	}
377b6cee71dSXin LI }
378b6cee71dSXin LI 
379b6cee71dSXin LI #ifndef COMPILE_ONLY
380b6cee71dSXin LI 
381b6cee71dSXin LI /*
382b6cee71dSXin LI  * find type of descriptor
383b6cee71dSXin LI  */
384b6cee71dSXin LI public const char *
385b6cee71dSXin LI magic_descriptor(struct magic_set *ms, int fd)
386b6cee71dSXin LI {
387b6cee71dSXin LI 	if (ms == NULL)
388b6cee71dSXin LI 		return NULL;
389b6cee71dSXin LI 	return file_or_fd(ms, NULL, fd);
390b6cee71dSXin LI }
391b6cee71dSXin LI 
392b6cee71dSXin LI /*
393b6cee71dSXin LI  * find type of named file
394b6cee71dSXin LI  */
395b6cee71dSXin LI public const char *
396b6cee71dSXin LI magic_file(struct magic_set *ms, const char *inname)
397b6cee71dSXin LI {
398b6cee71dSXin LI 	if (ms == NULL)
399b6cee71dSXin LI 		return NULL;
400b6cee71dSXin LI 	return file_or_fd(ms, inname, STDIN_FILENO);
401b6cee71dSXin LI }
402b6cee71dSXin LI 
403b6cee71dSXin LI private const char *
404b6cee71dSXin LI file_or_fd(struct magic_set *ms, const char *inname, int fd)
405b6cee71dSXin LI {
406b6cee71dSXin LI 	int	rv = -1;
407b6cee71dSXin LI 	unsigned char *buf;
408b6cee71dSXin LI 	struct stat	sb;
409b6cee71dSXin LI 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
410b6cee71dSXin LI 	int	ispipe = 0;
41148c779cdSXin LI 	int	okstat = 0;
41248c779cdSXin LI 	off_t	pos = CAST(off_t, -1);
413b6cee71dSXin LI 
41440427ccaSGordon Tetlow 	if (file_reset(ms, 1) == -1)
415b6cee71dSXin LI 		goto out;
416b6cee71dSXin LI 
417b6cee71dSXin LI 	/*
418b6cee71dSXin LI 	 * one extra for terminating '\0', and
419b6cee71dSXin LI 	 * some overlapping space for matches near EOF
420b6cee71dSXin LI 	 */
421b6cee71dSXin LI #define SLOP (1 + sizeof(union VALUETYPE))
4223e41d09dSXin LI 	if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
423b6cee71dSXin LI 		return NULL;
424b6cee71dSXin LI 
425b6cee71dSXin LI 	switch (file_fsmagic(ms, inname, &sb)) {
426b6cee71dSXin LI 	case -1:		/* error */
427b6cee71dSXin LI 		goto done;
428b6cee71dSXin LI 	case 0:			/* nothing found */
429b6cee71dSXin LI 		break;
430b6cee71dSXin LI 	default:		/* matched it and printed type */
431b6cee71dSXin LI 		rv = 0;
432b6cee71dSXin LI 		goto done;
433b6cee71dSXin LI 	}
434b6cee71dSXin LI 
435b6cee71dSXin LI #ifdef WIN32
436b6cee71dSXin LI 	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
437b6cee71dSXin LI 	if (fd == STDIN_FILENO)
438b6cee71dSXin LI 		_setmode(STDIN_FILENO, O_BINARY);
439b6cee71dSXin LI #endif
44048c779cdSXin LI 	if (inname != NULL) {
441*43a5ec4eSXin LI 		int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC;
442b6cee71dSXin LI 		errno = 0;
443b6cee71dSXin LI 		if ((fd = open(inname, flags)) < 0) {
44448c779cdSXin LI 			okstat = stat(inname, &sb) == 0;
44548c779cdSXin LI 			if (okstat && S_ISFIFO(sb.st_mode))
44648c779cdSXin LI 				ispipe = 1;
447b6cee71dSXin LI #ifdef WIN32
448b6cee71dSXin LI 			/*
449b6cee71dSXin LI 			 * Can't stat, can't open.  It may have been opened in
450b6cee71dSXin LI 			 * fsmagic, so if the user doesn't have read permission,
451b6cee71dSXin LI 			 * allow it to say so; otherwise an error was probably
452b6cee71dSXin LI 			 * displayed in fsmagic.
453b6cee71dSXin LI 			 */
454b6cee71dSXin LI 			if (!okstat && errno == EACCES) {
455b6cee71dSXin LI 				sb.st_mode = S_IFBLK;
456b6cee71dSXin LI 				okstat = 1;
457b6cee71dSXin LI 			}
458b6cee71dSXin LI #endif
459b6cee71dSXin LI 			if (okstat &&
460b6cee71dSXin LI 			    unreadable_info(ms, sb.st_mode, inname) == -1)
461b6cee71dSXin LI 				goto done;
462b6cee71dSXin LI 			rv = 0;
463b6cee71dSXin LI 			goto done;
464b6cee71dSXin LI 		}
465*43a5ec4eSXin LI #if O_CLOEXEC == 0
466*43a5ec4eSXin LI 		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
467*43a5ec4eSXin LI #endif
468b6cee71dSXin LI 	}
46948c779cdSXin LI 
47048c779cdSXin LI 	if (fd != -1) {
47148c779cdSXin LI 		okstat = fstat(fd, &sb) == 0;
47248c779cdSXin LI 		if (okstat && S_ISFIFO(sb.st_mode))
47348c779cdSXin LI 			ispipe = 1;
47448c779cdSXin LI 		if (inname == NULL)
47548c779cdSXin LI 			pos = lseek(fd, CAST(off_t, 0), SEEK_CUR);
476b6cee71dSXin LI 	}
477b6cee71dSXin LI 
478b6cee71dSXin LI 	/*
4793e41d09dSXin LI 	 * try looking at the first ms->bytes_max bytes
480b6cee71dSXin LI 	 */
481b6cee71dSXin LI 	if (ispipe) {
48248c779cdSXin LI 		if (fd != -1) {
483b6cee71dSXin LI 			ssize_t r = 0;
484b6cee71dSXin LI 
48548c779cdSXin LI 			while ((r = sread(fd, RCAST(void *, &buf[nbytes]),
48648c779cdSXin LI 			    CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) {
487b6cee71dSXin LI 				nbytes += r;
488b6cee71dSXin LI 				if (r < PIPE_BUF) break;
489b6cee71dSXin LI 			}
49048c779cdSXin LI 		}
491b6cee71dSXin LI 
492a5d223e6SXin LI 		if (nbytes == 0 && inname) {
493b6cee71dSXin LI 			/* We can not read it, but we were able to stat it. */
494b6cee71dSXin LI 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
495b6cee71dSXin LI 				goto done;
496b6cee71dSXin LI 			rv = 0;
497b6cee71dSXin LI 			goto done;
498b6cee71dSXin LI 		}
499b6cee71dSXin LI 
50048c779cdSXin LI 	} else if (fd != -1) {
501b6cee71dSXin LI 		/* Windows refuses to read from a big console buffer. */
502b6cee71dSXin LI 		size_t howmany =
5033e41d09dSXin LI #if defined(WIN32)
504b6cee71dSXin LI 		    _isatty(fd) ? 8 * 1024 :
505b6cee71dSXin LI #endif
5063e41d09dSXin LI 		    ms->bytes_max;
50748c779cdSXin LI 		if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) {
508b6cee71dSXin LI 			if (inname == NULL && fd != STDIN_FILENO)
509b6cee71dSXin LI 				file_error(ms, errno, "cannot read fd %d", fd);
510b6cee71dSXin LI 			else
511b6cee71dSXin LI 				file_error(ms, errno, "cannot read `%s'",
512b6cee71dSXin LI 				    inname == NULL ? "/dev/stdin" : inname);
513b6cee71dSXin LI 			goto done;
514b6cee71dSXin LI 		}
515b6cee71dSXin LI 	}
516b6cee71dSXin LI 
517b6cee71dSXin LI 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
51848c779cdSXin LI 	if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1)
519b6cee71dSXin LI 		goto done;
520b6cee71dSXin LI 	rv = 0;
521b6cee71dSXin LI done:
522b6cee71dSXin LI 	free(buf);
52320f8619dSXin LI 	if (fd != -1) {
52448c779cdSXin LI 		if (pos != CAST(off_t, -1))
525b6cee71dSXin LI 			(void)lseek(fd, pos, SEEK_SET);
526b6cee71dSXin LI 		close_and_restore(ms, inname, fd, &sb);
52720f8619dSXin LI 	}
528b6cee71dSXin LI out:
529b6cee71dSXin LI 	return rv == 0 ? file_getbuffer(ms) : NULL;
530b6cee71dSXin LI }
531b6cee71dSXin LI 
532b6cee71dSXin LI 
533b6cee71dSXin LI public const char *
534b6cee71dSXin LI magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
535b6cee71dSXin LI {
536b6cee71dSXin LI 	if (ms == NULL)
537b6cee71dSXin LI 		return NULL;
53840427ccaSGordon Tetlow 	if (file_reset(ms, 1) == -1)
539b6cee71dSXin LI 		return NULL;
540b6cee71dSXin LI 	/*
541b6cee71dSXin LI 	 * The main work is done here!
542b6cee71dSXin LI 	 * We have the file name and/or the data buffer to be identified.
543b6cee71dSXin LI 	 */
54448c779cdSXin LI 	if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) {
545b6cee71dSXin LI 		return NULL;
546b6cee71dSXin LI 	}
547b6cee71dSXin LI 	return file_getbuffer(ms);
548b6cee71dSXin LI }
549b6cee71dSXin LI #endif
550b6cee71dSXin LI 
551b6cee71dSXin LI public const char *
552b6cee71dSXin LI magic_error(struct magic_set *ms)
553b6cee71dSXin LI {
554b6cee71dSXin LI 	if (ms == NULL)
555b6cee71dSXin LI 		return "Magic database is not open";
556b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
557b6cee71dSXin LI }
558b6cee71dSXin LI 
559b6cee71dSXin LI public int
560b6cee71dSXin LI magic_errno(struct magic_set *ms)
561b6cee71dSXin LI {
562b6cee71dSXin LI 	if (ms == NULL)
563b6cee71dSXin LI 		return EINVAL;
564b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
565b6cee71dSXin LI }
566b6cee71dSXin LI 
567b6cee71dSXin LI public int
56840427ccaSGordon Tetlow magic_getflags(struct magic_set *ms)
56940427ccaSGordon Tetlow {
57040427ccaSGordon Tetlow 	if (ms == NULL)
57140427ccaSGordon Tetlow 		return -1;
57240427ccaSGordon Tetlow 
57340427ccaSGordon Tetlow 	return ms->flags;
57440427ccaSGordon Tetlow }
57540427ccaSGordon Tetlow 
57640427ccaSGordon Tetlow public int
577b6cee71dSXin LI magic_setflags(struct magic_set *ms, int flags)
578b6cee71dSXin LI {
579b6cee71dSXin LI 	if (ms == NULL)
580b6cee71dSXin LI 		return -1;
581b6cee71dSXin LI #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
582b6cee71dSXin LI 	if (flags & MAGIC_PRESERVE_ATIME)
583b6cee71dSXin LI 		return -1;
584b6cee71dSXin LI #endif
585b6cee71dSXin LI 	ms->flags = flags;
586b6cee71dSXin LI 	return 0;
587b6cee71dSXin LI }
588b6cee71dSXin LI 
589b6cee71dSXin LI public int
590b6cee71dSXin LI magic_version(void)
591b6cee71dSXin LI {
592b6cee71dSXin LI 	return MAGIC_VERSION;
593b6cee71dSXin LI }
594c2931133SXin LI 
595c2931133SXin LI public int
596c2931133SXin LI magic_setparam(struct magic_set *ms, int param, const void *val)
597c2931133SXin LI {
59848c779cdSXin LI 	if (ms == NULL)
59948c779cdSXin LI 		return -1;
600c2931133SXin LI 	switch (param) {
601c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
60248c779cdSXin LI 		ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
603c2931133SXin LI 		return 0;
604c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
60548c779cdSXin LI 		ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
606c2931133SXin LI 		return 0;
607c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
60848c779cdSXin LI 		ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
609c2931133SXin LI 		return 0;
610c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
61148c779cdSXin LI 		ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
612c2931133SXin LI 		return 0;
6134460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
61448c779cdSXin LI 		ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
6154460e5b0SXin LI 		return 0;
6169ce06829SXin LI 	case MAGIC_PARAM_REGEX_MAX:
61748c779cdSXin LI 		ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
6189ce06829SXin LI 		return 0;
6193e41d09dSXin LI 	case MAGIC_PARAM_BYTES_MAX:
62048c779cdSXin LI 		ms->bytes_max = *CAST(const size_t *, val);
6213e41d09dSXin LI 		return 0;
622*43a5ec4eSXin LI 	case MAGIC_PARAM_ENCODING_MAX:
623*43a5ec4eSXin LI 		ms->encoding_max = *CAST(const size_t *, val);
624*43a5ec4eSXin LI 		return 0;
625c2931133SXin LI 	default:
626c2931133SXin LI 		errno = EINVAL;
627c2931133SXin LI 		return -1;
628c2931133SXin LI 	}
629c2931133SXin LI }
630c2931133SXin LI 
631c2931133SXin LI public int
632c2931133SXin LI magic_getparam(struct magic_set *ms, int param, void *val)
633c2931133SXin LI {
63448c779cdSXin LI 	if (ms == NULL)
63548c779cdSXin LI 		return -1;
636c2931133SXin LI 	switch (param) {
637c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
63848c779cdSXin LI 		*CAST(size_t *, val) = ms->indir_max;
639c2931133SXin LI 		return 0;
640c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
64148c779cdSXin LI 		*CAST(size_t *, val) = ms->name_max;
642c2931133SXin LI 		return 0;
643c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
64448c779cdSXin LI 		*CAST(size_t *, val) = ms->elf_phnum_max;
645c2931133SXin LI 		return 0;
646c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
64748c779cdSXin LI 		*CAST(size_t *, val) = ms->elf_shnum_max;
648c2931133SXin LI 		return 0;
6494460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
65048c779cdSXin LI 		*CAST(size_t *, val) = ms->elf_notes_max;
6514460e5b0SXin LI 		return 0;
6529ce06829SXin LI 	case MAGIC_PARAM_REGEX_MAX:
65348c779cdSXin LI 		*CAST(size_t *, val) = ms->regex_max;
6549ce06829SXin LI 		return 0;
6553e41d09dSXin LI 	case MAGIC_PARAM_BYTES_MAX:
65648c779cdSXin LI 		*CAST(size_t *, val) = ms->bytes_max;
6573e41d09dSXin LI 		return 0;
658*43a5ec4eSXin LI 	case MAGIC_PARAM_ENCODING_MAX:
659*43a5ec4eSXin LI 		*CAST(size_t *, val) = ms->encoding_max;
660*43a5ec4eSXin LI 		return 0;
661c2931133SXin LI 	default:
662c2931133SXin LI 		errno = EINVAL;
663c2931133SXin LI 		return -1;
664c2931133SXin LI 	}
665c2931133SXin LI }
666