From: Christos Zoulas Date: Fri, 14 Mar 2014 18:48:11 +0000 (+0000) Subject: PR/320: Issue a warning for mime types that are attached to magic entries X-Git-Tag: FILE5_18~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=83f2273c08eb2ceb47e43f8b1cddcf2e0d776b6e;p=file PR/320: Issue a warning for mime types that are attached to magic entries that don't yet have descriptions. --- diff --git a/src/apprentice.c b/src/apprentice.c index 8b40fcc6..797e43a3 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.200 2014/03/14 14:58:59 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.201 2014/03/14 17:38:33 christos Exp $") #endif /* lint */ #include "magic.h" @@ -40,6 +40,9 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.200 2014/03/14 14:58:59 christos Exp $") #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_STDDEF_H +#include +#endif #include #include #include @@ -2056,33 +2059,43 @@ 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) +parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line, + off_t off, size_t len, const char *name, int nt) { size_t i; const char *l = line; struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; + char *buf = (char *)m + off; - if (m->apple[0] != '\0') { - file_magwarn(ms, "Current entry already has a APPLE type " - "`%.8s', new type `%s'", m->mimetype, l); + if (buf[0] != '\0') { + len = nt ? strlen(buf) : len; + file_magwarn(ms, "Current entry already has a %s type " + "`%.*s', new type `%s'", name, (int)len, buf, l); return -1; } + if (*m->desc == '\0') { + file_magwarn(ms, "Current entry does not yet have a " + "description for adding a %s type", name); + 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++) + i < len; buf[i++] = *l++) continue; - if (i == sizeof(m->apple) && *l) { - /* We don't need to NUL terminate here, printing handles it */ + + if (i == len && *l) { + if (nt) + buf[len - 1] = '\0'; if (ms->flags & MAGIC_CHECK) - file_magwarn(ms, "APPLE type `%s' truncated %" - SIZE_T_FORMAT "u", line, i); + file_magwarn(ms, "%s type `%s' truncated %" + SIZE_T_FORMAT "u", name, line, i); + } else { + if (nt) + buf[i] = '\0'; } if (i > 0) @@ -2091,6 +2104,19 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) 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) +{ + struct magic *m = &me->mp[0]; + + return parse_extra(ms, me, line, offsetof(struct magic, apple), + sizeof(m->apple), "APPLE", 0); +} + /* * parse a MIME annotation line from magic file, put into magic[index - 1] * if valid @@ -2098,33 +2124,10 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) private int parse_mime(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->mimetype[0] != '\0') { - file_magwarn(ms, "Current entry already has a MIME type `%s'," - " 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->mimetype); m->mimetype[i++] = *l++) - continue; - if (i == sizeof(m->mimetype)) { - m->mimetype[sizeof(m->mimetype) - 1] = '\0'; - if (ms->flags & MAGIC_CHECK) - file_magwarn(ms, "MIME type `%s' truncated %" - SIZE_T_FORMAT "u", m->mimetype, i); - } else - m->mimetype[i] = '\0'; + struct magic *m = &me->mp[0]; - if (i > 0) - return 0; - else - return -1; + return parse_extra(ms, me, line, offsetof(struct magic, mimetype), + sizeof(m->mimetype), "MIME", 1); } private int