From 6678cf7c2ac8c768ea5efe9fe0392f07e522c963 Mon Sep 17 00:00:00 2001 From: obrien Date: Sun, 13 Jan 2008 20:35:16 +0000 Subject: Import file version 4.23 onto the vendor branch --- contrib/file/funcs.c | 117 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 47 deletions(-) (limited to 'contrib/file/funcs.c') diff --git a/contrib/file/funcs.c b/contrib/file/funcs.c index 47f0e4f..5cf1181 100644 --- a/contrib/file/funcs.c +++ b/contrib/file/funcs.c @@ -41,14 +41,14 @@ #endif #ifndef SIZE_T_MAX #ifdef __LP64__ -#define SIZE_T_MAX (size_t)0xfffffffffffffffffU +#define SIZE_T_MAX (size_t)0xffffffffffffffffU #else #define SIZE_T_MAX (size_t)0xffffffffU #endif #endif #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.32 2007/05/24 17:22:27 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.35 2007/12/27 16:35:59 christos Exp $") #endif /* lint */ #ifndef HAVE_VSNPRINTF @@ -62,12 +62,16 @@ protected int file_printf(struct magic_set *ms, const char *fmt, ...) { va_list ap; - size_t len, size; + size_t size; + ssize_t len; char *buf; va_start(ap, fmt); - if ((len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap)) >= ms->o.left) { + len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap); + if (len == -1) + goto out; + if (len >= (ssize_t)ms->o.left) { long diff; /* XXX: really ptrdiff_t */ va_end(ap); @@ -84,11 +88,16 @@ file_printf(struct magic_set *ms, const char *fmt, ...) va_start(ap, fmt); len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap); + if (len == -1) + goto out; } va_end(ap); ms->o.ptr += len; ms->o.left -= len; return 0; +out: + file_error(ms, errno, "vsnprintf failed"); + return -1; } /* @@ -164,59 +173,73 @@ protected int file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf, size_t nb) { - int m; + int m; + int mime = ms->flags & MAGIC_MIME; + + if (nb == 0) { + if ((!mime || (mime & MAGIC_MIME_TYPE)) && + file_printf(ms, mime ? "application/x-empty" : + "empty") == -1) + return -1; + return 1; + } else if (nb == 1) { + if ((!mime || (mime & MAGIC_MIME_TYPE)) && + file_printf(ms, mime ? "application/octet-stream" : + "very short file (no magic)") == -1) + return -1; + return 1; + } #ifdef __EMX__ - if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { - switch (file_os2_apptype(ms, inname, buf, nb)) { - case -1: - return -1; - case 0: - break; - default: - return 1; + if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { + switch (file_os2_apptype(ms, inname, buf, nb)) { + case -1: + return -1; + case 0: + break; + default: + return 1; + } } - } #endif - /* try compression stuff */ - if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 || - (m = file_zmagic(ms, fd, inname, buf, nb)) == 0) { - /* Check if we have a tar file */ - if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 || - (m = file_is_tar(ms, buf, nb)) == 0) { - /* try tests in /etc/magic (or surrogate magic file) */ - if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 || - (m = file_softmagic(ms, buf, nb)) == 0) { - /* try known keywords, check whether it is ASCII */ - if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 || - (m = file_ascmagic(ms, buf, nb)) == 0) { - /* abandon hope, all ye who remain here */ - if (file_printf(ms, ms->flags & MAGIC_MIME ? - (nb ? "application/octet-stream" : - "application/empty") : - (nb ? "data" : - "empty")) == -1) - return -1; - m = 1; + /* try compression stuff */ + if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 || + (m = file_zmagic(ms, fd, inname, buf, nb)) == 0) { + /* Check if we have a tar file */ + if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 || + (m = file_is_tar(ms, buf, nb)) == 0) { + /* try tests in /etc/magic (or surrogate magic file) */ + if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 || + (m = file_softmagic(ms, buf, nb)) == 0) { + /* try known keywords, check whether it is ASCII */ + if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 || + (m = file_ascmagic(ms, buf, nb)) == 0) { + /* abandon hope, all ye who remain here */ + if ((!mime || (mime & MAGIC_MIME_TYPE)) && + file_printf(ms, mime ? "application/octet-stream" : + "data") == -1) + return -1; + m = 1; + } } } } - } #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. - */ - (void)file_tryelf(ms, fd, buf, nb); - } + 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. + */ + (void)file_tryelf(ms, fd, buf, nb); + } #endif - return m; + return m; } #endif -- cgit v1.1