magic.c revision 309847
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
36309847SdelphijFILE_RCSID("@(#)$File: magic.c,v 1.100 2016/07/18 11:43:05 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
140288143Sdelphij	if (module) {
141288143Sdelphij		char exepath[MAX_PATH];
142288143Sdelphij		GetModuleFileNameA(NULL, exepath, MAX_PATH);
143288143Sdelphij		PathRemoveFileSpecA(exepath);
144288143Sdelphij		if (stricmp(exepath, dllpath) == 0)
145288143Sdelphij			goto out;
146288143Sdelphij	}
147288143Sdelphij
148284237Sdelphij	sp = strlen(dllpath);
149284237Sdelphij	if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
150284237Sdelphij		_w32_append_path(hmagicpath,
151284237Sdelphij		    "%s/../share/misc/magic.mgc", dllpath);
152284237Sdelphij		goto out;
153284237Sdelphij	}
154284237Sdelphij
155284237Sdelphij	for (sp = 0; sp < __arraycount(trypaths); sp++)
156284237Sdelphij		_w32_append_path(hmagicpath, trypaths[sp], dllpath);
157284237Sdelphijout:
158284237Sdelphij	free(dllpath);
159284237Sdelphij}
160284237Sdelphij
161284237Sdelphij/* Placate GCC by offering a sacrificial previous prototype */
162284237SdelphijBOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
163284237Sdelphij
164284237SdelphijBOOL WINAPI
165284237SdelphijDllMain(HINSTANCE hinstDLL, DWORD fdwReason,
166284237Sdelphij    LPVOID lpvReserved __attribute__((__unused__)))
167284237Sdelphij{
168284237Sdelphij	if (fdwReason == DLL_PROCESS_ATTACH)
169284237Sdelphij		_w32_dll_instance = hinstDLL;
170284237Sdelphij	return TRUE;
171284237Sdelphij}
172284237Sdelphij#endif
173284237Sdelphij
174226048Sobrienprivate const char *
175226048Sobrienget_default_magic(void)
176226048Sobrien{
177226048Sobrien	static const char hmagic[] = "/.magic/magic.mgc";
178226048Sobrien	static char *default_magic;
179226048Sobrien	char *home, *hmagicpath;
180226048Sobrien
181226048Sobrien#ifndef WIN32
182226048Sobrien	struct stat st;
183226048Sobrien
184226048Sobrien	if (default_magic) {
185226048Sobrien		free(default_magic);
186226048Sobrien		default_magic = NULL;
187226048Sobrien	}
188226048Sobrien	if ((home = getenv("HOME")) == NULL)
189226048Sobrien		return MAGIC;
190226048Sobrien
191267843Sdelphij	if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
192226048Sobrien		return MAGIC;
193267843Sdelphij	if (stat(hmagicpath, &st) == -1) {
194226048Sobrien		free(hmagicpath);
195267843Sdelphij		if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
196226048Sobrien			return MAGIC;
197267843Sdelphij		if (stat(hmagicpath, &st) == -1)
198226048Sobrien			goto out;
199267843Sdelphij		if (S_ISDIR(st.st_mode)) {
200267843Sdelphij			free(hmagicpath);
201267843Sdelphij			if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
202267843Sdelphij				return MAGIC;
203267843Sdelphij			if (access(hmagicpath, R_OK) == -1)
204267843Sdelphij				goto out;
205267843Sdelphij		}
206226048Sobrien	}
207226048Sobrien
208226048Sobrien	if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
209226048Sobrien		goto out;
210226048Sobrien	free(hmagicpath);
211226048Sobrien	return default_magic;
212226048Sobrienout:
213226048Sobrien	default_magic = NULL;
214226048Sobrien	free(hmagicpath);
215226048Sobrien	return MAGIC;
216226048Sobrien#else
217267843Sdelphij	hmagicpath = NULL;
218226048Sobrien
219226048Sobrien	if (default_magic) {
220226048Sobrien		free(default_magic);
221226048Sobrien		default_magic = NULL;
222226048Sobrien	}
223226048Sobrien
224284237Sdelphij	/* First, try to get a magic file from user-application data */
225284237Sdelphij	if ((home = getenv("LOCALAPPDATA")) != NULL)
226284237Sdelphij		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
227226048Sobrien
228284237Sdelphij	/* Second, try to get a magic file from the user profile data */
229284237Sdelphij	if ((home = getenv("USERPROFILE")) != NULL)
230284237Sdelphij		_w32_append_path(&hmagicpath,
231284237Sdelphij		    "%s/Local Settings/Application Data%s", home, hmagic);
232226048Sobrien
233284237Sdelphij	/* Third, try to get a magic file from Common Files */
234284237Sdelphij	if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
235284237Sdelphij		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
236226048Sobrien
237284237Sdelphij	/* Fourth, try to get magic file relative to exe location */
238284237Sdelphij        _w32_get_magic_relative_to(&hmagicpath, NULL);
239226048Sobrien
240284237Sdelphij	/* Fifth, try to get magic file relative to dll location */
241284237Sdelphij        _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
242284237Sdelphij
243284237Sdelphij	/* Avoid MAGIC constant - it likely points to a file within MSys tree */
244226048Sobrien	default_magic = hmagicpath;
245226048Sobrien	return default_magic;
246226048Sobrien#endif
247226048Sobrien}
248226048Sobrien
249226048Sobrienpublic const char *
250226048Sobrienmagic_getpath(const char *magicfile, int action)
251226048Sobrien{
252226048Sobrien	if (magicfile != NULL)
253226048Sobrien		return magicfile;
254226048Sobrien
255226048Sobrien	magicfile = getenv("MAGIC");
256226048Sobrien	if (magicfile != NULL)
257226048Sobrien		return magicfile;
258226048Sobrien
259226048Sobrien	return action == FILE_LOAD ? get_default_magic() : MAGIC;
260226048Sobrien}
261226048Sobrien
262133359Sobrienpublic struct magic_set *
263133359Sobrienmagic_open(int flags)
264133359Sobrien{
265267843Sdelphij	return file_ms_alloc(flags);
266133359Sobrien}
267133359Sobrien
268169942Sobrienprivate int
269186690Sobrienunreadable_info(struct magic_set *ms, mode_t md, const char *file)
270169942Sobrien{
271267843Sdelphij	if (file) {
272267843Sdelphij		/* We cannot open it, but we were able to stat it. */
273267843Sdelphij		if (access(file, W_OK) == 0)
274267843Sdelphij			if (file_printf(ms, "writable, ") == -1)
275267843Sdelphij				return -1;
276267843Sdelphij		if (access(file, X_OK) == 0)
277267843Sdelphij			if (file_printf(ms, "executable, ") == -1)
278267843Sdelphij				return -1;
279267843Sdelphij	}
280169942Sobrien	if (S_ISREG(md))
281169942Sobrien		if (file_printf(ms, "regular file, ") == -1)
282169942Sobrien			return -1;
283169942Sobrien	if (file_printf(ms, "no read permission") == -1)
284169942Sobrien		return -1;
285169942Sobrien	return 0;
286169942Sobrien}
287169942Sobrien
288133359Sobrienpublic void
289169942Sobrienmagic_close(struct magic_set *ms)
290133359Sobrien{
291267843Sdelphij	if (ms == NULL)
292267843Sdelphij		return;
293267843Sdelphij	file_ms_free(ms);
294133359Sobrien}
295133359Sobrien
296133359Sobrien/*
297133359Sobrien * load a magic file
298133359Sobrien */
299133359Sobrienpublic int
300133359Sobrienmagic_load(struct magic_set *ms, const char *magicfile)
301133359Sobrien{
302267843Sdelphij	if (ms == NULL)
303267843Sdelphij		return -1;
304267843Sdelphij	return file_apprentice(ms, magicfile, FILE_LOAD);
305133359Sobrien}
306133359Sobrien
307275698Sdelphij#ifndef COMPILE_ONLY
308275698Sdelphij/*
309275698Sdelphij * Install a set of compiled magic buffers.
310275698Sdelphij */
311133359Sobrienpublic int
312275698Sdelphijmagic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
313275698Sdelphij    size_t nbufs)
314275698Sdelphij{
315275698Sdelphij	if (ms == NULL)
316275698Sdelphij		return -1;
317275698Sdelphij	return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
318275698Sdelphij}
319275698Sdelphij#endif
320275698Sdelphij
321275698Sdelphijpublic int
322133359Sobrienmagic_compile(struct magic_set *ms, const char *magicfile)
323133359Sobrien{
324267843Sdelphij	if (ms == NULL)
325267843Sdelphij		return -1;
326267843Sdelphij	return file_apprentice(ms, magicfile, FILE_COMPILE);
327133359Sobrien}
328133359Sobrien
329133359Sobrienpublic int
330133359Sobrienmagic_check(struct magic_set *ms, const char *magicfile)
331133359Sobrien{
332267843Sdelphij	if (ms == NULL)
333267843Sdelphij		return -1;
334267843Sdelphij	return file_apprentice(ms, magicfile, FILE_CHECK);
335133359Sobrien}
336133359Sobrien
337226048Sobrienpublic int
338226048Sobrienmagic_list(struct magic_set *ms, const char *magicfile)
339226048Sobrien{
340267843Sdelphij	if (ms == NULL)
341267843Sdelphij		return -1;
342267843Sdelphij	return file_apprentice(ms, magicfile, FILE_LIST);
343226048Sobrien}
344226048Sobrien
345133359Sobrienprivate void
346133359Sobrienclose_and_restore(const struct magic_set *ms, const char *name, int fd,
347133359Sobrien    const struct stat *sb)
348133359Sobrien{
349299234Sdelphij	if (fd == STDIN_FILENO || name == NULL)
350159764Sobrien		return;
351133359Sobrien	(void) close(fd);
352159764Sobrien
353159764Sobrien	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
354133359Sobrien		/*
355133359Sobrien		 * Try to restore access, modification times if read it.
356133359Sobrien		 * This is really *bad* because it will modify the status
357133359Sobrien		 * time of the file... And of course this will affect
358133359Sobrien		 * backup programs
359133359Sobrien		 */
360133359Sobrien#ifdef HAVE_UTIMES
361133359Sobrien		struct timeval  utsbuf[2];
362186690Sobrien		(void)memset(utsbuf, 0, sizeof(utsbuf));
363133359Sobrien		utsbuf[0].tv_sec = sb->st_atime;
364133359Sobrien		utsbuf[1].tv_sec = sb->st_mtime;
365133359Sobrien
366133359Sobrien		(void) utimes(name, utsbuf); /* don't care if loses */
367133359Sobrien#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
368133359Sobrien		struct utimbuf  utbuf;
369133359Sobrien
370191736Sobrien		(void)memset(&utbuf, 0, sizeof(utbuf));
371133359Sobrien		utbuf.actime = sb->st_atime;
372133359Sobrien		utbuf.modtime = sb->st_mtime;
373133359Sobrien		(void) utime(name, &utbuf); /* don't care if loses */
374133359Sobrien#endif
375133359Sobrien	}
376133359Sobrien}
377133359Sobrien
378133359Sobrien#ifndef COMPILE_ONLY
379175296Sobrien
380133359Sobrien/*
381175296Sobrien * find type of descriptor
382175296Sobrien */
383175296Sobrienpublic const char *
384175296Sobrienmagic_descriptor(struct magic_set *ms, int fd)
385175296Sobrien{
386267843Sdelphij	if (ms == NULL)
387267843Sdelphij		return NULL;
388175296Sobrien	return file_or_fd(ms, NULL, fd);
389175296Sobrien}
390175296Sobrien
391175296Sobrien/*
392133359Sobrien * find type of named file
393133359Sobrien */
394133359Sobrienpublic const char *
395133359Sobrienmagic_file(struct magic_set *ms, const char *inname)
396133359Sobrien{
397267843Sdelphij	if (ms == NULL)
398267843Sdelphij		return NULL;
399175296Sobrien	return file_or_fd(ms, inname, STDIN_FILENO);
400175296Sobrien}
401175296Sobrien
402175296Sobrienprivate const char *
403175296Sobrienfile_or_fd(struct magic_set *ms, const char *inname, int fd)
404175296Sobrien{
405159764Sobrien	int	rv = -1;
406159764Sobrien	unsigned char *buf;
407133359Sobrien	struct stat	sb;
408133359Sobrien	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
409169942Sobrien	int	ispipe = 0;
410267843Sdelphij	off_t	pos = (off_t)-1;
411133359Sobrien
412267843Sdelphij	if (file_reset(ms) == -1)
413267843Sdelphij		goto out;
414267843Sdelphij
415159764Sobrien	/*
416159764Sobrien	 * one extra for terminating '\0', and
417159764Sobrien	 * some overlapping space for matches near EOF
418159764Sobrien	 */
419159764Sobrien#define SLOP (1 + sizeof(union VALUETYPE))
420298192Sdelphij	if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
421133359Sobrien		return NULL;
422133359Sobrien
423133359Sobrien	switch (file_fsmagic(ms, inname, &sb)) {
424169942Sobrien	case -1:		/* error */
425159764Sobrien		goto done;
426169942Sobrien	case 0:			/* nothing found */
427133359Sobrien		break;
428169942Sobrien	default:		/* matched it and printed type */
429159764Sobrien		rv = 0;
430159764Sobrien		goto done;
431133359Sobrien	}
432133359Sobrien
433267843Sdelphij#ifdef WIN32
434267843Sdelphij	/* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
435267843Sdelphij	if (fd == STDIN_FILENO)
436267843Sdelphij		_setmode(STDIN_FILENO, O_BINARY);
437267843Sdelphij#endif
438267843Sdelphij
439169942Sobrien	if (inname == NULL) {
440169942Sobrien		if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
441169942Sobrien			ispipe = 1;
442267843Sdelphij		else
443267843Sdelphij			pos = lseek(fd, (off_t)0, SEEK_CUR);
444169942Sobrien	} else {
445169942Sobrien		int flags = O_RDONLY|O_BINARY;
446267843Sdelphij		int okstat = stat(inname, &sb) == 0;
447169942Sobrien
448267843Sdelphij		if (okstat && S_ISFIFO(sb.st_mode)) {
449226048Sobrien#ifdef O_NONBLOCK
450169942Sobrien			flags |= O_NONBLOCK;
451226048Sobrien#endif
452169942Sobrien			ispipe = 1;
453169942Sobrien		}
454169942Sobrien
455169942Sobrien		errno = 0;
456169942Sobrien		if ((fd = open(inname, flags)) < 0) {
457267843Sdelphij#ifdef WIN32
458267843Sdelphij			/*
459267843Sdelphij			 * Can't stat, can't open.  It may have been opened in
460267843Sdelphij			 * fsmagic, so if the user doesn't have read permission,
461267843Sdelphij			 * allow it to say so; otherwise an error was probably
462267843Sdelphij			 * displayed in fsmagic.
463267843Sdelphij			 */
464267843Sdelphij			if (!okstat && errno == EACCES) {
465267843Sdelphij				sb.st_mode = S_IFBLK;
466267843Sdelphij				okstat = 1;
467267843Sdelphij			}
468267843Sdelphij#endif
469267843Sdelphij			if (okstat &&
470267843Sdelphij			    unreadable_info(ms, sb.st_mode, inname) == -1)
471175296Sobrien				goto done;
472192348Sdelphij			rv = 0;
473192348Sdelphij			goto done;
474169942Sobrien		}
475169942Sobrien#ifdef O_NONBLOCK
476169942Sobrien		if ((flags = fcntl(fd, F_GETFL)) != -1) {
477169942Sobrien			flags &= ~O_NONBLOCK;
478169942Sobrien			(void)fcntl(fd, F_SETFL, flags);
479169942Sobrien		}
480169942Sobrien#endif
481133359Sobrien	}
482133359Sobrien
483133359Sobrien	/*
484298192Sdelphij	 * try looking at the first ms->bytes_max bytes
485133359Sobrien	 */
486169942Sobrien	if (ispipe) {
487169942Sobrien		ssize_t r = 0;
488169942Sobrien
489169942Sobrien		while ((r = sread(fd, (void *)&buf[nbytes],
490298192Sdelphij		    (size_t)(ms->bytes_max - nbytes), 1)) > 0) {
491169942Sobrien			nbytes += r;
492169942Sobrien			if (r < PIPE_BUF) break;
493169942Sobrien		}
494169942Sobrien
495309847Sdelphij		if (nbytes == 0 && inname) {
496169942Sobrien			/* We can not read it, but we were able to stat it. */
497186690Sobrien			if (unreadable_info(ms, sb.st_mode, inname) == -1)
498169942Sobrien				goto done;
499169942Sobrien			rv = 0;
500169942Sobrien			goto done;
501169942Sobrien		}
502169942Sobrien
503169942Sobrien	} else {
504267843Sdelphij		/* Windows refuses to read from a big console buffer. */
505267843Sdelphij		size_t howmany =
506298192Sdelphij#if defined(WIN32)
507267843Sdelphij				_isatty(fd) ? 8 * 1024 :
508267843Sdelphij#endif
509298192Sdelphij				ms->bytes_max;
510267843Sdelphij		if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
511267843Sdelphij			if (inname == NULL && fd != STDIN_FILENO)
512267843Sdelphij				file_error(ms, errno, "cannot read fd %d", fd);
513267843Sdelphij			else
514267843Sdelphij				file_error(ms, errno, "cannot read `%s'",
515267843Sdelphij				    inname == NULL ? "/dev/stdin" : inname);
516169942Sobrien			goto done;
517169942Sobrien		}
518133359Sobrien	}
519133359Sobrien
520175296Sobrien	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
521175296Sobrien	if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
522175296Sobrien		goto done;
523159764Sobrien	rv = 0;
524133359Sobriendone:
525159764Sobrien	free(buf);
526299736Sdelphij	if (fd != -1) {
527299736Sdelphij		if (pos != (off_t)-1)
528299736Sdelphij			(void)lseek(fd, pos, SEEK_SET);
529299736Sdelphij		close_and_restore(ms, inname, fd, &sb);
530299736Sdelphij	}
531267843Sdelphijout:
532159764Sobrien	return rv == 0 ? file_getbuffer(ms) : NULL;
533133359Sobrien}
534133359Sobrien
535133359Sobrien
536133359Sobrienpublic const char *
537133359Sobrienmagic_buffer(struct magic_set *ms, const void *buf, size_t nb)
538133359Sobrien{
539267843Sdelphij	if (ms == NULL)
540267843Sdelphij		return NULL;
541133359Sobrien	if (file_reset(ms) == -1)
542133359Sobrien		return NULL;
543133359Sobrien	/*
544133359Sobrien	 * The main work is done here!
545186690Sobrien	 * We have the file name and/or the data buffer to be identified.
546133359Sobrien	 */
547169962Sobrien	if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
548133359Sobrien		return NULL;
549133359Sobrien	}
550133359Sobrien	return file_getbuffer(ms);
551133359Sobrien}
552133359Sobrien#endif
553133359Sobrien
554133359Sobrienpublic const char *
555133359Sobrienmagic_error(struct magic_set *ms)
556133359Sobrien{
557267843Sdelphij	if (ms == NULL)
558267843Sdelphij		return "Magic database is not open";
559191736Sobrien	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
560133359Sobrien}
561133359Sobrien
562133359Sobrienpublic int
563133359Sobrienmagic_errno(struct magic_set *ms)
564133359Sobrien{
565267843Sdelphij	if (ms == NULL)
566267843Sdelphij		return EINVAL;
567191736Sobrien	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
568133359Sobrien}
569133359Sobrien
570133359Sobrienpublic int
571133359Sobrienmagic_setflags(struct magic_set *ms, int flags)
572133359Sobrien{
573267843Sdelphij	if (ms == NULL)
574267843Sdelphij		return -1;
575133359Sobrien#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
576133359Sobrien	if (flags & MAGIC_PRESERVE_ATIME)
577133359Sobrien		return -1;
578133359Sobrien#endif
579133359Sobrien	ms->flags = flags;
580133359Sobrien	return 0;
581133359Sobrien}
582267843Sdelphij
583267843Sdelphijpublic int
584267843Sdelphijmagic_version(void)
585267843Sdelphij{
586267843Sdelphij	return MAGIC_VERSION;
587267843Sdelphij}
588275698Sdelphij
589275698Sdelphijpublic int
590275698Sdelphijmagic_setparam(struct magic_set *ms, int param, const void *val)
591275698Sdelphij{
592275698Sdelphij	switch (param) {
593275698Sdelphij	case MAGIC_PARAM_INDIR_MAX:
594284237Sdelphij		ms->indir_max = (uint16_t)*(const size_t *)val;
595275698Sdelphij		return 0;
596275698Sdelphij	case MAGIC_PARAM_NAME_MAX:
597284237Sdelphij		ms->name_max = (uint16_t)*(const size_t *)val;
598275698Sdelphij		return 0;
599275698Sdelphij	case MAGIC_PARAM_ELF_PHNUM_MAX:
600284237Sdelphij		ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
601275698Sdelphij		return 0;
602275698Sdelphij	case MAGIC_PARAM_ELF_SHNUM_MAX:
603284237Sdelphij		ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
604275698Sdelphij		return 0;
605276577Sdelphij	case MAGIC_PARAM_ELF_NOTES_MAX:
606284237Sdelphij		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
607276577Sdelphij		return 0;
608288143Sdelphij	case MAGIC_PARAM_REGEX_MAX:
609288143Sdelphij		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
610288143Sdelphij		return 0;
611298192Sdelphij	case MAGIC_PARAM_BYTES_MAX:
612298192Sdelphij		ms->bytes_max = *(const size_t *)val;
613298192Sdelphij		return 0;
614275698Sdelphij	default:
615275698Sdelphij		errno = EINVAL;
616275698Sdelphij		return -1;
617275698Sdelphij	}
618275698Sdelphij}
619275698Sdelphij
620275698Sdelphijpublic int
621275698Sdelphijmagic_getparam(struct magic_set *ms, int param, void *val)
622275698Sdelphij{
623275698Sdelphij	switch (param) {
624275698Sdelphij	case MAGIC_PARAM_INDIR_MAX:
625275698Sdelphij		*(size_t *)val = ms->indir_max;
626275698Sdelphij		return 0;
627275698Sdelphij	case MAGIC_PARAM_NAME_MAX:
628275698Sdelphij		*(size_t *)val = ms->name_max;
629275698Sdelphij		return 0;
630275698Sdelphij	case MAGIC_PARAM_ELF_PHNUM_MAX:
631275698Sdelphij		*(size_t *)val = ms->elf_phnum_max;
632275698Sdelphij		return 0;
633275698Sdelphij	case MAGIC_PARAM_ELF_SHNUM_MAX:
634275698Sdelphij		*(size_t *)val = ms->elf_shnum_max;
635275698Sdelphij		return 0;
636276577Sdelphij	case MAGIC_PARAM_ELF_NOTES_MAX:
637276577Sdelphij		*(size_t *)val = ms->elf_notes_max;
638276577Sdelphij		return 0;
639288143Sdelphij	case MAGIC_PARAM_REGEX_MAX:
640288143Sdelphij		*(size_t *)val = ms->regex_max;
641288143Sdelphij		return 0;
642298192Sdelphij	case MAGIC_PARAM_BYTES_MAX:
643298192Sdelphij		*(size_t *)val = ms->bytes_max;
644298192Sdelphij		return 0;
645275698Sdelphij	default:
646275698Sdelphij		errno = EINVAL;
647275698Sdelphij		return -1;
648275698Sdelphij	}
649275698Sdelphij}
650