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 */
38 #include <fcntl.h> /* for open() */
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("@(#)$Id: magic.c,v 1.25 2005/01/07 19:17:27 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,
79 public struct magic_set
*
84 if ((ms
= malloc(sizeof(struct magic_set
))) == NULL
)
87 if (magic_setflags(ms
, flags
) == -1) {
92 ms
->o
.ptr
= ms
->o
.buf
= malloc(ms
->o
.size
= 1024);
93 if (ms
->o
.buf
== NULL
)
96 ms
->o
.pbuf
= malloc(ms
->o
.psize
= 1024);
97 if (ms
->o
.pbuf
== NULL
)
100 ms
->c
.off
= malloc((ms
->c
.len
= 10) * sizeof(*ms
->c
.off
));
101 if (ms
->c
.off
== NULL
)
119 free_mlist(struct mlist
*mlist
)
126 for (ml
= mlist
->next
; ml
!= mlist
;) {
127 struct mlist
*next
= ml
->next
;
128 struct magic
*mg
= ml
->magic
;
129 file_delmagic(mg
, ml
->mapped
, ml
->nmagic
);
138 struct magic_set
*ms
;
140 free_mlist(ms
->mlist
);
151 magic_load(struct magic_set
*ms
, const char *magicfile
)
153 struct mlist
*ml
= file_apprentice(ms
, magicfile
, FILE_LOAD
);
155 free_mlist(ms
->mlist
);
163 magic_compile(struct magic_set
*ms
, const char *magicfile
)
165 struct mlist
*ml
= file_apprentice(ms
, magicfile
, FILE_COMPILE
);
171 magic_check(struct magic_set
*ms
, const char *magicfile
)
173 struct mlist
*ml
= file_apprentice(ms
, magicfile
, FILE_CHECK
);
179 close_and_restore(const struct magic_set
*ms
, const char *name
, int fd
,
180 const struct stat
*sb
)
183 if (fd
!= STDIN_FILENO
&& (ms
->flags
& MAGIC_PRESERVE_ATIME
) != 0) {
185 * Try to restore access, modification times if read it.
186 * This is really *bad* because it will modify the status
187 * time of the file... And of course this will affect
191 struct timeval utsbuf
[2];
192 utsbuf
[0].tv_sec
= sb
->st_atime
;
193 utsbuf
[1].tv_sec
= sb
->st_mtime
;
195 (void) utimes(name
, utsbuf
); /* don't care if loses */
196 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
197 struct utimbuf utbuf
;
199 utbuf
.actime
= sb
->st_atime
;
200 utbuf
.modtime
= sb
->st_mtime
;
201 (void) utime(name
, &utbuf
); /* don't care if loses */
208 * find type of named file
211 magic_file(struct magic_set
*ms
, const char *inname
)
214 unsigned char buf
[HOWMANY
+1]; /* one extra for terminating '\0' */
216 ssize_t nbytes
= 0; /* number of bytes read from a datafile */
218 if (file_reset(ms
) == -1)
221 switch (file_fsmagic(ms
, inname
, &sb
)) {
227 return file_getbuffer(ms
);
231 #define STDIN_FILENO 0
235 else if ((fd
= open(inname
, O_RDONLY
)) < 0) {
236 /* We cannot open it, but we were able to stat it. */
237 if (sb
.st_mode
& 0222)
238 if (file_printf(ms
, "writable, ") == -1)
240 if (sb
.st_mode
& 0111)
241 if (file_printf(ms
, "executable, ") == -1)
243 if (S_ISREG(sb
.st_mode
))
244 if (file_printf(ms
, "regular file, ") == -1)
246 if (file_printf(ms
, "no read permission") == -1)
248 return file_getbuffer(ms
);
252 * try looking at the first HOWMANY bytes
254 if ((nbytes
= read(fd
, (char *)buf
, HOWMANY
)) == -1) {
255 file_error(ms
, errno
, "cannot read `%s'", inname
);
260 if (file_printf(ms
, (ms
->flags
& MAGIC_MIME
) ?
261 "application/x-empty" : "empty") == -1)
264 } else if (nbytes
== 1) {
265 if (file_printf(ms
, "very short file (no magic)") == -1)
269 buf
[nbytes
] = '\0'; /* null-terminate it */
271 switch (file_os2_apptype(ms
, inname
, buf
, nbytes
)) {
280 if (file_buffer(ms
, fd
, buf
, (size_t)nbytes
) == -1)
285 * We matched something in the file, so this *might*
286 * be an ELF file, and the file is at least 5 bytes
287 * long, so if it's an ELF file it has at least one
288 * byte past the ELF magic number - try extracting
289 * information from the ELF headers that cannot easily
290 * be extracted with rules in the magic file.
292 file_tryelf(ms
, fd
, buf
, (size_t)nbytes
);
297 close_and_restore(ms
, inname
, fd
, &sb
);
298 return file_getbuffer(ms
);
300 close_and_restore(ms
, inname
, fd
, &sb
);
306 magic_buffer(struct magic_set
*ms
, const void *buf
, size_t nb
)
308 if (file_reset(ms
) == -1)
311 * The main work is done here!
312 * We have the file name and/or the data buffer to be identified.
314 if (file_buffer(ms
, -1, buf
, nb
) == -1) {
317 return file_getbuffer(ms
);
322 magic_error(struct magic_set
*ms
)
324 return ms
->haderr
? ms
->o
.buf
: NULL
;
328 magic_errno(struct magic_set
*ms
)
330 return ms
->haderr
? ms
->error
: 0;
334 magic_setflags(struct magic_set
*ms
, int flags
)
336 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
337 if (flags
& MAGIC_PRESERVE_ATIME
)