summaryrefslogtreecommitdiff
path: root/ext/fileinfo/libmagic.patch
diff options
context:
space:
mode:
authorAnatol Belski <ab@php.net>2018-04-29 15:49:56 +0200
committerAnatol Belski <ab@php.net>2018-04-29 16:02:08 +0200
commite2182a1ba7cdd3c915cf29cd8367a6e02a0c10c8 (patch)
tree38bf257672c24c718776b4c5061e39fb9063928e /ext/fileinfo/libmagic.patch
parent74ecbbebe58eb290b9bf7f4b7753c4d5255263a6 (diff)
downloadphp-git-e2182a1ba7cdd3c915cf29cd8367a6e02a0c10c8.tar.gz
Port libmagic 5.33
Diffstat (limited to 'ext/fileinfo/libmagic.patch')
-rw-r--r--ext/fileinfo/libmagic.patch1712
1 files changed, 1274 insertions, 438 deletions
diff --git a/ext/fileinfo/libmagic.patch b/ext/fileinfo/libmagic.patch
index 306b221367..d00b18e69d 100644
--- a/ext/fileinfo/libmagic.patch
+++ b/ext/fileinfo/libmagic.patch
@@ -1,6 +1,24 @@
diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
---- libmagic.orig/apprentice.c 2017-05-08 20:10:13.000000000 +0200
-+++ libmagic/apprentice.c 2017-12-21 15:43:46.030516100 +0100
+--- libmagic.orig/apprentice.c 2018-03-11 01:46:42.000000000 +0100
++++ libmagic/apprentice.c 2018-04-27 19:30:48.686308139 +0200
+@@ -2,7 +2,7 @@
+ * Copyright (c) Ian F. Darwin 1986-1995.
+ * Software written by Ian F. Darwin and others;
+ * maintained 1995-present by Christos Zoulas and others.
+- *
++ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+@@ -12,7 +12,7 @@
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+- *
++ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
@@ -60,6 +78,24 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
#ifndef MAP_FAILED
#define MAP_FAILED (void *) -1
#endif
+@@ -91,7 +100,7 @@
+ #define MAP_TYPE_MMAP 2
+
+ struct magic_entry {
+- struct magic *mp;
++ struct magic *mp;
+ uint32_t cont_count;
+ uint32_t max_count;
+ };
+@@ -126,7 +135,7 @@
+ private size_t apprentice_magic_strength(const struct magic *);
+ private int apprentice_sort(const void *, const void *);
+ private void apprentice_list(struct mlist *, int );
+-private struct magic_map *apprentice_load(struct magic_set *,
++private struct magic_map *apprentice_load(struct magic_set *,
+ const char *, int);
+ private struct mlist *mlist_alloc(void);
+ private void mlist_free(struct mlist *);
@@ -170,38 +179,7 @@
{ NULL, 0, NULL }
};
@@ -262,7 +298,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
return NULL;
}
mlist->next = mlist->prev = mlist;
-@@ -593,61 +564,12 @@
+@@ -593,76 +564,45 @@
for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
if (ml->map)
apprentice_unmap(CAST(struct magic_map *, ml->map));
@@ -286,8 +322,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
- if (nbufs == 0)
- return -1;
-
-- if (ms->mlist[0] != NULL)
-- file_reset(ms);
+- (void)file_reset(ms, 0);
-
- init_file_tables();
-
@@ -325,9 +360,15 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
/* const char *fn: list of magic files and directories */
protected int
file_apprentice(struct magic_set *ms, const char *fn, int action)
-@@ -659,12 +581,28 @@
- if (ms->mlist[0] != NULL)
- file_reset(ms);
+ {
+ char *p, *mfn;
+- int fileerr, errs = -1;
++ int file_err, errs = -1;
+ size_t i;
+
+- (void)file_reset(ms, 0);
++ if (ms->mlist[0] != NULL)
++ (void)file_reset(ms, 0);
+/* XXX disabling default magic loading so the compiled in data is used */
+#if 0
@@ -355,7 +396,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
file_oomem(ms, strlen(fn));
return -1;
}
-@@ -677,7 +615,7 @@
+@@ -675,7 +615,7 @@
mlist_free(ms->mlist[i]);
ms->mlist[i] = NULL;
}
@@ -364,7 +405,14 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
return -1;
}
}
-@@ -694,7 +632,7 @@
+@@ -687,12 +627,12 @@
+ *p++ = '\0';
+ if (*fn == '\0')
+ break;
+- fileerr = apprentice_1(ms, fn, action);
+- errs = MAX(errs, fileerr);
++ file_err = apprentice_1(ms, fn, action);
++ errs = MAX(errs, file_err);
fn = p;
}
@@ -373,7 +421,34 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
if (errs == -1) {
for (i = 0; i < MAGIC_SETS; i++) {
-@@ -1078,7 +1016,7 @@
+@@ -974,7 +914,7 @@
+ return val;
+ }
+
+-/*
++/*
+ * Sort callback for sorting entries by "strength" (basically length)
+ */
+ private int
+@@ -992,7 +932,7 @@
+ return 1;
+ }
+
+-/*
++/*
+ * Shows sorted patterns list in the order which is used for the matching
+ */
+ private void
+@@ -1088,7 +1028,7 @@
+ mstart->flag |= BINTEST;
+ if (mstart->str_flags & STRING_TEXTTEST)
+ mstart->flag |= TEXTTEST;
+-
++
+ if (mstart->flag & (TEXTTEST|BINTEST))
+ break;
+
+@@ -1120,7 +1060,7 @@
mset[i].max += ALLOC_INCR;
if ((mp = CAST(struct magic_entry *,
@@ -382,7 +457,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
NULL) {
file_oomem(ms, sizeof(*mp) * mset[i].max);
return -1;
-@@ -1099,13 +1037,19 @@
+@@ -1141,13 +1081,19 @@
load_1(struct magic_set *ms, int action, const char *fn, int *errs,
struct magic_entry_set *mset)
{
@@ -406,7 +481,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
if (errno != ENOENT)
file_error(ms, errno, "cannot read magic file `%s'",
fn);
-@@ -1115,8 +1059,7 @@
+@@ -1157,8 +1103,7 @@
memset(&me, 0, sizeof(me));
/* read and parse this file */
@@ -416,7 +491,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
if (len == 0) /* null line, garbage, etc */
continue;
if (line[len - 1] == '\n') {
-@@ -1174,8 +1117,8 @@
+@@ -1216,8 +1161,8 @@
}
if (me.mp)
(void)addentry(ms, &me, mset);
@@ -427,7 +502,16 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
}
/*
-@@ -1254,7 +1197,7 @@
+@@ -1280,7 +1225,7 @@
+ file_magwarn(ms,
+ "level 0 \"default\" did not sort last");
+ }
+- return;
++ return;
+ }
+ }
+ }
+@@ -1296,7 +1241,7 @@
mentrycount += me[i].cont_count;
slen = sizeof(**ma) * mentrycount;
@@ -436,7 +520,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
file_oomem(ms, slen);
return -1;
}
-@@ -1276,8 +1219,8 @@
+@@ -1318,8 +1263,8 @@
if (me == NULL)
return;
for (i = 0; i < nme; i++)
@@ -447,7 +531,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
}
private struct magic_map *
-@@ -1286,18 +1229,19 @@
+@@ -1328,18 +1273,19 @@
int errs = 0;
uint32_t i, j;
size_t files = 0, maxfiles = 0;
@@ -461,7 +545,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
- struct dirent *d;
+ php_stream *dir;
+ php_stream_dirent d;
-+
++
memset(mset, 0, sizeof(mset));
ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
@@ -472,7 +556,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
{
file_oomem(ms, sizeof(*map));
return NULL;
-@@ -1309,24 +1253,26 @@
+@@ -1351,24 +1297,26 @@
(void)fprintf(stderr, "%s\n", usg_hdr);
/* load directory or file */
@@ -509,7 +593,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
continue;
}
if (files >= maxfiles) {
-@@ -1334,23 +1280,22 @@
+@@ -1376,23 +1324,22 @@
maxfiles = (maxfiles + 1) * 2;
mlen = maxfiles * sizeof(*filearr);
if ((filearr = CAST(char **,
@@ -539,7 +623,16 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
} else
load_1(ms, action, fn, &errs, mset);
if (errs)
-@@ -1817,7 +1762,7 @@
+@@ -1833,7 +1780,7 @@
+ */
+ while (*l == '>') {
+ ++l; /* step over */
+- cont_level++;
++ cont_level++;
+ }
+ #ifdef ENABLE_CONDITIONALS
+ if (cont_level == 0 || cont_level > last_cont_level)
+@@ -1859,7 +1806,7 @@
if (me->cont_count == me->max_count) {
struct magic *nm;
size_t cnt = me->max_count + ALLOC_CHUNK;
@@ -548,7 +641,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
sizeof(*nm) * cnt))) == NULL) {
file_oomem(ms, sizeof(*nm) * cnt);
return -1;
-@@ -1832,7 +1777,7 @@
+@@ -1874,7 +1821,7 @@
static const size_t len = sizeof(*m) * ALLOC_CHUNK;
if (me->mp != NULL)
return 1;
@@ -557,7 +650,52 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
file_oomem(ms, len);
return -1;
}
-@@ -2036,7 +1981,7 @@
+@@ -1916,17 +1863,6 @@
+ file_magwarn(ms, "offset `%s' invalid", l);
+ return -1;
+ }
+-#if 0
+- if (m->offset < 0 && cont_level != 0 &&
+- (m->flag & (OFFADD | INDIROFFADD)) == 0) {
+- if (ms->flags & MAGIC_CHECK) {
+- file_magwarn(ms,
+- "negative direct offset `%s' at level %u",
+- l, cont_level);
+- }
+- return -1;
+- }
+-#endif
+ l = t;
+
+ if (m->flag & INDIR) {
+@@ -2012,7 +1948,7 @@
+ }
+ l = t;
+ }
+- if (*l++ != ')' ||
++ if (*l++ != ')' ||
+ ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) {
+ if (ms->flags & MAGIC_CHECK)
+ file_magwarn(ms,
+@@ -2037,7 +1973,7 @@
+ /*
+ * Try it as a keyword type prefixed by "u"; match what
+ * follows the "u". If that fails, try it as an SUS
+- * integer type.
++ * integer type.
+ */
+ m->type = get_type(type_tbl, l + 1, &l);
+ if (m->type == FILE_INVALID) {
+@@ -2077,7 +2013,7 @@
+ /* Not found - try it as a special keyword. */
+ m->type = get_type(special_tbl, l, &l);
+ }
+-
++
+ if (m->type == FILE_INVALID) {
+ if (ms->flags & MAGIC_CHECK)
+ file_magwarn(ms, "type `%s' invalid", l);
+@@ -2089,7 +2025,7 @@
m->mask_op = 0;
if (*l == '~') {
@@ -566,7 +704,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
m->mask_op |= FILE_OPINVERSE;
else if (ms->flags & MAGIC_CHECK)
file_magwarn(ms, "'~' invalid for string types");
-@@ -2045,7 +1990,7 @@
+@@ -2098,7 +2034,7 @@
m->str_range = 0;
m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
if ((op = get_op(*l)) != -1) {
@@ -575,7 +713,34 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
int r;
if (op != FILE_OPDIVIDE) {
-@@ -2150,11 +2095,6 @@
+@@ -2124,7 +2060,7 @@
+ * anything if mask = 0 (unless you have a better idea)
+ */
+ EATAB;
+-
++
+ switch (*l) {
+ case '>':
+ case '<':
+@@ -2156,7 +2092,7 @@
+ break;
+ default:
+ m->reln = '='; /* the default relation */
+- if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
++ if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
+ isspace((unsigned char)l[1])) || !l[1])) {
+ m->reln = *l;
+ ++l;
+@@ -2171,7 +2107,7 @@
+
+ /*
+ * TODO finish this macro and start using it!
+- * #define offsetcheck {if (offset > ms->bytes_max -1)
++ * #define offsetcheck {if (offset > ms->bytes_max -1)
+ * magwarn("offset too big"); }
+ */
+
+@@ -2203,11 +2139,6 @@
if (check_format(ms, m) == -1)
return -1;
}
@@ -587,7 +752,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
m->mimetype[0] = '\0'; /* initialise MIME type to none */
return 0;
}
-@@ -2226,7 +2166,7 @@
+@@ -2279,7 +2210,7 @@
private int
parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
@@ -596,16 +761,123 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
{
size_t i;
const char *l = line;
-@@ -2308,7 +2248,7 @@
+@@ -2291,7 +2222,7 @@
+ 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 "
+@@ -2361,7 +2292,7 @@
struct magic *m = &me->mp[0];
return parse_extra(ms, me, line,
- CAST(off_t, offsetof(struct magic, mimetype)),
+ CAST(zend_off_t, offsetof(struct magic, mimetype)),
- sizeof(m->mimetype), "MIME", "+-/.", 1);
+ sizeof(m->mimetype), "MIME", "+-/.$?:{}", 1);
}
-@@ -2584,14 +2524,18 @@
+@@ -2428,7 +2359,7 @@
+ if (*ptr++ != 'l')
+ goto invalid;
+ }
+-
++
+ switch (*ptr++) {
+ #ifdef STRICT_FORMAT /* "long" formats are int formats for us */
+ /* so don't accept the 'l' modifier */
+@@ -2446,7 +2377,7 @@
+ default:
+ goto invalid;
+ }
+-
++
+ /*
+ * Don't accept h and hh modifiers. They make writing
+ * magic entries more complicated, for very little benefit
+@@ -2502,7 +2433,7 @@
+ default:
+ goto invalid;
+ }
+-
++
+ case FILE_FMT_FLOAT:
+ case FILE_FMT_DOUBLE:
+ if (*ptr == '-')
+@@ -2521,11 +2452,11 @@
+ case 'g':
+ case 'G':
+ return 0;
+-
++
+ default:
+ goto invalid;
+ }
+-
++
+
+ case FILE_FMT_STR:
+ if (*ptr == '-')
+@@ -2537,14 +2468,14 @@
+ while (isdigit((unsigned char )*ptr))
+ ptr++;
+ }
+-
++
+ switch (*ptr++) {
+ case 's':
+ return 0;
+ default:
+ goto invalid;
+ }
+-
++
+ default:
+ /* internal error */
+ abort();
+@@ -2555,7 +2486,7 @@
+ *estr = "too long";
+ return -1;
+ }
+-
++
+ /*
+ * Check that the optional printf format in description matches
+ * the type of the magic.
+@@ -2578,7 +2509,7 @@
+
+ if (m->type >= file_nformats) {
+ file_magwarn(ms, "Internal error inconsistency between "
+- "m->type and format strings");
++ "m->type and format strings");
+ return -1;
+ }
+ if (file_formats[m->type] == FILE_FMT_NONE) {
+@@ -2598,7 +2529,7 @@
+ file_names[m->type], m->desc);
+ return -1;
+ }
+-
++
+ for (; *ptr; ptr++) {
+ if (*ptr == '%') {
+ file_magwarn(ms,
+@@ -2611,9 +2542,9 @@
+ return 0;
+ }
+
+-/*
+- * Read a numeric value from a pointer, into the value union of a magic
+- * pointer, according to the magic type. Update the string pointer to point
++/*
++ * Read a numeric value from a pointer, into the value union of a magic
++ * pointer, according to the magic type. Update the string pointer to point
+ * just after the number read. Return 0 for success, non-zero for failure.
+ */
+ private int
+@@ -2640,14 +2571,18 @@
return -1;
}
if (m->type == FILE_REGEX) {
@@ -622,7 +894,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
+ convert_libmagic_pattern(&pattern, m->value.s, strlen(m->value.s), options);
+
+ if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
-+ return -1;
++ return -1;
}
- file_regfree(&rx);
- return rc ? -1 : 0;
@@ -630,8 +902,26 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
+ return 0;*/
}
return 0;
- case FILE_FLOAT:
-@@ -2909,7 +2853,7 @@
+ default:
+@@ -2770,7 +2705,7 @@
+ default:
+ if (warn) {
+ if (isprint((unsigned char)c)) {
+- /* Allow escaping of
++ /* Allow escaping of
+ * ``relations'' */
+ if (strchr("<>&^=!", c) == NULL
+ && (m->type != FILE_REGEX ||
+@@ -2975,7 +2910,7 @@
+ {
+ const char *l = *p;
+
+- if (LOWCASE(*l) == 'u')
++ if (LOWCASE(*l) == 'u')
+ l++;
+
+ switch (LOWCASE(*l)) {
+@@ -3001,7 +2936,7 @@
{
struct magic_map *map;
@@ -640,7 +930,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
file_oomem(ms, sizeof(*map));
return NULL;
}
-@@ -2930,79 +2874,145 @@
+@@ -3022,79 +2957,145 @@
private struct magic_map *
apprentice_map(struct magic_set *ms, const char *fn)
{
@@ -664,13 +954,13 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
file_oomem(ms, sizeof(*map));
- goto error;
+ return NULL;
- }
-- map->type = MAP_TYPE_USER; /* unspecified */
++ }
+
+ if (fn == NULL) {
+ map->p = (void *)&php_magic_database;
+ goto internal_loaded;
-+ }
+ }
+- map->type = MAP_TYPE_USER; /* unspecified */
+
+#ifdef PHP_WIN32
+ /* Don't bother on windows with php_stream_open_wrapper,
@@ -823,7 +1113,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
}
private int
-@@ -3028,7 +3038,7 @@
+@@ -3120,7 +3121,7 @@
version = ptr[1];
if (version != VERSIONNO) {
file_error(ms, 0, "File %s supports only version %d magic "
@@ -832,7 +1122,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
VERSIONNO, dbname, version);
return -1;
}
-@@ -3069,7 +3079,6 @@
+@@ -3161,7 +3162,6 @@
{
static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS;
static const size_t m = sizeof(**map->magic);
@@ -840,7 +1130,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
size_t len;
char *dbname;
int rv = -1;
-@@ -3078,14 +3087,17 @@
+@@ -3170,14 +3170,17 @@
struct magic m;
uint32_t h[2 + MAGIC_SETS];
} hdr;
@@ -848,7 +1138,8 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
dbname = mkdbname(ms, fn, 1);
- if (dbname == NULL)
+- if (dbname == NULL)
++ if (dbname == NULL)
goto out;
- if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
@@ -860,14 +1151,15 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
file_error(ms, errno, "cannot open `%s'", dbname);
goto out;
}
-@@ -3094,25 +3106,25 @@
+@@ -3186,26 +3189,25 @@
hdr.h[1] = VERSIONNO;
memcpy(hdr.h + 2, map->nmagic, nm);
- if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
+ if (php_stream_write(stream,(const char *)&hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
file_error(ms, errno, "error writing `%s'", dbname);
- goto out;
+- goto out2;
++ goto out;
}
for (i = 0; i < MAGIC_SETS; i++) {
@@ -875,16 +1167,18 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
- if (write(fd, map->magic[i], len) != (ssize_t)len) {
+ if (php_stream_write(stream, (const char *)map->magic[i], len) != (ssize_t)len) {
file_error(ms, errno, "error writing `%s'", dbname);
- goto out;
+- goto out2;
++ goto out;
}
}
-- if (fd != -1)
-- (void)close(fd);
+ if (stream) {
+ php_stream_close(stream);
+ }
rv = 0;
+-out2:
+- if (fd != -1)
+- (void)close(fd);
out:
- apprentice_unmap(map);
- free(dbname);
@@ -892,7 +1186,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
return rv;
}
-@@ -3146,16 +3158,18 @@
+@@ -3239,16 +3241,18 @@
q++;
/* Compatibility with old code that looked in .mime */
if (ms->flags & MAGIC_MIME) {
@@ -917,8 +1211,41 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
/* Compatibility with old code that looked in .mime */
if (strstr(fn, ".mime") != NULL)
-@@ -3245,7 +3259,7 @@
- m->offset = swap4((uint32_t)m->offset);
+@@ -3274,8 +3278,8 @@
+ swap2(uint16_t sv)
+ {
+ uint16_t rv;
+- uint8_t *s = (uint8_t *)(void *)&sv;
+- uint8_t *d = (uint8_t *)(void *)&rv;
++ uint8_t *s = (uint8_t *)(void *)&sv;
++ uint8_t *d = (uint8_t *)(void *)&rv;
+ d[0] = s[1];
+ d[1] = s[0];
+ return rv;
+@@ -3288,8 +3292,8 @@
+ swap4(uint32_t sv)
+ {
+ uint32_t rv;
+- uint8_t *s = (uint8_t *)(void *)&sv;
+- uint8_t *d = (uint8_t *)(void *)&rv;
++ uint8_t *s = (uint8_t *)(void *)&sv;
++ uint8_t *d = (uint8_t *)(void *)&rv;
+ d[0] = s[3];
+ d[1] = s[2];
+ d[2] = s[1];
+@@ -3304,8 +3308,8 @@
+ swap8(uint64_t sv)
+ {
+ uint64_t rv;
+- uint8_t *s = (uint8_t *)(void *)&sv;
+- uint8_t *d = (uint8_t *)(void *)&rv;
++ uint8_t *s = (uint8_t *)(void *)&sv;
++ uint8_t *d = (uint8_t *)(void *)&rv;
+ #if 0
+ d[0] = s[3];
+ d[1] = s[2];
+@@ -3338,7 +3342,7 @@
+ m->offset = swap4((int32_t)m->offset);
m->in_offset = swap4((uint32_t)m->in_offset);
m->lineno = swap4((uint32_t)m->lineno);
- if (IS_STRING(m->type)) {
@@ -926,10 +1253,63 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
m->str_range = swap4(m->str_range);
m->str_flags = swap4(m->str_flags);
}
+@@ -3348,7 +3352,7 @@
+ }
+ }
+
+-protected size_t
++protected size_t
+ file_pstring_length_size(const struct magic *m)
+ {
+ switch (m->str_flags & PSTRING_LEN) {
+diff -u libmagic.orig/apptype.c libmagic/apptype.c
+--- libmagic.orig/apptype.c 2011-09-07 23:57:15.000000000 +0200
++++ libmagic/apptype.c 2018-04-28 11:06:43.473101938 +0200
+@@ -1,15 +1,15 @@
+ /*
+ * Adapted from: apptype.c, Written by Eberhard Mattes and put into the
+ * public domain
+- *
++ *
+ * Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous
+ * searches.
+- *
++ *
+ * 2. DosQueryAppType will return FAPPTYP_DOS on a file ending with ".com"
+ * (other than an OS/2 exe or Win exe with this name). Eberhard Mattes
+ * remarks Tue, 6 Apr 93: Moreover, it reports the type of the (new and very
+ * bug ridden) Win Emacs as "OS/2 executable".
+- *
++ *
+ * 3. apptype() uses the filename if given, otherwise a tmp file is created with
+ * the contents of buf. If buf is not the complete file, apptype can
+ * incorrectly identify the exe type. The "-z" option of "file" is the reason
+@@ -18,10 +18,10 @@
+
+ /*
+ * amai: Darrel Hankerson did the changes described here.
+- *
++ *
+ * It remains to check the validity of comments (2.) since it's referred to an
+ * "old" OS/2 version.
+- *
++ *
+ */
+
+ #include "file.h"
diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
---- libmagic.orig/ascmagic.c 2016-06-27 22:56:25.000000000 +0200
-+++ libmagic/ascmagic.c 2017-10-23 06:47:43.278249200 +0200
-@@ -133,7 +133,7 @@
+--- libmagic.orig/ascmagic.c 2017-11-02 21:25:39.000000000 +0100
++++ libmagic/ascmagic.c 2018-04-28 11:05:05.397660689 +0200
+@@ -90,7 +90,7 @@
+ rv = file_ascmagic_with_encoding(ms, &bb,
+ ubuf, ulen, code, type, text);
+
+- free(ubuf);
++ efree(ubuf);
+
+ return rv;
+ }
+@@ -137,7 +137,7 @@
/* malloc size is a conservative overestimate; could be
improved, or at least realloced after conversion. */
mlen = ulen * 6;
@@ -938,7 +1318,7 @@ diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
file_oomem(ms, mlen);
goto done;
}
-@@ -298,7 +298,8 @@
+@@ -305,7 +305,8 @@
}
rv = 1;
done:
@@ -948,9 +1328,60 @@ diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
return rv;
}
+diff -u libmagic.orig/buffer.c libmagic/buffer.c
+--- libmagic.orig/buffer.c 2018-03-11 01:46:42.000000000 +0100
++++ libmagic/buffer.c 2018-04-27 19:30:49.006293549 +0200
+@@ -31,7 +31,11 @@
+ #endif /* lint */
+
+ #include "magic.h"
++#ifdef PHP_WIN32
++#include "win32/unistd.h"
++#else
+ #include <unistd.h>
++#endif
+ #include <string.h>
+ #include <stdlib.h>
+ #include <sys/stat.h>
+@@ -40,7 +44,7 @@
+ buffer_init(struct buffer *b, int fd, const void *data, size_t len)
+ {
+ b->fd = fd;
+- if (b->fd == -1 || fstat(b->fd, &b->st) == -1)
++ if (b->fd == -1 || zend_fstat(b->fd, &b->st) == -1)
+ memset(&b->st, 0, sizeof(b->st));
+ b->fbuf = data;
+ b->flen = len;
+@@ -52,7 +56,7 @@
+ void
+ buffer_fini(struct buffer *b)
+ {
+- free(b->ebuf);
++ efree(b->ebuf);
+ }
+
+ int
+@@ -68,12 +72,14 @@
+
+ b->elen = (size_t)b->st.st_size < b->flen ?
+ (size_t)b->st.st_size : b->flen;
+- if ((b->ebuf = malloc(b->elen)) == NULL)
++ if ((b->ebuf = emalloc(b->elen)) == NULL)
+ goto out;
+
+ b->eoff = b->st.st_size - b->elen;
+- if (pread(b->fd, b->ebuf, b->elen, b->eoff) == -1) {
+- free(b->ebuf);
++ if (FINFO_LSEEK_FUNC(b->fd, b->eoff, SEEK_SET) == (zend_off_t)-1 ||
++ FINFO_READ_FUNC(b->fd, b->ebuf, b->elen) != (ssize_t)b->elen)
++ {
++ efree(b->ebuf);
+ goto out;
+ }
+
diff -u libmagic.orig/cdf.c libmagic/cdf.c
---- libmagic.orig/cdf.c 2017-05-08 20:10:13.000000000 +0200
-+++ libmagic/cdf.c 2017-10-23 06:47:43.278249200 +0200
+--- libmagic.orig/cdf.c 2018-03-11 01:46:42.000000000 +0100
++++ libmagic/cdf.c 2018-04-28 09:02:14.865216158 +0200
@@ -43,7 +43,17 @@
#include <err.h>
#endif
@@ -969,35 +1400,57 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
#include <string.h>
#include <time.h>
#include <ctype.h>
-@@ -86,24 +96,21 @@
+@@ -80,37 +90,9 @@
+ CDF_TOLE8(CAST(uint64_t, x))))
+ #define CDF_GETUINT32(x, y) cdf_getuint32(x, y)
-
- static void *
+-#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
+-#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
+-#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
+-
+-
+-/*ARGSUSED*/
+-static void *
-cdf_malloc(const char *file __attribute__((__unused__)),
- size_t line __attribute__((__unused__)), size_t n)
-+cdf_malloc(const char *file, size_t line, size_t n)
- {
- DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
- return malloc(n);
- }
-
- static void *
+-{
+- DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
+- return malloc(n);
+-}
+-
+-/*ARGSUSED*/
+-static void *
-cdf_realloc(const char *file __attribute__((__unused__)),
- size_t line __attribute__((__unused__)), void *p, size_t n)
-+cdf_realloc(const char *file, size_t line, void *p, size_t n)
- {
- DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
- return realloc(p, n);
- }
-
- static void *
+-{
+- DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
+- return realloc(p, n);
+-}
+-
+-/*ARGSUSED*/
+-static void *
-cdf_calloc(const char *file __attribute__((__unused__)),
- size_t line __attribute__((__unused__)), size_t n, size_t u)
-+cdf_calloc(const char *file, size_t line, size_t n, size_t u)
- {
- DPRINTF(("%s,%zu: %s %zu %zu\n", file, line, __func__, n, u));
- return calloc(n, u);
-@@ -333,12 +340,13 @@
+-{
+- DPRINTF(("%s,%zu: %s %zu %zu\n", file, line, __func__, n, u));
+- return calloc(n, u);
+-}
++#define CDF_MALLOC(n) emalloc(n)
++#define CDF_REALLOC(p, n) erealloc(p, n)
++#define CDF_CALLOC(n, u) ecalloc(n, u)
+
+ /*
+ * swap a short
+@@ -303,7 +285,7 @@
+ scn->sst_len = 0;
+ scn->sst_dirlen = 0;
+ scn->sst_ss = 0;
+- free(scn->sst_tab);
++ efree(scn->sst_tab);
+ scn->sst_tab = NULL;
+ return -1;
+ }
+@@ -336,12 +318,13 @@
}
static ssize_t
@@ -1013,7 +1466,7 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
if (info->i_buf != NULL && info->i_len >= siz) {
(void)memcpy(buf, &info->i_buf[off], len);
-@@ -348,7 +356,10 @@
+@@ -351,7 +334,10 @@
if (info->i_fd == -1)
goto out;
@@ -1025,7 +1478,7 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
return -1;
return (ssize_t)len;
-@@ -363,7 +374,7 @@
+@@ -366,7 +352,7 @@
char buf[512];
(void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
@@ -1034,7 +1487,7 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
return -1;
cdf_unpack_header(h, buf);
cdf_swap_header(h);
-@@ -397,7 +408,7 @@
+@@ -400,7 +386,7 @@
size_t ss = CDF_SEC_SIZE(h);
size_t pos = CDF_SEC_POS(h, id);
assert(ss == len);
@@ -1043,7 +1496,103 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
}
ssize_t
-@@ -1380,7 +1391,7 @@
+@@ -501,14 +487,14 @@
+ }
+ out:
+ sat->sat_len = i;
+- free(msa);
++ efree(msa);
+ return 0;
+ out3:
+ errno = EFTYPE;
+ out2:
+- free(msa);
++ efree(msa);
+ out1:
+- free(sat->sat_tab);
++ efree(sat->sat_tab);
+ return -1;
+ }
+
+@@ -676,7 +662,7 @@
+ return -1;
+
+ if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
+- free(dir->dir_tab);
++ efree(dir->dir_tab);
+ return -1;
+ }
+
+@@ -698,11 +684,11 @@
+ if (NEED_SWAP)
+ for (i = 0; i < dir->dir_len; i++)
+ cdf_swap_dir(&dir->dir_tab[i]);
+- free(buf);
++ efree(buf);
+ return 0;
+ out:
+- free(dir->dir_tab);
+- free(buf);
++ efree(dir->dir_tab);
++ efree(buf);
+ errno = EFTYPE;
+ return -1;
+ }
+@@ -747,7 +733,7 @@
+ out:
+ errno = EFTYPE;
+ out1:
+- free(ssat->sat_tab);
++ efree(ssat->sat_tab);
+ return -1;
+ }
+
+@@ -859,7 +845,7 @@
+ }
+
+ static const uint8_t *
+-cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h,
++cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h,
+ const uint8_t *p, const uint8_t *e, size_t i)
+ {
+ size_t tail = (i << 1) + 1;
+@@ -874,7 +860,7 @@
+ __LINE__) == -1)
+ return NULL;
+ ofs = CDF_GETUINT32(p, tail);
+- q = CAST(const uint8_t *, cdf_offset(CAST(const void *, p),
++ q = CAST(const uint8_t *, cdf_offset(CAST(const void *, p),
+ ofs - 2 * sizeof(uint32_t)));
+
+ if (q < p) {
+@@ -896,7 +882,7 @@
+ size_t newcount = *maxcount + incr;
+
+ if (newcount > CDF_PROP_LIMIT) {
+- DPRINTF(("exceeded property limit %zu > %zu\n",
++ DPRINTF(("exceeded property limit %zu > %zu\n",
+ newcount, CDF_PROP_LIMIT));
+ goto out;
+ }
+@@ -909,7 +895,7 @@
+ *maxcount = newcount;
+ return inp;
+ out:
+- free(*info);
++ efree(*info);
+ *maxcount = 0;
+ *info = NULL;
+ return NULL;
+@@ -1092,7 +1078,7 @@
+ }
+ return 0;
+ out:
+- free(*info);
++ efree(*info);
+ *info = NULL;
+ *count = 0;
+ *maxcount = 0;
+@@ -1383,7 +1369,7 @@
cdf_directory_t *d;
char name[__arraycount(d->d_name)];
cdf_stream_t scn;
@@ -1052,7 +1601,16 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
static const char *types[] = { "empty", "user storage",
"user stream", "lockbytes", "property", "root storage" };
-@@ -1435,7 +1446,7 @@
+@@ -1425,7 +1411,7 @@
+ break;
+ }
+ cdf_dump_stream(&scn);
+- free(scn.sst_tab);
++ efree(scn.sst_tab);
+ break;
+ default:
+ break;
+@@ -1438,7 +1424,7 @@
cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
{
cdf_timestamp_t tp;
@@ -1061,21 +1619,27 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
char buf[64];
size_t i, j;
-@@ -1557,10 +1568,7 @@
- cdf_dir_t dir;
- cdf_info_t info;
- const cdf_directory_t *root;
--#ifdef __linux__
--#define getprogname() __progname
-- extern char *__progname;
--#endif
-+
- if (argc < 2) {
- (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
- return -1;
+@@ -1523,7 +1509,7 @@
+ (void)fprintf(stderr, "Class %s\n", buf);
+ (void)fprintf(stderr, "Count %d\n", ssi.si_count);
+ cdf_dump_property_info(info, count);
+- free(info);
++ efree(info);
+ }
+
+
+@@ -1544,7 +1530,7 @@
+ cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
+ cdf_ctime(&ts.tv_sec, tbuf));
+ }
+- free(cat);
++ efree(cat);
+ }
+
+ #endif
diff -u libmagic.orig/cdf.h libmagic/cdf.h
---- libmagic.orig/cdf.h 2017-03-16 16:06:24.000000000 +0100
-+++ libmagic/cdf.h 2017-10-23 06:47:43.278249200 +0200
+--- libmagic.orig/cdf.h 2017-03-09 17:57:17.000000000 +0100
++++ libmagic/cdf.h 2018-04-27 19:30:49.234283137 +0200
@@ -35,10 +35,12 @@
#ifndef _H_CDF_
#define _H_CDF_
@@ -1090,6 +1654,15 @@ diff -u libmagic.orig/cdf.h libmagic/cdf.h
#endif
#ifdef __DJGPP__
#define timespec timeval
+@@ -272,7 +274,7 @@
+ typedef struct {
+ uint16_t ce_namlen;
+ uint32_t ce_num;
+- uint64_t ce_timestamp;
++ uint64_t ce_timestamp;
+ uint16_t ce_name[256];
+ } cdf_catalog_entry_t;
+
@@ -281,9 +283,9 @@
cdf_catalog_entry_t cat_e[1];
} cdf_catalog_t;
@@ -1105,7 +1678,25 @@ diff -u libmagic.orig/cdf.h libmagic/cdf.h
void cdf_unpack_header(cdf_header_t *, char *);
diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
--- libmagic.orig/cdf_time.c 2017-03-29 17:57:48.000000000 +0200
-+++ libmagic/cdf_time.c 2017-10-23 06:47:43.278249200 +0200
++++ libmagic/cdf_time.c 2018-04-27 19:30:49.326278937 +0200
+@@ -56,7 +56,7 @@
+
+ for (y = CDF_BASE_YEAR; y < year; y++)
+ days += isleap(y) + 365;
+-
++
+ return days;
+ }
+
+@@ -77,7 +77,7 @@
+ return days;
+ }
+
+-/*
++/*
+ * Return the 0...11 month number.
+ */
+ static int
@@ -96,7 +96,7 @@
}
@@ -1155,8 +1746,42 @@ diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
static const char *ref = "Sat Apr 23 01:30:00 1977";
diff -u libmagic.orig/compress.c libmagic/compress.c
---- libmagic.orig/compress.c 2017-03-29 17:57:48.000000000 +0200
-+++ libmagic/compress.c 2017-10-23 06:47:43.278249200 +0200
+--- libmagic.orig/compress.c 2017-11-02 21:25:39.000000000 +0100
++++ libmagic/compress.c 2018-04-28 11:18:14.190722374 +0200
+@@ -2,7 +2,7 @@
+ * Copyright (c) Ian F. Darwin 1986-1995.
+ * Software written by Ian F. Darwin and others;
+ * maintained 1995-present by Christos Zoulas and others.
+- *
++ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+@@ -12,7 +12,7 @@
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+- *
++ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+@@ -29,13 +29,13 @@
+ * compress routines:
+ * zmagic() - returns 0 if not recognized, uncompresses and prints
+ * information if recognized
+- * uncompress(method, old, n, newch) - uncompress old into new,
++ * uncompress(method, old, n, newch) - uncompress old into new,
+ * using method, return sizeof new
+ */
+ #include "file.h"
+
+ #ifndef lint
+-FILE_RCSID("@(#)$File: compress.c,v 1.106 2017/11/02 20:25:39 christos Exp $")
++FILE_RCSID("@(#)$File: compress.c,v 1.104 2017/03/29 15:57:48 christos Exp $")
+ #endif
+
+ #include "magic.h"
@@ -45,15 +45,13 @@
#endif
#include <string.h>
@@ -1168,8 +1793,9 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
# ifndef HAVE_SIG_T
typedef void (*sig_t)(int);
# endif /* HAVE_SIG_T */
- #endif
+-#endif
-#if !defined(__MINGW32__) && !defined(WIN32)
++#endif
+#ifndef PHP_WIN32
#include <sys/ioctl.h>
#endif
@@ -1195,7 +1821,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
-#else
-#define DPRINTF(...)
-#endif
-
+-
-#ifdef ZLIBSUPPORT
-/*
- * The following python code is not really used because ZLIBSUPPORT is only
@@ -1206,7 +1832,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
- "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
-
-static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
--
+
-static int
-zlibcmp(const unsigned char *buf)
-{
@@ -1237,7 +1863,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
private int uncompressbuf(int, size_t, size_t, const unsigned char *,
unsigned char **, size_t *);
-@@ -179,8 +138,7 @@
+@@ -179,12 +138,12 @@
private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
size_t *);
#endif
@@ -1247,7 +1873,45 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
private const char *methodname(size_t);
protected int
-@@ -275,7 +233,8 @@
+-file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name)
++file_zmagic(struct magic_set *ms, int fd, const char *name,
++ const unsigned char *buf, size_t nbytes)
+ {
+ unsigned char *newbuf = NULL;
+ size_t i, nsz;
+@@ -192,9 +151,6 @@
+ file_pushbuf_t *pb;
+ int urv, prv, rv = 0;
+ int mime = ms->flags & MAGIC_MIME;
+- int fd = b->fd;
+- const unsigned char *buf = b->fbuf;
+- size_t nbytes = b->flen;
+ #ifdef HAVE_SIGNAL_H
+ sig_t osigpipe;
+ #endif
+@@ -226,7 +182,7 @@
+ switch (urv) {
+ case OKDATA:
+ case ERRDATA:
+-
++
+ ms->flags &= ~MAGIC_COMPRESS;
+ if (urv == ERRDATA)
+ prv = file_printf(ms, "%s ERROR: %s",
+@@ -253,10 +209,10 @@
+ goto error;
+ if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
+ if (file_printf(ms, "%s", rbuf) == -1) {
+- free(rbuf);
++ efree(rbuf);
+ goto error;
+ }
+- free(rbuf);
++ efree(rbuf);
+ }
+ if (!mime && file_printf(ms, ")") == -1)
+ goto error;
+@@ -277,7 +233,8 @@
#ifdef HAVE_SIGNAL_H
(void)signal(SIGPIPE, osigpipe);
#endif
@@ -1257,7 +1921,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
ms->flags |= MAGIC_COMPRESS;
DPRINTF("Zmagic returns %d\n", rv);
return rv;
-@@ -310,7 +269,7 @@
+@@ -312,7 +269,7 @@
* `safe' read for sockets and pipes.
*/
protected ssize_t
@@ -1266,7 +1930,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
{
ssize_t rv;
#ifdef FIONREAD
-@@ -358,7 +317,7 @@
+@@ -360,7 +317,7 @@
nocheck:
do
@@ -1275,7 +1939,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
case -1:
if (errno == EINTR)
continue;
-@@ -435,13 +394,14 @@
+@@ -437,13 +394,14 @@
return -1;
}
(void)close(tfd);
@@ -1292,7 +1956,68 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
#ifdef BUILTIN_DECOMPRESS
#define FHCRC (1 << 1)
-@@ -736,27 +696,5 @@
+@@ -494,7 +452,7 @@
+ int rc;
+ z_stream z;
+
+- if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
++ if ((*newch = CAST(unsigned char *, emalloc(bytes_max + 1))) == NULL)
+ return makeerror(newch, n, "No buffer, %s", strerror(errno));
+
+ z.next_in = CCAST(Bytef *, old);
+@@ -518,7 +476,7 @@
+ rc = inflateEnd(&z);
+ if (rc != Z_OK)
+ goto err;
+-
++
+ /* let's keep the nul-terminate tradition */
+ (*newch)[*n] = '\0';
+
+@@ -586,7 +544,7 @@
+ int status;
+
+ closefd(fdp[STDIN_FILENO], 0);
+- /*
++ /*
+ * fork again, to avoid blocking because both
+ * pipes filled
+ */
+@@ -689,13 +647,13 @@
+ fdp[STDIN_FILENO][0] = fd;
+ (void) lseek(fd, (off_t)0, SEEK_SET);
+ }
+-
++
+ for (i = 0; i < __arraycount(fdp); i++)
+ copydesc(CAST(int, i), fdp[i]);
+
+ (void)execvp(compr[method].argv[0],
+ (char *const *)(intptr_t)compr[method].argv);
+- dprintf(STDERR_FILENO, "exec `%s' failed, %s",
++ dprintf(STDERR_FILENO, "exec `%s' failed, %s",
+ compr[method].argv[0], strerror(errno));
+ exit(1);
+ /*NOTREACHED*/
+@@ -711,7 +669,7 @@
+ if (fd == -1)
+ writechild(fdp, old, *n);
+
+- *newch = CAST(unsigned char *, malloc(bytes_max + 1));
++ *newch = CAST(unsigned char *, emalloc(bytes_max + 1));
+ if (*newch == NULL) {
+ rv = makeerror(newch, n, "No buffer, %s",
+ strerror(errno));
+@@ -730,7 +688,7 @@
+ r = filter_error(*newch, r);
+ break;
+ }
+- free(*newch);
++ efree(*newch);
+ if (r == 0)
+ rv = makeerror(newch, n, "Read failed, %s",
+ strerror(errno));
+@@ -738,27 +696,5 @@
rv = makeerror(newch, n, "No data");
goto err;
}
@@ -1311,7 +2036,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
- } else if (!WIFEXITED(status)) {
- DPRINTF("Child not exited (%#x)\n", status);
- } else if (WEXITSTATUS(status) != 0) {
-- DPRINTF("Child exited (%#u)\n", WEXITSTATUS(status));
+- DPRINTF("Child exited (%#x)\n", WEXITSTATUS(status));
- }
-
- closefd(fdp[STDIN_FILENO], 0);
@@ -1322,8 +2047,8 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
-#endif
+#endif /* if PHP_FILEINFO_UNCOMPRESS */
diff -u libmagic.orig/der.c libmagic/der.c
---- libmagic.orig/der.c 2017-03-07 23:20:58.000000000 +0100
-+++ libmagic/der.c 2017-10-23 06:47:43.278249200 +0200
+--- libmagic.orig/der.c 2017-02-10 19:14:01.000000000 +0100
++++ libmagic/der.c 2018-04-27 19:30:49.682262679 +0200
@@ -51,7 +51,9 @@
#include "magic.h"
#include "der.h"
@@ -1334,26 +2059,54 @@ diff -u libmagic.orig/der.c libmagic/der.c
#include <sys/stat.h>
#include <err.h>
#endif
-@@ -219,6 +221,7 @@
- der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
+@@ -207,7 +209,7 @@
+ static const char *
+ der_tag(char *buf, size_t len, uint32_t tag)
{
- const uint8_t *d = CAST(const uint8_t *, q);
-+ uint32_t i;
- switch (tag) {
- case DER_TAG_PRINTABLE_STRING:
- case DER_TAG_UTF8_STRING:
-@@ -229,7 +232,7 @@
+- if (tag < DER_TAG_LONG)
++ if (tag < DER_TAG_LONG)
+ strlcpy(buf, der__tag[tag], len);
+ else
+ snprintf(buf, len, "%#x", tag);
+@@ -343,7 +345,7 @@
+ default:
break;
}
-
-- for (uint32_t i = 0; i < len; i++) {
-+ for (i = 0; i < len; i++) {
- uint32_t z = i << 1;
- if (z < blen - 2)
- snprintf(buf + z, blen - z, "%.2x", d[i]);
+-
++
+ for (uint32_t i = 0; i < len; i++)
+ printf("%.2x", d[i]);
+ printf("\n");
+@@ -367,7 +369,7 @@
+ if (p + x >= ep)
+ break;
+ uint32_t len = getlength(p, &x, ep - p + x);
+-
++
+ printf("%zu %zu-%zu %c,%c,%s,%u:", level, ox, x,
+ der_class[c], der_type[t],
+ der_tag(buf, sizeof(buf), tag), len);
diff -u libmagic.orig/elfclass.h libmagic/elfclass.h
---- libmagic.orig/elfclass.h 2014-12-16 23:23:50.000000000 +0100
-+++ libmagic/elfclass.h 2015-07-18 21:35:36.472082000 +0200
+--- libmagic.orig/elfclass.h 2014-12-17 00:18:40.000000000 +0100
++++ libmagic/elfclass.h 2018-04-27 19:30:49.866254276 +0200
+@@ -1,7 +1,7 @@
+ /*
+ * Copyright (c) Christos Zoulas 2008.
+ * All Rights Reserved.
+- *
++ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+@@ -11,7 +11,7 @@
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+- *
++ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -41,7 +41,7 @@
return toomany(ms, "program headers", phnum);
flags |= FLAGS_IS_CORE;
@@ -1381,10 +2134,47 @@ diff -u libmagic.orig/elfclass.h libmagic/elfclass.h
(size_t)elf_getu16(swap, elfhdr.e_shentsize),
fsize, elf_getu16(swap, elfhdr.e_machine),
(int)elf_getu16(swap, elfhdr.e_shstrndx),
+diff -u libmagic.orig/encoding.c libmagic/encoding.c
+--- libmagic.orig/encoding.c 2017-11-02 21:25:39.000000000 +0100
++++ libmagic/encoding.c 2018-04-28 11:08:01.380663120 +0200
+@@ -88,12 +88,12 @@
+ *code_mime = "binary";
+
+ mlen = (nbytes + 1) * sizeof((*ubuf)[0]);
+- if ((*ubuf = CAST(unichar *, calloc((size_t)1, mlen))) == NULL) {
++ if ((*ubuf = CAST(unichar *, ecalloc((size_t)1, mlen))) == NULL) {
+ file_oomem(ms, mlen);
+ goto done;
+ }
+ mlen = (nbytes + 1) * sizeof(nbuf[0]);
+- if ((nbuf = CAST(unsigned char *, calloc((size_t)1, mlen))) == NULL) {
++ if ((nbuf = CAST(unsigned char *, ecalloc((size_t)1, mlen))) == NULL) {
+ file_oomem(ms, mlen);
+ goto done;
+ }
+@@ -153,9 +153,9 @@
+ }
+
+ done:
+- free(nbuf);
++ efree(nbuf);
+ if (ubuf == &udefbuf)
+- free(udefbuf);
++ efree(udefbuf);
+
+ return rv;
+ }
diff -u libmagic.orig/file.h libmagic/file.h
---- libmagic.orig/file.h 2017-05-08 20:10:13.000000000 +0200
-+++ libmagic/file.h 2017-10-23 06:47:43.278249200 +0200
-@@ -33,15 +33,9 @@
+--- libmagic.orig/file.h 2018-03-11 01:46:42.000000000 +0100
++++ libmagic/file.h 2018-04-27 19:30:50.194239284 +0200
+@@ -27,21 +27,15 @@
+ */
+ /*
+ * file.h - definitions for file(1) program
+- * @(#)$File: file.h,v 1.191 2018/02/21 21:26:00 christos Exp $
++ * @(#)$File: file.h,v 1.182 2017/04/07 19:46:44 christos Exp $
+ */
+
#ifndef __file_h__
#define __file_h__
@@ -1461,7 +2251,20 @@ diff -u libmagic.orig/file.h libmagic/file.h
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
-@@ -230,7 +230,7 @@
+@@ -147,10 +147,10 @@
+
+ struct buffer {
+ int fd;
+- struct stat st;
++ zend_stat_t st;
+ const void *fbuf;
+ size_t flen;
+- off_t eoff;
++ zend_off_t eoff;
+ void *ebuf;
+ size_t elen;
+ };
+@@ -240,7 +240,7 @@
#define FILE_DER 48
#define FILE_NAMES_SIZE 49 /* size of array to contain all names */
@@ -1470,7 +2273,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
((t) == FILE_STRING || \
(t) == FILE_PSTRING || \
(t) == FILE_BESTRING16 || \
-@@ -431,28 +431,22 @@
+@@ -443,26 +443,22 @@
/* Type for Unicode characters */
typedef unsigned long unichar;
@@ -1493,21 +2296,15 @@ diff -u libmagic.orig/file.h libmagic/file.h
-protected int file_printf(struct magic_set *, const char *, ...)
- __attribute__((__format__(__printf__, 2, 3)));
+protected int file_printf(struct magic_set *, const char *, ...);
- protected int file_reset(struct magic_set *);
--protected int file_tryelf(struct magic_set *, int, const unsigned char *,
-- size_t);
- protected int file_trycdf(struct magic_set *, int, const unsigned char *,
- size_t);
+ protected int file_reset(struct magic_set *, int);
+ protected int file_tryelf(struct magic_set *, const struct buffer *);
+ protected int file_trycdf(struct magic_set *, const struct buffer *);
-#if HAVE_FORK
+#ifdef PHP_FILEINFO_UNCOMPRESS
- protected int file_zmagic(struct magic_set *, int, const char *,
- const unsigned char *, size_t);
+ protected int file_zmagic(struct magic_set *, const struct buffer *,
+ const char *);
#endif
-@@ -472,16 +466,13 @@
- protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
- protected uint64_t file_signextend(struct magic_set *, struct magic *,
- uint64_t);
-+protected void file_delmagic(struct magic *, int type, size_t entries);
+@@ -484,13 +480,9 @@
protected void file_badread(struct magic_set *);
protected void file_badseek(struct magic_set *);
protected void file_oomem(struct magic_set *, size_t);
@@ -1524,9 +2321,9 @@ diff -u libmagic.orig/file.h libmagic/file.h
protected void file_showstr(FILE *, const char *, size_t);
protected size_t file_mbswidth(const char *);
protected const char *file_getbuffer(struct magic_set *);
-@@ -497,32 +488,6 @@
- size_t);
- #endif /* __EMX__ */
+@@ -510,32 +502,6 @@
+ protected void buffer_fini(struct buffer *);
+ protected int buffer_fill(const struct buffer *);
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
@@ -1557,7 +2354,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
typedef struct {
char *buf;
uint32_t offset;
-@@ -531,10 +496,8 @@
+@@ -544,10 +510,8 @@
protected file_pushbuf_t *file_push_buffer(struct magic_set *);
protected char *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
@@ -1568,7 +2365,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
#ifndef HAVE_STRERROR
extern int sys_nerr;
-@@ -547,23 +510,10 @@
+@@ -560,23 +524,10 @@
#define strtoul(a, b, c) strtol(a, b, c)
#endif
@@ -1594,7 +2391,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
size_t strlcat(char *, const char *, size_t);
#endif
#ifndef HAVE_STRCASESTR
-@@ -579,16 +529,6 @@
+@@ -592,39 +543,6 @@
#ifndef HAVE_ASCTIME_R
char *asctime_r(const struct tm *, char *);
#endif
@@ -1608,10 +2405,33 @@ diff -u libmagic.orig/file.h libmagic/file.h
-const char *fmtcheck(const char *, const char *)
- __attribute__((__format_arg__(2)));
-#endif
+-
+-#ifdef HAVE_LIBSECCOMP
+-// basic filter
+-// this mode should not interfere with normal operations
+-// only some dangerous syscalls are blacklisted
+-int enable_sandbox_basic(void);
+-
+-// enhanced filter
+-// this mode allows only the necessary syscalls used during normal operation
+-// extensive testing required !!!
+-int enable_sandbox_full(void);
+-#endif
+-
+-protected const char *file_getprogname(void);
+-protected void file_setprogname(const char *);
+-protected void file_err(int, const char *, ...)
+- __attribute__((__format__(__printf__, 2, 3)));
+-protected void file_errx(int, const char *, ...)
+- __attribute__((__format__(__printf__, 2, 3)));
+-protected void file_warn(const char *, ...)
+- __attribute__((__format__(__printf__, 1, 2)));
+-protected void file_warnx(const char *, ...)
+- __attribute__((__format__(__printf__, 1, 2)));
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
#define QUICK
-@@ -611,6 +551,18 @@
+@@ -647,6 +565,18 @@
#else
#define FILE_RCSID(id)
#endif
@@ -1631,8 +2451,26 @@ diff -u libmagic.orig/file.h libmagic/file.h
#define __RCSID(a)
#endif
diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
---- libmagic.orig/fsmagic.c 2016-05-03 17:48:37.000000000 +0200
-+++ libmagic/fsmagic.c 2017-10-23 06:47:43.293874100 +0200
+--- libmagic.orig/fsmagic.c 2017-05-24 21:17:50.000000000 +0200
++++ libmagic/fsmagic.c 2018-04-27 19:30:50.306234161 +0200
+@@ -2,7 +2,7 @@
+ * Copyright (c) Ian F. Darwin 1986-1995.
+ * Software written by Ian F. Darwin and others;
+ * maintained 1995-present by Christos Zoulas and others.
+- *
++ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+@@ -12,7 +12,7 @@
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+- *
++ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -63,27 +63,21 @@
# define minor(dev) ((dev) & 0xff)
#endif
@@ -1675,7 +2513,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
private int
handle_mime(struct magic_set *ms, int mime, const char *str)
{
-@@ -100,71 +94,38 @@
+@@ -100,70 +94,39 @@
}
protected int
@@ -1684,15 +2522,16 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
{
int ret, did = 0;
int mime = ms->flags & MAGIC_MIME;
+ int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
-#ifdef S_IFLNK
- char buf[BUFSIZ+4];
- ssize_t nch;
- struct stat tstatbuf;
-#endif
- if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
- return 0;
- if (fn == NULL)
++ if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
++ return 0;
+
+ if (fn == NULL && !stream) {
return 0;
@@ -1709,7 +2548,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- else
-#endif
- ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
--
+
-#ifdef WIN32
- {
- HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
@@ -1733,7 +2572,6 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- sb->st_mode |= S_IFIFO;
- sb->st_mode &= ~S_IFREG;
- break;
-+
+ if (stream) {
+ php_stream_statbuf ssb;
+ if (php_stream_stat(stream, &ssb) < 0) {
@@ -1767,14 +2605,30 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
}
ret = 1;
-@@ -187,30 +148,24 @@
+@@ -174,44 +137,36 @@
+ return -1;
+ #endif
+ #ifdef S_ISGID
+- if (sb->st_mode & S_ISGID)
++ if (sb->st_mode & S_ISGID)
+ if (file_printf(ms, "%ssetgid", COMMA) == -1)
+ return -1;
+ #endif
+ #ifdef S_ISVTX
+- if (sb->st_mode & S_ISVTX)
++ if (sb->st_mode & S_ISVTX)
+ if (file_printf(ms, "%ssticky", COMMA) == -1)
+ return -1;
+ #endif
}
-
+-
++
switch (sb->st_mode & S_IFMT) {
- case S_IFDIR:
- if (mime) {
- if (handle_mime(ms, mime, "directory") == -1)
- return -1;
+- } else if (silent) {
- } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
- return -1;
- break;
@@ -1792,6 +2646,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- if (mime) {
- if (handle_mime(ms, mime, "chardevice") == -1)
- return -1;
+- } else if (silent) {
- } else {
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
@@ -1816,7 +2671,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
if (file_printf(ms, "%scharacter special (%d/%d/%d)",
COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
dv_subunit(sb->st_rdev)) == -1)
-@@ -225,44 +180,11 @@
+@@ -226,45 +181,11 @@
if (file_printf(ms, "%scharacter special", COMMA) == -1)
return -1;
#endif
@@ -1837,6 +2692,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- if (mime) {
- if (handle_mime(ms, mime, "blockdevice") == -1)
- return -1;
+- } else if (silent) {
- } else {
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
@@ -1864,7 +2720,22 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
#ifdef S_IFIFO
case S_IFIFO:
if((ms->flags & MAGIC_DEVICES) != 0)
-@@ -285,79 +207,14 @@
+@@ -272,7 +193,6 @@
+ if (mime) {
+ if (handle_mime(ms, mime, "fifo") == -1)
+ return -1;
+- } else if (silent) {
+ } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
+ return -1;
+ break;
+@@ -282,89 +202,20 @@
+ if (mime) {
+ if (handle_mime(ms, mime, "door") == -1)
+ return -1;
+- } else if (silent) {
+ } else if (file_printf(ms, "%sdoor", COMMA) == -1)
+ return -1;
+ break;
#endif
#ifdef S_IFLNK
case S_IFLNK:
@@ -1879,6 +2750,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- if (mime) {
- if (handle_mime(ms, mime, "symlink") == -1)
- return -1;
+- } else if (silent) {
- } else if (file_printf(ms,
- "%sunreadable symlink `%s' (%s)", COMMA, fn,
- strerror(errno)) == -1)
@@ -1908,6 +2780,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- if (handle_mime(ms, mime,
- "x-path-too-long") == -1)
- return -1;
+- } else if (silent) {
- } else if (file_printf(ms,
- "%spath too long: `%s'", COMMA,
- fn) == -1)
@@ -1937,6 +2810,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
- if (mime) {
- if (handle_mime(ms, mime, "symlink") == -1)
- return -1;
+- } else if (silent) {
- } else if (file_printf(ms, "%ssymbolic link to %s",
- COMMA, buf) == -1)
- return -1;
@@ -1949,8 +2823,8 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
#ifndef __COHERENT__
case S_IFSOCK:
diff -u libmagic.orig/funcs.c libmagic/funcs.c
---- libmagic.orig/funcs.c 2017-05-08 20:10:13.000000000 +0200
-+++ libmagic/funcs.c 2017-11-13 19:49:45.968069700 +0100
+--- libmagic.orig/funcs.c 2017-11-02 21:25:39.000000000 +0100
++++ libmagic/funcs.c 2018-04-28 12:46:26.840103237 +0200
@@ -31,7 +31,6 @@
#endif /* lint */
@@ -2096,8 +2970,18 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
+ size_t nb)
{
int m = 0, rv = 0, looks_text = 0;
- const unsigned char *ubuf = CAST(const unsigned char *, buf);
-@@ -216,10 +215,10 @@
+ const char *code = NULL;
+@@ -184,7 +183,8 @@
+ const char *def = "data";
+ const char *ftype = NULL;
+ struct buffer b;
+-
++ int fd = -1;
++
+ buffer_init(&b, fd, buf, nb);
+
+ if (nb == 0) {
+@@ -216,8 +216,8 @@
}
}
#endif
@@ -2106,28 +2990,30 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
+
+#if PHP_FILEINFO_UNCOMPRESS
if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
-- m = file_zmagic(ms, fd, inname, ubuf, nb);
-+ m = file_zmagic(ms, stream, inname, ubuf, nb);
+ m = file_zmagic(ms, &b, inname);
if ((ms->flags & MAGIC_DEBUG) != 0)
- (void)fprintf(stderr, "[try zmagic %d]\n", m);
- if (m) {
-@@ -240,12 +239,15 @@
+@@ -240,12 +240,14 @@
/* Check if we have a CDF file */
if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
-+ php_socket_t fd;
+- m = file_trycdf(ms, &b);
+- if ((ms->flags & MAGIC_DEBUG) != 0)
+- (void)fprintf(stderr, "[try cdf %d]\n", m);
+- if (m) {
+- if (checkdone(ms, &rv))
+- goto done;
+ if (stream && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, 0)) {
- m = file_trycdf(ms, fd, ubuf, nb);
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void)fprintf(stderr, "[try cdf %d]\n", m);
- if (m) {
- if (checkdone(ms, &rv))
- goto done;
++ m = file_trycdf(ms, &b);
++ if ((ms->flags & MAGIC_DEBUG) != 0)
++ (void)fprintf(stderr, "[try cdf %d]\n", m);
++ if (m) {
++ if (checkdone(ms, &rv))
++ goto done;
+ }
}
}
-@@ -316,7 +318,7 @@
+@@ -315,7 +317,7 @@
if (file_printf(ms, "%s", code_mime) == -1)
rv = -1;
}
@@ -2135,16 +3021,16 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
+#if PHP_FILEINFO_UNCOMPRESS
done_encoding:
#endif
- free(u8buf);
-@@ -325,7 +327,6 @@
+ buffer_fini(&b);
+@@ -324,7 +326,6 @@
return m;
}
-#endif
protected int
- file_reset(struct magic_set *ms)
-@@ -335,11 +336,11 @@
+ file_reset(struct magic_set *ms, int checkloaded)
+@@ -334,11 +335,11 @@
return -1;
}
if (ms->o.buf) {
@@ -2158,7 +3044,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
ms->o.pbuf = NULL;
}
ms->event_flags &= ~EVENT_HAD_ERR;
-@@ -377,7 +378,7 @@
+@@ -376,7 +377,7 @@
return NULL;
}
psize = len * 4 + 1;
@@ -2167,7 +3053,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
file_oomem(ms, psize);
return NULL;
}
-@@ -441,8 +442,8 @@
+@@ -440,8 +441,8 @@
if (level >= ms->c.len) {
len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
@@ -2178,7 +3064,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
if (ms->c.li == NULL) {
file_oomem(ms, len);
return -1;
-@@ -465,76 +466,41 @@
+@@ -464,76 +465,41 @@
protected int
file_replace(struct magic_set *ms, const char *pat, const char *rep)
{
@@ -2238,12 +3124,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
- return rx->rc = regcomp(&rx->rx, pat, flags);
-}
-+ zend_string_release(repl);
-+ if (NULL == res) {
-+ rep_cnt = -1;
-+ goto out;
-+ }
-
+-
-protected int
-file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
- regmatch_t* pmatch, int eflags)
@@ -2253,8 +3134,11 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
- memset(pmatch, 0, nmatch * sizeof(*pmatch));
- return regexec(&rx->rx, str, nmatch, pmatch, eflags);
-}
-+ strncpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res));
-+ ms->o.buf[ZSTR_LEN(res)] = '\0';
++ zend_string_release(repl);
++ if (NULL == res) {
++ rep_cnt = -1;
++ goto out;
++ }
-protected void
-file_regfree(file_regex_t *rx)
@@ -2268,7 +3152,9 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
- (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
-#endif
-}
--
++ strncpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res));
++ ms->o.buf[ZSTR_LEN(res)] = '\0';
+
-protected void
-file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
-{
@@ -2284,7 +3170,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
}
protected file_pushbuf_t *
-@@ -545,7 +511,7 @@
+@@ -544,7 +510,7 @@
if (ms->event_flags & EVENT_HAD_ERR)
return NULL;
@@ -2293,7 +3179,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
return NULL;
pb->buf = ms->o.buf;
-@@ -563,8 +529,8 @@
+@@ -562,8 +528,8 @@
char *rbuf;
if (ms->event_flags & EVENT_HAD_ERR) {
@@ -2304,7 +3190,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
return NULL;
}
-@@ -573,7 +539,7 @@
+@@ -572,7 +538,7 @@
ms->o.buf = pb->buf;
ms->offset = pb->offset;
@@ -2314,8 +3200,8 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
}
diff -u libmagic.orig/magic.c libmagic/magic.c
---- libmagic.orig/magic.c 2016-07-18 13:43:05.000000000 +0200
-+++ libmagic/magic.c 2017-10-23 06:47:43.293874100 +0200
+--- libmagic.orig/magic.c 2017-08-28 15:39:18.000000000 +0200
++++ libmagic/magic.c 2018-04-27 19:30:50.918206168 +0200
@@ -25,11 +25,6 @@
* SUCH DAMAGE.
*/
@@ -2460,7 +3346,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
-{
- if (fdwReason == DLL_PROCESS_ATTACH)
- _w32_dll_instance = hinstDLL;
-- return TRUE;
+- return 1;
-}
-#endif
-
@@ -2576,34 +3462,16 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
public int
magic_compile(struct magic_set *ms, const char *magicfile)
{
-@@ -326,13 +141,6 @@
- return file_apprentice(ms, magicfile, FILE_COMPILE);
- }
-
--public int
--magic_check(struct magic_set *ms, const char *magicfile)
--{
-- if (ms == NULL)
-- return -1;
-- return file_apprentice(ms, magicfile, FILE_CHECK);
--}
-
- public int
- magic_list(struct magic_set *ms, const char *magicfile)
-@@ -344,11 +152,8 @@
+@@ -344,7 +159,7 @@
private void
close_and_restore(const struct magic_set *ms, const char *name, int fd,
- const struct stat *sb)
+ const zend_stat_t *sb)
{
-- if (fd == STDIN_FILENO || name == NULL)
-- return;
-- (void) close(fd);
-
- if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
- /*
-@@ -375,7 +180,6 @@
+ if (fd == STDIN_FILENO || name == NULL)
+ return;
+@@ -375,7 +190,6 @@
}
}
@@ -2611,7 +3479,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
/*
* find type of descriptor
-@@ -385,7 +189,7 @@
+@@ -385,7 +199,7 @@
{
if (ms == NULL)
return NULL;
@@ -2620,7 +3488,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
}
/*
-@@ -396,31 +200,42 @@
+@@ -396,31 +210,42 @@
{
if (ms == NULL)
return NULL;
@@ -2649,7 +3517,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
- off_t pos = (off_t)-1;
+ int no_in_stream = 0;
- if (file_reset(ms) == -1)
+ if (file_reset(ms, 1) == -1)
goto out;
+ if (!inname && !stream) {
@@ -2670,7 +3538,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
case -1: /* error */
goto done;
case 0: /* nothing found */
-@@ -430,103 +245,41 @@
+@@ -430,103 +255,41 @@
goto done;
}
@@ -2794,12 +3662,9 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
}
out:
return rv == 0 ? file_getbuffer(ms) : NULL;
-@@ -542,14 +295,13 @@
- return NULL;
- /*
+@@ -544,12 +307,11 @@
* The main work is done here!
-- * We have the file name and/or the data buffer to be identified.
-+ * We have the file name and/or the data buffer to be identified.
+ * We have the file name and/or the data buffer to be identified.
*/
- if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
+ if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
@@ -2811,9 +3676,38 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
public const char *
magic_error(struct magic_set *ms)
+diff -u libmagic.orig/magic.h libmagic/magic.h
+--- libmagic.orig/magic.h 2018-04-23 20:45:58.824428361 +0200
++++ libmagic/magic.h 2018-04-27 19:30:51.050200127 +0200
+@@ -122,6 +122,7 @@
+
+ const char *magic_getpath(const char *, int);
+ const char *magic_file(magic_t, const char *);
++const char *magic_stream(magic_t, php_stream *);
+ const char *magic_descriptor(magic_t, int);
+ const char *magic_buffer(magic_t, const void *, size_t);
+
diff -u libmagic.orig/print.c libmagic/print.c
---- libmagic.orig/print.c 2017-03-07 23:20:58.000000000 +0100
-+++ libmagic/print.c 2017-10-23 06:47:43.293874100 +0200
+--- libmagic.orig/print.c 2017-02-10 19:14:01.000000000 +0100
++++ libmagic/print.c 2018-04-28 11:22:53.605682359 +0200
+@@ -2,7 +2,7 @@
+ * Copyright (c) Ian F. Darwin 1986-1995.
+ * Software written by Ian F. Darwin and others;
+ * maintained 1995-present by Christos Zoulas and others.
+- *
++ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+@@ -12,7 +12,7 @@
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+- *
++ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -28,6 +28,8 @@
/*
* print.c - debugging printout routines
@@ -2823,7 +3717,7 @@ diff -u libmagic.orig/print.c libmagic/print.c
#include "file.h"
-@@ -43,202 +45,44 @@
+@@ -43,6 +45,11 @@
#endif
#include <time.h>
@@ -2835,172 +3729,16 @@ diff -u libmagic.orig/print.c libmagic/print.c
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
#include "cdf.h"
+@@ -75,7 +82,7 @@
+ if (m->mask_op & FILE_OPINVERSE)
+ (void) fputc('~', stderr);
--#ifndef COMPILE_ONLY
--protected void
--file_mdump(struct magic *m)
--{
-- static const char optyp[] = { FILE_OPS };
-- char tbuf[26];
--
-- (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
-- (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
--
-- if (m->flag & INDIR) {
-- (void) fprintf(stderr, "(%s,",
-- /* Note: type is unsigned */
-- (m->in_type < file_nnames) ? file_names[m->in_type] :
-- "*bad in_type*");
-- if (m->in_op & FILE_OPINVERSE)
-- (void) fputc('~', stderr);
-- (void) fprintf(stderr, "%c%u),",
-- ((size_t)(m->in_op & FILE_OPS_MASK) <
-- SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
-- m->in_offset);
-- }
-- (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
-- /* Note: type is unsigned */
-- (m->type < file_nnames) ? file_names[m->type] : "*bad type");
-- if (m->mask_op & FILE_OPINVERSE)
-- (void) fputc('~', stderr);
--
- if (IS_STRING(m->type)) {
-- if (m->str_flags) {
-- (void) fputc('/', stderr);
-- if (m->str_flags & STRING_COMPACT_WHITESPACE)
-- (void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
-- if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
-- (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
-- stderr);
-- if (m->str_flags & STRING_IGNORE_LOWERCASE)
-- (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
-- if (m->str_flags & STRING_IGNORE_UPPERCASE)
-- (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
-- if (m->str_flags & REGEX_OFFSET_START)
-- (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
-- if (m->str_flags & STRING_TEXTTEST)
-- (void) fputc(CHAR_TEXTTEST, stderr);
-- if (m->str_flags & STRING_BINTEST)
-- (void) fputc(CHAR_BINTEST, stderr);
-- if (m->str_flags & PSTRING_1_BE)
-- (void) fputc(CHAR_PSTRING_1_BE, stderr);
-- if (m->str_flags & PSTRING_2_BE)
-- (void) fputc(CHAR_PSTRING_2_BE, stderr);
-- if (m->str_flags & PSTRING_2_LE)
-- (void) fputc(CHAR_PSTRING_2_LE, stderr);
-- if (m->str_flags & PSTRING_4_BE)
-- (void) fputc(CHAR_PSTRING_4_BE, stderr);
-- if (m->str_flags & PSTRING_4_LE)
-- (void) fputc(CHAR_PSTRING_4_LE, stderr);
-- if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
-- (void) fputc(
-- CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
-- stderr);
-- }
-- if (m->str_range)
-- (void) fprintf(stderr, "/%u", m->str_range);
-- }
-- else {
-- if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
-- (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
-- else
-- (void) fputc('?', stderr);
--
-- if (m->num_mask) {
-- (void) fprintf(stderr, "%.8llx",
-- (unsigned long long)m->num_mask);
-- }
-- }
-- (void) fprintf(stderr, ",%c", m->reln);
--
-- if (m->reln != 'x') {
-- switch (m->type) {
-- case FILE_BYTE:
-- case FILE_SHORT:
-- case FILE_LONG:
-- case FILE_LESHORT:
-- case FILE_LELONG:
-- case FILE_MELONG:
-- case FILE_BESHORT:
-- case FILE_BELONG:
-- case FILE_INDIRECT:
-- (void) fprintf(stderr, "%d", m->value.l);
-- break;
-- case FILE_BEQUAD:
-- case FILE_LEQUAD:
-- case FILE_QUAD:
-- (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
-- (unsigned long long)m->value.q);
-- break;
-- case FILE_PSTRING:
-- case FILE_STRING:
-- case FILE_REGEX:
-- case FILE_BESTRING16:
-- case FILE_LESTRING16:
-- case FILE_SEARCH:
-- file_showstr(stderr, m->value.s, (size_t)m->vallen);
-- break;
-- case FILE_DATE:
-- case FILE_LEDATE:
-- case FILE_BEDATE:
-- case FILE_MEDATE:
-- (void)fprintf(stderr, "%s,",
-- file_fmttime(m->value.l, 0, tbuf));
-- break;
-- case FILE_LDATE:
-- case FILE_LELDATE:
-- case FILE_BELDATE:
-- case FILE_MELDATE:
-- (void)fprintf(stderr, "%s,",
-- file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
-- break;
-- case FILE_QDATE:
-- case FILE_LEQDATE:
-- case FILE_BEQDATE:
-- (void)fprintf(stderr, "%s,",
-- file_fmttime(m->value.q, 0, tbuf));
-- break;
-- case FILE_QLDATE:
-- case FILE_LEQLDATE:
-- case FILE_BEQLDATE:
-- (void)fprintf(stderr, "%s,",
-- file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
-- break;
-- case FILE_QWDATE:
-- case FILE_LEQWDATE:
-- case FILE_BEQWDATE:
-- (void)fprintf(stderr, "%s,",
-- file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
-- break;
-- case FILE_FLOAT:
-- case FILE_BEFLOAT:
-- case FILE_LEFLOAT:
-- (void) fprintf(stderr, "%G", m->value.f);
-- break;
-- case FILE_DOUBLE:
-- case FILE_BEDOUBLE:
-- case FILE_LEDOUBLE:
-- (void) fprintf(stderr, "%G", m->value.d);
-- break;
-- case FILE_DEFAULT:
-- /* XXX - do anything here? */
-- break;
-- case FILE_USE:
-- case FILE_NAME:
-- case FILE_DER:
-- (void) fprintf(stderr, "'%s'", m->value.s);
-- break;
-- default:
-- (void) fprintf(stderr, "*bad type %d*", m->type);
-- break;
-- }
-- }
-- (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
--}
--#endif
--
- /*VARARGS*/
- protected void
++ if (IS_LIBMAGIC_STRING(m->type)) {
+ if (m->str_flags) {
+ (void) fputc('/', stderr);
+ if (m->str_flags & STRING_COMPACT_WHITESPACE)
+@@ -215,30 +222,30 @@
file_magwarn(struct magic_set *ms, const char *f, ...)
{
va_list va;
@@ -3044,7 +3782,7 @@ diff -u libmagic.orig/print.c libmagic/print.c
t = ts.tv_sec;
} else {
// XXX: perhaps detect and print something if overflow
-@@ -247,9 +91,29 @@
+@@ -247,9 +254,29 @@
}
if (flags & FILE_T_LOCAL) {
@@ -3077,15 +3815,8 @@ diff -u libmagic.orig/print.c libmagic/print.c
if (tm == NULL)
goto out;
diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
---- libmagic.orig/readcdf.c 2017-05-08 20:10:13.000000000 +0200
-+++ libmagic/readcdf.c 2017-10-23 06:47:43.293874100 +0200
-@@ -1,5 +1,5 @@
- /*-
-- * Copyright (c) 2008, 2016 Christos Zoulas
-+ * Copyright (c) 2008 Christos Zoulas
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
+--- libmagic.orig/readcdf.c 2017-11-02 21:25:39.000000000 +0100
++++ libmagic/readcdf.c 2018-04-28 08:59:06.029692824 +0200
@@ -31,7 +31,11 @@
#include <assert.h>
@@ -3183,9 +3914,78 @@ diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
if (c != NULL &&
(ec = strchr(c, '\n')) != NULL)
*ec = '\0';
+@@ -281,10 +274,10 @@
+ if (file_printf(ms, "%s%s",
+ cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
+ i == cat->cat_num - 1 ? "]" : ", ") == -1) {
+- free(cat);
++ efree(cat);
+ return -1;
+ }
+- free(cat);
++ efree(cat);
+ } else {
+ if (file_printf(ms, "application/CDFV2") == -1)
+ return -1;
+@@ -345,7 +338,7 @@
+ }
+
+ m = cdf_file_property_info(ms, info, count, root_storage);
+- free(info);
++ efree(info);
+
+ return m == -1 ? -2 : m;
+ }
+@@ -353,11 +346,11 @@
+ #ifdef notdef
+ private char *
+ format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
+- snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
++ snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
+ PRIx64 "-%.12" PRIx64,
+ (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
+ (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
+- (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
++ (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
+ (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
+ (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
+ return buf;
+@@ -436,7 +429,7 @@
+ const char *sections[5];
+ const int types[5];
+ } sectioninfo[] = {
+- { "Encrypted", "encrypted",
++ { "Encrypted", "encrypted",
+ {
+ "EncryptedPackage", "EncryptedSummary",
+ NULL, NULL, NULL,
+@@ -448,7 +441,7 @@
+
+ },
+ },
+- { "QuickBooks", "quickbooks",
++ { "QuickBooks", "quickbooks",
+ {
+ #if 0
+ "TaxForms", "PDFTaxForms", "modulesInBackup",
+@@ -655,11 +648,11 @@
+ cdf_zero_stream(&scn);
+ cdf_zero_stream(&sst);
+ out3:
+- free(dir.dir_tab);
++ efree(dir.dir_tab);
+ out2:
+- free(ssat.sat_tab);
++ efree(ssat.sat_tab);
+ out1:
+- free(sat.sat_tab);
++ efree(sat.sat_tab);
+ out0:
+ if (i == -1) {
+ if (NOTMIME(ms)) {
diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
---- libmagic.orig/softmagic.c 2017-05-08 20:10:13.000000000 +0200
-+++ libmagic/softmagic.c 2017-11-14 17:06:52.022040000 +0100
+--- libmagic.orig/softmagic.c 2018-04-15 20:49:15.000000000 +0200
++++ libmagic/softmagic.c 2018-04-27 19:30:51.678171356 +0200
@@ -43,6 +43,10 @@
#include <time.h>
#include "der.h"
@@ -3195,9 +3995,9 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
+#endif
+
private int match(struct magic_set *, struct magic *, uint32_t,
- const unsigned char *, size_t, size_t, int, int, int, uint16_t *,
+ const struct buffer *, size_t, int, int, int, uint16_t *,
uint16_t *, int *, int *, int *);
-@@ -113,8 +117,8 @@
+@@ -119,8 +123,8 @@
return 0;
}
@@ -3208,19 +4008,19 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
#define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
private const char * __attribute__((__format_arg__(3)))
-@@ -128,8 +132,10 @@
- " with `%s'", file, line, m->desc, def);
+@@ -134,8 +138,10 @@
+ " with `%s'", file, line, desc, def);
return ptr;
}
-#else
+#elif defined(HAVE_FMTCHECK)
- #define F(a, b, c) fmtcheck((b)->desc, (c))
+ #define F(a, b, c) fmtcheck((b), (c))
+#else
-+#define F(a, b, c) ((b)->desc)
++#define F(a, b, c) ((b))
#endif
/*
-@@ -182,7 +188,7 @@
+@@ -189,7 +195,7 @@
struct magic *m = &magic[magindex];
if (m->type != FILE_NAME)
@@ -3229,9 +4029,9 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
#define FLT (STRING_BINTEST | STRING_TEXTTEST)
((text && (m->str_flags & FLT) == STRING_BINTEST) ||
(!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
-@@ -406,42 +412,30 @@
+@@ -416,42 +422,30 @@
private int
- check_fmt(struct magic_set *ms, struct magic *m)
+ check_fmt(struct magic_set *ms, const char *fmt)
{
- file_regex_t rx;
- int rc, rv = -1;
@@ -3240,7 +4040,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
+ int rv = -1;
+ zend_string *pattern;
- if (strchr(m->desc, '%') == NULL)
+ if (strchr(fmt, '%') == NULL)
return 0;
- rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
@@ -3251,11 +4051,11 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
+ if ((pce = pcre_get_compiled_regex(pattern, &capture_count, &re_options)) == NULL) {
+ rv = -1;
} else {
-- rc = file_regexec(&rx, m->desc, 0, 0, 0);
+- rc = file_regexec(&rx, fmt, 0, 0, 0);
- rv = !rc;
+ pcre2_match_data *match_data = php_pcre_create_match_data(capture_count, pce);
+ if (match_data) {
-+ rv = pcre2_match(pce, (PCRE2_SPTR)m->desc, strlen(m->desc), 0, re_options, match_data, php_pcre_mctx()) > 0;
++ rv = pcre2_match(pce, (PCRE2_SPTR)fmt, strlen(fmt), 0, re_options, match_data, php_pcre_mctx()) > 0;
+ php_pcre_free_match_data(match_data);
+ }
}
@@ -3284,10 +4084,10 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
-}
-#endif /* HAVE_STRNDUP */
-
- private int32_t
- mprint(struct magic_set *ms, struct magic *m)
+ static int
+ varexpand(char *buf, size_t len, const struct buffer *b, const char *str)
{
-@@ -667,19 +661,18 @@
+@@ -738,19 +732,18 @@
t = ms->offset + sizeof(double);
break;
@@ -3302,19 +4102,19 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
file_oomem(ms, ms->search.rm_len);
return -1;
}
- rval = file_printf(ms, F(ms, m, "%s"),
+ rval = file_printf(ms, F(ms, desc, "%s"),
file_printable(sbuf, sizeof(sbuf), cp));
- free(cp);
+ efree(cp);
if (rval == -1)
return -1;
-@@ -691,6 +684,15 @@
+@@ -762,6 +755,15 @@
break;
}
+ case FILE_SEARCH:
-+ if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
++ if (file_printf(ms, F(ms, desc, "%s"), m->value.s) == -1)
+ return -1;
+ if ((m->str_flags & REGEX_OFFSET_START))
+ t = ms->search.offset;
@@ -3325,7 +4125,25 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
case FILE_DEFAULT:
case FILE_CLEAR:
if (file_printf(ms, "%s", m->desc) == -1)
-@@ -1205,21 +1207,28 @@
+@@ -1135,7 +1137,7 @@
+ * string by p->s, so we need to deduct sz.
+ * Because we can use one of the bytes of the length
+ * after we shifted as NUL termination.
+- */
++ */
+ len = sz;
+ }
+ while (len--)
+@@ -1209,7 +1211,7 @@
+ goto out;
+ return 1;
+ case FILE_BEDOUBLE:
+- p->q = BE64(p);
++ p->q = BE64(p);
+ if (cvt_double(p, m) == -1)
+ goto out;
+ return 1;
+@@ -1278,21 +1280,28 @@
return 0;
}
@@ -3366,7 +4184,25 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
/* mget() guarantees buf <= last */
for (lines = linecnt, b = buf; lines && b < end &&
((b = CAST(const char *,
-@@ -1373,9 +1382,6 @@
+@@ -1305,7 +1314,7 @@
+ b++;
+ }
+ if (lines)
+- last = end;
++ last = RCAST(const char *, s) + bytecnt;
+
+ ms->search.s = buf;
+ ms->search.s_len = last - buf;
+@@ -1481,8 +1490,6 @@
+ return -1;
+ }
+
+-
+-
+ if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
+ (uint32_t)nbytes, m) == -1)
+ return -1;
+@@ -1494,9 +1501,6 @@
m->type, m->flag, offset, o, nbytes,
*indir_count, *name_count);
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
@@ -3376,7 +4212,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
}
if (m->flag & INDIR) {
-@@ -1488,9 +1494,6 @@
+@@ -1609,9 +1613,6 @@
if ((ms->flags & MAGIC_DEBUG) != 0) {
mdebug(offset, (char *)(void *)p,
sizeof(union VALUETYPE));
@@ -3386,10 +4222,10 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
}
}
-@@ -1572,15 +1575,15 @@
+@@ -1696,15 +1697,15 @@
if (rv == 1) {
if ((ms->flags & MAGIC_NODESC) == 0 &&
- file_printf(ms, F(ms, m, "%u"), offset) == -1) {
+ file_printf(ms, F(ms, m->desc, "%u"), offset) == -1) {
- free(rbuf);
+ if (rbuf) efree(rbuf);
return -1;
@@ -3405,7 +4241,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
return rv;
case FILE_USE:
-@@ -1703,6 +1706,41 @@
+@@ -1827,6 +1828,41 @@
return file_strncmp(a, b, len, flags);
}
@@ -3432,7 +4268,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
+ }
+ ZSTR_VAL(t)[j++] = '~';
+
-+ if (options & PCRE2_CASELESS)
++ if (options & PCRE2_CASELESS)
+ ZSTR_VAL(t)[j++] = 'i';
+
+ if (options & PCRE2_MULTILINE)
@@ -3447,7 +4283,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
private int
magiccheck(struct magic_set *ms, struct magic *m)
{
-@@ -1863,65 +1901,77 @@
+@@ -1987,65 +2023,77 @@
break;
}
case FILE_REGEX: {
@@ -3578,8 +4414,8 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
}
case FILE_INDIRECT:
diff -u libmagic.orig/strcasestr.c libmagic/strcasestr.c
---- libmagic.orig/strcasestr.c 2014-09-11 17:05:33.000000000 +0200
-+++ libmagic/strcasestr.c 2015-07-18 21:35:36.510103000 +0200
+--- libmagic.orig/strcasestr.c 2014-05-13 18:48:12.000000000 +0200
++++ libmagic/strcasestr.c 2018-04-27 19:30:52.154149541 +0200
@@ -39,6 +39,8 @@
#include "file.h"