diff options
author | Christos Zoulas <christos@zoulas.com> | 2018-02-21 21:26:48 +0000 |
---|---|---|
committer | Christos Zoulas <christos@zoulas.com> | 2018-02-21 21:26:48 +0000 |
commit | 7dbecfe406a6bb2de1fe7ec2fe413dcd8871ac74 (patch) | |
tree | 01643d9168ded4ddcddfe358fca0e11cd6876b12 | |
parent | d0eed82b184c70ec6e6c7ad29ff1927eae197ee1 (diff) | |
download | file-git-7dbecfe406a6bb2de1fe7ec2fe413dcd8871ac74.tar.gz |
Add variable expansions (experimental) to mime types. This is now used
to combine file mode information to mime printing.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | magic/Magdir/elf | 4 | ||||
-rw-r--r-- | src/apprentice.c | 4 | ||||
-rw-r--r-- | src/softmagic.c | 76 |
4 files changed, 79 insertions, 10 deletions
@@ -1,3 +1,8 @@ +2018-02-21 16:25 Christos Zoulas <christos@zoulas.com> + + * add support for ${x?:} in mime types to handle + pie binaries. + 2017-11-03 9:23 Christos Zoulas <christos@zoulas.com> * add support for negative offsets (offsets from the end of file) diff --git a/magic/Magdir/elf b/magic/Magdir/elf index f237afda..83b886dd 100644 --- a/magic/Magdir/elf +++ b/magic/Magdir/elf @@ -1,6 +1,6 @@ #------------------------------------------------------------------------------ -# $File: elf,v 1.69 2015/06/16 17:23:08 christos Exp $ +# $File: elf,v 1.71 2018/02/21 21:26:48 christos Exp $ # elf: file(1) magic for ELF executables # # We have to check the byte order flag to see what byte order all the @@ -49,7 +49,7 @@ >16 leshort 2 executable, !:mime application/x-executable >16 leshort 3 shared object, -!:mime application/x-sharedlib +!:mime application/x-${x?pie-executable:sharedlib} >16 leshort 4 core file !:mime application/x-coredump # Core file detection is not reliable. diff --git a/src/apprentice.c b/src/apprentice.c index a2e4c684..e2ce2d3a 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.269 2017/11/14 15:19:45 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.270 2018/02/21 21:26:48 christos Exp $") #endif /* lint */ #include "magic.h" @@ -2362,7 +2362,7 @@ parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) return parse_extra(ms, me, line, CAST(off_t, offsetof(struct magic, mimetype)), - sizeof(m->mimetype), "MIME", "+-/.", 1); + sizeof(m->mimetype), "MIME", "+-/.$?:{}", 1); } private int diff --git a/src/softmagic.c b/src/softmagic.c index a2efa14d..7a27185e 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.256 2017/11/14 15:19:45 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.257 2018/02/21 21:26:48 christos Exp $") #endif /* lint */ #include "magic.h" @@ -61,7 +61,8 @@ private int mcopy(struct magic_set *, union VALUETYPE *, int, int, const unsigned char *, uint32_t, size_t, struct magic *); private int mconvert(struct magic_set *, struct magic *, int); private int print_sep(struct magic_set *, int); -private int handle_annotation(struct magic_set *, struct magic *, int); +private int handle_annotation(struct magic_set *, struct magic *, + const struct buffer *, int); private int cvt_8(union VALUETYPE *, const struct magic *); private int cvt_16(union VALUETYPE *, const struct magic *); private int cvt_32(union VALUETYPE *, const struct magic *); @@ -238,7 +239,7 @@ flush: goto flush; } - if ((e = handle_annotation(ms, m, firstline)) != 0) { + if ((e = handle_annotation(ms, m, b, firstline)) != 0) { *need_separator = 1; *printed_something = 1; *returnval = 1; @@ -338,7 +339,8 @@ flush: } else ms->c.li[cont_level].got_match = 1; - if ((e = handle_annotation(ms, m, firstline)) != 0) { + if ((e = handle_annotation(ms, m, b, firstline)) + != 0) { *need_separator = 1; *printed_something = 1; *returnval = 1; @@ -2095,8 +2097,64 @@ magiccheck(struct magic_set *ms, struct magic *m) return matched; } + +static int +varexpand(char *buf, size_t len, const struct buffer *b, const char *str) +{ + const char *ptr, *sptr, *e, *t, *ee, *et; + size_t l; + + for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) { + l = (size_t)(ptr - sptr); + if (l >= len) + return -1; + memcpy(buf, sptr, l); + buf += l; + len -= l; + ptr += 2; + if (!*ptr || ptr[1] != '?') + return -1; + for (et = t = ptr + 2; *et && *et != ':'; et++) + continue; + if (*et != ':') + return -1; + for (ee = e = et + 1; *ee && *ee != '}'; ee++) + continue; + if (*ee != '}') + return -1; + switch (*ptr) { + case 'x': + if (b->st.st_mode & 0111) { + ptr = t; + l = et - t; + } else { + ptr = e; + l = ee - e; + } + break; + default: + return -1; + } + if (l >= len) + return -1; + memcpy(buf, ptr, l); + buf += l; + len -= l; + sptr = ee + 1; + } + + l = strlen(sptr); + if (l >= len) + return -1; + + memcpy(buf, sptr, l); + buf[l] = '\0'; + return 0; +} + private int -handle_annotation(struct magic_set *ms, struct magic *m, int firstline) +handle_annotation(struct magic_set *ms, struct magic *m, const struct buffer *b, + int firstline) { if ((ms->flags & MAGIC_APPLE) && m->apple[0]) { if (!firstline && file_printf(ms, "\n- ") == -1) @@ -2113,9 +2171,15 @@ handle_annotation(struct magic_set *ms, struct magic *m, int firstline) return 1; } if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) { + char buf[1024]; + const char *p; if (!firstline && file_printf(ms, "\n- ") == -1) return -1; - if (file_printf(ms, "%s", m->mimetype) == -1) + if (varexpand(buf, sizeof(buf), b, m->mimetype) == -1) + p = m->mimetype; + else + p = buf; + if (file_printf(ms, "%s", p) == -1) return -1; return 1; } |