Skip to content

Commit

Permalink
fileinfo: Followup fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Anatol Belski <ab@php.net>
  • Loading branch information
weltling committed Aug 29, 2020
1 parent c3eeab0 commit bf93f1d
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 5,417 deletions.
14 changes: 7 additions & 7 deletions ext/fileinfo/data_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -257716,7 +257716,7 @@ const unsigned char php_magic_database[6652944] = {
0x6E, 0x74, 0x20, 0x62, 0x69, 0x74, 0x6D, 0x61, 0x70, 0x20, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69,
0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D, 0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -258021,8 +258021,8 @@ const unsigned char php_magic_database[6652944] = {
0x70, 0x2C, 0x20, 0x4F, 0x53, 0x2F, 0x32, 0x20, 0x32, 0x2E, 0x78, 0x20, 0x66, 0x6F, 0x72, 0x6D,
0x61, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D,
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D,
0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -258233,7 +258233,7 @@ const unsigned char php_magic_database[6652944] = {
0x20, 0x50, 0x68, 0x6F, 0x74, 0x6F, 0x73, 0x68, 0x6F, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D, 0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -258327,7 +258327,7 @@ const unsigned char php_magic_database[6652944] = {
0x20, 0x50, 0x68, 0x6F, 0x74, 0x6F, 0x73, 0x68, 0x6F, 0x70, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20,
0x61, 0x6C, 0x70, 0x68, 0x61, 0x20, 0x63, 0x68, 0x61, 0x6E, 0x6E, 0x65, 0x6C, 0x20, 0x6D, 0x61,
0x73, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D, 0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -258444,8 +258444,8 @@ const unsigned char php_magic_database[6652944] = {
0x70, 0x2C, 0x20, 0x57, 0x69, 0x6E, 0x64, 0x6F, 0x77, 0x73, 0x20, 0x33, 0x2E, 0x78, 0x20, 0x66,
0x6F, 0x72, 0x6D, 0x61, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D,
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D,
0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down
135 changes: 9 additions & 126 deletions ext/fileinfo/libmagic/apprentice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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 *);
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -3055,28 +3016,6 @@ eatsize(const char **p)
*p = l;
}

/*
* 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.
*/
Expand Down Expand Up @@ -3224,62 +3163,6 @@ apprentice_map(struct magic_set *ms, const char *fn)
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.
*/
Expand Down
3 changes: 1 addition & 2 deletions ext/fileinfo/libmagic/der.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]);
Expand Down
2 changes: 0 additions & 2 deletions ext/fileinfo/libmagic/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions ext/fileinfo/libmagic/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -465,6 +467,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, zend_stat_t *st,

return m;
}
#endif

protected int
file_reset(struct magic_set *ms, int checkloaded)
Expand Down
38 changes: 0 additions & 38 deletions ext/fileinfo/libmagic/magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

/*
Expand Down
2 changes: 0 additions & 2 deletions ext/fileinfo/libmagic/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion ext/fileinfo/libmagic/softmagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bf93f1d

Please sign in to comment.