From 9705b9b2f010aca8638e6124d0141eb1d6392a5f Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Sat, 18 Oct 2008 20:47:47 +0000 Subject: [PATCH] add apple creator/type --- ChangeLog | 4 ++++ doc/file.man | 4 +++- doc/magic.man | 7 ++++++- magic/Magdir/compress | 1 + magic/Magdir/images | 1 + magic/Magdir/jpeg | 1 + magic/Magdir/macintosh | 3 +++ magic/Magdir/printer | 2 ++ src/apprentice.c | 45 +++++++++++++++++++++++++++++++++++------- src/ascmagic.c | 5 ++++- src/file.c | 7 +++++-- src/file.h | 12 ++++++----- src/file_opts.h | 1 + src/fsmagic.c | 4 +++- src/is_tar.c | 9 +++++---- src/magic.h | 1 + src/readcdf.c | 4 +++- src/readelf.c | 4 ++-- src/softmagic.c | 24 ++++++++++++++-------- 19 files changed, 106 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1cd8d338..42a5c132 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2008-10-18 16:45 Christos Zoulas + + * Added APPLE file creator/type + 2008-10-12 10:20 Christos Zoulas * Added CDF parsing diff --git a/doc/file.man b/doc/file.man index cc55b339..ec2512d6 100644 --- a/doc/file.man +++ b/doc/file.man @@ -1,4 +1,4 @@ -.\" $File: file.man,v 1.74 2008/10/09 17:19:11 christos Exp $ +.\" $File: file.man,v 1.75 2008/10/09 17:25:01 christos Exp $ .Dd October 9, 2008 .Dt FILE __CSECTION__ .Os @@ -182,6 +182,8 @@ Check for application type (only on EMX). .It ascii Check for various types of ascii files. +.It cdf +Don't look for Compound Document Files. .It compress Don't look for, or inside compressed files. .It elf diff --git a/doc/magic.man b/doc/magic.man index b624c73e..db92b3e4 100644 --- a/doc/magic.man +++ b/doc/magic.man @@ -1,4 +1,4 @@ -.\" $File: magic.man,v 1.56 2008/03/01 22:21:48 rrt Exp $ +.\" $File: magic.man,v 1.57 2008/08/30 09:50:20 christos Exp $ .Dd August 30, 2008 .Dt MAGIC __FSECTION__ .Os @@ -290,6 +290,11 @@ added before it: multiple matches are normally separated by a single space. .El .Pp +An APPLE 4+4 character APPLE creator and type can be specified as: +.Bd -literal -offset indent +!:apple CREATYPE +.Ed +.Pp A MIME type is given on a separate line, which must be the next non-blank or comment line after the magic line that identifies the file type, and has the following format: diff --git a/magic/Magdir/compress b/magic/Magdir/compress index e2e4e032..016d0932 100644 --- a/magic/Magdir/compress +++ b/magic/Magdir/compress @@ -11,6 +11,7 @@ # standard unix compress 0 string \037\235 compress'd data !:mime application/x-compress +!:apple LZIVZIVU >2 byte&0x80 >0 block compressed >2 byte&0x1f x %d bits diff --git a/magic/Magdir/images b/magic/Magdir/images index cccc70be..22474538 100644 --- a/magic/Magdir/images +++ b/magic/Magdir/images @@ -110,6 +110,7 @@ # GIF 0 string GIF8 GIF image data !:mime image/gif +!:apple 8BIMGIFf >4 string 7a \b, version 8%s, >4 string 9a \b, version 8%s, >6 leshort >0 %hd x diff --git a/magic/Magdir/jpeg b/magic/Magdir/jpeg index d728de6e..4470be4a 100644 --- a/magic/Magdir/jpeg +++ b/magic/Magdir/jpeg @@ -10,6 +10,7 @@ # 0 beshort 0xffd8 JPEG image data !:mime image/jpeg +!:apple 8BIMJPEG !:strength +1 >6 string JFIF \b, JFIF standard # The following added by Erik Rossen 1999-09-06 diff --git a/magic/Magdir/macintosh b/magic/Magdir/macintosh index 77187a39..ca665ded 100644 --- a/magic/Magdir/macintosh +++ b/magic/Magdir/macintosh @@ -11,6 +11,8 @@ # Stuffit archives are the de facto standard of compression for Macintosh # files obtained from most archives. (franklsm@tuns.ca) 0 string SIT! StuffIt Archive (data) +!:mime application/x-stuffit +!:apple SIT!SIT! >2 string x : %s 0 string SITD StuffIt Deluxe (data) >2 string x : %s @@ -20,6 +22,7 @@ # Newer StuffIt archives (grant@netbsd.org) 0 string StuffIt StuffIt Archive !:mime application/x-stuffit +!:apple SIT!SIT! #>162 string >0 : %s # Macintosh Applications and Installation binaries (franklsm@tuns.ca) diff --git a/magic/Magdir/printer b/magic/Magdir/printer index 15a17582..e25d03e9 100644 --- a/magic/Magdir/printer +++ b/magic/Magdir/printer @@ -6,6 +6,7 @@ # PostScript, updated by Daniel Quinlan (quinlan@yggdrasil.com) 0 string %! PostScript document text !:mime application/postscript +!:apple ASPSTEXT >2 string PS-Adobe- conforming >>11 string >\0 DSC level %.3s >>>15 string EPS \b, type %s @@ -16,6 +17,7 @@ # Some PCs have the annoying habit of adding a ^D as a document separator 0 string \004%! PostScript document text !:mime application/postscript +!:apple ASPSTEXT >3 string PS-Adobe- conforming >>12 string >\0 DSC level %.3s >>>16 string EPS \b, type %s diff --git a/src/apprentice.c b/src/apprentice.c index be84f20f..3130c068 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -49,7 +49,7 @@ #include #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.140 2008/07/20 04:02:15 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.141 2008/08/31 07:58:00 christos Exp $") #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -115,8 +115,8 @@ private int check_format_type(const char *, int); private int check_format(struct magic_set *, struct magic *); 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_strength(struct magic_set *, struct magic_entry *, const char *); +private int parse_apple(struct magic_set *, struct magic_entry *, const char *); private size_t maxmagic = 0; @@ -131,6 +131,7 @@ private struct { } bang[] = { #define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name } DECLARE_FIELD(mime), + DECLARE_FIELD(apple), DECLARE_FIELD(strength), #undef DECLARE_FIELD { NULL, 0, NULL } @@ -1473,6 +1474,38 @@ out: return -1; } +/* + * Parse an Apple CREATOR/TYPE annotation from magic file and put it into magic[index - 1] + */ +private int +parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) +{ + size_t i; + const char *l = line; + struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; + + if (m->apple[0] != '\0') { + file_magwarn(ms, "Current entry already has a APPLE type `%.8s'," + " new type `%s'", m->mimetype, l); + return -1; + } + + EATAB; + for (i = 0; *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l)) + || strchr("-+/.", *l)) && i < sizeof(m->apple); m->apple[i++] = *l++) + continue; + if (i == sizeof(m->apple) && *l) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "APPLE type `%s' truncated %zu", + line, i); + } + + if (i > 0) + return 0; + else + return -1; +} + /* * parse a MIME annotation line from magic file, put into magic[index - 1] * if valid @@ -1491,10 +1524,8 @@ parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) } EATAB; - for (i = 0; - *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l)) - || strchr("-+/.", *l)) && i < sizeof(m->mimetype); - m->mimetype[i++] = *l++) + for (i = 0; *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l)) + || strchr("-+/.", *l)) && i < sizeof(m->mimetype); m->mimetype[i++] = *l++) continue; if (i == sizeof(m->mimetype)) { m->desc[sizeof(m->mimetype) - 1] = '\0'; diff --git a/src/ascmagic.c b/src/ascmagic.c index b480c5f8..4efd1956 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -49,7 +49,7 @@ #include "names.h" #ifndef lint -FILE_RCSID("@(#)$File: ascmagic.c,v 1.65 2008/08/31 07:58:00 christos Exp $") +FILE_RCSID("@(#)$File: ascmagic.c,v 1.66 2008/10/16 16:31:16 christos Exp $") #endif /* lint */ #define MAXLINELEN 300 /* longest sane line length */ @@ -97,6 +97,9 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes) size_t last_line_end = (size_t)-1; int has_long_lines = 0; + if (ms->flags & MAGIC_APPLE) + return 0; + /* * Undo the NUL-termination kindly provided by process() * but leave at least one byte to look at diff --git a/src/file.c b/src/file.c index 0d9ee085..17862a5d 100644 --- a/src/file.c +++ b/src/file.c @@ -74,7 +74,7 @@ int getopt_long(int argc, char * const *argv, const char *optstring, const struc #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.124 2008/10/09 17:24:03 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.125 2008/10/12 15:38:51 christos Exp $") #endif /* lint */ @@ -193,9 +193,12 @@ main(int argc, char *argv[]) help(); break; case 10: - flags |= MAGIC_MIME_TYPE; + flags |= MAGIC_APPLE; break; case 11: + flags |= MAGIC_MIME_TYPE; + break; + case 12: flags |= MAGIC_MIME_ENCODING; break; } diff --git a/src/file.h b/src/file.h index 8a1bafcb..8937c909 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.109 2008/09/02 07:11:30 christos Exp $ + * @(#)$File: file.h,v 1.110 2008/10/12 15:38:52 christos Exp $ */ #ifndef __file_h__ @@ -103,8 +103,8 @@ #define MAXstring 32 /* max leng of "string" types */ #define MAGICNO 0xF11E041C -#define VERSIONNO 6 -#define FILE_MAGICSIZE (32 * 6) +#define VERSIONNO 7 +#define FILE_MAGICSIZE 200 #define FILE_LOAD 0 #define FILE_CHECK 1 @@ -258,10 +258,12 @@ struct magic { #define str_flags _u._s._flags /* Words 9-16 */ union VALUETYPE value; /* either number or string */ - /* Words 17..31 */ + /* Words 17-25 */ char desc[MAXDESC]; /* description */ - /* Words 32..47 */ + /* Words 26-34 */ char mimetype[MAXDESC]; /* MIME type */ + /* Words 35-43 */ + char apple[8]; }; #define BIT(A) (1 << (A)) diff --git a/src/file_opts.h b/src/file_opts.h index 46bc08ae..f38354ad 100644 --- a/src/file_opts.h +++ b/src/file_opts.h @@ -28,6 +28,7 @@ OPT('f', "files-from", 1, " FILE read the filenames to be examined from FIL 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("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 1881e408..596b779d 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -57,7 +57,7 @@ #undef HAVE_MAJOR #ifndef lint -FILE_RCSID("@(#)$File: fsmagic.c,v 1.52 2008/07/25 23:59:01 rrt Exp $") +FILE_RCSID("@(#)$File: fsmagic.c,v 1.53 2008/10/16 16:31:16 christos Exp $") #endif /* lint */ private int @@ -106,6 +106,8 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) struct stat tstatbuf; #endif + if (ms->flags & MAGIC_APPLE) + return 0; if (fn == NULL) return 0; diff --git a/src/is_tar.c b/src/is_tar.c index 3a4360be..208a2545 100644 --- a/src/is_tar.c +++ b/src/is_tar.c @@ -45,7 +45,7 @@ #include "tar.h" #ifndef lint -FILE_RCSID("@(#)$File: is_tar.c,v 1.30 2008/01/26 18:45:16 christos Exp $") +FILE_RCSID("@(#)$File: is_tar.c,v 1.31 2008/02/04 20:51:17 christos Exp $") #endif #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) @@ -66,13 +66,14 @@ 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. */ - int tar = is_tar(buf, nbytes); + int tar; int mime = ms->flags & MAGIC_MIME; - if (tar < 1 || tar > 3) + if ((ms->flags & MAGIC_APPLE) != 0 || mime == MAGIC_MIME_ENCODING) return 0; - if (mime == MAGIC_MIME_ENCODING) + tar = is_tar(buf, nbytes); + if (tar < 1 || tar > 3) return 0; if (file_printf(ms, mime ? "application/x-tar" : diff --git a/src/magic.h b/src/magic.h index 2c085701..5ff78a29 100644 --- a/src/magic.h +++ b/src/magic.h @@ -42,6 +42,7 @@ #define MAGIC_ERROR 0x000200 /* Handle ENOENT etc as real errors */ #define MAGIC_MIME_ENCODING 0x000400 /* Return only the MIME encoding */ #define MAGIC_MIME (MAGIC_MIME_TYPE|MAGIC_MIME_ENCODING) +#define MAGIC_APPLE 0x000800 /* Return APPLE CREATOR/TYPE */ #define MAGIC_NO_CHECK_COMPRESS 0x001000 /* Don't check for compressed files */ #define MAGIC_NO_CHECK_TAR 0x002000 /* Don't check for tar files */ #define MAGIC_NO_CHECK_SOFT 0x004000 /* Don't check magic entries */ diff --git a/src/readcdf.c b/src/readcdf.c index 250fdf07..dd7bb42b 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.5 2008/10/13 18:23:47 christos Exp $") +FILE_RCSID("@(#)$File: readcdf.c,v 1.6 2008/10/14 10:38:22 christos Exp $") #endif #include @@ -191,6 +191,8 @@ file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf, (void)&nbytes; (void)&buf; + if (ms->flags & MAGIC_APPLE) + return 0; if (cdf_read_header(fd, &h) == -1) return 0; #ifdef CDF_DEBUG diff --git a/src/readelf.c b/src/readelf.c index d2ddb19f..c807d040 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -38,7 +38,7 @@ #include "magic.h" #ifndef lint -FILE_RCSID("@(#)$File: readelf.c,v 1.78 2008/08/31 07:58:00 christos Exp $") +FILE_RCSID("@(#)$File: readelf.c,v 1.79 2008/10/12 19:06:36 christos Exp $") #endif #ifdef ELFCORE @@ -1141,7 +1141,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf, Elf64_Ehdr elf64hdr; uint16_t type; - if (ms->flags & MAGIC_MIME) + if (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) return 0; /* * ELF executables have multiple section headers in arbitrary diff --git a/src/softmagic.c b/src/softmagic.c index 61296113..ea5481ca 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -38,7 +38,7 @@ #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.122 2008/09/23 14:34:25 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.123 2008/10/16 16:31:16 christos Exp $") #endif /* lint */ private int match(struct magic_set *, struct magic *, uint32_t, @@ -52,7 +52,7 @@ private int mcopy(struct magic_set *, union VALUETYPE *, int, int, const unsigned char *, uint32_t, size_t, size_t); private int mconvert(struct magic_set *, struct magic *); private int print_sep(struct magic_set *, int); -private int handle_mime(struct magic_set *, struct magic *); +private int handle_annotation(struct magic_set *, struct magic *); private void cvt_8(union VALUETYPE *, const struct magic *); private void cvt_16(union VALUETYPE *, const struct magic *); private void cvt_32(union VALUETYPE *, const struct magic *); @@ -168,7 +168,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic, if (*m->desc) { need_separator = 1; printed_something = 1; - if ((e = handle_mime(ms, m)) != 0) + if ((e = handle_annotation(ms, m)) != 0) return e; if (print_sep(ms, firstline) == -1) return -1; @@ -236,7 +236,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic, */ if (*m->desc) { printed_something = 1; - if ((e = handle_mime(ms, m)) != 0) + if ((e = handle_annotation(ms, m)) != 0) return e; if (print_sep(ms, firstline) == -1) return -1; @@ -1592,7 +1592,8 @@ magiccheck(struct magic_set *ms, struct magic *m) default: matched = 0; - file_magerror(ms, "cannot happen with float: invalid relation `%c'", m->reln); + file_magerror(ms, "cannot happen with float: invalid relation `%c'", + m->reln); return -1; } return matched; @@ -1820,10 +1821,17 @@ magiccheck(struct magic_set *ms, struct magic *m) } private int -handle_mime(struct magic_set *ms, struct magic *m) +handle_annotation(struct magic_set *ms, struct magic *m) { - if (ms->flags & MAGIC_MIME_TYPE) - return file_printf(ms, "%s", m->mimetype); + if (ms->flags & MAGIC_APPLE) { + if (file_printf(ms, "%.8s", m->apple) == -1) + return -1; + return 1; + } + if (ms->flags & MAGIC_MIME_TYPE) { + if (file_printf(ms, "%.8s", m->mimetype) == -1) + return 1; + } return 0; } -- 2.40.0