diff options
author | Anatol Belski <ab@php.net> | 2020-07-05 19:48:24 +0200 |
---|---|---|
committer | Anatol Belski <ab@php.net> | 2020-08-29 19:03:41 +0200 |
commit | bf93f1d824518de3764ecab1265514c6e96f88e8 (patch) | |
tree | 9954c58d7c54b92c15c48e6c9296101dcf0ab428 /ext/fileinfo/libmagic | |
parent | c3eeab01bac39234bbe09407f4bddf8f22b6af53 (diff) | |
download | php-git-bf93f1d824518de3764ecab1265514c6e96f88e8.tar.gz |
fileinfo: Followup fixes
Signed-off-by: Anatol Belski <ab@php.net>
Diffstat (limited to 'ext/fileinfo/libmagic')
-rw-r--r-- | ext/fileinfo/libmagic/apprentice.c | 135 | ||||
-rw-r--r-- | ext/fileinfo/libmagic/der.c | 3 | ||||
-rw-r--r-- | ext/fileinfo/libmagic/file.h | 2 | ||||
-rw-r--r-- | ext/fileinfo/libmagic/funcs.c | 3 | ||||
-rw-r--r-- | ext/fileinfo/libmagic/magic.c | 38 | ||||
-rw-r--r-- | ext/fileinfo/libmagic/print.c | 2 | ||||
-rw-r--r-- | ext/fileinfo/libmagic/softmagic.c | 5 |
7 files changed, 17 insertions, 171 deletions
diff --git a/ext/fileinfo/libmagic/apprentice.c b/ext/fileinfo/libmagic/apprentice.c index 3fc33a7c09..9201e8b671 100644 --- a/ext/fileinfo/libmagic/apprentice.c +++ b/ext/fileinfo/libmagic/apprentice.c @@ -53,8 +53,10 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.297 2020/05/09 18:57:15 christos Exp $") #include "win32/unistd.h" #define strtoull _strtoui64 #else +#ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#endif #include <string.h> #include <assert.h> #include <ctype.h> @@ -149,10 +151,7 @@ private uint16_t swap2(uint16_t); private uint32_t swap4(uint32_t); private uint64_t swap8(uint64_t); private char *mkdbname(struct magic_set *, const char *, int); -private struct magic_map *apprentice_buf(struct magic_set *, struct magic *, - size_t); private struct magic_map *apprentice_map(struct magic_set *, const char *); -private int check_buffer(struct magic_set *, struct magic_map *, const char *); private void apprentice_unmap(struct magic_map *); private int apprentice_compile(struct magic_set *, struct magic_map *, const char *); @@ -428,8 +427,10 @@ private int apprentice_1(struct magic_set *ms, const char *fn, int action) { struct magic_map *map; +#ifndef COMPILE_ONLY struct mlist *ml; size_t i; +#endif if (magicsize != FILE_MAGICSIZE) { file_error(ms, 0, "magic element size %lu != %lu", @@ -609,51 +610,6 @@ mlist_free(struct mlist *mlist) mlist_free_one(mlist); } -#ifndef COMPILE_ONLY -/* void **bufs: an array of compiled magic files */ -protected int -buffer_apprentice(struct magic_set *ms, struct magic **bufs, - size_t *sizes, size_t nbufs) -{ - size_t i, j; - struct mlist *ml; - struct magic_map *map; - - if (nbufs == 0) - return -1; - - (void)file_reset(ms, 0); - - init_file_tables(); - - for (i = 0; i < MAGIC_SETS; i++) { - mlist_free(ms->mlist[i]); - if ((ms->mlist[i] = mlist_alloc()) == NULL) { - file_oomem(ms, sizeof(*ms->mlist[i])); - goto fail; - } - } - - for (i = 0; i < nbufs; i++) { - map = apprentice_buf(ms, bufs[i], sizes[i]); - if (map == NULL) - goto fail; - - for (j = 0; j < MAGIC_SETS; j++) { - if (add_mlist(ms->mlist[j], map, j) == -1) { - file_oomem(ms, sizeof(*ml)); - goto fail; - } - } - } - - return 0; -fail: - mlist_free_all(ms); - return -1; -} -#endif - /* const char *fn: list of magic files and directories */ protected int file_apprentice(struct magic_set *ms, const char *fn, int action) @@ -2251,6 +2207,11 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line, if (check_format(ms, m) == -1) return -1; } +#ifndef COMPILE_ONLY + if (action == FILE_CHECK) { + file_mdump(m); + } +#endif m->mimetype[0] = '\0'; /* initialise MIME type to none */ return 0; } @@ -3056,28 +3017,6 @@ eatsize(const char **p) } /* - * handle a buffer containing a compiled file. - */ -private struct magic_map * -apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len) -{ - struct magic_map *map; - - if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { - file_oomem(ms, sizeof(*map)); - return NULL; - } - map->len = len; - map->p = buf; - map->type = MAP_TYPE_USER; - if (check_buffer(ms, map, "buffer") != 0) { - apprentice_unmap(map); - return NULL; - } - return map; -} - -/* * handle a compiled file. */ @@ -3224,62 +3163,6 @@ error: return NULL; } -private int -check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname) -{ - uint32_t *ptr; - uint32_t entries, nentries; - uint32_t version; - int i, needsbyteswap; - - ptr = CAST(uint32_t *, map->p); - if (*ptr != MAGICNO) { - if (swap4(*ptr) != MAGICNO) { - file_error(ms, 0, "bad magic in `%s'", dbname); - return -1; - } - needsbyteswap = 1; - } else - needsbyteswap = 0; - if (needsbyteswap) - version = swap4(ptr[1]); - else - version = ptr[1]; - if (version != VERSIONNO) { - file_error(ms, 0, "File %s supports only version %d magic " - "files. `%s' is version %d", FILE_VERSION_MAJOR, - VERSIONNO, dbname, version); - return -1; - } - entries = CAST(uint32_t, map->len / sizeof(struct magic)); - if ((entries * sizeof(struct magic)) != map->len) { - file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not " - "a multiple of %" SIZE_T_FORMAT "u", - dbname, map->len, sizeof(struct magic)); - return -1; - } - map->magic[0] = CAST(struct magic *, map->p) + 1; - nentries = 0; - for (i = 0; i < MAGIC_SETS; i++) { - if (needsbyteswap) - map->nmagic[i] = swap4(ptr[i + 2]); - else - map->nmagic[i] = ptr[i + 2]; - if (i != MAGIC_SETS - 1) - map->magic[i + 1] = map->magic[i] + map->nmagic[i]; - nentries += map->nmagic[i]; - } - if (entries != nentries + 1) { - file_error(ms, 0, "Inconsistent entries in `%s' %u != %u", - dbname, entries, nentries + 1); - return -1; - } - if (needsbyteswap) - for (i = 0; i < MAGIC_SETS; i++) - byteswap(map->magic[i], map->nmagic[i]); - return 0; -} - /* * handle an mmaped file. */ diff --git a/ext/fileinfo/libmagic/der.c b/ext/fileinfo/libmagic/der.c index f6cc80051c..a122e18d30 100644 --- a/ext/fileinfo/libmagic/der.c +++ b/ext/fileinfo/libmagic/der.c @@ -237,7 +237,6 @@ der_tag(char *buf, size_t len, uint32_t tag) static int der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len) { - uint32_t i = 0; const uint8_t *d = CAST(const uint8_t *, q); switch (tag) { case DER_TAG_PRINTABLE_STRING: @@ -255,7 +254,7 @@ der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len) break; } - for (; i < len; i++) { + for (uint32_t i = 0; i < len; i++) { uint32_t z = i << 1; if (z < blen - 2) snprintf(buf + z, blen - z, "%.2x", d[i]); diff --git a/ext/fileinfo/libmagic/file.h b/ext/fileinfo/libmagic/file.h index 72ba63d708..42a82b8351 100644 --- a/ext/fileinfo/libmagic/file.h +++ b/ext/fileinfo/libmagic/file.h @@ -493,8 +493,6 @@ protected int file_is_tar(struct magic_set *, const struct buffer *); protected int file_softmagic(struct magic_set *, const struct buffer *, uint16_t *, uint16_t *, int, int); protected int file_apprentice(struct magic_set *, const char *, int); -protected int buffer_apprentice(struct magic_set *, struct magic **, - size_t *, size_t); protected int file_magicfind(struct magic_set *, const char *, struct mlist *); protected uint64_t file_signextend(struct magic_set *, struct magic *, uint64_t); diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c index 2ab015b279..7d1e40a565 100644 --- a/ext/fileinfo/libmagic/funcs.c +++ b/ext/fileinfo/libmagic/funcs.c @@ -227,6 +227,8 @@ file_badread(struct magic_set *ms) file_error(ms, errno, "error reading"); } +#ifndef COMPILE_ONLY + protected int file_separator(struct magic_set *ms) { @@ -465,6 +467,7 @@ simple: return m; } +#endif protected int file_reset(struct magic_set *ms, int checkloaded) diff --git a/ext/fileinfo/libmagic/magic.c b/ext/fileinfo/libmagic/magic.c index 8cd40119b5..601a4b05db 100644 --- a/ext/fileinfo/libmagic/magic.c +++ b/ext/fileinfo/libmagic/magic.c @@ -76,9 +76,6 @@ FILE_RCSID("@(#)$File: magic.c,v 1.112 2020/06/08 19:44:10 christos Exp $") #endif private int unreadable_info(struct magic_set *, mode_t, const char *); -#if 0 -private const char* get_default_magic(void); -#endif private const char *file_or_stream(struct magic_set *, const char *, php_stream *); #ifndef STDIN_FILENO @@ -154,41 +151,6 @@ magic_list(struct magic_set *ms, const char *magicfile) return file_apprentice(ms, magicfile, FILE_LIST); } -#if 0 -private void -close_and_restore(const struct magic_set *ms, const char *name, int fd, - const zend_stat_t *sb) -{ - if (fd == STDIN_FILENO || name == NULL) - return; - (void) close(fd); - - if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { - /* - * Try to restore access, modification times if read it. - * This is really *bad* because it will modify the status - * time of the file... And of course this will affect - * backup programs - */ -#ifdef HAVE_UTIMES - struct timeval utsbuf[2]; - (void)memset(utsbuf, 0, sizeof(utsbuf)); - utsbuf[0].tv_sec = sb->st_atime; - utsbuf[1].tv_sec = sb->st_mtime; - - (void) utimes(name, utsbuf); /* don't care if loses */ -#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) - struct utimbuf utbuf; - - (void)memset(&utbuf, 0, sizeof(utbuf)); - utbuf.actime = sb->st_atime; - utbuf.modtime = sb->st_mtime; - (void) utime(name, &utbuf); /* don't care if loses */ -#endif - } -} -#endif - #ifndef COMPILE_ONLY /* diff --git a/ext/fileinfo/libmagic/print.c b/ext/fileinfo/libmagic/print.c index edd4a6320a..96d490eeab 100644 --- a/ext/fileinfo/libmagic/print.c +++ b/ext/fileinfo/libmagic/print.c @@ -44,8 +44,6 @@ FILE_RCSID("@(#)$File: print.c,v 1.88 2020/05/09 18:57:15 christos Exp $") #endif #include <time.h> -#define SZOF(a) (sizeof(a) / sizeof(a[0])) - #include "cdf.h" #ifndef COMPILE_ONLY diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c index d791d65ff4..a99e60c563 100644 --- a/ext/fileinfo/libmagic/softmagic.c +++ b/ext/fileinfo/libmagic/softmagic.c @@ -1934,10 +1934,13 @@ file_strncmp(const char *s1, const char *s2, size_t len, size_t maxlen, } else if ((flags & STRING_COMPACT_WHITESPACE) && isspace(*a)) { + /* XXX Dirty. The data and the pattern is what is causing this. + Revert _i for the next port and see if it still matters. */ + uint32_t _i = 0; a++; if (isspace(*b++)) { if (!isspace(*a)) - while (b < eb && isspace(*b)) + while (EXPECTED(_i++ < 2048) && b < eb && isspace(*b)) b++; } else { |