xref: /freebsd/contrib/file/src/magic.c (revision 898496ee)
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*898496eeSXin LI FILE_RCSID("@(#)$File: magic.c,v 1.121 2023/02/09 17:45:19 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 
72*898496eeSXin LI file_private void close_and_restore(const struct magic_set *, const char *, int,
73b6cee71dSXin LI     const struct stat *);
74*898496eeSXin LI file_private int unreadable_info(struct magic_set *, mode_t, const char *);
75*898496eeSXin LI file_private const char* get_default_magic(void);
76b6cee71dSXin LI #ifndef COMPILE_ONLY
77*898496eeSXin LI file_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
_w32_append_path(char ** hmagicpath,const char * fmt,...)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
_w32_get_magic_relative_to(char ** hmagicpath,HINSTANCE module)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 
15943a5ec4eSXin 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
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)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
17243a5ec4eSXin LI #endif
1735f0216bdSXin LI 
174*898496eeSXin LI file_private const char *
get_default_magic(void)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 
224a4d6d3b8SXin LI 	/* Before anything else, try to get a magic file from user HOME */
225a4d6d3b8SXin LI 	if ((home = getenv("HOME")) != NULL)
226a4d6d3b8SXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
227a4d6d3b8SXin LI 
2285f0216bdSXin LI 	/* First, try to get a magic file from user-application data */
2295f0216bdSXin LI 	if ((home = getenv("LOCALAPPDATA")) != NULL)
2305f0216bdSXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
2315f0216bdSXin LI 
2325f0216bdSXin LI 	/* Second, try to get a magic file from the user profile data */
233b6cee71dSXin LI 	if ((home = getenv("USERPROFILE")) != NULL)
2345f0216bdSXin LI 		_w32_append_path(&hmagicpath,
2355f0216bdSXin LI 		    "%s/Local Settings/Application Data%s", home, hmagic);
236b6cee71dSXin LI 
2375f0216bdSXin LI 	/* Third, try to get a magic file from Common Files */
2385f0216bdSXin LI 	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
2395f0216bdSXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
240b6cee71dSXin LI 
2415f0216bdSXin LI 	/* Fourth, try to get magic file relative to exe location */
2425f0216bdSXin LI         _w32_get_magic_relative_to(&hmagicpath, NULL);
243b6cee71dSXin LI 
2445f0216bdSXin LI 	/* Fifth, try to get magic file relative to dll location */
2455f0216bdSXin LI         _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
246b6cee71dSXin LI 
2475f0216bdSXin LI 	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
248b6cee71dSXin LI 	default_magic = hmagicpath;
249b6cee71dSXin LI 	return default_magic;
250b6cee71dSXin LI #endif
251b6cee71dSXin LI }
252b6cee71dSXin LI 
253*898496eeSXin LI file_public const char *
magic_getpath(const char * magicfile,int action)254b6cee71dSXin LI magic_getpath(const char *magicfile, int action)
255b6cee71dSXin LI {
256b6cee71dSXin LI 	if (magicfile != NULL)
257b6cee71dSXin LI 		return magicfile;
258b6cee71dSXin LI 
259b6cee71dSXin LI 	magicfile = getenv("MAGIC");
260b6cee71dSXin LI 	if (magicfile != NULL)
261b6cee71dSXin LI 		return magicfile;
262b6cee71dSXin LI 
263b6cee71dSXin LI 	return action == FILE_LOAD ? get_default_magic() : MAGIC;
264b6cee71dSXin LI }
265b6cee71dSXin LI 
266*898496eeSXin LI file_public struct magic_set *
magic_open(int flags)267b6cee71dSXin LI magic_open(int flags)
268b6cee71dSXin LI {
269b6cee71dSXin LI 	return file_ms_alloc(flags);
270b6cee71dSXin LI }
271b6cee71dSXin LI 
272*898496eeSXin LI file_private int
unreadable_info(struct magic_set * ms,mode_t md,const char * file)273b6cee71dSXin LI unreadable_info(struct magic_set *ms, mode_t md, const char *file)
274b6cee71dSXin LI {
275b6cee71dSXin LI 	if (file) {
276b6cee71dSXin LI 		/* We cannot open it, but we were able to stat it. */
277b6cee71dSXin LI 		if (access(file, W_OK) == 0)
278b6cee71dSXin LI 			if (file_printf(ms, "writable, ") == -1)
279b6cee71dSXin LI 				return -1;
280a4d6d3b8SXin LI #ifndef WIN32
281b6cee71dSXin LI 		if (access(file, X_OK) == 0)
282b6cee71dSXin LI 			if (file_printf(ms, "executable, ") == -1)
283b6cee71dSXin LI 				return -1;
284a4d6d3b8SXin LI #else
285a4d6d3b8SXin LI 		/* X_OK doesn't work well on MS-Windows */
286a4d6d3b8SXin LI 		{
287a4d6d3b8SXin LI 			const char *p = strrchr(file, '.');
288a4d6d3b8SXin LI 			if (p && (stricmp(p, ".exe")
289a4d6d3b8SXin LI 				  || stricmp(p, ".dll")
290a4d6d3b8SXin LI 				  || stricmp(p, ".bat")
291a4d6d3b8SXin LI 				  || stricmp(p, ".cmd")))
292a4d6d3b8SXin LI 				if (file_printf(ms, "writable, ") == -1)
293a4d6d3b8SXin LI 					return -1;
294a4d6d3b8SXin LI 		}
295a4d6d3b8SXin LI #endif
296b6cee71dSXin LI 	}
297b6cee71dSXin LI 	if (S_ISREG(md))
298b6cee71dSXin LI 		if (file_printf(ms, "regular file, ") == -1)
299b6cee71dSXin LI 			return -1;
300b6cee71dSXin LI 	if (file_printf(ms, "no read permission") == -1)
301b6cee71dSXin LI 		return -1;
302b6cee71dSXin LI 	return 0;
303b6cee71dSXin LI }
304b6cee71dSXin LI 
305*898496eeSXin LI file_public void
magic_close(struct magic_set * ms)306b6cee71dSXin LI magic_close(struct magic_set *ms)
307b6cee71dSXin LI {
308b6cee71dSXin LI 	if (ms == NULL)
309b6cee71dSXin LI 		return;
310b6cee71dSXin LI 	file_ms_free(ms);
311b6cee71dSXin LI }
312b6cee71dSXin LI 
313b6cee71dSXin LI /*
314b6cee71dSXin LI  * load a magic file
315b6cee71dSXin LI  */
316*898496eeSXin LI file_public int
magic_load(struct magic_set * ms,const char * magicfile)317b6cee71dSXin LI magic_load(struct magic_set *ms, const char *magicfile)
318b6cee71dSXin LI {
319b6cee71dSXin LI 	if (ms == NULL)
320b6cee71dSXin LI 		return -1;
321b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_LOAD);
322b6cee71dSXin LI }
323b6cee71dSXin LI 
324c2931133SXin LI #ifndef COMPILE_ONLY
325c2931133SXin LI /*
326c2931133SXin LI  * Install a set of compiled magic buffers.
327c2931133SXin LI  */
328*898496eeSXin LI file_public int
magic_load_buffers(struct magic_set * ms,void ** bufs,size_t * sizes,size_t nbufs)329c2931133SXin LI magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
330c2931133SXin LI     size_t nbufs)
331c2931133SXin LI {
332c2931133SXin LI 	if (ms == NULL)
333c2931133SXin LI 		return -1;
33448c779cdSXin LI 	return buffer_apprentice(ms, RCAST(struct magic **, bufs),
33548c779cdSXin LI 	    sizes, nbufs);
336c2931133SXin LI }
337c2931133SXin LI #endif
338c2931133SXin LI 
339*898496eeSXin LI file_public int
magic_compile(struct magic_set * ms,const char * magicfile)340b6cee71dSXin LI magic_compile(struct magic_set *ms, const char *magicfile)
341b6cee71dSXin LI {
342b6cee71dSXin LI 	if (ms == NULL)
343b6cee71dSXin LI 		return -1;
344b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_COMPILE);
345b6cee71dSXin LI }
346b6cee71dSXin LI 
347*898496eeSXin LI file_public int
magic_check(struct magic_set * ms,const char * magicfile)348b6cee71dSXin LI magic_check(struct magic_set *ms, const char *magicfile)
349b6cee71dSXin LI {
350b6cee71dSXin LI 	if (ms == NULL)
351b6cee71dSXin LI 		return -1;
352b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_CHECK);
353b6cee71dSXin LI }
354b6cee71dSXin LI 
355*898496eeSXin LI file_public int
magic_list(struct magic_set * ms,const char * magicfile)356b6cee71dSXin LI magic_list(struct magic_set *ms, const char *magicfile)
357b6cee71dSXin LI {
358b6cee71dSXin LI 	if (ms == NULL)
359b6cee71dSXin LI 		return -1;
360b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_LIST);
361b6cee71dSXin LI }
362b6cee71dSXin LI 
363*898496eeSXin LI file_private void
close_and_restore(const struct magic_set * ms,const char * name,int fd,const struct stat * sb)364b6cee71dSXin LI close_and_restore(const struct magic_set *ms, const char *name, int fd,
365b6cee71dSXin LI     const struct stat *sb)
366b6cee71dSXin LI {
3677206c4fcSXin LI 	if (fd == STDIN_FILENO || name == NULL)
368b6cee71dSXin LI 		return;
369b6cee71dSXin LI 	(void) close(fd);
370b6cee71dSXin LI 
371b6cee71dSXin LI 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
372b6cee71dSXin LI 		/*
373b6cee71dSXin LI 		 * Try to restore access, modification times if read it.
374b6cee71dSXin LI 		 * This is really *bad* because it will modify the status
375b6cee71dSXin LI 		 * time of the file... And of course this will affect
376b6cee71dSXin LI 		 * backup programs
377b6cee71dSXin LI 		 */
378b6cee71dSXin LI #ifdef HAVE_UTIMES
379b6cee71dSXin LI 		struct timeval  utsbuf[2];
380b6cee71dSXin LI 		(void)memset(utsbuf, 0, sizeof(utsbuf));
381b6cee71dSXin LI 		utsbuf[0].tv_sec = sb->st_atime;
382b6cee71dSXin LI 		utsbuf[1].tv_sec = sb->st_mtime;
383b6cee71dSXin LI 
384b6cee71dSXin LI 		(void) utimes(name, utsbuf); /* don't care if loses */
385b6cee71dSXin LI #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
386b6cee71dSXin LI 		struct utimbuf  utbuf;
387b6cee71dSXin LI 
388b6cee71dSXin LI 		(void)memset(&utbuf, 0, sizeof(utbuf));
389b6cee71dSXin LI 		utbuf.actime = sb->st_atime;
390b6cee71dSXin LI 		utbuf.modtime = sb->st_mtime;
391b6cee71dSXin LI 		(void) utime(name, &utbuf); /* don't care if loses */
392b6cee71dSXin LI #endif
393b6cee71dSXin LI 	}
394b6cee71dSXin LI }
395b6cee71dSXin LI 
396b6cee71dSXin LI #ifndef COMPILE_ONLY
397b6cee71dSXin LI 
398b6cee71dSXin LI /*
399b6cee71dSXin LI  * find type of descriptor
400b6cee71dSXin LI  */
401*898496eeSXin LI file_public const char *
magic_descriptor(struct magic_set * ms,int fd)402b6cee71dSXin LI magic_descriptor(struct magic_set *ms, int fd)
403b6cee71dSXin LI {
404b6cee71dSXin LI 	if (ms == NULL)
405b6cee71dSXin LI 		return NULL;
406b6cee71dSXin LI 	return file_or_fd(ms, NULL, fd);
407b6cee71dSXin LI }
408b6cee71dSXin LI 
409b6cee71dSXin LI /*
410b6cee71dSXin LI  * find type of named file
411b6cee71dSXin LI  */
412*898496eeSXin LI file_public const char *
magic_file(struct magic_set * ms,const char * inname)413b6cee71dSXin LI magic_file(struct magic_set *ms, const char *inname)
414b6cee71dSXin LI {
415b6cee71dSXin LI 	if (ms == NULL)
416b6cee71dSXin LI 		return NULL;
417b6cee71dSXin LI 	return file_or_fd(ms, inname, STDIN_FILENO);
418b6cee71dSXin LI }
419b6cee71dSXin LI 
420*898496eeSXin LI file_private const char *
file_or_fd(struct magic_set * ms,const char * inname,int fd)421b6cee71dSXin LI file_or_fd(struct magic_set *ms, const char *inname, int fd)
422b6cee71dSXin LI {
423b6cee71dSXin LI 	int	rv = -1;
424b6cee71dSXin LI 	unsigned char *buf;
425b6cee71dSXin LI 	struct stat	sb;
426b6cee71dSXin LI 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
427b6cee71dSXin LI 	int	ispipe = 0;
42848c779cdSXin LI 	int	okstat = 0;
42948c779cdSXin LI 	off_t	pos = CAST(off_t, -1);
430b6cee71dSXin LI 
43140427ccaSGordon Tetlow 	if (file_reset(ms, 1) == -1)
432b6cee71dSXin LI 		goto out;
433b6cee71dSXin LI 
434b6cee71dSXin LI 	/*
435b6cee71dSXin LI 	 * one extra for terminating '\0', and
436b6cee71dSXin LI 	 * some overlapping space for matches near EOF
437b6cee71dSXin LI 	 */
438b6cee71dSXin LI #define SLOP (1 + sizeof(union VALUETYPE))
4393e41d09dSXin LI 	if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
440b6cee71dSXin LI 		return NULL;
441b6cee71dSXin LI 
442b6cee71dSXin LI 	switch (file_fsmagic(ms, inname, &sb)) {
443b6cee71dSXin LI 	case -1:		/* error */
444b6cee71dSXin LI 		goto done;
445b6cee71dSXin LI 	case 0:			/* nothing found */
446b6cee71dSXin LI 		break;
447b6cee71dSXin LI 	default:		/* matched it and printed type */
448b6cee71dSXin LI 		rv = 0;
449b6cee71dSXin LI 		goto done;
450b6cee71dSXin LI 	}
451b6cee71dSXin LI 
452b6cee71dSXin LI #ifdef WIN32
453b6cee71dSXin LI 	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
454b6cee71dSXin LI 	if (fd == STDIN_FILENO)
455b6cee71dSXin LI 		_setmode(STDIN_FILENO, O_BINARY);
456b6cee71dSXin LI #endif
45748c779cdSXin LI 	if (inname != NULL) {
45843a5ec4eSXin LI 		int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC;
459b6cee71dSXin LI 		errno = 0;
460b6cee71dSXin LI 		if ((fd = open(inname, flags)) < 0) {
46148c779cdSXin LI 			okstat = stat(inname, &sb) == 0;
462b6cee71dSXin LI #ifdef WIN32
463b6cee71dSXin LI 			/*
464b6cee71dSXin LI 			 * Can't stat, can't open.  It may have been opened in
465b6cee71dSXin LI 			 * fsmagic, so if the user doesn't have read permission,
466b6cee71dSXin LI 			 * allow it to say so; otherwise an error was probably
467b6cee71dSXin LI 			 * displayed in fsmagic.
468b6cee71dSXin LI 			 */
469b6cee71dSXin LI 			if (!okstat && errno == EACCES) {
470b6cee71dSXin LI 				sb.st_mode = S_IFBLK;
471b6cee71dSXin LI 				okstat = 1;
472b6cee71dSXin LI 			}
473b6cee71dSXin LI #endif
474b6cee71dSXin LI 			if (okstat &&
475b6cee71dSXin LI 			    unreadable_info(ms, sb.st_mode, inname) == -1)
476b6cee71dSXin LI 				goto done;
477b6cee71dSXin LI 			rv = 0;
478b6cee71dSXin LI 			goto done;
479b6cee71dSXin LI 		}
480a4d6d3b8SXin LI #if O_CLOEXEC == 0 && defined(F_SETFD)
48143a5ec4eSXin LI 		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
48243a5ec4eSXin LI #endif
483b6cee71dSXin LI 	}
48448c779cdSXin LI 
48548c779cdSXin LI 	if (fd != -1) {
48648c779cdSXin LI 		okstat = fstat(fd, &sb) == 0;
48748c779cdSXin LI 		if (okstat && S_ISFIFO(sb.st_mode))
48848c779cdSXin LI 			ispipe = 1;
48948c779cdSXin LI 		if (inname == NULL)
49048c779cdSXin LI 			pos = lseek(fd, CAST(off_t, 0), SEEK_CUR);
491b6cee71dSXin LI 	}
492b6cee71dSXin LI 
493b6cee71dSXin LI 	/*
4943e41d09dSXin LI 	 * try looking at the first ms->bytes_max bytes
495b6cee71dSXin LI 	 */
496b6cee71dSXin LI 	if (ispipe) {
49748c779cdSXin LI 		if (fd != -1) {
498b6cee71dSXin LI 			ssize_t r = 0;
499b6cee71dSXin LI 
50048c779cdSXin LI 			while ((r = sread(fd, RCAST(void *, &buf[nbytes]),
50148c779cdSXin LI 			    CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) {
502b6cee71dSXin LI 				nbytes += r;
503b6cee71dSXin LI 				if (r < PIPE_BUF) break;
504b6cee71dSXin LI 			}
50548c779cdSXin LI 		}
506b6cee71dSXin LI 
507a5d223e6SXin LI 		if (nbytes == 0 && inname) {
508b6cee71dSXin LI 			/* We can not read it, but we were able to stat it. */
509b6cee71dSXin LI 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
510b6cee71dSXin LI 				goto done;
511b6cee71dSXin LI 			rv = 0;
512b6cee71dSXin LI 			goto done;
513b6cee71dSXin LI 		}
514b6cee71dSXin LI 
51548c779cdSXin LI 	} else if (fd != -1) {
516b6cee71dSXin LI 		/* Windows refuses to read from a big console buffer. */
517b6cee71dSXin LI 		size_t howmany =
518a4d6d3b8SXin LI #ifdef WIN32
519b6cee71dSXin LI 		    _isatty(fd) ? 8 * 1024 :
520b6cee71dSXin LI #endif
5213e41d09dSXin LI 		    ms->bytes_max;
52248c779cdSXin LI 		if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) {
523b6cee71dSXin LI 			if (inname == NULL && fd != STDIN_FILENO)
524b6cee71dSXin LI 				file_error(ms, errno, "cannot read fd %d", fd);
525b6cee71dSXin LI 			else
526b6cee71dSXin LI 				file_error(ms, errno, "cannot read `%s'",
527b6cee71dSXin LI 				    inname == NULL ? "/dev/stdin" : inname);
528b6cee71dSXin LI 			goto done;
529b6cee71dSXin LI 		}
530b6cee71dSXin LI 	}
531b6cee71dSXin LI 
532b6cee71dSXin LI 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
53348c779cdSXin LI 	if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1)
534b6cee71dSXin LI 		goto done;
535b6cee71dSXin LI 	rv = 0;
536b6cee71dSXin LI done:
537b6cee71dSXin LI 	free(buf);
53820f8619dSXin LI 	if (fd != -1) {
53948c779cdSXin LI 		if (pos != CAST(off_t, -1))
540b6cee71dSXin LI 			(void)lseek(fd, pos, SEEK_SET);
541b6cee71dSXin LI 		close_and_restore(ms, inname, fd, &sb);
54220f8619dSXin LI 	}
543b6cee71dSXin LI out:
544b6cee71dSXin LI 	return rv == 0 ? file_getbuffer(ms) : NULL;
545b6cee71dSXin LI }
546b6cee71dSXin LI 
547b6cee71dSXin LI 
548*898496eeSXin LI file_public const char *
magic_buffer(struct magic_set * ms,const void * buf,size_t nb)549b6cee71dSXin LI magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
550b6cee71dSXin LI {
551b6cee71dSXin LI 	if (ms == NULL)
552b6cee71dSXin LI 		return NULL;
55340427ccaSGordon Tetlow 	if (file_reset(ms, 1) == -1)
554b6cee71dSXin LI 		return NULL;
555b6cee71dSXin LI 	/*
556b6cee71dSXin LI 	 * The main work is done here!
557b6cee71dSXin LI 	 * We have the file name and/or the data buffer to be identified.
558b6cee71dSXin LI 	 */
55948c779cdSXin LI 	if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) {
560b6cee71dSXin LI 		return NULL;
561b6cee71dSXin LI 	}
562b6cee71dSXin LI 	return file_getbuffer(ms);
563b6cee71dSXin LI }
564b6cee71dSXin LI #endif
565b6cee71dSXin LI 
566*898496eeSXin LI file_public const char *
magic_error(struct magic_set * ms)567b6cee71dSXin LI magic_error(struct magic_set *ms)
568b6cee71dSXin LI {
569b6cee71dSXin LI 	if (ms == NULL)
570b6cee71dSXin LI 		return "Magic database is not open";
571b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
572b6cee71dSXin LI }
573b6cee71dSXin LI 
574*898496eeSXin LI file_public int
magic_errno(struct magic_set * ms)575b6cee71dSXin LI magic_errno(struct magic_set *ms)
576b6cee71dSXin LI {
577b6cee71dSXin LI 	if (ms == NULL)
578b6cee71dSXin LI 		return EINVAL;
579b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
580b6cee71dSXin LI }
581b6cee71dSXin LI 
582*898496eeSXin LI file_public int
magic_getflags(struct magic_set * ms)58340427ccaSGordon Tetlow magic_getflags(struct magic_set *ms)
58440427ccaSGordon Tetlow {
58540427ccaSGordon Tetlow 	if (ms == NULL)
58640427ccaSGordon Tetlow 		return -1;
58740427ccaSGordon Tetlow 
58840427ccaSGordon Tetlow 	return ms->flags;
58940427ccaSGordon Tetlow }
59040427ccaSGordon Tetlow 
591*898496eeSXin LI file_public int
magic_setflags(struct magic_set * ms,int flags)592b6cee71dSXin LI magic_setflags(struct magic_set *ms, int flags)
593b6cee71dSXin LI {
594b6cee71dSXin LI 	if (ms == NULL)
595b6cee71dSXin LI 		return -1;
596b6cee71dSXin LI #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
597b6cee71dSXin LI 	if (flags & MAGIC_PRESERVE_ATIME)
598b6cee71dSXin LI 		return -1;
599b6cee71dSXin LI #endif
600b6cee71dSXin LI 	ms->flags = flags;
601b6cee71dSXin LI 	return 0;
602b6cee71dSXin LI }
603b6cee71dSXin LI 
604*898496eeSXin LI file_public int
magic_version(void)605b6cee71dSXin LI magic_version(void)
606b6cee71dSXin LI {
607b6cee71dSXin LI 	return MAGIC_VERSION;
608b6cee71dSXin LI }
609c2931133SXin LI 
610*898496eeSXin LI file_public int
magic_setparam(struct magic_set * ms,int param,const void * val)611c2931133SXin LI magic_setparam(struct magic_set *ms, int param, const void *val)
612c2931133SXin LI {
61348c779cdSXin LI 	if (ms == NULL)
61448c779cdSXin LI 		return -1;
615c2931133SXin LI 	switch (param) {
616c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
61748c779cdSXin LI 		ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
618c2931133SXin LI 		return 0;
619c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
62048c779cdSXin LI 		ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
621c2931133SXin LI 		return 0;
622c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
62348c779cdSXin LI 		ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
624c2931133SXin LI 		return 0;
625c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
62648c779cdSXin LI 		ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
627c2931133SXin LI 		return 0;
628*898496eeSXin LI 	case MAGIC_PARAM_ELF_SHSIZE_MAX:
629*898496eeSXin LI 		ms->elf_shsize_max = *CAST(const size_t *, val);
630*898496eeSXin LI 		return 0;
6314460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
63248c779cdSXin LI 		ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
6334460e5b0SXin LI 		return 0;
6349ce06829SXin LI 	case MAGIC_PARAM_REGEX_MAX:
63548c779cdSXin LI 		ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
6369ce06829SXin LI 		return 0;
6373e41d09dSXin LI 	case MAGIC_PARAM_BYTES_MAX:
63848c779cdSXin LI 		ms->bytes_max = *CAST(const size_t *, val);
6393e41d09dSXin LI 		return 0;
64043a5ec4eSXin LI 	case MAGIC_PARAM_ENCODING_MAX:
64143a5ec4eSXin LI 		ms->encoding_max = *CAST(const size_t *, val);
64243a5ec4eSXin LI 		return 0;
643c2931133SXin LI 	default:
644c2931133SXin LI 		errno = EINVAL;
645c2931133SXin LI 		return -1;
646c2931133SXin LI 	}
647c2931133SXin LI }
648c2931133SXin LI 
649*898496eeSXin LI file_public int
magic_getparam(struct magic_set * ms,int param,void * val)650c2931133SXin LI magic_getparam(struct magic_set *ms, int param, void *val)
651c2931133SXin LI {
65248c779cdSXin LI 	if (ms == NULL)
65348c779cdSXin LI 		return -1;
654c2931133SXin LI 	switch (param) {
655c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
65648c779cdSXin LI 		*CAST(size_t *, val) = ms->indir_max;
657c2931133SXin LI 		return 0;
658c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
65948c779cdSXin LI 		*CAST(size_t *, val) = ms->name_max;
660c2931133SXin LI 		return 0;
661c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
66248c779cdSXin LI 		*CAST(size_t *, val) = ms->elf_phnum_max;
663c2931133SXin LI 		return 0;
664c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
66548c779cdSXin LI 		*CAST(size_t *, val) = ms->elf_shnum_max;
666c2931133SXin LI 		return 0;
667*898496eeSXin LI 	case MAGIC_PARAM_ELF_SHSIZE_MAX:
668*898496eeSXin LI 		*CAST(size_t *, val) = ms->elf_shsize_max;
669*898496eeSXin LI 		return 0;
6704460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
67148c779cdSXin LI 		*CAST(size_t *, val) = ms->elf_notes_max;
6724460e5b0SXin LI 		return 0;
6739ce06829SXin LI 	case MAGIC_PARAM_REGEX_MAX:
67448c779cdSXin LI 		*CAST(size_t *, val) = ms->regex_max;
6759ce06829SXin LI 		return 0;
6763e41d09dSXin LI 	case MAGIC_PARAM_BYTES_MAX:
67748c779cdSXin LI 		*CAST(size_t *, val) = ms->bytes_max;
6783e41d09dSXin LI 		return 0;
67943a5ec4eSXin LI 	case MAGIC_PARAM_ENCODING_MAX:
68043a5ec4eSXin LI 		*CAST(size_t *, val) = ms->encoding_max;
68143a5ec4eSXin LI 		return 0;
682c2931133SXin LI 	default:
683c2931133SXin LI 		errno = EINVAL;
684c2931133SXin LI 		return -1;
685c2931133SXin LI 	}
686c2931133SXin LI }
687