magic.c revision 284237
1133359Sobrien/*
2133359Sobrien * Copyright (c) Christos Zoulas 2003.
3133359Sobrien * All Rights Reserved.
4186690Sobrien *
5133359Sobrien * Redistribution and use in source and binary forms, with or without
6133359Sobrien * modification, are permitted provided that the following conditions
7133359Sobrien * are met:
8133359Sobrien * 1. Redistributions of source code must retain the above copyright
9133359Sobrien *    notice immediately at the beginning of the file, without modification,
10133359Sobrien *    this list of conditions, and the following disclaimer.
11133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
12133359Sobrien *    notice, this list of conditions and the following disclaimer in the
13133359Sobrien *    documentation and/or other materials provided with the distribution.
14186690Sobrien *
15133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25133359Sobrien * SUCH DAMAGE.
26133359Sobrien */
27133359Sobrien
28226048Sobrien#ifdef WIN32
29226048Sobrien#include <windows.h>
30226048Sobrien#include <shlwapi.h>
31226048Sobrien#endif
32226048Sobrien
33133359Sobrien#include "file.h"
34191736Sobrien
35191736Sobrien#ifndef	lint
36284237SdelphijFILE_RCSID("@(#)$File: magic.c,v 1.93 2015/04/15 23:47:58 christos Exp $")
37191736Sobrien#endif	/* lint */
38191736Sobrien
39133359Sobrien#include "magic.h"
40133359Sobrien
41133359Sobrien#include <stdlib.h>
42133359Sobrien#include <unistd.h>
43133359Sobrien#include <string.h>
44133359Sobrien#ifdef QUICK
45133359Sobrien#include <sys/mman.h>
46133359Sobrien#endif
47186690Sobrien#ifdef HAVE_LIMITS_H
48169942Sobrien#include <limits.h>	/* for PIPE_BUF */
49186690Sobrien#endif
50133359Sobrien
51133359Sobrien#if defined(HAVE_UTIMES)
52133359Sobrien# include <sys/time.h>
53133359Sobrien#elif defined(HAVE_UTIME)
54133359Sobrien# if defined(HAVE_SYS_UTIME_H)
55133359Sobrien#  include <sys/utime.h>
56133359Sobrien# elif defined(HAVE_UTIME_H)
57133359Sobrien#  include <utime.h>
58133359Sobrien# endif
59133359Sobrien#endif
60133359Sobrien
61133359Sobrien#ifdef HAVE_UNISTD_H
62133359Sobrien#include <unistd.h>	/* for read() */
63133359Sobrien#endif
64133359Sobrien
65186690Sobrien#ifndef PIPE_BUF
66186690Sobrien/* Get the PIPE_BUF from pathconf */
67186690Sobrien#ifdef _PC_PIPE_BUF
68186690Sobrien#define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
69186690Sobrien#else
70186690Sobrien#define PIPE_BUF 512
71186690Sobrien#endif
72186690Sobrien#endif
73186690Sobrien
74133359Sobrienprivate void close_and_restore(const struct magic_set *, const char *, int,
75133359Sobrien    const struct stat *);
76186690Sobrienprivate int unreadable_info(struct magic_set *, mode_t, const char *);
77226048Sobrienprivate const char* get_default_magic(void);
78175296Sobrien#ifndef COMPILE_ONLY
79175296Sobrienprivate const char *file_or_fd(struct magic_set *, const char *, int);
80175296Sobrien#endif
81133359Sobrien
82159764Sobrien#ifndef	STDIN_FILENO
83159764Sobrien#define	STDIN_FILENO	0
84159764Sobrien#endif
85159764Sobrien
86284237Sdelphij#ifdef WIN32
87284237Sdelphij/* HINSTANCE of this shared library. Needed for get_default_magic() */
88284237Sdelphijstatic HINSTANCE _w32_dll_instance = NULL;
89284237Sdelphij
90284237Sdelphijstatic void
91284237Sdelphij_w32_append_path(char **hmagicpath, const char *fmt, ...)
92284237Sdelphij{
93284237Sdelphij	char *tmppath;
94284237Sdelphij        char *newpath;
95284237Sdelphij	va_list ap;
96284237Sdelphij
97284237Sdelphij	va_start(ap, fmt);
98284237Sdelphij	if (vasprintf(&tmppath, fmt, ap) < 0) {
99284237Sdelphij		va_end(ap);
100284237Sdelphij		return;
101284237Sdelphij	}
102284237Sdelphij	va_end(ap);
103284237Sdelphij
104284237Sdelphij	if (access(tmppath, R_OK) == -1)
105284237Sdelphij		goto out;
106284237Sdelphij
107284237Sdelphij	if (*hmagicpath == NULL) {
108284237Sdelphij		*hmagicpath = tmppath;
109284237Sdelphij		return;
110284237Sdelphij	}
111284237Sdelphij
112284237Sdelphij	if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
113284237Sdelphij		goto out;
114284237Sdelphij
115284237Sdelphij	free(*hmagicpath);
116284237Sdelphij	free(tmppath);
117284237Sdelphij	*hmagicpath = newpath;
118284237Sdelphij	return;
119284237Sdelphijout:
120284237Sdelphij	free(tmppath);
121284237Sdelphij}
122284237Sdelphij
123284237Sdelphijstatic void
124284237Sdelphij_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
125284237Sdelphij{
126284237Sdelphij	static const char *trypaths[] = {
127284237Sdelphij		"%s/share/misc/magic.mgc",
128284237Sdelphij		"%s/magic.mgc",
129284237Sdelphij	};
130284237Sdelphij	LPSTR dllpath;
131284237Sdelphij	size_t sp;
132284237Sdelphij
133284237Sdelphij	dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
134284237Sdelphij
135284237Sdelphij	if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
136284237Sdelphij		goto out;
137284237Sdelphij
138284237Sdelphij	PathRemoveFileSpecA(dllpath);
139284237Sdelphij
140284237Sdelphij	sp = strlen(dllpath);
141284237Sdelphij	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
142284237Sdelphij		_w32_append_path(hmagicpath,
143284237Sdelphij		    "%s/../share/misc/magic.mgc", dllpath);
144284237Sdelphij		goto out;
145284237Sdelphij	}
146284237Sdelphij
147284237Sdelphij	for (sp = 0; sp < __arraycount(trypaths); sp++)
148284237Sdelphij		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
149284237Sdelphijout:
150284237Sdelphij	free(dllpath);
151284237Sdelphij}
152284237Sdelphij
153284237Sdelphij/* Placate GCC by offering a sacrificial previous prototype */
154284237SdelphijBOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
155284237Sdelphij
156284237SdelphijBOOL WINAPI
157284237SdelphijDllMain(HINSTANCE hinstDLL, DWORD fdwReason,
158284237Sdelphij    LPVOID lpvReserved __attribute__((__unused__)))
159284237Sdelphij{
160284237Sdelphij	if (fdwReason == DLL_PROCESS_ATTACH)
161284237Sdelphij		_w32_dll_instance = hinstDLL;
162284237Sdelphij	return TRUE;
163284237Sdelphij}
164284237Sdelphij#endif
165284237Sdelphij
166226048Sobrienprivate const char *
167226048Sobrienget_default_magic(void)
168226048Sobrien{
169226048Sobrien	static const char hmagic[] = "/.magic/magic.mgc";
170226048Sobrien	static char *default_magic;
171226048Sobrien	char *home, *hmagicpath;
172226048Sobrien
173226048Sobrien#ifndef WIN32
174226048Sobrien	struct stat st;
175226048Sobrien
176226048Sobrien	if (default_magic) {
177226048Sobrien		free(default_magic);
178226048Sobrien		default_magic = NULL;
179226048Sobrien	}
180226048Sobrien	if ((home = getenv("HOME")) == NULL)
181226048Sobrien		return MAGIC;
182226048Sobrien
183267843Sdelphij	if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
184226048Sobrien		return MAGIC;
185267843Sdelphij	if (stat(hmagicpath, &st) == -1) {
186226048Sobrien		free(hmagicpath);
187267843Sdelphij		if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
188226048Sobrien			return MAGIC;
189267843Sdelphij		if (stat(hmagicpath, &st) == -1)
190226048Sobrien			goto out;
191267843Sdelphij		if (S_ISDIR(st.st_mode)) {
192267843Sdelphij			free(hmagicpath);
193267843Sdelphij			if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
194267843Sdelphij				return MAGIC;
195267843Sdelphij			if (access(hmagicpath, R_OK) == -1)
196267843Sdelphij				goto out;
197267843Sdelphij		}
198226048Sobrien	}
199226048Sobrien
200226048Sobrien	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
201226048Sobrien		goto out;
202226048Sobrien	free(hmagicpath);
203226048Sobrien	return default_magic;
204226048Sobrienout:
205226048Sobrien	default_magic = NULL;
206226048Sobrien	free(hmagicpath);
207226048Sobrien	return MAGIC;
208226048Sobrien#else
209267843Sdelphij	hmagicpath = NULL;
210226048Sobrien
211226048Sobrien	if (default_magic) {
212226048Sobrien		free(default_magic);
213226048Sobrien		default_magic = NULL;
214226048Sobrien	}
215226048Sobrien
216284237Sdelphij	/* First, try to get a magic file from user-application data */
217284237Sdelphij	if ((home = getenv("LOCALAPPDATA")) != NULL)
218284237Sdelphij		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
219226048Sobrien
220284237Sdelphij	/* Second, try to get a magic file from the user profile data */
221284237Sdelphij	if ((home = getenv("USERPROFILE")) != NULL)
222284237Sdelphij		_w32_append_path(&hmagicpath,
223284237Sdelphij		    "%s/Local Settings/Application Data%s", home, hmagic);
224226048Sobrien
225284237Sdelphij	/* Third, try to get a magic file from Common Files */
226284237Sdelphij	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
227284237Sdelphij		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
228226048Sobrien
229284237Sdelphij	/* Fourth, try to get magic file relative to exe location */
230284237Sdelphij        _w32_get_magic_relative_to(&hmagicpath, NULL);
231226048Sobrien
232284237Sdelphij	/* Fifth, try to get magic file relative to dll location */
233284237Sdelphij        _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
234284237Sdelphij
235284237Sdelphij	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
236226048Sobrien	default_magic = hmagicpath;
237226048Sobrien	return default_magic;
238226048Sobrien#endif
239226048Sobrien}
240226048Sobrien
241226048Sobrienpublic const char *
242226048Sobrienmagic_getpath(const char *magicfile, int action)
243226048Sobrien{
244226048Sobrien	if (magicfile != NULL)
245226048Sobrien		return magicfile;
246226048Sobrien
247226048Sobrien	magicfile = getenv("MAGIC");
248226048Sobrien	if (magicfile != NULL)
249226048Sobrien		return magicfile;
250226048Sobrien
251226048Sobrien	return action == FILE_LOAD ? get_default_magic() : MAGIC;
252226048Sobrien}
253226048Sobrien
254133359Sobrienpublic struct magic_set *
255133359Sobrienmagic_open(int flags)
256133359Sobrien{
257267843Sdelphij	return file_ms_alloc(flags);
258133359Sobrien}
259133359Sobrien
260169942Sobrienprivate int
261186690Sobrienunreadable_info(struct magic_set *ms, mode_t md, const char *file)
262169942Sobrien{
263267843Sdelphij	if (file) {
264267843Sdelphij		/* We cannot open it, but we were able to stat it. */
265267843Sdelphij		if (access(file, W_OK) == 0)
266267843Sdelphij			if (file_printf(ms, "writable, ") == -1)
267267843Sdelphij				return -1;
268267843Sdelphij		if (access(file, X_OK) == 0)
269267843Sdelphij			if (file_printf(ms, "executable, ") == -1)
270267843Sdelphij				return -1;
271267843Sdelphij	}
272169942Sobrien	if (S_ISREG(md))
273169942Sobrien		if (file_printf(ms, "regular file, ") == -1)
274169942Sobrien			return -1;
275169942Sobrien	if (file_printf(ms, "no read permission") == -1)
276169942Sobrien		return -1;
277169942Sobrien	return 0;
278169942Sobrien}
279169942Sobrien
280133359Sobrienpublic void
281169942Sobrienmagic_close(struct magic_set *ms)
282133359Sobrien{
283267843Sdelphij	if (ms == NULL)
284267843Sdelphij		return;
285267843Sdelphij	file_ms_free(ms);
286133359Sobrien}
287133359Sobrien
288133359Sobrien/*
289133359Sobrien * load a magic file
290133359Sobrien */
291133359Sobrienpublic int
292133359Sobrienmagic_load(struct magic_set *ms, const char *magicfile)
293133359Sobrien{
294267843Sdelphij	if (ms == NULL)
295267843Sdelphij		return -1;
296267843Sdelphij	return file_apprentice(ms, magicfile, FILE_LOAD);
297133359Sobrien}
298133359Sobrien
299275698Sdelphij#ifndef COMPILE_ONLY
300275698Sdelphij/*
301275698Sdelphij * Install a set of compiled magic buffers.
302275698Sdelphij */
303133359Sobrienpublic int
304275698Sdelphijmagic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
305275698Sdelphij    size_t nbufs)
306275698Sdelphij{
307275698Sdelphij	if (ms == NULL)
308275698Sdelphij		return -1;
309275698Sdelphij	return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
310275698Sdelphij}
311275698Sdelphij#endif
312275698Sdelphij
313275698Sdelphijpublic int
314133359Sobrienmagic_compile(struct magic_set *ms, const char *magicfile)
315133359Sobrien{
316267843Sdelphij	if (ms == NULL)
317267843Sdelphij		return -1;
318267843Sdelphij	return file_apprentice(ms, magicfile, FILE_COMPILE);
319133359Sobrien}
320133359Sobrien
321133359Sobrienpublic int
322133359Sobrienmagic_check(struct magic_set *ms, const char *magicfile)
323133359Sobrien{
324267843Sdelphij	if (ms == NULL)
325267843Sdelphij		return -1;
326267843Sdelphij	return file_apprentice(ms, magicfile, FILE_CHECK);
327133359Sobrien}
328133359Sobrien
329226048Sobrienpublic int
330226048Sobrienmagic_list(struct magic_set *ms, const char *magicfile)
331226048Sobrien{
332267843Sdelphij	if (ms == NULL)
333267843Sdelphij		return -1;
334267843Sdelphij	return file_apprentice(ms, magicfile, FILE_LIST);
335226048Sobrien}
336226048Sobrien
337133359Sobrienprivate void
338133359Sobrienclose_and_restore(const struct magic_set *ms, const char *name, int fd,
339133359Sobrien    const struct stat *sb)
340133359Sobrien{
341267843Sdelphij	if (fd == STDIN_FILENO || name == NULL)
342159764Sobrien		return;
343133359Sobrien	(void) close(fd);
344159764Sobrien
345159764Sobrien	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
346133359Sobrien		/*
347133359Sobrien		 * Try to restore access, modification times if read it.
348133359Sobrien		 * This is really *bad* because it will modify the status
349133359Sobrien		 * time of the file... And of course this will affect
350133359Sobrien		 * backup programs
351133359Sobrien		 */
352133359Sobrien#ifdef HAVE_UTIMES
353133359Sobrien		struct timeval  utsbuf[2];
354186690Sobrien		(void)memset(utsbuf, 0, sizeof(utsbuf));
355133359Sobrien		utsbuf[0].tv_sec = sb->st_atime;
356133359Sobrien		utsbuf[1].tv_sec = sb->st_mtime;
357133359Sobrien
358133359Sobrien		(void) utimes(name, utsbuf); /* don't care if loses */
359133359Sobrien#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
360133359Sobrien		struct utimbuf  utbuf;
361133359Sobrien
362191736Sobrien		(void)memset(&utbuf, 0, sizeof(utbuf));
363133359Sobrien		utbuf.actime = sb->st_atime;
364133359Sobrien		utbuf.modtime = sb->st_mtime;
365133359Sobrien		(void) utime(name, &utbuf); /* don't care if loses */
366133359Sobrien#endif
367133359Sobrien	}
368133359Sobrien}
369133359Sobrien
370133359Sobrien#ifndef COMPILE_ONLY
371175296Sobrien
372133359Sobrien/*
373175296Sobrien * find type of descriptor
374175296Sobrien */
375175296Sobrienpublic const char *
376175296Sobrienmagic_descriptor(struct magic_set *ms, int fd)
377175296Sobrien{
378267843Sdelphij	if (ms == NULL)
379267843Sdelphij		return NULL;
380175296Sobrien	return file_or_fd(ms, NULL, fd);
381175296Sobrien}
382175296Sobrien
383175296Sobrien/*
384133359Sobrien * find type of named file
385133359Sobrien */
386133359Sobrienpublic const char *
387133359Sobrienmagic_file(struct magic_set *ms, const char *inname)
388133359Sobrien{
389267843Sdelphij	if (ms == NULL)
390267843Sdelphij		return NULL;
391175296Sobrien	return file_or_fd(ms, inname, STDIN_FILENO);
392175296Sobrien}
393175296Sobrien
394175296Sobrienprivate const char *
395175296Sobrienfile_or_fd(struct magic_set *ms, const char *inname, int fd)
396175296Sobrien{
397159764Sobrien	int	rv = -1;
398159764Sobrien	unsigned char *buf;
399133359Sobrien	struct stat	sb;
400133359Sobrien	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
401169942Sobrien	int	ispipe = 0;
402267843Sdelphij	off_t	pos = (off_t)-1;
403133359Sobrien
404267843Sdelphij	if (file_reset(ms) == -1)
405267843Sdelphij		goto out;
406267843Sdelphij
407159764Sobrien	/*
408159764Sobrien	 * one extra for terminating '\0', and
409159764Sobrien	 * some overlapping space for matches near EOF
410159764Sobrien	 */
411159764Sobrien#define SLOP (1 + sizeof(union VALUETYPE))
412186690Sobrien	if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
413133359Sobrien		return NULL;
414133359Sobrien
415133359Sobrien	switch (file_fsmagic(ms, inname, &sb)) {
416169942Sobrien	case -1:		/* error */
417159764Sobrien		goto done;
418169942Sobrien	case 0:			/* nothing found */
419133359Sobrien		break;
420169942Sobrien	default:		/* matched it and printed type */
421159764Sobrien		rv = 0;
422159764Sobrien		goto done;
423133359Sobrien	}
424133359Sobrien
425267843Sdelphij#ifdef WIN32
426267843Sdelphij	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
427267843Sdelphij	if (fd == STDIN_FILENO)
428267843Sdelphij		_setmode(STDIN_FILENO, O_BINARY);
429267843Sdelphij#endif
430267843Sdelphij
431169942Sobrien	if (inname == NULL) {
432169942Sobrien		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
433169942Sobrien			ispipe = 1;
434267843Sdelphij		else
435267843Sdelphij			pos = lseek(fd, (off_t)0, SEEK_CUR);
436169942Sobrien	} else {
437169942Sobrien		int flags = O_RDONLY|O_BINARY;
438267843Sdelphij		int okstat = stat(inname, &sb) == 0;
439169942Sobrien
440267843Sdelphij		if (okstat && S_ISFIFO(sb.st_mode)) {
441226048Sobrien#ifdef O_NONBLOCK
442169942Sobrien			flags |= O_NONBLOCK;
443226048Sobrien#endif
444169942Sobrien			ispipe = 1;
445169942Sobrien		}
446169942Sobrien
447169942Sobrien		errno = 0;
448169942Sobrien		if ((fd = open(inname, flags)) < 0) {
449267843Sdelphij#ifdef WIN32
450267843Sdelphij			/*
451267843Sdelphij			 * Can't stat, can't open.  It may have been opened in
452267843Sdelphij			 * fsmagic, so if the user doesn't have read permission,
453267843Sdelphij			 * allow it to say so; otherwise an error was probably
454267843Sdelphij			 * displayed in fsmagic.
455267843Sdelphij			 */
456267843Sdelphij			if (!okstat && errno == EACCES) {
457267843Sdelphij				sb.st_mode = S_IFBLK;
458267843Sdelphij				okstat = 1;
459267843Sdelphij			}
460267843Sdelphij#endif
461267843Sdelphij			if (okstat &&
462267843Sdelphij			    unreadable_info(ms, sb.st_mode, inname) == -1)
463175296Sobrien				goto done;
464192348Sdelphij			rv = 0;
465192348Sdelphij			goto done;
466169942Sobrien		}
467169942Sobrien#ifdef O_NONBLOCK
468169942Sobrien		if ((flags = fcntl(fd, F_GETFL)) != -1) {
469169942Sobrien			flags &= ~O_NONBLOCK;
470169942Sobrien			(void)fcntl(fd, F_SETFL, flags);
471169942Sobrien		}
472169942Sobrien#endif
473133359Sobrien	}
474133359Sobrien
475133359Sobrien	/*
476133359Sobrien	 * try looking at the first HOWMANY bytes
477133359Sobrien	 */
478169942Sobrien	if (ispipe) {
479169942Sobrien		ssize_t r = 0;
480169942Sobrien
481169942Sobrien		while ((r = sread(fd, (void *)&buf[nbytes],
482169962Sobrien		    (size_t)(HOWMANY - nbytes), 1)) > 0) {
483169942Sobrien			nbytes += r;
484169942Sobrien			if (r < PIPE_BUF) break;
485169942Sobrien		}
486169942Sobrien
487169942Sobrien		if (nbytes == 0) {
488169942Sobrien			/* We can not read it, but we were able to stat it. */
489186690Sobrien			if (unreadable_info(ms, sb.st_mode, inname) == -1)
490169942Sobrien				goto done;
491169942Sobrien			rv = 0;
492169942Sobrien			goto done;
493169942Sobrien		}
494169942Sobrien
495169942Sobrien	} else {
496267843Sdelphij		/* Windows refuses to read from a big console buffer. */
497267843Sdelphij		size_t howmany =
498267843Sdelphij#if defined(WIN32) && HOWMANY > 8 * 1024
499267843Sdelphij				_isatty(fd) ? 8 * 1024 :
500267843Sdelphij#endif
501267843Sdelphij				HOWMANY;
502267843Sdelphij		if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
503267843Sdelphij			if (inname == NULL && fd != STDIN_FILENO)
504267843Sdelphij				file_error(ms, errno, "cannot read fd %d", fd);
505267843Sdelphij			else
506267843Sdelphij				file_error(ms, errno, "cannot read `%s'",
507267843Sdelphij				    inname == NULL ? "/dev/stdin" : inname);
508169942Sobrien			goto done;
509169942Sobrien		}
510133359Sobrien	}
511133359Sobrien
512175296Sobrien	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
513175296Sobrien	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
514175296Sobrien		goto done;
515159764Sobrien	rv = 0;
516133359Sobriendone:
517159764Sobrien	free(buf);
518267843Sdelphij	if (pos != (off_t)-1)
519267843Sdelphij		(void)lseek(fd, pos, SEEK_SET);
520133359Sobrien	close_and_restore(ms, inname, fd, &sb);
521267843Sdelphijout:
522159764Sobrien	return rv == 0 ? file_getbuffer(ms) : NULL;
523133359Sobrien}
524133359Sobrien
525133359Sobrien
526133359Sobrienpublic const char *
527133359Sobrienmagic_buffer(struct magic_set *ms, const void *buf, size_t nb)
528133359Sobrien{
529267843Sdelphij	if (ms == NULL)
530267843Sdelphij		return NULL;
531133359Sobrien	if (file_reset(ms) == -1)
532133359Sobrien		return NULL;
533133359Sobrien	/*
534133359Sobrien	 * The main work is done here!
535186690Sobrien	 * We have the file name and/or the data buffer to be identified.
536133359Sobrien	 */
537169962Sobrien	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
538133359Sobrien		return NULL;
539133359Sobrien	}
540133359Sobrien	return file_getbuffer(ms);
541133359Sobrien}
542133359Sobrien#endif
543133359Sobrien
544133359Sobrienpublic const char *
545133359Sobrienmagic_error(struct magic_set *ms)
546133359Sobrien{
547267843Sdelphij	if (ms == NULL)
548267843Sdelphij		return "Magic database is not open";
549191736Sobrien	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
550133359Sobrien}
551133359Sobrien
552133359Sobrienpublic int
553133359Sobrienmagic_errno(struct magic_set *ms)
554133359Sobrien{
555267843Sdelphij	if (ms == NULL)
556267843Sdelphij		return EINVAL;
557191736Sobrien	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
558133359Sobrien}
559133359Sobrien
560133359Sobrienpublic int
561133359Sobrienmagic_setflags(struct magic_set *ms, int flags)
562133359Sobrien{
563267843Sdelphij	if (ms == NULL)
564267843Sdelphij		return -1;
565133359Sobrien#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
566133359Sobrien	if (flags & MAGIC_PRESERVE_ATIME)
567133359Sobrien		return -1;
568133359Sobrien#endif
569133359Sobrien	ms->flags = flags;
570133359Sobrien	return 0;
571133359Sobrien}
572267843Sdelphij
573267843Sdelphijpublic int
574267843Sdelphijmagic_version(void)
575267843Sdelphij{
576267843Sdelphij	return MAGIC_VERSION;
577267843Sdelphij}
578275698Sdelphij
579275698Sdelphijpublic int
580275698Sdelphijmagic_setparam(struct magic_set *ms, int param, const void *val)
581275698Sdelphij{
582275698Sdelphij	switch (param) {
583275698Sdelphij	case MAGIC_PARAM_INDIR_MAX:
584284237Sdelphij		ms->indir_max = (uint16_t)*(const size_t *)val;
585275698Sdelphij		return 0;
586275698Sdelphij	case MAGIC_PARAM_NAME_MAX:
587284237Sdelphij		ms->name_max = (uint16_t)*(const size_t *)val;
588275698Sdelphij		return 0;
589275698Sdelphij	case MAGIC_PARAM_ELF_PHNUM_MAX:
590284237Sdelphij		ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
591275698Sdelphij		return 0;
592275698Sdelphij	case MAGIC_PARAM_ELF_SHNUM_MAX:
593284237Sdelphij		ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
594275698Sdelphij		return 0;
595276577Sdelphij	case MAGIC_PARAM_ELF_NOTES_MAX:
596284237Sdelphij		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
597276577Sdelphij		return 0;
598275698Sdelphij	default:
599275698Sdelphij		errno = EINVAL;
600275698Sdelphij		return -1;
601275698Sdelphij	}
602275698Sdelphij}
603275698Sdelphij
604275698Sdelphijpublic int
605275698Sdelphijmagic_getparam(struct magic_set *ms, int param, void *val)
606275698Sdelphij{
607275698Sdelphij	switch (param) {
608275698Sdelphij	case MAGIC_PARAM_INDIR_MAX:
609275698Sdelphij		*(size_t *)val = ms->indir_max;
610275698Sdelphij		return 0;
611275698Sdelphij	case MAGIC_PARAM_NAME_MAX:
612275698Sdelphij		*(size_t *)val = ms->name_max;
613275698Sdelphij		return 0;
614275698Sdelphij	case MAGIC_PARAM_ELF_PHNUM_MAX:
615275698Sdelphij		*(size_t *)val = ms->elf_phnum_max;
616275698Sdelphij		return 0;
617275698Sdelphij	case MAGIC_PARAM_ELF_SHNUM_MAX:
618275698Sdelphij		*(size_t *)val = ms->elf_shnum_max;
619275698Sdelphij		return 0;
620276577Sdelphij	case MAGIC_PARAM_ELF_NOTES_MAX:
621276577Sdelphij		*(size_t *)val = ms->elf_notes_max;
622276577Sdelphij		return 0;
623275698Sdelphij	default:
624275698Sdelphij		errno = EINVAL;
625275698Sdelphij		return -1;
626275698Sdelphij	}
627275698Sdelphij}
628