From 29d36efc28f5c5e94b549de4fc9cf13aade3a7d4 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Thu, 9 Apr 2015 20:01:40 +0000 Subject: [PATCH] Add --extension --- ChangeLog | 5 +++++ TODO | 3 +++ doc/file.man | 7 +++++-- doc/libmagic.man | 6 ++++-- magic/Magdir/jpeg | 3 ++- magic/Magdir/mathematica | 13 ++++++++++++- src/apprentice.c | 17 ++++++++++++++++- src/ascmagic.c | 6 +++--- src/file.c | 9 ++++++--- src/file.h | 10 ++++++---- src/file_opts.h | 1 + src/fsmagic.c | 4 ++-- src/is_tar.c | 4 ++-- src/readcdf.c | 4 ++-- src/readelf.c | 4 ++-- src/softmagic.c | 11 ++++++++--- 16 files changed, 79 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9ee8a1ff..ac392bed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-04-09 15:59 Christos Zoulas + + * Add --extension to list the known extensions for this file type + Idea by Andrew J Roazen + 2015-02-14 12:23 Christos Zoulas * Bump file search buffer size to 1M. diff --git a/TODO b/TODO index eef01edb..5c2ed49e 100644 --- a/TODO +++ b/TODO @@ -17,3 +17,6 @@ hard-wired routines). In this regard, note that hplip, which is BSD-licensed, has a magic reimplementation in Python. Read the kerberos magic entry for more ideas. + +Write a string merger to make magic entry sizes dynamic. +Strings will be converted to offsets from the string table. diff --git a/doc/file.man b/doc/file.man index 714f1928..6a1fd904 100644 --- a/doc/file.man +++ b/doc/file.man @@ -1,5 +1,5 @@ -.\" $File: file.man,v 1.112 2015/02/14 17:24:30 christos Exp $ -.Dd February 14, 2015 +.\" $File: file.man,v 1.113 2015/04/09 20:01:40 christos Exp $ +.Dd April 9, 2015 .Dt FILE __CSECTION__ .Os .Sh NAME @@ -10,6 +10,7 @@ .Bk -words .Op Fl bcEhiklLNnprsvz0 .Op Fl Fl apple +.Op Fl Fl extension .Op Fl Fl mime-encoding .Op Fl Fl mime-type .Op Fl e Ar testname @@ -213,6 +214,8 @@ Consults magic files. .It tar Examines tar files. .El +.It Fl Fl extension +Print a comma-separated list of valid extensions for the file type found. .It Fl F , Fl Fl separator Ar separator Use the specified string as the separator between the filename and the file result returned. diff --git a/doc/libmagic.man b/doc/libmagic.man index 3c907cad..933e4233 100644 --- a/doc/libmagic.man +++ b/doc/libmagic.man @@ -1,4 +1,4 @@ -.\" $File: libmagic.man,v 1.34 2014/12/16 23:18:40 christos Exp $ +.\" $File: libmagic.man,v 1.35 2015/04/09 20:01:40 christos Exp $ .\" .\" Copyright (c) Christos Zoulas 2003. .\" All Rights Reserved. @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd December 16, 2014 +.Dd April 9, 2015 .Dt LIBMAGIC 3 .Os .Sh NAME @@ -132,6 +132,8 @@ Treat operating system errors while trying to open files and follow symlinks as real errors, instead of printing them in the magic buffer. .It Dv MAGIC_APPLE Return the Apple creator and type. +.It Dv MAGIC_EXTENSION +Return a comma-separated list of extensions for this file type. .It Dv MAGIC_NO_CHECK_APPTYPE Don't check for .Dv EMX diff --git a/magic/Magdir/jpeg b/magic/Magdir/jpeg index 1c5cc517..daa86602 100644 --- a/magic/Magdir/jpeg +++ b/magic/Magdir/jpeg @@ -1,6 +1,6 @@ #------------------------------------------------------------------------------ -# $File: jpeg,v 1.27 2015/02/06 17:08:31 christos Exp $ +# $File: jpeg,v 1.28 2015/04/09 20:01:40 christos Exp $ # JPEG images # SunOS 5.5.1 had # @@ -13,6 +13,7 @@ !:mime image/jpeg !:apple 8BIMJPEG !:strength *3 +!:ext jpeg,jpg,jpe,jfif >6 string JFIF \b, JFIF standard # The following added by Erik Rossen 1999-09-06 # in a vain attempt to add image size reporting for JFIF. Note that these diff --git a/magic/Magdir/mathematica b/magic/Magdir/mathematica index 3c10a57d..a93899e8 100644 --- a/magic/Magdir/mathematica +++ b/magic/Magdir/mathematica @@ -1,6 +1,6 @@ #------------------------------------------------------------------------------ -# $File: mathematica,v 1.7 2009/09/19 16:28:10 christos Exp $ +# $File: mathematica,v 1.8 2015/04/09 20:01:40 christos Exp $ # mathematica: file(1) magic for mathematica files # "H. Nanosecond" # Mathematica a multi-purpose math program @@ -8,13 +8,16 @@ #mathematica .mb 0 string \064\024\012\000\035\000\000\000 Mathematica version 2 notebook +!:ext mb 0 string \064\024\011\000\035\000\000\000 Mathematica version 2 notebook +!:ext mb # .ma # multiple possibilites: 0 string (*^\n\n::[\011frontEndVersion\ =\ Mathematica notebook #>41 string >\0 %s +!:ext mb #0 string (*^\n\n::[\011palette Mathematica notebook version 2.x @@ -25,13 +28,21 @@ # generic: 0 string (*^\r\r::[\011 Mathematica notebook version 2.x +!:ext mb 0 string (*^\r\n\r\n::[\011 Mathematica notebook version 2.x +!:ext mb 0 string (*^\015 Mathematica notebook version 2.x +!:ext mb 0 string (*^\n\r\n\r::[\011 Mathematica notebook version 2.x +!:ext mb 0 string (*^\r::[\011 Mathematica notebook version 2.x +!:ext mb 0 string (*^\r\n::[\011 Mathematica notebook version 2.x +!:ext mb 0 string (*^\n\n::[\011 Mathematica notebook version 2.x +!:ext mb 0 string (*^\n::[\011 Mathematica notebook version 2.x +!:ext mb # Mathematica .mx files diff --git a/src/apprentice.c b/src/apprentice.c index 5b076b3b..e810ee76 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.231 2015/02/06 17:08:58 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.232 2015/04/09 20:01:40 christos Exp $") #endif /* lint */ #include "magic.h" @@ -149,6 +149,7 @@ private int get_op(char); private int parse_mime(struct magic_set *, struct magic_entry *, const char *); private int parse_strength(struct magic_set *, struct magic_entry *, const char *); private int parse_apple(struct magic_set *, struct magic_entry *, const char *); +private int parse_ext(struct magic_set *, struct magic_entry *, const char *); private size_t magicsize = sizeof(struct magic); @@ -163,6 +164,7 @@ private struct { #define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name } DECLARE_FIELD(mime), DECLARE_FIELD(apple), + DECLARE_FIELD(ext), DECLARE_FIELD(strength), #undef DECLARE_FIELD { NULL, 0, NULL } @@ -2254,6 +2256,19 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) sizeof(m->apple), "APPLE", "!+-./", 0); } +/* + * Parse a comma-separated list of extensions + */ +private int +parse_ext(struct magic_set *ms, struct magic_entry *me, const char *line) +{ + struct magic *m = &me->mp[0]; + + return parse_extra(ms, me, line, + CAST(off_t, offsetof(struct magic, ext)), + sizeof(m->ext), "EXTENSION", ",!+-/", 0); +} + /* * parse a MIME annotation line from magic file, put into magic[index - 1] * if valid diff --git a/src/ascmagic.c b/src/ascmagic.c index 78a6dbb7..9e0f6637 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: ascmagic.c,v 1.91 2014/11/28 02:46:39 christos Exp $") +FILE_RCSID("@(#)$File: ascmagic.c,v 1.92 2015/04/09 20:01:41 christos Exp $") #endif /* lint */ #include "magic.h" @@ -79,7 +79,7 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, const char *code_mime = NULL; const char *type = NULL; - if (ms->flags & MAGIC_APPLE) + if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) return 0; nbytes = trim_nuls(buf, nbytes); @@ -123,7 +123,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf, size_t last_line_end = (size_t)-1; int has_long_lines = 0; - if (ms->flags & MAGIC_APPLE) + if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) return 0; nbytes = trim_nuls(buf, nbytes); diff --git a/src/file.c b/src/file.c index 81723789..45202a0d 100644 --- a/src/file.c +++ b/src/file.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.162 2015/02/09 20:15:50 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.163 2015/04/09 20:01:41 christos Exp $") #endif /* lint */ #include "magic.h" @@ -75,7 +75,7 @@ int getopt_long(int argc, char * const *argv, const char *optstring, const struc # define USAGE \ "Usage: %s [" FILE_FLAGS \ - "] [--apple] [--mime-encoding] [--mime-type]\n" \ + "] [--apple] [--extension] [--mime-encoding] [--mime-type]\n" \ " [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \ "file ...\n" \ " %s -C [-m magicfiles]\n" \ @@ -191,9 +191,12 @@ main(int argc, char *argv[]) flags |= MAGIC_APPLE; break; case 11: - flags |= MAGIC_MIME_TYPE; + flags |= MAGIC_EXTENSION; break; case 12: + flags |= MAGIC_MIME_TYPE; + break; + case 13: flags |= MAGIC_MIME_ENCODING; break; } diff --git a/src/file.h b/src/file.h index 08bbd12b..1b4ef6f7 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.167 2015/02/14 17:19:29 christos Exp $ + * @(#)$File: file.h,v 1.168 2015/04/09 20:01:41 christos Exp $ */ #ifndef __file_h__ @@ -135,8 +135,8 @@ #define MAXstring 64 /* max len of "string" types */ #define MAGICNO 0xF11E041C -#define VERSIONNO 12 -#define FILE_MAGICSIZE 248 +#define VERSIONNO 13 +#define FILE_MAGICSIZE 312 #define FILE_LOAD 0 #define FILE_CHECK 1 @@ -307,7 +307,9 @@ struct magic { /* Words 33-52 */ char mimetype[MAXMIME]; /* MIME type */ /* Words 53-54 */ - char apple[8]; + char apple[8]; /* APPLE CREATOR/TYPE */ + /* Words 55-63 */ + char ext[64]; /* Popular extensions */ }; #define BIT(A) (1 << (A)) diff --git a/src/file_opts.h b/src/file_opts.h index 3286ac60..79ced3a4 100644 --- a/src/file_opts.h +++ b/src/file_opts.h @@ -29,6 +29,7 @@ OPT('F', "separator", 1, " STRING use string as separator instead of `:'\n") OPT('i', "mime", 0, " output MIME type strings (--mime-type and\n" " --mime-encoding)\n") OPT_LONGONLY("apple", 0, " output the Apple CREATOR/TYPE\n") +OPT_LONGONLY("extension", 0, " output a comma-separated list of extnsions\n") OPT_LONGONLY("mime-type", 0, " output the MIME type\n") OPT_LONGONLY("mime-encoding", 0, " output the MIME encoding\n") OPT('k', "keep-going", 0, " don't stop at the first match\n") diff --git a/src/fsmagic.c b/src/fsmagic.c index 1e8fd74d..27f982a5 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: fsmagic.c,v 1.75 2014/12/04 15:56:46 christos Exp $") +FILE_RCSID("@(#)$File: fsmagic.c,v 1.76 2015/04/09 20:01:41 christos Exp $") #endif /* lint */ #include "magic.h" @@ -110,7 +110,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) struct stat tstatbuf; #endif - if (ms->flags & MAGIC_APPLE) + if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) return 0; if (fn == NULL) return 0; diff --git a/src/is_tar.c b/src/is_tar.c index 876c631b..a3e5dbf2 100644 --- a/src/is_tar.c +++ b/src/is_tar.c @@ -40,7 +40,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: is_tar.c,v 1.37 2010/11/30 14:58:53 rrt Exp $") +FILE_RCSID("@(#)$File: is_tar.c,v 1.38 2015/04/09 20:01:41 christos Exp $") #endif #include "magic.h" @@ -69,7 +69,7 @@ file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) int tar; int mime = ms->flags & MAGIC_MIME; - if ((ms->flags & MAGIC_APPLE) != 0) + if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0) return 0; tar = is_tar(buf, nbytes); diff --git a/src/readcdf.c b/src/readcdf.c index 50aa6d68..99d8ec01 100644 --- a/src/readcdf.c +++ b/src/readcdf.c @@ -26,7 +26,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readcdf.c,v 1.52 2015/02/27 21:16:38 christos Exp $") +FILE_RCSID("@(#)$File: readcdf.c,v 1.53 2015/04/09 20:01:41 christos Exp $") #endif #include @@ -454,7 +454,7 @@ file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf, info.i_fd = fd; info.i_buf = buf; info.i_len = nbytes; - if (ms->flags & MAGIC_APPLE) + if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) return 0; if (cdf_read_header(&info, &h) == -1) return 0; diff --git a/src/readelf.c b/src/readelf.c index 49b101e4..55009e80 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readelf.c,v 1.118 2015/01/02 21:29:39 christos Exp $") +FILE_RCSID("@(#)$File: readelf.c,v 1.119 2015/04/09 20:01:41 christos Exp $") #endif #ifdef BUILTIN_ELF @@ -1353,7 +1353,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf, Elf64_Ehdr elf64hdr; uint16_t type, phnum, shnum, notecount; - if (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) + if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION)) return 0; /* * ELF executables have multiple section headers in arbitrary diff --git a/src/softmagic.c b/src/softmagic.c index ad663af3..09c2ff62 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.213 2015/02/14 18:43:12 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.214 2015/04/09 20:01:41 christos Exp $") #endif /* lint */ #include "magic.h" @@ -147,7 +147,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic, unsigned int cont_level = 0; int returnvalv = 0, e; /* if a match is found it is set to 1*/ int firstline = 1; /* a flag to print X\n X\n- X */ - int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0; + int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION)) == 0; if (returnval == NULL) returnval = &returnvalv; @@ -1674,7 +1674,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m, return -1; if (rv == 1) { - if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 && + if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION)) == 0 && file_printf(ms, F(ms, m, "%u"), offset) == -1) { free(rbuf); return -1; @@ -2136,6 +2136,11 @@ handle_annotation(struct magic_set *ms, struct magic *m) return -1; return 1; } + if (ms->flags & MAGIC_EXTENSION) { + if (file_printf(ms, "%s", m->ext) == -1) + return -1; + return 1; + } if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) { if (file_printf(ms, "%s", m->mimetype) == -1) return -1; -- 2.50.1