From: Christos Zoulas Date: Thu, 27 Mar 2003 18:34:21 +0000 (+0000) Subject: move the tar stuff and match it before the softmagic X-Git-Tag: FILE5_05~1003 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=998acdca9425f2e2f47b92a07d34d20a96ee3465;p=file move the tar stuff and match it before the softmagic --- diff --git a/src/ascmagic.c b/src/ascmagic.c index 6fd55f28..3fd3dfb0 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -54,7 +54,7 @@ #include "names.h" #ifndef lint -FILE_RCSID("@(#)$Id: ascmagic.c,v 1.36 2003/03/26 15:35:30 christos Exp $") +FILE_RCSID("@(#)$Id: ascmagic.c,v 1.37 2003/03/27 18:34:21 christos Exp $") #endif /* lint */ typedef unsigned long unichar; @@ -98,23 +98,6 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes) int last_line_end = -1; int has_long_lines = 0; - /* - * Do the tar test first, because if the first file in the tar - * archive starts with a dot, we can confuse it with an nroff file. - */ - switch (file_is_tar(buf, nbytes)) { - case 1: - if (file_printf(ms, (ms->flags & MAGIC_MIME) ? - "application/x-tar" : "tar archive") == -1) - return -1; - return 1; - case 2: - if (file_printf(ms, (ms->flags & MAGIC_MIME) ? - "application/x-tar, POSIX" : "POSIX tar archive") == -1) - return -1; - return 1; - } - /* * Undo the NUL-termination kindly provided by process() * but leave at least one byte to look at diff --git a/src/file.h b/src/file.h index 2528601a..47778060 100644 --- a/src/file.h +++ b/src/file.h @@ -32,7 +32,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$Id: file.h,v 1.51 2003/03/26 16:25:58 christos Exp $ + * @(#)$Id: file.h,v 1.52 2003/03/27 18:34:21 christos Exp $ */ #ifndef __file_h__ @@ -184,7 +184,7 @@ protected int file_reset(struct magic_set *); protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t); protected int file_zmagic(struct magic_set *, const unsigned char *, size_t); protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t); -protected int file_is_tar(const unsigned char *, size_t); +protected int file_is_tar(struct magic_set *, const unsigned char *, size_t); protected int file_softmagic(struct magic_set *, const unsigned char *, size_t); protected struct mlist *file_apprentice(struct magic_set *, const char *, int); protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_t); diff --git a/src/funcs.c b/src/funcs.c index 10db8164..f89bd3be 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -110,15 +110,18 @@ file_buffer(struct magic_set *ms, const void *buf, size_t nb) int m; /* try compression stuff */ if ((m = file_zmagic(ms, buf, nb)) == 0) { - /* try tests in /etc/magic (or surrogate magic file) */ - if ((m = file_softmagic(ms, buf, nb)) == 0) { - /* try known keywords, check whether it is ASCII */ - if ((m = file_ascmagic(ms, buf, nb)) == 0) { - /* abandon hope, all ye who remain here */ - if (file_printf(ms, ms->flags & MAGIC_MIME ? - "application/octet-stream" : "data") == -1) - return -1; - m = 1; + /* Check if we have a tar file */ + if ((m = file_is_tar(ms, buf, nb)) == 0) { + /* try tests in /etc/magic (or surrogate magic file) */ + if ((m = file_softmagic(ms, buf, nb)) == 0) { + /* try known keywords, check whether it is ASCII */ + if ((m = file_ascmagic(ms, buf, nb)) == 0) { + /* abandon hope, all ye who remain here */ + if (file_printf(ms, ms->flags & MAGIC_MIME ? + "application/octet-stream" : "data") == -1) + return -1; + m = 1; + } } } } diff --git a/src/is_tar.c b/src/is_tar.c index be9311b6..ba1967f0 100644 --- a/src/is_tar.c +++ b/src/is_tar.c @@ -49,21 +49,45 @@ #include "tar.h" #ifndef lint -FILE_RCSID("@(#)$Id: is_tar.c,v 1.20 2003/03/26 15:35:30 christos Exp $") +FILE_RCSID("@(#)$Id: is_tar.c,v 1.21 2003/03/27 18:34:21 christos Exp $") #endif #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) +private int is_tar(const unsigned char *, size_t); private int from_oct(int, const char *); /* Decode octal number */ +protected int +file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) +{ + /* + * Do the tar test first, because if the first file in the tar + * archive starts with a dot, we can confuse it with an nroff file. + */ + switch (is_tar(buf, nbytes)) { + case 1: + if (file_printf(ms, (ms->flags & MAGIC_MIME) ? + "application/x-tar" : "tar archive") == -1) + return -1; + return 1; + case 2: + if (file_printf(ms, (ms->flags & MAGIC_MIME) ? + "application/x-tar, POSIX" : "POSIX tar archive") == -1) + return -1; + return 1; + default: + return 0; + } +} + /* * Return * 0 if the checksum is bad (i.e., probably not a tar archive), * 1 for old UNIX tar file, * 2 for Unix Std (POSIX) tar file. */ -protected int -file_is_tar(const unsigned char *buf, size_t nbytes) +private int +is_tar(const unsigned char *buf, size_t nbytes) { const union record *header = (const union record *)(const void *)buf; int i;