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
36 FILE_RCSID("@(#)$File: magic.c,v 1.117 2021/12/06 15:33:00 christos Exp $")
47 #include <limits.h> /* for PIPE_BUF */
49 #if defined(HAVE_UTIMES)
50 # include <sys/time.h>
51 #elif defined(HAVE_UTIME)
52 # if defined(HAVE_SYS_UTIME_H)
53 # include <sys/utime.h>
54 # elif defined(HAVE_UTIME_H)
60 #include <unistd.h> /* for read() */
64 /* Get the PIPE_BUF from pathconf */
66 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
72 private void close_and_restore(const struct magic_set
*, const char *, int,
74 private int unreadable_info(struct magic_set
*, mode_t
, const char *);
75 private const char* get_default_magic(void);
77 private const char *file_or_fd(struct magic_set
*, const char *, int);
81 #define STDIN_FILENO 0
85 /* HINSTANCE of this shared library. Needed for get_default_magic() */
86 static HINSTANCE _w32_dll_instance
= NULL
;
89 _w32_append_path(char **hmagicpath
, const char *fmt
, ...)
96 if (vasprintf(&tmppath
, fmt
, ap
) < 0) {
102 if (access(tmppath
, R_OK
) == -1)
105 if (*hmagicpath
== NULL
) {
106 *hmagicpath
= tmppath
;
110 if (asprintf(&newpath
, "%s%c%s", *hmagicpath
, PATHSEP
, tmppath
) < 0)
115 *hmagicpath
= newpath
;
122 _w32_get_magic_relative_to(char **hmagicpath
, HINSTANCE module
)
124 static const char *trypaths
[] = {
125 "%s/share/misc/magic.mgc",
131 dllpath
= calloc(MAX_PATH
+ 1, sizeof(*dllpath
));
133 if (!GetModuleFileNameA(module
, dllpath
, MAX_PATH
))
136 PathRemoveFileSpecA(dllpath
);
139 char exepath
[MAX_PATH
];
140 GetModuleFileNameA(NULL
, exepath
, MAX_PATH
);
141 PathRemoveFileSpecA(exepath
);
142 if (stricmp(exepath
, dllpath
) == 0)
146 sp
= strlen(dllpath
);
147 if (sp
> 3 && stricmp(&dllpath
[sp
- 3], "bin") == 0) {
148 _w32_append_path(hmagicpath
,
149 "%s/../share/misc/magic.mgc", dllpath
);
153 for (sp
= 0; sp
< __arraycount(trypaths
); sp
++)
154 _w32_append_path(hmagicpath
, trypaths
[sp
], dllpath
);
159 #ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY
160 /* Placate GCC by offering a sacrificial previous prototype */
161 BOOL WINAPI
DllMain(HINSTANCE
, DWORD
, LPVOID
);
164 DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
,
165 LPVOID lpvReserved
__attribute__((__unused__
)))
167 if (fdwReason
== DLL_PROCESS_ATTACH
)
168 _w32_dll_instance
= hinstDLL
;
175 get_default_magic(void)
177 static const char hmagic
[] = "/.magic/magic.mgc";
178 static char *default_magic
;
179 char *home
, *hmagicpath
;
186 default_magic
= NULL
;
188 if ((home
= getenv("HOME")) == NULL
)
191 if (asprintf(&hmagicpath
, "%s/.magic.mgc", home
) < 0)
193 if (stat(hmagicpath
, &st
) == -1) {
195 if (asprintf(&hmagicpath
, "%s/.magic", home
) < 0)
197 if (stat(hmagicpath
, &st
) == -1)
199 if (S_ISDIR(st
.st_mode
)) {
201 if (asprintf(&hmagicpath
, "%s/%s", home
, hmagic
) < 0)
203 if (access(hmagicpath
, R_OK
) == -1)
208 if (asprintf(&default_magic
, "%s:%s", hmagicpath
, MAGIC
) < 0)
211 return default_magic
;
213 default_magic
= NULL
;
221 default_magic
= NULL
;
224 /* Before anything else, try to get a magic file from user HOME */
225 if ((home
= getenv("HOME")) != NULL
)
226 _w32_append_path(&hmagicpath
, "%s%s", home
, hmagic
);
228 /* First, try to get a magic file from user-application data */
229 if ((home
= getenv("LOCALAPPDATA")) != NULL
)
230 _w32_append_path(&hmagicpath
, "%s%s", home
, hmagic
);
232 /* Second, try to get a magic file from the user profile data */
233 if ((home
= getenv("USERPROFILE")) != NULL
)
234 _w32_append_path(&hmagicpath
,
235 "%s/Local Settings/Application Data%s", home
, hmagic
);
237 /* Third, try to get a magic file from Common Files */
238 if ((home
= getenv("COMMONPROGRAMFILES")) != NULL
)
239 _w32_append_path(&hmagicpath
, "%s%s", home
, hmagic
);
241 /* Fourth, try to get magic file relative to exe location */
242 _w32_get_magic_relative_to(&hmagicpath
, NULL
);
244 /* Fifth, try to get magic file relative to dll location */
245 _w32_get_magic_relative_to(&hmagicpath
, _w32_dll_instance
);
247 /* Avoid MAGIC constant - it likely points to a file within MSys tree */
248 default_magic
= hmagicpath
;
249 return default_magic
;
254 magic_getpath(const char *magicfile
, int action
)
256 if (magicfile
!= NULL
)
259 magicfile
= getenv("MAGIC");
260 if (magicfile
!= NULL
)
263 return action
== FILE_LOAD
? get_default_magic() : MAGIC
;
266 public struct magic_set
*
267 magic_open(int flags
)
269 return file_ms_alloc(flags
);
273 unreadable_info(struct magic_set
*ms
, mode_t md
, const char *file
)
276 /* We cannot open it, but we were able to stat it. */
277 if (access(file
, W_OK
) == 0)
278 if (file_printf(ms
, "writable, ") == -1)
281 if (access(file
, X_OK
) == 0)
282 if (file_printf(ms
, "executable, ") == -1)
285 /* X_OK doesn't work well on MS-Windows */
287 const char *p
= strrchr(file
, '.');
288 if (p
&& (stricmp(p
, ".exe")
289 || stricmp(p
, ".dll")
290 || stricmp(p
, ".bat")
291 || stricmp(p
, ".cmd")))
292 if (file_printf(ms
, "writable, ") == -1)
298 if (file_printf(ms
, "regular file, ") == -1)
300 if (file_printf(ms
, "no read permission") == -1)
306 magic_close(struct magic_set
*ms
)
317 magic_load(struct magic_set
*ms
, const char *magicfile
)
321 return file_apprentice(ms
, magicfile
, FILE_LOAD
);
326 * Install a set of compiled magic buffers.
329 magic_load_buffers(struct magic_set
*ms
, void **bufs
, size_t *sizes
,
334 return buffer_apprentice(ms
, RCAST(struct magic
**, bufs
),
340 magic_compile(struct magic_set
*ms
, const char *magicfile
)
344 return file_apprentice(ms
, magicfile
, FILE_COMPILE
);
348 magic_check(struct magic_set
*ms
, const char *magicfile
)
352 return file_apprentice(ms
, magicfile
, FILE_CHECK
);
356 magic_list(struct magic_set
*ms
, const char *magicfile
)
360 return file_apprentice(ms
, magicfile
, FILE_LIST
);
364 close_and_restore(const struct magic_set
*ms
, const char *name
, int fd
,
365 const struct stat
*sb
)
367 if (fd
== STDIN_FILENO
|| name
== NULL
)
371 if ((ms
->flags
& MAGIC_PRESERVE_ATIME
) != 0) {
373 * Try to restore access, modification times if read it.
374 * This is really *bad* because it will modify the status
375 * time of the file... And of course this will affect
379 struct timeval utsbuf
[2];
380 (void)memset(utsbuf
, 0, sizeof(utsbuf
));
381 utsbuf
[0].tv_sec
= sb
->st_atime
;
382 utsbuf
[1].tv_sec
= sb
->st_mtime
;
384 (void) utimes(name
, utsbuf
); /* don't care if loses */
385 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
386 struct utimbuf utbuf
;
388 (void)memset(&utbuf
, 0, sizeof(utbuf
));
389 utbuf
.actime
= sb
->st_atime
;
390 utbuf
.modtime
= sb
->st_mtime
;
391 (void) utime(name
, &utbuf
); /* don't care if loses */
399 * find type of descriptor
402 magic_descriptor(struct magic_set
*ms
, int fd
)
406 return file_or_fd(ms
, NULL
, fd
);
410 * find type of named file
413 magic_file(struct magic_set
*ms
, const char *inname
)
417 return file_or_fd(ms
, inname
, STDIN_FILENO
);
421 file_or_fd(struct magic_set
*ms
, const char *inname
, int fd
)
426 ssize_t nbytes
= 0; /* number of bytes read from a datafile */
429 off_t pos
= CAST(off_t
, -1);
431 if (file_reset(ms
, 1) == -1)
435 * one extra for terminating '\0', and
436 * some overlapping space for matches near EOF
438 #define SLOP (1 + sizeof(union VALUETYPE))
439 if ((buf
= CAST(unsigned char *, malloc(ms
->bytes_max
+ SLOP
))) == NULL
)
442 switch (file_fsmagic(ms
, inname
, &sb
)) {
445 case 0: /* nothing found */
447 default: /* matched it and printed type */
453 /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
454 if (fd
== STDIN_FILENO
)
455 _setmode(STDIN_FILENO
, O_BINARY
);
457 if (inname
!= NULL
) {
458 int flags
= O_RDONLY
|O_BINARY
|O_NONBLOCK
|O_CLOEXEC
;
460 if ((fd
= open(inname
, flags
)) < 0) {
461 okstat
= stat(inname
, &sb
) == 0;
464 * Can't stat, can't open. It may have been opened in
465 * fsmagic, so if the user doesn't have read permission,
466 * allow it to say so; otherwise an error was probably
467 * displayed in fsmagic.
469 if (!okstat
&& errno
== EACCES
) {
470 sb
.st_mode
= S_IFBLK
;
475 unreadable_info(ms
, sb
.st_mode
, inname
) == -1)
480 #if O_CLOEXEC == 0 && defined(F_SETFD)
481 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
486 okstat
= fstat(fd
, &sb
) == 0;
487 if (okstat
&& S_ISFIFO(sb
.st_mode
))
490 pos
= lseek(fd
, CAST(off_t
, 0), SEEK_CUR
);
494 * try looking at the first ms->bytes_max bytes
500 while ((r
= sread(fd
, RCAST(void *, &buf
[nbytes
]),
501 CAST(size_t, ms
->bytes_max
- nbytes
), 1)) > 0) {
503 if (r
< PIPE_BUF
) break;
507 if (nbytes
== 0 && inname
) {
508 /* We can not read it, but we were able to stat it. */
509 if (unreadable_info(ms
, sb
.st_mode
, inname
) == -1)
515 } else if (fd
!= -1) {
516 /* Windows refuses to read from a big console buffer. */
519 _isatty(fd
) ? 8 * 1024 :
522 if ((nbytes
= read(fd
, RCAST(void *, buf
), howmany
)) == -1) {
523 if (inname
== NULL
&& fd
!= STDIN_FILENO
)
524 file_error(ms
, errno
, "cannot read fd %d", fd
);
526 file_error(ms
, errno
, "cannot read `%s'",
527 inname
== NULL
? "/dev/stdin" : inname
);
532 (void)memset(buf
+ nbytes
, 0, SLOP
); /* NUL terminate */
533 if (file_buffer(ms
, fd
, okstat
? &sb
: NULL
, inname
, buf
, CAST(size_t, nbytes
)) == -1)
539 if (pos
!= CAST(off_t
, -1))
540 (void)lseek(fd
, pos
, SEEK_SET
);
541 close_and_restore(ms
, inname
, fd
, &sb
);
544 return rv
== 0 ? file_getbuffer(ms
) : NULL
;
549 magic_buffer(struct magic_set
*ms
, const void *buf
, size_t nb
)
553 if (file_reset(ms
, 1) == -1)
556 * The main work is done here!
557 * We have the file name and/or the data buffer to be identified.
559 if (file_buffer(ms
, -1, NULL
, NULL
, buf
, nb
) == -1) {
562 return file_getbuffer(ms
);
567 magic_error(struct magic_set
*ms
)
570 return "Magic database is not open";
571 return (ms
->event_flags
& EVENT_HAD_ERR
) ? ms
->o
.buf
: NULL
;
575 magic_errno(struct magic_set
*ms
)
579 return (ms
->event_flags
& EVENT_HAD_ERR
) ? ms
->error
: 0;
583 magic_getflags(struct magic_set
*ms
)
592 magic_setflags(struct magic_set
*ms
, int flags
)
596 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
597 if (flags
& MAGIC_PRESERVE_ATIME
)
607 return MAGIC_VERSION
;
611 magic_setparam(struct magic_set
*ms
, int param
, const void *val
)
616 case MAGIC_PARAM_INDIR_MAX
:
617 ms
->indir_max
= CAST(uint16_t, *CAST(const size_t *, val
));
619 case MAGIC_PARAM_NAME_MAX
:
620 ms
->name_max
= CAST(uint16_t, *CAST(const size_t *, val
));
622 case MAGIC_PARAM_ELF_PHNUM_MAX
:
623 ms
->elf_phnum_max
= CAST(uint16_t, *CAST(const size_t *, val
));
625 case MAGIC_PARAM_ELF_SHNUM_MAX
:
626 ms
->elf_shnum_max
= CAST(uint16_t, *CAST(const size_t *, val
));
628 case MAGIC_PARAM_ELF_NOTES_MAX
:
629 ms
->elf_notes_max
= CAST(uint16_t, *CAST(const size_t *, val
));
631 case MAGIC_PARAM_REGEX_MAX
:
632 ms
->regex_max
= CAST(uint16_t, *CAST(const size_t *, val
));
634 case MAGIC_PARAM_BYTES_MAX
:
635 ms
->bytes_max
= *CAST(const size_t *, val
);
637 case MAGIC_PARAM_ENCODING_MAX
:
638 ms
->encoding_max
= *CAST(const size_t *, val
);
647 magic_getparam(struct magic_set
*ms
, int param
, void *val
)
652 case MAGIC_PARAM_INDIR_MAX
:
653 *CAST(size_t *, val
) = ms
->indir_max
;
655 case MAGIC_PARAM_NAME_MAX
:
656 *CAST(size_t *, val
) = ms
->name_max
;
658 case MAGIC_PARAM_ELF_PHNUM_MAX
:
659 *CAST(size_t *, val
) = ms
->elf_phnum_max
;
661 case MAGIC_PARAM_ELF_SHNUM_MAX
:
662 *CAST(size_t *, val
) = ms
->elf_shnum_max
;
664 case MAGIC_PARAM_ELF_NOTES_MAX
:
665 *CAST(size_t *, val
) = ms
->elf_notes_max
;
667 case MAGIC_PARAM_REGEX_MAX
:
668 *CAST(size_t *, val
) = ms
->regex_max
;
670 case MAGIC_PARAM_BYTES_MAX
:
671 *CAST(size_t *, val
) = ms
->bytes_max
;
673 case MAGIC_PARAM_ENCODING_MAX
:
674 *CAST(size_t *, val
) = ms
->encoding_max
;