2 * Copyright (c) Christos Zoulas 2003.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
36 #include <sys/param.h> /* for MAXPATHLEN */
41 #include <limits.h> /* for PIPE_BUF */
43 #if defined(HAVE_UTIMES)
44 # include <sys/time.h>
45 #elif defined(HAVE_UTIME)
46 # if defined(HAVE_SYS_UTIME_H)
47 # include <sys/utime.h>
48 # elif defined(HAVE_UTIME_H)
54 #include <unistd.h> /* for read() */
61 #include <netinet/in.h> /* for byte swapping */
63 #include "patchlevel.h"
66 FILE_RCSID("@(#)$File: magic.c,v 1.41 2007/03/26 17:59:50 christos Exp $")
70 private char *apptypeName = NULL;
71 protected int file_os2_apptype(struct magic_set *ms, const char *fn,
72 const void *buf, size_t nb);
75 private void free_mlist(struct mlist *);
76 private void close_and_restore(const struct magic_set *, const char *, int,
78 private int info_from_stat(struct magic_set *, mode_t);
81 #define STDIN_FILENO 0
84 public struct magic_set *
89 if ((ms = calloc((size_t)1, sizeof(struct magic_set))) == NULL)
92 if (magic_setflags(ms, flags) == -1) {
97 ms->o.ptr = ms->o.buf = malloc(ms->o.left = ms->o.size = 1024);
98 if (ms->o.buf == NULL)
101 ms->o.pbuf = malloc(ms->o.psize = 1024);
102 if (ms->o.pbuf == NULL)
105 ms->c.li = malloc((ms->c.len = 10) * sizeof(*ms->c.li));
106 if (ms->c.li == NULL)
112 ms->file = "unknown";
125 free_mlist(struct mlist *mlist)
132 for (ml = mlist->next; ml != mlist;) {
133 struct mlist *next = ml->next;
134 struct magic *mg = ml->magic;
135 file_delmagic(mg, ml->mapped, ml->nmagic);
143 info_from_stat(struct magic_set *ms, mode_t md)
145 /* We cannot open it, but we were able to stat it. */
147 if (file_printf(ms, "writable, ") == -1)
150 if (file_printf(ms, "executable, ") == -1)
153 if (file_printf(ms, "regular file, ") == -1)
155 if (file_printf(ms, "no read permission") == -1)
161 magic_close(struct magic_set *ms)
163 free_mlist(ms->mlist);
174 magic_load(struct magic_set *ms, const char *magicfile)
176 struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
178 free_mlist(ms->mlist);
186 magic_compile(struct magic_set *ms, const char *magicfile)
188 struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
194 magic_check(struct magic_set *ms, const char *magicfile)
196 struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
202 close_and_restore(const struct magic_set *ms, const char *name, int fd,
203 const struct stat *sb)
205 if (fd == STDIN_FILENO)
209 if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
211 * Try to restore access, modification times if read it.
212 * This is really *bad* because it will modify the status
213 * time of the file... And of course this will affect
217 struct timeval utsbuf[2];
218 utsbuf[0].tv_sec = sb->st_atime;
219 utsbuf[1].tv_sec = sb->st_mtime;
221 (void) utimes(name, utsbuf); /* don't care if loses */
222 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
223 struct utimbuf utbuf;
225 utbuf.actime = sb->st_atime;
226 utbuf.modtime = sb->st_mtime;
227 (void) utime(name, &utbuf); /* don't care if loses */
234 * find type of named file
237 magic_file(struct magic_set *ms, const char *inname)
243 ssize_t nbytes = 0; /* number of bytes read from a datafile */
247 * one extra for terminating '\0', and
248 * some overlapping space for matches near EOF
250 #define SLOP (1 + sizeof(union VALUETYPE))
251 if ((buf = malloc(HOWMANY + SLOP)) == NULL)
254 if (file_reset(ms) == -1)
257 switch (file_fsmagic(ms, inname, &sb)) {
260 case 0: /* nothing found */
262 default: /* matched it and printed type */
267 if (inname == NULL) {
269 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
272 int flags = O_RDONLY|O_BINARY;
274 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
280 if ((fd = open(inname, flags)) < 0) {
282 char *tmp = alloca(strlen(inname) + 5);
283 (void)strcat(strcpy(tmp, inname), ".exe");
284 if ((fd = open(tmp, flags)) < 0) {
286 if (info_from_stat(ms, sb.st_mode) == -1)
295 if ((flags = fcntl(fd, F_GETFL)) != -1) {
296 flags &= ~O_NONBLOCK;
297 (void)fcntl(fd, F_SETFL, flags);
303 * try looking at the first HOWMANY bytes
308 while ((r = sread(fd, (void *)&buf[nbytes],
309 (size_t)(HOWMANY - nbytes), 1)) > 0) {
311 if (r < PIPE_BUF) break;
315 /* We can not read it, but we were able to stat it. */
316 if (info_from_stat(ms, sb.st_mode) == -1)
323 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
324 file_error(ms, errno, "cannot read `%s'", inname);
330 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
331 "application/x-empty" : "empty") == -1)
333 } else if (nbytes == 1) {
334 if (file_printf(ms, "very short file (no magic)") == -1)
337 (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
338 if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
344 close_and_restore(ms, inname, fd, &sb);
345 return rv == 0 ? file_getbuffer(ms) : NULL;
350 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
352 if (file_reset(ms) == -1)
355 * The main work is done here!
356 * We have the file name and/or the data buffer to be identified.
358 if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
361 return file_getbuffer(ms);
366 magic_error(struct magic_set *ms)
368 return ms->haderr ? ms->o.buf : NULL;
372 magic_errno(struct magic_set *ms)
374 return ms->haderr ? ms->error : 0;
378 magic_setflags(struct magic_set *ms, int flags)
380 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
381 if (flags & MAGIC_PRESERVE_ATIME)