diff options
Diffstat (limited to 'contrib/file/src/funcs.c')
-rw-r--r-- | contrib/file/src/funcs.c | 53 |
1 files changed, 34 insertions, 19 deletions
diff --git a/contrib/file/src/funcs.c b/contrib/file/src/funcs.c index f59f4a1b97dc..0bf92fe1c250 100644 --- a/contrib/file/src/funcs.c +++ b/contrib/file/src/funcs.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.94 2017/11/02 20:25:39 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.95 2018/05/24 18:09:17 christos Exp $") #endif /* lint */ #include "magic.h" @@ -183,9 +183,11 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u const char *type = "application/octet-stream"; const char *def = "data"; const char *ftype = NULL; + char *rbuf = NULL; struct buffer b; buffer_init(&b, fd, buf, nb); + ms->mode = b.st.st_mode; if (nb == 0) { def = "empty"; @@ -248,31 +250,43 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u goto done; } } +#ifdef BUILTIN_ELF + if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) { + file_pushbuf_t *pb; + /* + * We matched something in the file, so this + * *might* be an ELF file, and the file is at + * least 5 bytes long, so if it's an ELF file + * it has at least one byte past the ELF magic + * number - try extracting information from the + * ELF headers that cannot easily be extracted + * with rules in the magic file. We we don't + * print the information yet. + */ + if ((pb = file_push_buffer(ms)) == NULL) + return -1; + + rv = file_tryelf(ms, &b); + rbuf = file_pop_buffer(ms, pb); + if (rv != 1) { + free(rbuf); + rbuf = NULL; + } + if ((ms->flags & MAGIC_DEBUG) != 0) + (void)fprintf(stderr, "[try elf %d]\n", m); + } +#endif /* try soft magic tests */ if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try softmagic %d]\n", m); + if (m == 1 && rbuf) { + if (file_printf(ms, "%s", rbuf) == -1) + goto done; + } if (m) { -#ifdef BUILTIN_ELF - if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 && - nb > 5 && fd != -1) { - /* - * We matched something in the file, so this - * *might* be an ELF file, and the file is at - * least 5 bytes long, so if it's an ELF file - * it has at least one byte past the ELF magic - * number - try extracting information from the - * ELF headers that cannot easily * be - * extracted with rules in the magic file. - */ - m = file_tryelf(ms, &b); - if ((ms->flags & MAGIC_DEBUG) != 0) - (void)fprintf(stderr, "[try elf %d]\n", - m); - } -#endif if (checkdone(ms, &rv)) goto done; } @@ -318,6 +332,7 @@ simple: #if HAVE_FORK done_encoding: #endif + free(rbuf); buffer_fini(&b); if (rv) return rv; |