xref: /freebsd/contrib/file/src/magic.c (revision 5f0216bd)
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*5f0216bdSXin LI FILE_RCSID("@(#)$File: magic.c,v 1.93 2015/04/15 23:47:58 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 
86*5f0216bdSXin LI #ifdef WIN32
87*5f0216bdSXin LI /* HINSTANCE of this shared library. Needed for get_default_magic() */
88*5f0216bdSXin LI static HINSTANCE _w32_dll_instance = NULL;
89*5f0216bdSXin LI 
90*5f0216bdSXin LI static void
91*5f0216bdSXin LI _w32_append_path(char **hmagicpath, const char *fmt, ...)
92*5f0216bdSXin LI {
93*5f0216bdSXin LI 	char *tmppath;
94*5f0216bdSXin LI         char *newpath;
95*5f0216bdSXin LI 	va_list ap;
96*5f0216bdSXin LI 
97*5f0216bdSXin LI 	va_start(ap, fmt);
98*5f0216bdSXin LI 	if (vasprintf(&tmppath, fmt, ap) < 0) {
99*5f0216bdSXin LI 		va_end(ap);
100*5f0216bdSXin LI 		return;
101*5f0216bdSXin LI 	}
102*5f0216bdSXin LI 	va_end(ap);
103*5f0216bdSXin LI 
104*5f0216bdSXin LI 	if (access(tmppath, R_OK) == -1)
105*5f0216bdSXin LI 		goto out;
106*5f0216bdSXin LI 
107*5f0216bdSXin LI 	if (*hmagicpath == NULL) {
108*5f0216bdSXin LI 		*hmagicpath = tmppath;
109*5f0216bdSXin LI 		return;
110*5f0216bdSXin LI 	}
111*5f0216bdSXin LI 
112*5f0216bdSXin LI 	if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
113*5f0216bdSXin LI 		goto out;
114*5f0216bdSXin LI 
115*5f0216bdSXin LI 	free(*hmagicpath);
116*5f0216bdSXin LI 	free(tmppath);
117*5f0216bdSXin LI 	*hmagicpath = newpath;
118*5f0216bdSXin LI 	return;
119*5f0216bdSXin LI out:
120*5f0216bdSXin LI 	free(tmppath);
121*5f0216bdSXin LI }
122*5f0216bdSXin LI 
123*5f0216bdSXin LI static void
124*5f0216bdSXin LI _w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
125*5f0216bdSXin LI {
126*5f0216bdSXin LI 	static const char *trypaths[] = {
127*5f0216bdSXin LI 		"%s/share/misc/magic.mgc",
128*5f0216bdSXin LI 		"%s/magic.mgc",
129*5f0216bdSXin LI 	};
130*5f0216bdSXin LI 	LPSTR dllpath;
131*5f0216bdSXin LI 	size_t sp;
132*5f0216bdSXin LI 
133*5f0216bdSXin LI 	dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
134*5f0216bdSXin LI 
135*5f0216bdSXin LI 	if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
136*5f0216bdSXin LI 		goto out;
137*5f0216bdSXin LI 
138*5f0216bdSXin LI 	PathRemoveFileSpecA(dllpath);
139*5f0216bdSXin LI 
140*5f0216bdSXin LI 	sp = strlen(dllpath);
141*5f0216bdSXin LI 	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
142*5f0216bdSXin LI 		_w32_append_path(hmagicpath,
143*5f0216bdSXin LI 		    "%s/../share/misc/magic.mgc", dllpath);
144*5f0216bdSXin LI 		goto out;
145*5f0216bdSXin LI 	}
146*5f0216bdSXin LI 
147*5f0216bdSXin LI 	for (sp = 0; sp < __arraycount(trypaths); sp++)
148*5f0216bdSXin LI 		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
149*5f0216bdSXin LI out:
150*5f0216bdSXin LI 	free(dllpath);
151*5f0216bdSXin LI }
152*5f0216bdSXin LI 
153*5f0216bdSXin LI /* Placate GCC by offering a sacrificial previous prototype */
154*5f0216bdSXin LI BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
155*5f0216bdSXin LI 
156*5f0216bdSXin LI BOOL WINAPI
157*5f0216bdSXin LI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
158*5f0216bdSXin LI     LPVOID lpvReserved __attribute__((__unused__)))
159*5f0216bdSXin LI {
160*5f0216bdSXin LI 	if (fdwReason == DLL_PROCESS_ATTACH)
161*5f0216bdSXin LI 		_w32_dll_instance = hinstDLL;
162*5f0216bdSXin LI 	return TRUE;
163*5f0216bdSXin LI }
164*5f0216bdSXin LI #endif
165*5f0216bdSXin LI 
166b6cee71dSXin LI private const char *
167b6cee71dSXin LI get_default_magic(void)
168b6cee71dSXin LI {
169b6cee71dSXin LI 	static const char hmagic[] = "/.magic/magic.mgc";
170b6cee71dSXin LI 	static char *default_magic;
171b6cee71dSXin LI 	char *home, *hmagicpath;
172b6cee71dSXin LI 
173b6cee71dSXin LI #ifndef WIN32
174b6cee71dSXin LI 	struct stat st;
175b6cee71dSXin LI 
176b6cee71dSXin LI 	if (default_magic) {
177b6cee71dSXin LI 		free(default_magic);
178b6cee71dSXin LI 		default_magic = NULL;
179b6cee71dSXin LI 	}
180b6cee71dSXin LI 	if ((home = getenv("HOME")) == NULL)
181b6cee71dSXin LI 		return MAGIC;
182b6cee71dSXin LI 
183b6cee71dSXin LI 	if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
184b6cee71dSXin LI 		return MAGIC;
185b6cee71dSXin LI 	if (stat(hmagicpath, &st) == -1) {
186b6cee71dSXin LI 		free(hmagicpath);
187b6cee71dSXin LI 		if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
188b6cee71dSXin LI 			return MAGIC;
189b6cee71dSXin LI 		if (stat(hmagicpath, &st) == -1)
190b6cee71dSXin LI 			goto out;
191b6cee71dSXin LI 		if (S_ISDIR(st.st_mode)) {
192b6cee71dSXin LI 			free(hmagicpath);
193b6cee71dSXin LI 			if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
194b6cee71dSXin LI 				return MAGIC;
195b6cee71dSXin LI 			if (access(hmagicpath, R_OK) == -1)
196b6cee71dSXin LI 				goto out;
197b6cee71dSXin LI 		}
198b6cee71dSXin LI 	}
199b6cee71dSXin LI 
200b6cee71dSXin LI 	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
201b6cee71dSXin LI 		goto out;
202b6cee71dSXin LI 	free(hmagicpath);
203b6cee71dSXin LI 	return default_magic;
204b6cee71dSXin LI out:
205b6cee71dSXin LI 	default_magic = NULL;
206b6cee71dSXin LI 	free(hmagicpath);
207b6cee71dSXin LI 	return MAGIC;
208b6cee71dSXin LI #else
209b6cee71dSXin LI 	hmagicpath = NULL;
210b6cee71dSXin LI 
211b6cee71dSXin LI 	if (default_magic) {
212b6cee71dSXin LI 		free(default_magic);
213b6cee71dSXin LI 		default_magic = NULL;
214b6cee71dSXin LI 	}
215b6cee71dSXin LI 
216*5f0216bdSXin LI 	/* First, try to get a magic file from user-application data */
217*5f0216bdSXin LI 	if ((home = getenv("LOCALAPPDATA")) != NULL)
218*5f0216bdSXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
219*5f0216bdSXin LI 
220*5f0216bdSXin LI 	/* Second, try to get a magic file from the user profile data */
221b6cee71dSXin LI 	if ((home = getenv("USERPROFILE")) != NULL)
222*5f0216bdSXin LI 		_w32_append_path(&hmagicpath,
223*5f0216bdSXin LI 		    "%s/Local Settings/Application Data%s", home, hmagic);
224b6cee71dSXin LI 
225*5f0216bdSXin LI 	/* Third, try to get a magic file from Common Files */
226*5f0216bdSXin LI 	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
227*5f0216bdSXin LI 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
228b6cee71dSXin LI 
229*5f0216bdSXin LI 	/* Fourth, try to get magic file relative to exe location */
230*5f0216bdSXin LI         _w32_get_magic_relative_to(&hmagicpath, NULL);
231b6cee71dSXin LI 
232*5f0216bdSXin LI 	/* Fifth, try to get magic file relative to dll location */
233*5f0216bdSXin LI         _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
234b6cee71dSXin LI 
235*5f0216bdSXin LI 	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
236b6cee71dSXin LI 	default_magic = hmagicpath;
237b6cee71dSXin LI 	return default_magic;
238b6cee71dSXin LI #endif
239b6cee71dSXin LI }
240b6cee71dSXin LI 
241b6cee71dSXin LI public const char *
242b6cee71dSXin LI magic_getpath(const char *magicfile, int action)
243b6cee71dSXin LI {
244b6cee71dSXin LI 	if (magicfile != NULL)
245b6cee71dSXin LI 		return magicfile;
246b6cee71dSXin LI 
247b6cee71dSXin LI 	magicfile = getenv("MAGIC");
248b6cee71dSXin LI 	if (magicfile != NULL)
249b6cee71dSXin LI 		return magicfile;
250b6cee71dSXin LI 
251b6cee71dSXin LI 	return action == FILE_LOAD ? get_default_magic() : MAGIC;
252b6cee71dSXin LI }
253b6cee71dSXin LI 
254b6cee71dSXin LI public struct magic_set *
255b6cee71dSXin LI magic_open(int flags)
256b6cee71dSXin LI {
257b6cee71dSXin LI 	return file_ms_alloc(flags);
258b6cee71dSXin LI }
259b6cee71dSXin LI 
260b6cee71dSXin LI private int
261b6cee71dSXin LI unreadable_info(struct magic_set *ms, mode_t md, const char *file)
262b6cee71dSXin LI {
263b6cee71dSXin LI 	if (file) {
264b6cee71dSXin LI 		/* We cannot open it, but we were able to stat it. */
265b6cee71dSXin LI 		if (access(file, W_OK) == 0)
266b6cee71dSXin LI 			if (file_printf(ms, "writable, ") == -1)
267b6cee71dSXin LI 				return -1;
268b6cee71dSXin LI 		if (access(file, X_OK) == 0)
269b6cee71dSXin LI 			if (file_printf(ms, "executable, ") == -1)
270b6cee71dSXin LI 				return -1;
271b6cee71dSXin LI 	}
272b6cee71dSXin LI 	if (S_ISREG(md))
273b6cee71dSXin LI 		if (file_printf(ms, "regular file, ") == -1)
274b6cee71dSXin LI 			return -1;
275b6cee71dSXin LI 	if (file_printf(ms, "no read permission") == -1)
276b6cee71dSXin LI 		return -1;
277b6cee71dSXin LI 	return 0;
278b6cee71dSXin LI }
279b6cee71dSXin LI 
280b6cee71dSXin LI public void
281b6cee71dSXin LI magic_close(struct magic_set *ms)
282b6cee71dSXin LI {
283b6cee71dSXin LI 	if (ms == NULL)
284b6cee71dSXin LI 		return;
285b6cee71dSXin LI 	file_ms_free(ms);
286b6cee71dSXin LI }
287b6cee71dSXin LI 
288b6cee71dSXin LI /*
289b6cee71dSXin LI  * load a magic file
290b6cee71dSXin LI  */
291b6cee71dSXin LI public int
292b6cee71dSXin LI magic_load(struct magic_set *ms, const char *magicfile)
293b6cee71dSXin LI {
294b6cee71dSXin LI 	if (ms == NULL)
295b6cee71dSXin LI 		return -1;
296b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_LOAD);
297b6cee71dSXin LI }
298b6cee71dSXin LI 
299c2931133SXin LI #ifndef COMPILE_ONLY
300c2931133SXin LI /*
301c2931133SXin LI  * Install a set of compiled magic buffers.
302c2931133SXin LI  */
303c2931133SXin LI public int
304c2931133SXin LI magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
305c2931133SXin LI     size_t nbufs)
306c2931133SXin LI {
307c2931133SXin LI 	if (ms == NULL)
308c2931133SXin LI 		return -1;
309c2931133SXin LI 	return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
310c2931133SXin LI }
311c2931133SXin LI #endif
312c2931133SXin LI 
313b6cee71dSXin LI public int
314b6cee71dSXin LI magic_compile(struct magic_set *ms, const char *magicfile)
315b6cee71dSXin LI {
316b6cee71dSXin LI 	if (ms == NULL)
317b6cee71dSXin LI 		return -1;
318b6cee71dSXin LI 	return file_apprentice(ms, magicfile, FILE_COMPILE);
319b6cee71dSXin LI }
320b6cee71dSXin LI 
321b6cee71dSXin LI public int
322b6cee71dSXin LI magic_check(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_CHECK);
327b6cee71dSXin LI }
328b6cee71dSXin LI 
329b6cee71dSXin LI public int
330b6cee71dSXin LI magic_list(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_LIST);
335b6cee71dSXin LI }
336b6cee71dSXin LI 
337b6cee71dSXin LI private void
338b6cee71dSXin LI close_and_restore(const struct magic_set *ms, const char *name, int fd,
339b6cee71dSXin LI     const struct stat *sb)
340b6cee71dSXin LI {
341b6cee71dSXin LI 	if (fd == STDIN_FILENO || name == NULL)
342b6cee71dSXin LI 		return;
343b6cee71dSXin LI 	(void) close(fd);
344b6cee71dSXin LI 
345b6cee71dSXin LI 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
346b6cee71dSXin LI 		/*
347b6cee71dSXin LI 		 * Try to restore access, modification times if read it.
348b6cee71dSXin LI 		 * This is really *bad* because it will modify the status
349b6cee71dSXin LI 		 * time of the file... And of course this will affect
350b6cee71dSXin LI 		 * backup programs
351b6cee71dSXin LI 		 */
352b6cee71dSXin LI #ifdef HAVE_UTIMES
353b6cee71dSXin LI 		struct timeval  utsbuf[2];
354b6cee71dSXin LI 		(void)memset(utsbuf, 0, sizeof(utsbuf));
355b6cee71dSXin LI 		utsbuf[0].tv_sec = sb->st_atime;
356b6cee71dSXin LI 		utsbuf[1].tv_sec = sb->st_mtime;
357b6cee71dSXin LI 
358b6cee71dSXin LI 		(void) utimes(name, utsbuf); /* don't care if loses */
359b6cee71dSXin LI #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
360b6cee71dSXin LI 		struct utimbuf  utbuf;
361b6cee71dSXin LI 
362b6cee71dSXin LI 		(void)memset(&utbuf, 0, sizeof(utbuf));
363b6cee71dSXin LI 		utbuf.actime = sb->st_atime;
364b6cee71dSXin LI 		utbuf.modtime = sb->st_mtime;
365b6cee71dSXin LI 		(void) utime(name, &utbuf); /* don't care if loses */
366b6cee71dSXin LI #endif
367b6cee71dSXin LI 	}
368b6cee71dSXin LI }
369b6cee71dSXin LI 
370b6cee71dSXin LI #ifndef COMPILE_ONLY
371b6cee71dSXin LI 
372b6cee71dSXin LI /*
373b6cee71dSXin LI  * find type of descriptor
374b6cee71dSXin LI  */
375b6cee71dSXin LI public const char *
376b6cee71dSXin LI magic_descriptor(struct magic_set *ms, int fd)
377b6cee71dSXin LI {
378b6cee71dSXin LI 	if (ms == NULL)
379b6cee71dSXin LI 		return NULL;
380b6cee71dSXin LI 	return file_or_fd(ms, NULL, fd);
381b6cee71dSXin LI }
382b6cee71dSXin LI 
383b6cee71dSXin LI /*
384b6cee71dSXin LI  * find type of named file
385b6cee71dSXin LI  */
386b6cee71dSXin LI public const char *
387b6cee71dSXin LI magic_file(struct magic_set *ms, const char *inname)
388b6cee71dSXin LI {
389b6cee71dSXin LI 	if (ms == NULL)
390b6cee71dSXin LI 		return NULL;
391b6cee71dSXin LI 	return file_or_fd(ms, inname, STDIN_FILENO);
392b6cee71dSXin LI }
393b6cee71dSXin LI 
394b6cee71dSXin LI private const char *
395b6cee71dSXin LI file_or_fd(struct magic_set *ms, const char *inname, int fd)
396b6cee71dSXin LI {
397b6cee71dSXin LI 	int	rv = -1;
398b6cee71dSXin LI 	unsigned char *buf;
399b6cee71dSXin LI 	struct stat	sb;
400b6cee71dSXin LI 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
401b6cee71dSXin LI 	int	ispipe = 0;
402b6cee71dSXin LI 	off_t	pos = (off_t)-1;
403b6cee71dSXin LI 
404b6cee71dSXin LI 	if (file_reset(ms) == -1)
405b6cee71dSXin LI 		goto out;
406b6cee71dSXin LI 
407b6cee71dSXin LI 	/*
408b6cee71dSXin LI 	 * one extra for terminating '\0', and
409b6cee71dSXin LI 	 * some overlapping space for matches near EOF
410b6cee71dSXin LI 	 */
411b6cee71dSXin LI #define SLOP (1 + sizeof(union VALUETYPE))
412b6cee71dSXin LI 	if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
413b6cee71dSXin LI 		return NULL;
414b6cee71dSXin LI 
415b6cee71dSXin LI 	switch (file_fsmagic(ms, inname, &sb)) {
416b6cee71dSXin LI 	case -1:		/* error */
417b6cee71dSXin LI 		goto done;
418b6cee71dSXin LI 	case 0:			/* nothing found */
419b6cee71dSXin LI 		break;
420b6cee71dSXin LI 	default:		/* matched it and printed type */
421b6cee71dSXin LI 		rv = 0;
422b6cee71dSXin LI 		goto done;
423b6cee71dSXin LI 	}
424b6cee71dSXin LI 
425b6cee71dSXin LI #ifdef WIN32
426b6cee71dSXin LI 	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
427b6cee71dSXin LI 	if (fd == STDIN_FILENO)
428b6cee71dSXin LI 		_setmode(STDIN_FILENO, O_BINARY);
429b6cee71dSXin LI #endif
430b6cee71dSXin LI 
431b6cee71dSXin LI 	if (inname == NULL) {
432b6cee71dSXin LI 		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
433b6cee71dSXin LI 			ispipe = 1;
434b6cee71dSXin LI 		else
435b6cee71dSXin LI 			pos = lseek(fd, (off_t)0, SEEK_CUR);
436b6cee71dSXin LI 	} else {
437b6cee71dSXin LI 		int flags = O_RDONLY|O_BINARY;
438b6cee71dSXin LI 		int okstat = stat(inname, &sb) == 0;
439b6cee71dSXin LI 
440b6cee71dSXin LI 		if (okstat && S_ISFIFO(sb.st_mode)) {
441b6cee71dSXin LI #ifdef O_NONBLOCK
442b6cee71dSXin LI 			flags |= O_NONBLOCK;
443b6cee71dSXin LI #endif
444b6cee71dSXin LI 			ispipe = 1;
445b6cee71dSXin LI 		}
446b6cee71dSXin LI 
447b6cee71dSXin LI 		errno = 0;
448b6cee71dSXin LI 		if ((fd = open(inname, flags)) < 0) {
449b6cee71dSXin LI #ifdef WIN32
450b6cee71dSXin LI 			/*
451b6cee71dSXin LI 			 * Can't stat, can't open.  It may have been opened in
452b6cee71dSXin LI 			 * fsmagic, so if the user doesn't have read permission,
453b6cee71dSXin LI 			 * allow it to say so; otherwise an error was probably
454b6cee71dSXin LI 			 * displayed in fsmagic.
455b6cee71dSXin LI 			 */
456b6cee71dSXin LI 			if (!okstat && errno == EACCES) {
457b6cee71dSXin LI 				sb.st_mode = S_IFBLK;
458b6cee71dSXin LI 				okstat = 1;
459b6cee71dSXin LI 			}
460b6cee71dSXin LI #endif
461b6cee71dSXin LI 			if (okstat &&
462b6cee71dSXin LI 			    unreadable_info(ms, sb.st_mode, inname) == -1)
463b6cee71dSXin LI 				goto done;
464b6cee71dSXin LI 			rv = 0;
465b6cee71dSXin LI 			goto done;
466b6cee71dSXin LI 		}
467b6cee71dSXin LI #ifdef O_NONBLOCK
468b6cee71dSXin LI 		if ((flags = fcntl(fd, F_GETFL)) != -1) {
469b6cee71dSXin LI 			flags &= ~O_NONBLOCK;
470b6cee71dSXin LI 			(void)fcntl(fd, F_SETFL, flags);
471b6cee71dSXin LI 		}
472b6cee71dSXin LI #endif
473b6cee71dSXin LI 	}
474b6cee71dSXin LI 
475b6cee71dSXin LI 	/*
476b6cee71dSXin LI 	 * try looking at the first HOWMANY bytes
477b6cee71dSXin LI 	 */
478b6cee71dSXin LI 	if (ispipe) {
479b6cee71dSXin LI 		ssize_t r = 0;
480b6cee71dSXin LI 
481b6cee71dSXin LI 		while ((r = sread(fd, (void *)&buf[nbytes],
482b6cee71dSXin LI 		    (size_t)(HOWMANY - nbytes), 1)) > 0) {
483b6cee71dSXin LI 			nbytes += r;
484b6cee71dSXin LI 			if (r < PIPE_BUF) break;
485b6cee71dSXin LI 		}
486b6cee71dSXin LI 
487b6cee71dSXin LI 		if (nbytes == 0) {
488b6cee71dSXin LI 			/* We can not read it, but we were able to stat it. */
489b6cee71dSXin LI 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
490b6cee71dSXin LI 				goto done;
491b6cee71dSXin LI 			rv = 0;
492b6cee71dSXin LI 			goto done;
493b6cee71dSXin LI 		}
494b6cee71dSXin LI 
495b6cee71dSXin LI 	} else {
496b6cee71dSXin LI 		/* Windows refuses to read from a big console buffer. */
497b6cee71dSXin LI 		size_t howmany =
498b6cee71dSXin LI #if defined(WIN32) && HOWMANY > 8 * 1024
499b6cee71dSXin LI 				_isatty(fd) ? 8 * 1024 :
500b6cee71dSXin LI #endif
501b6cee71dSXin LI 				HOWMANY;
502b6cee71dSXin LI 		if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
503b6cee71dSXin LI 			if (inname == NULL && fd != STDIN_FILENO)
504b6cee71dSXin LI 				file_error(ms, errno, "cannot read fd %d", fd);
505b6cee71dSXin LI 			else
506b6cee71dSXin LI 				file_error(ms, errno, "cannot read `%s'",
507b6cee71dSXin LI 				    inname == NULL ? "/dev/stdin" : inname);
508b6cee71dSXin LI 			goto done;
509b6cee71dSXin LI 		}
510b6cee71dSXin LI 	}
511b6cee71dSXin LI 
512b6cee71dSXin LI 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
513b6cee71dSXin LI 	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
514b6cee71dSXin LI 		goto done;
515b6cee71dSXin LI 	rv = 0;
516b6cee71dSXin LI done:
517b6cee71dSXin LI 	free(buf);
518b6cee71dSXin LI 	if (pos != (off_t)-1)
519b6cee71dSXin LI 		(void)lseek(fd, pos, SEEK_SET);
520b6cee71dSXin LI 	close_and_restore(ms, inname, fd, &sb);
521b6cee71dSXin LI out:
522b6cee71dSXin LI 	return rv == 0 ? file_getbuffer(ms) : NULL;
523b6cee71dSXin LI }
524b6cee71dSXin LI 
525b6cee71dSXin LI 
526b6cee71dSXin LI public const char *
527b6cee71dSXin LI magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
528b6cee71dSXin LI {
529b6cee71dSXin LI 	if (ms == NULL)
530b6cee71dSXin LI 		return NULL;
531b6cee71dSXin LI 	if (file_reset(ms) == -1)
532b6cee71dSXin LI 		return NULL;
533b6cee71dSXin LI 	/*
534b6cee71dSXin LI 	 * The main work is done here!
535b6cee71dSXin LI 	 * We have the file name and/or the data buffer to be identified.
536b6cee71dSXin LI 	 */
537b6cee71dSXin LI 	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
538b6cee71dSXin LI 		return NULL;
539b6cee71dSXin LI 	}
540b6cee71dSXin LI 	return file_getbuffer(ms);
541b6cee71dSXin LI }
542b6cee71dSXin LI #endif
543b6cee71dSXin LI 
544b6cee71dSXin LI public const char *
545b6cee71dSXin LI magic_error(struct magic_set *ms)
546b6cee71dSXin LI {
547b6cee71dSXin LI 	if (ms == NULL)
548b6cee71dSXin LI 		return "Magic database is not open";
549b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
550b6cee71dSXin LI }
551b6cee71dSXin LI 
552b6cee71dSXin LI public int
553b6cee71dSXin LI magic_errno(struct magic_set *ms)
554b6cee71dSXin LI {
555b6cee71dSXin LI 	if (ms == NULL)
556b6cee71dSXin LI 		return EINVAL;
557b6cee71dSXin LI 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
558b6cee71dSXin LI }
559b6cee71dSXin LI 
560b6cee71dSXin LI public int
561b6cee71dSXin LI magic_setflags(struct magic_set *ms, int flags)
562b6cee71dSXin LI {
563b6cee71dSXin LI 	if (ms == NULL)
564b6cee71dSXin LI 		return -1;
565b6cee71dSXin LI #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
566b6cee71dSXin LI 	if (flags & MAGIC_PRESERVE_ATIME)
567b6cee71dSXin LI 		return -1;
568b6cee71dSXin LI #endif
569b6cee71dSXin LI 	ms->flags = flags;
570b6cee71dSXin LI 	return 0;
571b6cee71dSXin LI }
572b6cee71dSXin LI 
573b6cee71dSXin LI public int
574b6cee71dSXin LI magic_version(void)
575b6cee71dSXin LI {
576b6cee71dSXin LI 	return MAGIC_VERSION;
577b6cee71dSXin LI }
578c2931133SXin LI 
579c2931133SXin LI public int
580c2931133SXin LI magic_setparam(struct magic_set *ms, int param, const void *val)
581c2931133SXin LI {
582c2931133SXin LI 	switch (param) {
583c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
584*5f0216bdSXin LI 		ms->indir_max = (uint16_t)*(const size_t *)val;
585c2931133SXin LI 		return 0;
586c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
587*5f0216bdSXin LI 		ms->name_max = (uint16_t)*(const size_t *)val;
588c2931133SXin LI 		return 0;
589c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
590*5f0216bdSXin LI 		ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
591c2931133SXin LI 		return 0;
592c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
593*5f0216bdSXin LI 		ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
594c2931133SXin LI 		return 0;
5954460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
596*5f0216bdSXin LI 		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
5974460e5b0SXin LI 		return 0;
598c2931133SXin LI 	default:
599c2931133SXin LI 		errno = EINVAL;
600c2931133SXin LI 		return -1;
601c2931133SXin LI 	}
602c2931133SXin LI }
603c2931133SXin LI 
604c2931133SXin LI public int
605c2931133SXin LI magic_getparam(struct magic_set *ms, int param, void *val)
606c2931133SXin LI {
607c2931133SXin LI 	switch (param) {
608c2931133SXin LI 	case MAGIC_PARAM_INDIR_MAX:
609c2931133SXin LI 		*(size_t *)val = ms->indir_max;
610c2931133SXin LI 		return 0;
611c2931133SXin LI 	case MAGIC_PARAM_NAME_MAX:
612c2931133SXin LI 		*(size_t *)val = ms->name_max;
613c2931133SXin LI 		return 0;
614c2931133SXin LI 	case MAGIC_PARAM_ELF_PHNUM_MAX:
615c2931133SXin LI 		*(size_t *)val = ms->elf_phnum_max;
616c2931133SXin LI 		return 0;
617c2931133SXin LI 	case MAGIC_PARAM_ELF_SHNUM_MAX:
618c2931133SXin LI 		*(size_t *)val = ms->elf_shnum_max;
619c2931133SXin LI 		return 0;
6204460e5b0SXin LI 	case MAGIC_PARAM_ELF_NOTES_MAX:
6214460e5b0SXin LI 		*(size_t *)val = ms->elf_notes_max;
6224460e5b0SXin LI 		return 0;
623c2931133SXin LI 	default:
624c2931133SXin LI 		errno = EINVAL;
625c2931133SXin LI 		return -1;
626c2931133SXin LI 	}
627c2931133SXin LI }
628