xref: /freebsd/contrib/file/src/magic.c (revision 40427cca)
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*40427ccaSGordon Tetlow FILE_RCSID("@(#)$File: magic.c,v 1.102 2017/08/28 13:39:18 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 #ifdef HAVE_LIMITS_H
48b6cee71dSXin LI #include <limits.h>	/* for PIPE_BUF */
49b6cee71dSXin LI #endif
50b6cee71dSXin LI 
51b6cee71dSXin LI #if defined(HAVE_UTIMES)
52b6cee71dSXin LI # include <sys/time.h>
53b6cee71dSXin LI #elif defined(HAVE_UTIME)
54b6cee71dSXin LI # if defined(HAVE_SYS_UTIME_H)
55b6cee71dSXin LI #  include <sys/utime.h>
56b6cee71dSXin LI # elif defined(HAVE_UTIME_H)
57b6cee71dSXin LI #  include <utime.h>
58b6cee71dSXin LI # endif
59b6cee71dSXin LI #endif
60b6cee71dSXin LI 
61b6cee71dSXin LI #ifdef HAVE_UNISTD_H
62b6cee71dSXin LI #include <unistd.h>	/* for read() */
63b6cee71dSXin LI #endif
64b6cee71dSXin LI 
65b6cee71dSXin LI #ifndef PIPE_BUF
66b6cee71dSXin LI /* Get the PIPE_BUF from pathconf */
67b6cee71dSXin LI #ifdef _PC_PIPE_BUF
68b6cee71dSXin LI #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
69b6cee71dSXin LI #else
70b6cee71dSXin LI #define PIPE_BUF 512
71b6cee71dSXin LI #endif
72b6cee71dSXin LI #endif
73b6cee71dSXin LI 
74b6cee71dSXin LI private void close_and_restore(const struct magic_set *, const char *, int,
75b6cee71dSXin LI     const struct stat *);
76b6cee71dSXin LI private int unreadable_info(struct magic_set *, mode_t, const char *);
77b6cee71dSXin LI private const char* get_default_magic(void);
78b6cee71dSXin LI #ifndef COMPILE_ONLY
79b6cee71dSXin LI private const char *file_or_fd(struct magic_set *, const char *, int);
80b6cee71dSXin LI #endif
81b6cee71dSXin LI 
82b6cee71dSXin LI #ifndef	STDIN_FILENO
83b6cee71dSXin LI #define	STDIN_FILENO	0
84b6cee71dSXin LI #endif
85b6cee71dSXin LI 
865f0216bdSXin LI #ifdef WIN32
875f0216bdSXin LI /* HINSTANCE of this shared library. Needed for get_default_magic() */
885f0216bdSXin LI static HINSTANCE _w32_dll_instance = NULL;
895f0216bdSXin LI 
905f0216bdSXin LI static void
915f0216bdSXin LI _w32_append_path(char **hmagicpath, const char *fmt, ...)
925f0216bdSXin LI {
935f0216bdSXin LI 	char *tmppath;
945f0216bdSXin LI         char *newpath;
955f0216bdSXin LI 	va_list ap;
965f0216bdSXin LI 
975f0216bdSXin LI 	va_start(ap, fmt);
985f0216bdSXin LI 	if (vasprintf(&tmppath, fmt, ap) < 0) {
995f0216bdSXin LI 		va_end(ap);
1005f0216bdSXin LI 		return;
1015f0216bdSXin LI 	}
1025f0216bdSXin LI 	va_end(ap);
1035f0216bdSXin LI 
1045f0216bdSXin LI 	if (access(tmppath, R_OK) == -1)
1055f0216bdSXin LI 		goto out;
1065f0216bdSXin LI 
1075f0216bdSXin LI 	if (*hmagicpath == NULL) {
1085f0216bdSXin LI 		*hmagicpath = tmppath;
1095f0216bdSXin LI 		return;
1105f0216bdSXin LI 	}
1115f0216bdSXin LI 
1125f0216bdSXin LI 	if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
1135f0216bdSXin LI 		goto out;
1145f0216bdSXin LI 
1155f0216bdSXin LI 	free(*hmagicpath);
1165f0216bdSXin LI 	free(tmppath);
1175f0216bdSXin LI 	*hmagicpath = newpath;
1185f0216bdSXin LI 	return;
1195f0216bdSXin LI out:
1205f0216bdSXin LI 	free(tmppath);
1215f0216bdSXin LI }
1225f0216bdSXin LI 
1235f0216bdSXin LI static void
1245f0216bdSXin LI _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
1255f0216bdSXin LI {
1265f0216bdSXin LI 	static const char *trypaths[] = {
1275f0216bdSXin LI 		"%s/share/misc/magic.mgc",
1285f0216bdSXin LI 		"%s/magic.mgc",
1295f0216bdSXin LI 	};
1305f0216bdSXin LI 	LPSTR dllpath;
1315f0216bdSXin LI 	size_t sp;
1325f0216bdSXin LI 
1335f0216bdSXin LI 	dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
1345f0216bdSXin LI 
1355f0216bdSXin LI 	if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
1365f0216bdSXin LI 		goto out;
1375f0216bdSXin LI 
1385f0216bdSXin LI 	PathRemoveFileSpecA(dllpath);
1395f0216bdSXin LI 
1409ce06829SXin LI 	if (module) {
1419ce06829SXin LI 		char exepath[MAX_PATH];
1429ce06829SXin LI 		GetModuleFileNameA(NULL, exepath, MAX_PATH);
1439ce06829SXin LI 		PathRemoveFileSpecA(exepath);
1449ce06829SXin LI 		if (stricmp(exepath, dllpath) == 0)
1459ce06829SXin LI 			goto out;
1469ce06829SXin LI 	}
1479ce06829SXin LI 
1485f0216bdSXin LI 	sp = strlen(dllpath);
1495f0216bdSXin LI 	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
1505f0216bdSXin LI 		_w32_append_path(hmagicpath,
1515f0216bdSXin LI 		    "%s/../share/misc/magic.mgc", dllpath);
1525f0216bdSXin LI 		goto out;
1535f0216bdSXin LI 	}
1545f0216bdSXin LI 
1555f0216bdSXin LI 	for (sp = 0; sp < __arraycount(trypaths); sp++)
1565f0216bdSXin LI 		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
1575f0216bdSXin LI out:
1585f0216bdSXin LI 	free(dllpath);
1595f0216bdSXin LI }
1605f0216bdSXin LI 
1615f0216bdSXin LI /* Placate GCC by offering a sacrificial previous prototype */
1625f0216bdSXin LI BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
1635f0216bdSXin LI 
1645f0216bdSXin LI BOOL WINAPI
1655f0216bdSXin LI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
1665f0216bdSXin LI     LPVOID lpvReserved __attribute__((__unused__)))
1675f0216bdSXin LI {
1685f0216bdSXin LI 	if (fdwReason == DLL_PROCESS_ATTACH)
1695f0216bdSXin LI 		_w32_dll_instance = hinstDLL;
170*40427ccaSGordon Tetlow 	return 1;
1715f0216bdSXin LI }
1725f0216bdSXin 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;
317c2931133SXin LI 	return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
318c2931133SXin LI }
319c2931133SXin LI #endif
320c2931133SXin LI 
321b6cee71dSXin LI public int
322b6cee71dSXin LI magic_compile(struct magic_set *ms, const char *magicfile)
323b6cee71dSXin LI {
324b6cee71dSXin LI 	if (ms == NULL)
325b6cee71dSXin LI 		return -1;
326b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_COMPILE);
327b6cee71dSXin LI }
328b6cee71dSXin LI 
329b6cee71dSXin LI public int
330b6cee71dSXin LI magic_check(struct magic_set *ms, const char *magicfile)
331b6cee71dSXin LI {
332b6cee71dSXin LI 	if (ms == NULL)
333b6cee71dSXin LI 		return -1;
334b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_CHECK);
335b6cee71dSXin LI }
336b6cee71dSXin LI 
337b6cee71dSXin LI public int
338b6cee71dSXin LI magic_list(struct magic_set *ms, const char *magicfile)
339b6cee71dSXin LI {
340b6cee71dSXin LI 	if (ms == NULL)
341b6cee71dSXin LI 		return -1;
342b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_LIST);
343b6cee71dSXin LI }
344b6cee71dSXin LI 
345b6cee71dSXin LI private void
346b6cee71dSXin LI close_and_restore(const struct magic_set *ms, const char *name, int fd,
347b6cee71dSXin LI     const struct stat *sb)
348b6cee71dSXin LI {
3497206c4fcSXin LI 	if (fd == STDIN_FILENO || name == NULL)
350b6cee71dSXin LI 		return;
351b6cee71dSXin LI 	(void) close(fd);
352b6cee71dSXin LI 
353b6cee71dSXin LI 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
354b6cee71dSXin LI 		/*
355b6cee71dSXin LI 		 * Try to restore access, modification times if read it.
356b6cee71dSXin LI 		 * This is really *bad* because it will modify the status
357b6cee71dSXin LI 		 * time of the file... And of course this will affect
358b6cee71dSXin LI 		 * backup programs
359b6cee71dSXin LI 		 */
360b6cee71dSXin LI #ifdef HAVE_UTIMES
361b6cee71dSXin LI 		struct timeval  utsbuf[2];
362b6cee71dSXin LI 		(void)memset(utsbuf, 0, sizeof(utsbuf));
363b6cee71dSXin LI 		utsbuf[0].tv_sec = sb->st_atime;
364b6cee71dSXin LI 		utsbuf[1].tv_sec = sb->st_mtime;
365b6cee71dSXin LI 
366b6cee71dSXin LI 		(void) utimes(name, utsbuf); /* don't care if loses */
367b6cee71dSXin LI #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
368b6cee71dSXin LI 		struct utimbuf  utbuf;
369b6cee71dSXin LI 
370b6cee71dSXin LI 		(void)memset(&utbuf, 0, sizeof(utbuf));
371b6cee71dSXin LI 		utbuf.actime = sb->st_atime;
372b6cee71dSXin LI 		utbuf.modtime = sb->st_mtime;
373b6cee71dSXin LI 		(void) utime(name, &utbuf); /* don't care if loses */
374b6cee71dSXin LI #endif
375b6cee71dSXin LI 	}
376b6cee71dSXin LI }
377b6cee71dSXin LI 
378b6cee71dSXin LI #ifndef COMPILE_ONLY
379b6cee71dSXin LI 
380b6cee71dSXin LI /*
381b6cee71dSXin LI  * find type of descriptor
382b6cee71dSXin LI  */
383b6cee71dSXin LI public const char *
384b6cee71dSXin LI magic_descriptor(struct magic_set *ms, int fd)
385b6cee71dSXin LI {
386b6cee71dSXin LI 	if (ms == NULL)
387b6cee71dSXin LI 		return NULL;
388b6cee71dSXin LI 	return file_or_fd(ms, NULL, fd);
389b6cee71dSXin LI }
390b6cee71dSXin LI 
391b6cee71dSXin LI /*
392b6cee71dSXin LI  * find type of named file
393b6cee71dSXin LI  */
394b6cee71dSXin LI public const char *
395b6cee71dSXin LI magic_file(struct magic_set *ms, const char *inname)
396b6cee71dSXin LI {
397b6cee71dSXin LI 	if (ms == NULL)
398b6cee71dSXin LI 		return NULL;
399b6cee71dSXin LI 	return file_or_fd(ms, inname, STDIN_FILENO);
400b6cee71dSXin LI }
401b6cee71dSXin LI 
402b6cee71dSXin LI private const char *
403b6cee71dSXin LI file_or_fd(struct magic_set *ms, const char *inname, int fd)
404b6cee71dSXin LI {
405b6cee71dSXin LI 	int	rv = -1;
406b6cee71dSXin LI 	unsigned char *buf;
407b6cee71dSXin LI 	struct stat	sb;
408b6cee71dSXin LI 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
409b6cee71dSXin LI 	int	ispipe = 0;
410b6cee71dSXin LI 	off_t	pos = (off_t)-1;
411b6cee71dSXin LI 
412*40427ccaSGordon Tetlow 	if (file_reset(ms, 1) == -1)
413b6cee71dSXin LI 		goto out;
414b6cee71dSXin LI 
415b6cee71dSXin LI 	/*
416b6cee71dSXin LI 	 * one extra for terminating '\0', and
417b6cee71dSXin LI 	 * some overlapping space for matches near EOF
418b6cee71dSXin LI 	 */
419b6cee71dSXin LI #define SLOP (1 + sizeof(union VALUETYPE))
4203e41d09dSXin LI 	if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
421b6cee71dSXin LI 		return NULL;
422b6cee71dSXin LI 
423b6cee71dSXin LI 	switch (file_fsmagic(ms, inname, &sb)) {
424b6cee71dSXin LI 	case -1:		/* error */
425b6cee71dSXin LI 		goto done;
426b6cee71dSXin LI 	case 0:			/* nothing found */
427b6cee71dSXin LI 		break;
428b6cee71dSXin LI 	default:		/* matched it and printed type */
429b6cee71dSXin LI 		rv = 0;
430b6cee71dSXin LI 		goto done;
431b6cee71dSXin LI 	}
432b6cee71dSXin LI 
433b6cee71dSXin LI #ifdef WIN32
434b6cee71dSXin LI 	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
435b6cee71dSXin LI 	if (fd == STDIN_FILENO)
436b6cee71dSXin LI 		_setmode(STDIN_FILENO, O_BINARY);
437b6cee71dSXin LI #endif
438b6cee71dSXin LI 
439b6cee71dSXin LI 	if (inname == NULL) {
440b6cee71dSXin LI 		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
441b6cee71dSXin LI 			ispipe = 1;
442b6cee71dSXin LI 		else
443b6cee71dSXin LI 			pos = lseek(fd, (off_t)0, SEEK_CUR);
444b6cee71dSXin LI 	} else {
445b6cee71dSXin LI 		int flags = O_RDONLY|O_BINARY;
446b6cee71dSXin LI 		int okstat = stat(inname, &sb) == 0;
447b6cee71dSXin LI 
448b6cee71dSXin LI 		if (okstat && S_ISFIFO(sb.st_mode)) {
449b6cee71dSXin LI #ifdef O_NONBLOCK
450b6cee71dSXin LI 			flags |= O_NONBLOCK;
451b6cee71dSXin LI #endif
452b6cee71dSXin LI 			ispipe = 1;
453b6cee71dSXin LI 		}
454b6cee71dSXin LI 
455b6cee71dSXin LI 		errno = 0;
456b6cee71dSXin LI 		if ((fd = open(inname, flags)) < 0) {
457b6cee71dSXin LI #ifdef WIN32
458b6cee71dSXin LI 			/*
459b6cee71dSXin LI 			 * Can't stat, can't open.  It may have been opened in
460b6cee71dSXin LI 			 * fsmagic, so if the user doesn't have read permission,
461b6cee71dSXin LI 			 * allow it to say so; otherwise an error was probably
462b6cee71dSXin LI 			 * displayed in fsmagic.
463b6cee71dSXin LI 			 */
464b6cee71dSXin LI 			if (!okstat && errno == EACCES) {
465b6cee71dSXin LI 				sb.st_mode = S_IFBLK;
466b6cee71dSXin LI 				okstat = 1;
467b6cee71dSXin LI 			}
468b6cee71dSXin LI #endif
469b6cee71dSXin LI 			if (okstat &&
470b6cee71dSXin LI 			    unreadable_info(ms, sb.st_mode, inname) == -1)
471b6cee71dSXin LI 				goto done;
472b6cee71dSXin LI 			rv = 0;
473b6cee71dSXin LI 			goto done;
474b6cee71dSXin LI 		}
475b6cee71dSXin LI #ifdef O_NONBLOCK
476b6cee71dSXin LI 		if ((flags = fcntl(fd, F_GETFL)) != -1) {
477b6cee71dSXin LI 			flags &= ~O_NONBLOCK;
478b6cee71dSXin LI 			(void)fcntl(fd, F_SETFL, flags);
479b6cee71dSXin LI 		}
480b6cee71dSXin LI #endif
481b6cee71dSXin LI 	}
482b6cee71dSXin LI 
483b6cee71dSXin LI 	/*
4843e41d09dSXin LI 	 * try looking at the first ms->bytes_max bytes
485b6cee71dSXin LI 	 */
486b6cee71dSXin LI 	if (ispipe) {
487b6cee71dSXin LI 		ssize_t r = 0;
488b6cee71dSXin LI 
489b6cee71dSXin LI 		while ((r = sread(fd, (void *)&buf[nbytes],
4903e41d09dSXin LI 		    (size_t)(ms->bytes_max - nbytes), 1)) > 0) {
491b6cee71dSXin LI 			nbytes += r;
492b6cee71dSXin LI 			if (r < PIPE_BUF) break;
493b6cee71dSXin LI 		}
494b6cee71dSXin LI 
495a5d223e6SXin LI 		if (nbytes == 0 && inname) {
496b6cee71dSXin LI 			/* We can not read it, but we were able to stat it. */
497b6cee71dSXin LI 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
498b6cee71dSXin LI 				goto done;
499b6cee71dSXin LI 			rv = 0;
500b6cee71dSXin LI 			goto done;
501b6cee71dSXin LI 		}
502b6cee71dSXin LI 
503b6cee71dSXin LI 	} else {
504b6cee71dSXin LI 		/* Windows refuses to read from a big console buffer. */
505b6cee71dSXin LI 		size_t howmany =
5063e41d09dSXin LI #if defined(WIN32)
507b6cee71dSXin LI 				_isatty(fd) ? 8 * 1024 :
508b6cee71dSXin LI #endif
5093e41d09dSXin LI 				ms->bytes_max;
510b6cee71dSXin LI 		if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
511b6cee71dSXin LI 			if (inname == NULL && fd != STDIN_FILENO)
512b6cee71dSXin LI 				file_error(ms, errno, "cannot read fd %d", fd);
513b6cee71dSXin LI 			else
514b6cee71dSXin LI 				file_error(ms, errno, "cannot read `%s'",
515b6cee71dSXin LI 				    inname == NULL ? "/dev/stdin" : inname);
516b6cee71dSXin LI 			goto done;
517b6cee71dSXin LI 		}
518b6cee71dSXin LI 	}
519b6cee71dSXin LI 
520b6cee71dSXin LI 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
521b6cee71dSXin LI 	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
522b6cee71dSXin LI 		goto done;
523b6cee71dSXin LI 	rv = 0;
524b6cee71dSXin LI done:
525b6cee71dSXin LI 	free(buf);
52620f8619dSXin LI 	if (fd != -1) {
527b6cee71dSXin LI 		if (pos != (off_t)-1)
528b6cee71dSXin LI 			(void)lseek(fd, pos, SEEK_SET);
529b6cee71dSXin LI 		close_and_restore(ms, inname, fd, &sb);
53020f8619dSXin LI 	}
531b6cee71dSXin LI out:
532b6cee71dSXin LI 	return rv == 0 ? file_getbuffer(ms) : NULL;
533b6cee71dSXin LI }
534b6cee71dSXin LI 
535b6cee71dSXin LI 
536b6cee71dSXin LI public const char *
537b6cee71dSXin LI magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
538b6cee71dSXin LI {
539b6cee71dSXin LI 	if (ms == NULL)
540b6cee71dSXin LI 		return NULL;
541*40427ccaSGordon Tetlow 	if (file_reset(ms, 1) == -1)
542b6cee71dSXin LI 		return NULL;
543b6cee71dSXin LI 	/*
544b6cee71dSXin LI 	 * The main work is done here!
545b6cee71dSXin LI 	 * We have the file name and/or the data buffer to be identified.
546b6cee71dSXin LI 	 */
547b6cee71dSXin LI 	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
548b6cee71dSXin LI 		return NULL;
549b6cee71dSXin LI 	}
550b6cee71dSXin LI 	return file_getbuffer(ms);
551b6cee71dSXin LI }
552b6cee71dSXin LI #endif
553b6cee71dSXin LI 
554b6cee71dSXin LI public const char *
555b6cee71dSXin LI magic_error(struct magic_set *ms)
556b6cee71dSXin LI {
557b6cee71dSXin LI 	if (ms == NULL)
558b6cee71dSXin LI 		return "Magic database is not open";
559b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
560b6cee71dSXin LI }
561b6cee71dSXin LI 
562b6cee71dSXin LI public int
563b6cee71dSXin LI magic_errno(struct magic_set *ms)
564b6cee71dSXin LI {
565b6cee71dSXin LI 	if (ms == NULL)
566b6cee71dSXin LI 		return EINVAL;
567b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
568b6cee71dSXin LI }
569b6cee71dSXin LI 
570b6cee71dSXin LI public int
571*40427ccaSGordon Tetlow magic_getflags(struct magic_set *ms)
572*40427ccaSGordon Tetlow {
573*40427ccaSGordon Tetlow 	if (ms == NULL)
574*40427ccaSGordon Tetlow 		return -1;
575*40427ccaSGordon Tetlow 
576*40427ccaSGordon Tetlow 	return ms->flags;
577*40427ccaSGordon Tetlow }
578*40427ccaSGordon Tetlow 
579*40427ccaSGordon Tetlow public int
580b6cee71dSXin LI magic_setflags(struct magic_set *ms, int flags)
581b6cee71dSXin LI {
582b6cee71dSXin LI 	if (ms == NULL)
583b6cee71dSXin LI 		return -1;
584b6cee71dSXin LI #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
585b6cee71dSXin LI 	if (flags & MAGIC_PRESERVE_ATIME)
586b6cee71dSXin LI 		return -1;
587b6cee71dSXin LI #endif
588b6cee71dSXin LI 	ms->flags = flags;
589b6cee71dSXin LI 	return 0;
590b6cee71dSXin LI }
591b6cee71dSXin LI 
592b6cee71dSXin LI public int
593b6cee71dSXin LI magic_version(void)
594b6cee71dSXin LI {
595b6cee71dSXin LI 	return MAGIC_VERSION;
596b6cee71dSXin LI }
597c2931133SXin LI 
598c2931133SXin LI public int
599c2931133SXin LI magic_setparam(struct magic_set *ms, int param, const void *val)
600c2931133SXin LI {
601c2931133SXin LI 	switch (param) {
602c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
6035f0216bdSXin LI 		ms->indir_max = (uint16_t)*(const size_t *)val;
604c2931133SXin LI 		return 0;
605c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
6065f0216bdSXin LI 		ms->name_max = (uint16_t)*(const size_t *)val;
607c2931133SXin LI 		return 0;
608c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
6095f0216bdSXin LI 		ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
610c2931133SXin LI 		return 0;
611c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
6125f0216bdSXin LI 		ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
613c2931133SXin LI 		return 0;
6144460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
6155f0216bdSXin LI 		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
6164460e5b0SXin LI 		return 0;
6179ce06829SXin LI 	case MAGIC_PARAM_REGEX_MAX:
6189ce06829SXin LI 		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
6199ce06829SXin LI 		return 0;
6203e41d09dSXin LI 	case MAGIC_PARAM_BYTES_MAX:
6213e41d09dSXin LI 		ms->bytes_max = *(const size_t *)val;
6223e41d09dSXin LI 		return 0;
623c2931133SXin LI 	default:
624c2931133SXin LI 		errno = EINVAL;
625c2931133SXin LI 		return -1;
626c2931133SXin LI 	}
627c2931133SXin LI }
628c2931133SXin LI 
629c2931133SXin LI public int
630c2931133SXin LI magic_getparam(struct magic_set *ms, int param, void *val)
631c2931133SXin LI {
632c2931133SXin LI 	switch (param) {
633c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
634c2931133SXin LI 		*(size_t *)val = ms->indir_max;
635c2931133SXin LI 		return 0;
636c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
637c2931133SXin LI 		*(size_t *)val = ms->name_max;
638c2931133SXin LI 		return 0;
639c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
640c2931133SXin LI 		*(size_t *)val = ms->elf_phnum_max;
641c2931133SXin LI 		return 0;
642c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
643c2931133SXin LI 		*(size_t *)val = ms->elf_shnum_max;
644c2931133SXin LI 		return 0;
6454460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
6464460e5b0SXin LI 		*(size_t *)val = ms->elf_notes_max;
6474460e5b0SXin LI 		return 0;
6489ce06829SXin LI 	case MAGIC_PARAM_REGEX_MAX:
6499ce06829SXin LI 		*(size_t *)val = ms->regex_max;
6509ce06829SXin LI 		return 0;
6513e41d09dSXin LI 	case MAGIC_PARAM_BYTES_MAX:
6523e41d09dSXin LI 		*(size_t *)val = ms->bytes_max;
6533e41d09dSXin LI 		return 0;
654c2931133SXin LI 	default:
655c2931133SXin LI 		errno = EINVAL;
656c2931133SXin LI 		return -1;
657c2931133SXin LI 	}
658c2931133SXin LI }
659