php-src/ext/fileinfo/libmagic.patch

3588 lines
88 KiB
Diff
Raw Normal View History

diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/apprentice.c 2017-05-08 20:10:13.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/apprentice.c 2017-10-23 19:16:23.937911800 +0200
2015-03-29 16:22:42 +00:00
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
+#include "php.h"
+
#include "file.h"
#ifndef lint
2015-03-29 16:22:42 +00:00
@@ -36,24 +38,27 @@
#endif /* lint */
#include "magic.h"
+#include "patchlevel.h"
#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
2015-03-08 16:24:44 +00:00
-#include <unistd.h>
+
+#if defined(__hpux) && !defined(HAVE_STRTOULL)
+#if SIZEOF_LONG == 8
+# define strtoull strtoul
+#else
+# define strtoull __strtoull
2015-03-08 16:24:44 +00:00
#endif
-#ifdef HAVE_STDDEF_H
-#include <stddef.h>
+#endif
+
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#define strtoull _strtoui64
+#else
2015-03-08 16:24:44 +00:00
+#include <unistd.h>
#endif
2015-03-08 16:24:44 +00:00
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
-#ifdef QUICK
-#include <sys/mman.h>
-#endif
-#include <dirent.h>
2015-03-05 14:32:04 +00:00
-#if defined(HAVE_LIMITS_H)
-#include <limits.h>
-#endif
2015-03-05 14:32:04 +00:00
#ifndef SSIZE_MAX
#define MAXMAGIC_SIZE ((ssize_t)0x7fffffff)
@@ -75,6 +80,10 @@
#endif
#endif
+#ifndef offsetof
+#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
+#endif
+
#ifndef MAP_FAILED
#define MAP_FAILED (void *) -1
#endif
@@ -170,38 +179,7 @@
{ NULL, 0, NULL }
};
-#ifdef COMPILE_ONLY
-
-int main(int, char *[]);
-
-int
-main(int argc, char *argv[])
-{
- int ret;
- struct magic_set *ms;
- char *progname;
-
- if ((progname = strrchr(argv[0], '/')) != NULL)
- progname++;
- else
- progname = argv[0];
-
- if (argc != 2) {
- (void)fprintf(stderr, "Usage: %s file\n", progname);
- return 1;
- }
-
- if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
- (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- return 1;
- }
- ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
- if (ret == 1)
- (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
- magic_close(ms);
- return ret;
-}
-#endif /* COMPILE_ONLY */
+#include "../data_file.c"
2013-04-07 20:15:56 +00:00
struct type_tbl_s {
const char name[16];
@@ -285,6 +263,10 @@
2013-04-07 20:15:56 +00:00
# undef XX
# undef XX_NULL
+#ifndef S_ISDIR
+#define S_ISDIR(mode) ((mode) & _S_IFDIR)
+#endif
+
private int
2013-04-07 20:15:56 +00:00
get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
{
@@ -409,7 +391,7 @@
2013-04-07 20:15:56 +00:00
struct mlist *ml;
2016-11-24 13:58:54 +00:00
mlp->map = NULL;
2013-04-07 20:15:56 +00:00
- if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL)
+ if ((ml = CAST(struct mlist *, emalloc(sizeof(*ml)))) == NULL)
return -1;
2016-11-24 13:58:54 +00:00
ml->map = idx == 0 ? map : NULL;
@@ -430,10 +412,8 @@
2015-03-05 14:32:04 +00:00
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",
@@ -449,14 +429,15 @@
2013-04-07 20:15:56 +00:00
return apprentice_compile(ms, map, fn);
}
-#ifndef COMPILE_ONLY
2013-04-07 20:15:56 +00:00
map = apprentice_map(ms, fn);
2016-11-24 13:58:54 +00:00
if (map == (struct magic_map *)-1)
return -1;
2013-04-07 20:15:56 +00:00
if (map == NULL) {
- if (ms->flags & MAGIC_CHECK)
- file_magwarn(ms, "using regular magic file `%s'", fn);
2013-04-07 20:15:56 +00:00
- map = apprentice_load(ms, fn, action);
+ if (fn) {
+ if (ms->flags & MAGIC_CHECK)
+ file_magwarn(ms, "using regular magic file `%s'", fn);
2013-04-07 20:15:56 +00:00
+ map = apprentice_load(ms, fn, action);
+ }
2013-04-07 20:15:56 +00:00
if (map == NULL)
return -1;
}
@@ -478,9 +459,6 @@
2015-03-08 16:24:44 +00:00
}
}
2015-03-08 16:24:44 +00:00
return 0;
2015-03-05 14:32:04 +00:00
-#else
- return 0;
-#endif /* COMPILE_ONLY */
}
protected void
@@ -491,10 +469,16 @@
return;
2013-04-07 20:15:56 +00:00
for (i = 0; i < MAGIC_SETS; i++)
mlist_free(ms->mlist[i]);
- free(ms->o.pbuf);
- free(ms->o.buf);
- free(ms->c.li);
- free(ms);
+ if (ms->o.pbuf) {
+ efree(ms->o.pbuf);
+ }
+ if (ms->o.buf) {
+ efree(ms->o.buf);
+ }
+ if (ms->c.li) {
+ efree(ms->c.li);
+ }
+ efree(ms);
}
protected struct magic_set *
@@ -503,7 +487,7 @@
2013-04-07 20:15:56 +00:00
struct magic_set *ms;
size_t i, len;
- if ((ms = CAST(struct magic_set *, calloc((size_t)1,
+ if ((ms = CAST(struct magic_set *, ecalloc((size_t)1,
sizeof(struct magic_set)))) == NULL)
return NULL;
@@ -515,7 +499,7 @@
2013-04-07 20:15:56 +00:00
ms->o.buf = ms->o.pbuf = NULL;
len = (ms->c.len = 10) * sizeof(*ms->c.li);
- if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
+ if ((ms->c.li = CAST(struct level_info *, emalloc(len))) == NULL)
goto free;
ms->event_flags = 0;
2017-10-11 16:18:55 +00:00
@@ -533,48 +517,35 @@
2016-11-24 13:58:54 +00:00
ms->bytes_max = FILE_BYTES_MAX;
2013-04-07 20:15:56 +00:00
return ms;
free:
- free(ms);
+ efree(ms);
return NULL;
}
2016-11-24 13:58:54 +00:00
private void
apprentice_unmap(struct magic_map *map)
2013-04-07 20:15:56 +00:00
{
2016-11-24 13:58:54 +00:00
- size_t i;
2013-04-07 20:15:56 +00:00
if (map == NULL)
return;
2015-03-05 14:32:04 +00:00
-
- switch (map->type) {
- case MAP_TYPE_USER:
- break;
2016-11-24 13:58:54 +00:00
- case MAP_TYPE_MALLOC:
- for (i = 0; i < MAGIC_SETS; i++) {
2017-10-11 16:18:55 +00:00
- void *b = map->magic[i];
- void *p = map->p;
- if (CAST(char *, b) >= CAST(char *, p) &&
- CAST(char *, b) <= CAST(char *, p) + map->len)
2016-11-24 13:58:54 +00:00
- continue;
- free(map->magic[i]);
2013-04-08 14:23:43 +00:00
+ if (map->p != php_magic_database) {
2014-03-31 15:24:15 +00:00
+ if (map->p == NULL) {
+ int j;
+ for (j = 0; j < MAGIC_SETS; j++) {
+ if (map->magic[j]) {
+ efree(map->magic[j]);
+ }
+ }
+ } else {
2013-04-08 14:23:43 +00:00
+ efree(map->p);
2016-11-24 13:58:54 +00:00
}
- free(map->p);
- break;
-#ifdef QUICK
- case MAP_TYPE_MMAP:
- if (map->p && map->p != MAP_FAILED)
- (void)munmap(map->p, map->len);
- break;
-#endif
- default:
- abort();
2015-03-05 14:32:04 +00:00
}
- free(map);
2013-04-07 20:15:56 +00:00
+ efree(map);
}
private struct mlist *
mlist_alloc(void)
{
struct mlist *mlist;
- if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) {
+ if ((mlist = CAST(struct mlist *, ecalloc(1, sizeof(*mlist)))) == NULL) {
return NULL;
}
2013-04-07 20:15:56 +00:00
mlist->next = mlist->prev = mlist;
2017-10-11 16:18:55 +00:00
@@ -593,61 +564,12 @@
2015-03-05 14:32:04 +00:00
for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
2013-04-07 20:15:56 +00:00
if (ml->map)
2016-11-24 13:58:54 +00:00
apprentice_unmap(CAST(struct magic_map *, ml->map));
2013-04-07 20:15:56 +00:00
- free(ml);
+ efree(ml);
2015-03-05 14:32:04 +00:00
if (ml == mlist)
break;
2013-04-07 20:15:56 +00:00
}
2015-03-08 16:24:44 +00:00
}
-#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;
-
- if (ms->mlist[0] != NULL)
- file_reset(ms);
-
- 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:
- for (i = 0; i < MAGIC_SETS; i++) {
- mlist_free(ms->mlist[i]);
- ms->mlist[i] = NULL;
- }
- 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)
2017-10-11 16:18:55 +00:00
@@ -659,12 +581,28 @@
2015-03-05 14:32:04 +00:00
if (ms->mlist[0] != NULL)
file_reset(ms);
2014-02-19 10:20:24 +00:00
+/* XXX disabling default magic loading so the compiled in data is used */
+#if 0
if ((fn = magic_getpath(fn, action)) == NULL)
2013-04-07 20:15:56 +00:00
return -1;
+#endif
init_file_tables();
- if ((mfn = strdup(fn)) == NULL) {
+ if (fn == NULL)
+ fn = getenv("MAGIC");
+ if (fn == NULL) {
2013-04-07 20:15:56 +00:00
+ 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]));
+ return -1;
+ }
+ }
+ return apprentice_1(ms, fn, action);
+ }
+
2013-04-07 20:15:56 +00:00
+ if ((mfn = estrdup(fn)) == NULL) {
file_oomem(ms, strlen(fn));
return -1;
}
2017-10-11 16:18:55 +00:00
@@ -677,7 +615,7 @@
2015-03-05 14:32:04 +00:00
mlist_free(ms->mlist[i]);
ms->mlist[i] = NULL;
2013-04-07 20:15:56 +00:00
}
- free(mfn);
+ efree(mfn);
return -1;
}
}
2017-10-11 16:18:55 +00:00
@@ -694,7 +632,7 @@
2013-04-07 20:15:56 +00:00
fn = p;
}
2013-04-07 20:15:56 +00:00
- free(mfn);
+ efree(mfn);
2013-04-07 20:15:56 +00:00
if (errs == -1) {
for (i = 0; i < MAGIC_SETS; i++) {
2017-10-11 16:18:55 +00:00
@@ -1078,7 +1016,7 @@
2013-04-07 20:15:56 +00:00
2014-02-19 10:20:24 +00:00
mset[i].max += ALLOC_INCR;
2013-04-07 20:15:56 +00:00
if ((mp = CAST(struct magic_entry *,
2014-02-19 10:20:24 +00:00
- realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
+ erealloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
2013-04-07 20:15:56 +00:00
NULL) {
2014-02-19 10:20:24 +00:00
file_oomem(ms, sizeof(*mp) * mset[i].max);
2013-04-07 20:15:56 +00:00
return -1;
2017-10-11 16:18:55 +00:00
@@ -1099,13 +1037,19 @@
load_1(struct magic_set *ms, int action, const char *fn, int *errs,
2014-02-19 10:20:24 +00:00
struct magic_entry_set *mset)
{
- size_t lineno = 0, llen = 0;
+ char buffer[BUFSIZ + 1];
char *line = NULL;
- ssize_t len;
+ size_t len;
+ size_t lineno = 0;
2013-04-07 20:15:56 +00:00
struct magic_entry me;
2012-07-15 10:25:58 +00:00
- FILE *f = fopen(ms->file = fn, "r");
- if (f == NULL) {
2013-04-07 20:15:56 +00:00
+ php_stream *stream;
+
2014-12-30 19:28:13 +00:00
+
2013-04-07 20:15:56 +00:00
+ ms->file = fn;
+ stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
2012-07-15 10:25:58 +00:00
+
+ if (stream == NULL) {
if (errno != ENOENT)
file_error(ms, errno, "cannot read magic file `%s'",
fn);
2017-10-11 16:18:55 +00:00
@@ -1115,8 +1059,7 @@
2013-04-07 20:15:56 +00:00
memset(&me, 0, sizeof(me));
/* read and parse this file */
- for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
- ms->line++) {
+ for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &len)) != NULL; ms->line++) {
if (len == 0) /* null line, garbage, etc */
continue;
if (line[len - 1] == '\n') {
2017-10-11 16:18:55 +00:00
@@ -1174,8 +1117,8 @@
}
2013-04-07 20:15:56 +00:00
if (me.mp)
2014-02-19 10:20:24 +00:00
(void)addentry(ms, &me, mset);
- free(line);
- (void)fclose(f);
2015-03-08 16:24:44 +00:00
+ efree(line);
+ php_stream_close(stream);
}
/*
2017-10-11 16:18:55 +00:00
@@ -1254,7 +1197,7 @@
2013-04-07 20:15:56 +00:00
mentrycount += me[i].cont_count;
slen = sizeof(**ma) * mentrycount;
- if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) {
+ if ((*ma = CAST(struct magic *, emalloc(slen))) == NULL) {
file_oomem(ms, slen);
return -1;
}
2017-10-11 16:18:55 +00:00
@@ -1276,8 +1219,8 @@
2013-04-07 20:15:56 +00:00
if (me == NULL)
return;
for (i = 0; i < nme; i++)
- free(me[i].mp);
- free(me);
+ efree(me[i].mp);
+ efree(me);
}
private struct magic_map *
2017-10-11 16:18:55 +00:00
@@ -1286,18 +1229,19 @@
2014-02-19 10:20:24 +00:00
int errs = 0;
2013-04-07 20:15:56 +00:00
uint32_t i, j;
2013-04-27 12:09:29 +00:00
size_t files = 0, maxfiles = 0;
- char **filearr = NULL, *mfn;
2014-10-25 10:06:17 +00:00
- struct stat st;
2013-04-27 12:09:29 +00:00
+ char **filearr = NULL;
2014-10-25 10:06:17 +00:00
+ zend_stat_t st;
2013-04-07 20:15:56 +00:00
struct magic_map *map;
2014-02-19 10:20:24 +00:00
struct magic_entry_set mset[MAGIC_SETS];
- DIR *dir;
- struct dirent *d;
+ php_stream *dir;
+ php_stream_dirent d;
2014-02-19 10:20:24 +00:00
+
2014-12-30 19:28:13 +00:00
2014-02-19 10:20:24 +00:00
memset(mset, 0, sizeof(mset));
ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
2014-02-19 10:20:24 +00:00
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL)
+ if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL)
{
2013-04-07 20:15:56 +00:00
file_oomem(ms, sizeof(*map));
return NULL;
2017-10-11 16:18:55 +00:00
@@ -1309,24 +1253,26 @@
(void)fprintf(stderr, "%s\n", usg_hdr);
/* load directory or file */
- if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
- dir = opendir(fn);
2013-04-08 14:23:43 +00:00
+ /* FIXME: Read file names and sort them to prevent
+ non-determinism. See Debian bug #488562. */
+ if (php_sys_stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
+ int mflen;
+ char mfn[MAXPATHLEN];
+
2013-04-27 12:09:29 +00:00
+ dir = php_stream_opendir((char *)fn, REPORT_ERRORS, NULL);
if (!dir) {
errs++;
goto out;
}
- while ((d = readdir(dir)) != NULL) {
2017-10-11 16:18:55 +00:00
- if (d->d_name[0] == '.')
- continue;
- if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
+ while (php_stream_readdir(dir, &d)) {
+ if ((mflen = snprintf(mfn, sizeof(mfn), "%s/%s", fn, d.d_name)) < 0) {
file_oomem(ms,
- strlen(fn) + strlen(d->d_name) + 2);
+ strlen(fn) + strlen(d.d_name) + 2);
errs++;
- closedir(dir);
+ php_stream_closedir(dir);
goto out;
}
2014-10-25 10:06:17 +00:00
- if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
- free(mfn);
2014-10-25 10:06:17 +00:00
+ if (zend_stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
continue;
}
if (files >= maxfiles) {
2017-10-11 16:18:55 +00:00
@@ -1334,23 +1280,22 @@
2013-04-07 20:15:56 +00:00
maxfiles = (maxfiles + 1) * 2;
mlen = maxfiles * sizeof(*filearr);
if ((filearr = CAST(char **,
2013-04-07 20:15:56 +00:00
- realloc(filearr, mlen))) == NULL) {
+ erealloc(filearr, mlen))) == NULL) {
file_oomem(ms, mlen);
- free(mfn);
- closedir(dir);
+ php_stream_closedir(dir);
errs++;
goto out;
2012-07-15 10:25:58 +00:00
}
}
- filearr[files++] = mfn;
2012-09-11 03:43:47 +00:00
+ filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen);
2012-07-15 10:25:58 +00:00
}
- closedir(dir);
+ php_stream_closedir(dir);
2012-07-15 10:25:58 +00:00
qsort(filearr, files, sizeof(*filearr), cmpstrp);
for (i = 0; i < files; i++) {
2014-02-19 10:20:24 +00:00
load_1(ms, action, filearr[i], &errs, mset);
2012-07-15 10:25:58 +00:00
- free(filearr[i]);
+ efree(filearr[i]);
}
2013-04-07 20:15:56 +00:00
- free(filearr);
+ efree(filearr);
2012-07-15 10:25:58 +00:00
} else
2014-02-19 10:20:24 +00:00
load_1(ms, action, fn, &errs, mset);
2013-04-07 20:15:56 +00:00
if (errs)
2017-10-11 16:18:55 +00:00
@@ -1817,7 +1762,7 @@
if (me->cont_count == me->max_count) {
struct magic *nm;
size_t cnt = me->max_count + ALLOC_CHUNK;
- if ((nm = CAST(struct magic *, realloc(me->mp,
2013-04-07 20:15:56 +00:00
+ if ((nm = CAST(struct magic *, erealloc(me->mp,
sizeof(*nm) * cnt))) == NULL) {
file_oomem(ms, sizeof(*nm) * cnt);
return -1;
2017-10-11 16:18:55 +00:00
@@ -1832,7 +1777,7 @@
2013-04-07 20:15:56 +00:00
static const size_t len = sizeof(*m) * ALLOC_CHUNK;
if (me->mp != NULL)
return 1;
- if ((m = CAST(struct magic *, malloc(len))) == NULL) {
+ if ((m = CAST(struct magic *, emalloc(len))) == NULL) {
file_oomem(ms, len);
return -1;
}
2017-10-11 16:18:55 +00:00
@@ -2036,7 +1981,7 @@
m->mask_op = 0;
if (*l == '~') {
- if (!IS_STRING(m->type))
+ if (!IS_LIBMAGIC_STRING(m->type))
m->mask_op |= FILE_OPINVERSE;
else if (ms->flags & MAGIC_CHECK)
file_magwarn(ms, "'~' invalid for string types");
2017-10-11 16:18:55 +00:00
@@ -2045,7 +1990,7 @@
m->str_range = 0;
m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
if ((op = get_op(*l)) != -1) {
2015-03-05 14:32:04 +00:00
- if (IS_STRING(m->type)) {
+ if (IS_LIBMAGIC_STRING(m->type)) {
int r;
if (op != FILE_OPDIVIDE) {
2017-10-11 16:18:55 +00:00
@@ -2150,11 +2095,6 @@
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 */
2013-04-07 20:15:56 +00:00
return 0;
}
2017-10-11 16:18:55 +00:00
@@ -2226,7 +2166,7 @@
2015-03-08 16:24:44 +00:00
private int
parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
- off_t off, size_t len, const char *name, const char *extra, int nt)
+ zend_off_t off, size_t len, const char *name, const char *extra, int nt)
{
size_t i;
const char *l = line;
2017-10-11 16:18:55 +00:00
@@ -2308,7 +2248,7 @@
struct magic *m = &me->mp[0];
2015-03-29 16:22:42 +00:00
return parse_extra(ms, me, line,
- CAST(off_t, offsetof(struct magic, mimetype)),
2015-03-08 16:24:44 +00:00
+ CAST(zend_off_t, offsetof(struct magic, mimetype)),
sizeof(m->mimetype), "MIME", "+-/.", 1);
}
2017-10-11 16:18:55 +00:00
@@ -2584,14 +2524,18 @@
2015-03-05 14:32:04 +00:00
return -1;
}
2015-03-08 16:24:44 +00:00
if (m->type == FILE_REGEX) {
- file_regex_t rx;
- int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED);
- if (rc) {
- if (ms->flags & MAGIC_CHECK)
- file_regerror(&rx, rc, ms);
+ /* XXX do we need this? */
+ /*zval pattern;
+ int options = 0;
+ pcre_cache_entry *pce;
+
+ 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;
2015-03-05 14:32:04 +00:00
}
2015-03-08 16:24:44 +00:00
- file_regfree(&rx);
- return rc ? -1 : 0;
+
+ return 0;*/
}
2015-03-05 14:32:04 +00:00
return 0;
case FILE_FLOAT:
2017-10-23 17:18:25 +00:00
@@ -2909,7 +2853,7 @@
{
struct magic_map *map;
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
+ if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
file_oomem(ms, sizeof(*map));
return NULL;
}
2017-10-11 16:18:55 +00:00
@@ -2930,79 +2874,145 @@
2013-04-07 20:15:56 +00:00
private struct magic_map *
apprentice_map(struct magic_set *ms, const char *fn)
{
- int fd;
- struct stat st;
2015-03-05 14:32:04 +00:00
+ uint32_t *ptr;
+ uint32_t version, entries, nentries;
+ int needsbyteswap;
char *dbname = NULL;
2013-04-07 20:15:56 +00:00
struct magic_map *map;
2016-11-24 13:58:54 +00:00
- struct magic_map *rv = NULL;
2015-03-05 14:32:04 +00:00
+ size_t i;
+ php_stream *stream = NULL;
+ php_stream_statbuf st;
2017-10-23 17:18:25 +00:00
+
+
2013-04-07 20:15:56 +00:00
- fd = -1;
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
+ if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
file_oomem(ms, sizeof(*map));
2015-03-05 14:32:04 +00:00
- goto error;
+ return NULL;
2015-03-08 16:24:44 +00:00
}
- map->type = MAP_TYPE_USER; /* unspecified */
2016-11-24 13:58:54 +00:00
+
+ if (fn == NULL) {
2013-04-07 20:15:56 +00:00
+ map->p = (void *)&php_magic_database;
+ goto internal_loaded;
2015-03-08 16:24:44 +00:00
+ }
+
+#ifdef PHP_WIN32
+ /* Don't bother on windows with php_stream_open_wrapper,
+ return to give apprentice_load() a chance. */
2013-04-27 12:09:29 +00:00
+ if (php_stream_stat_path_ex((char *)fn, 0, &st, NULL) == SUCCESS) {
+ if (st.sb.st_mode & S_IFDIR) {
2015-03-05 14:32:04 +00:00
+ return NULL;
+ }
+ }
+#endif
2016-11-24 13:58:54 +00:00
dbname = mkdbname(ms, fn, 0);
if (dbname == NULL)
2013-04-07 20:15:56 +00:00
goto error;
- if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
+ stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
+
+ if (!stream) {
2013-04-07 20:15:56 +00:00
goto error;
+ }
- if (fstat(fd, &st) == -1) {
+ if (php_stream_stat(stream, &st) < 0) {
file_error(ms, errno, "cannot stat `%s'", dbname);
2013-04-07 20:15:56 +00:00
goto error;
}
2015-03-05 14:32:04 +00:00
- if (st.st_size < 8 || st.st_size > MAXMAGIC_SIZE) {
+ if (st.sb.st_size < 8 || st.sb.st_size > MAXMAGIC_SIZE) {
file_error(ms, 0, "file `%s' is too %s", dbname,
- st.st_size < 8 ? "small" : "large");
+ st.sb.st_size < 8 ? "small" : "large");
2013-04-07 20:15:56 +00:00
goto error;
}
2013-04-07 20:15:56 +00:00
- map->len = (size_t)st.st_size;
-#ifdef QUICK
2016-11-24 13:58:54 +00:00
- map->type = MAP_TYPE_MMAP;
2013-04-07 20:15:56 +00:00
- if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
- file_error(ms, errno, "cannot map `%s'", dbname);
2013-04-07 20:15:56 +00:00
- goto error;
- }
-#else
2016-11-24 13:58:54 +00:00
- map->type = MAP_TYPE_MALLOC;
2013-04-07 20:15:56 +00:00
- if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
+ map->len = (size_t)st.sb.st_size;
+ if ((map->p = CAST(void *, emalloc(map->len))) == NULL) {
file_oomem(ms, map->len);
goto error;
}
- if (read(fd, map->p, map->len) != (ssize_t)map->len) {
+ if (php_stream_read(stream, map->p, (size_t)st.sb.st_size) != (size_t)st.sb.st_size) {
file_badread(ms);
2013-04-07 20:15:56 +00:00
goto error;
}
2015-03-05 14:32:04 +00:00
+ map->len = 0;
2013-04-07 20:15:56 +00:00
#define RET 1
-#endif
- (void)close(fd);
- fd = -1;
2015-03-05 14:32:04 +00:00
2016-11-24 13:58:54 +00:00
- if (check_buffer(ms, map, dbname) != 0) {
- rv = (struct magic_map *)-1;
+ php_stream_close(stream);
+ stream = NULL;
+
+internal_loaded:
2013-04-07 20:15:56 +00:00
+ ptr = (uint32_t *)(void *)map->p;
2015-03-05 14:32:04 +00:00
+ if (*ptr != MAGICNO) {
+ if (swap4(*ptr) != MAGICNO) {
+ file_error(ms, 0, "bad magic in `%s'", dbname);
+ goto error;
+ }
+ needsbyteswap = 1;
+ } else
+ needsbyteswap = 0;
+ if (needsbyteswap)
+ version = swap4(ptr[1]);
+ else
+ version = ptr[1];
+ if (version != VERSIONNO) {
+ file_error(ms, 0, "File %d.%d supports only version %d magic "
+ "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
2015-03-05 14:32:04 +00:00
+ VERSIONNO, dbname, version);
2016-11-24 13:58:54 +00:00
goto error;
}
-#ifdef QUICK
- if (mprotect(map->p, (size_t)st.st_size, PROT_READ) == -1) {
- file_error(ms, errno, "cannot mprotect `%s'", dbname);
2015-03-08 16:24:44 +00:00
+
+ /* php_magic_database is a const, performing writes will segfault. This is for big-endian
+ machines only, PPC and Sparc specifically. Consider static variable or MINIT in
+ future. */
+ if (needsbyteswap && fn == NULL) {
2013-04-07 20:15:56 +00:00
+ map->p = emalloc(sizeof(php_magic_database));
+ map->p = memcpy(map->p, php_magic_database, sizeof(php_magic_database));
+ }
+
2013-04-07 20:15:56 +00:00
+ if (NULL != fn) {
+ nentries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
+ entries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
2014-10-25 10:06:17 +00:00
+ if ((zend_off_t)(entries * sizeof(struct magic)) != st.sb.st_size) {
2013-04-07 20:15:56 +00:00
+ file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
+ dbname, (unsigned long long)st.sb.st_size,
+ sizeof(struct magic));
+ goto error;
+ }
2015-03-05 14:32:04 +00:00
+ }
+ 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];
+ }
2013-04-07 20:15:56 +00:00
+ if (NULL != fn && entries != nentries + 1) {
2015-03-05 14:32:04 +00:00
+ file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
+ dbname, entries, nentries + 1);
2015-03-08 16:24:44 +00:00
goto error;
2016-11-24 13:58:54 +00:00
}
-#endif
2015-03-05 14:32:04 +00:00
+ if (needsbyteswap)
+ for (i = 0; i < MAGIC_SETS; i++)
+ byteswap(map->magic[i], map->nmagic[i]);
2015-03-08 16:24:44 +00:00
- free(dbname);
+ if (dbname) {
+ efree(dbname);
+ }
2013-04-07 20:15:56 +00:00
return map;
2013-04-07 20:15:56 +00:00
error:
- if (fd != -1)
- (void)close(fd);
+ if (stream) {
+ php_stream_close(stream);
+ }
2013-04-07 20:15:56 +00:00
apprentice_unmap(map);
- free(dbname);
2016-11-24 13:58:54 +00:00
- return rv;
+ if (dbname) {
+ efree(dbname);
+ }
2016-11-24 13:58:54 +00:00
+ return NULL;
}
2016-11-24 13:58:54 +00:00
private int
2017-10-11 16:18:55 +00:00
@@ -3028,7 +3038,7 @@
2015-03-05 14:32:04 +00:00
version = ptr[1];
if (version != VERSIONNO) {
file_error(ms, 0, "File %s supports only version %d magic "
- "files. `%s' is version %d", VERSION,
+ "files. `%s' is version %d", FILE_VERSION_MAJOR,
VERSIONNO, dbname, version);
return -1;
}
2017-10-11 16:18:55 +00:00
@@ -3069,7 +3079,6 @@
2016-01-25 03:45:20 +00:00
{
static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS;
static const size_t m = sizeof(**map->magic);
- int fd = -1;
size_t len;
char *dbname;
int rv = -1;
2017-10-11 16:18:55 +00:00
@@ -3078,14 +3087,17 @@
2015-03-08 16:24:44 +00:00
struct magic m;
uint32_t h[2 + MAGIC_SETS];
} hdr;
+ php_stream *stream;
2014-12-30 19:28:13 +00:00
2016-11-24 13:58:54 +00:00
dbname = mkdbname(ms, fn, 1);
2016-11-24 13:58:54 +00:00
if (dbname == NULL)
goto out;
2013-04-07 20:15:56 +00:00
- if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
- {
2014-02-19 10:20:24 +00:00
+ /* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */
+ stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS, NULL);
+
+ if (!stream) {
file_error(ms, errno, "cannot open `%s'", dbname);
goto out;
}
2017-10-11 16:18:55 +00:00
@@ -3094,25 +3106,25 @@
2015-03-08 16:24:44 +00:00
hdr.h[1] = VERSIONNO;
memcpy(hdr.h + 2, map->nmagic, nm);
2015-03-05 14:32:04 +00:00
- if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
2015-03-08 16:24:44 +00:00
+ if (php_stream_write(stream,(const char *)&hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
file_error(ms, errno, "error writing `%s'", dbname);
goto out;
}
2013-04-07 20:15:56 +00:00
for (i = 0; i < MAGIC_SETS; i++) {
len = m * map->nmagic[i];
- if (write(fd, map->magic[i], len) != (ssize_t)len) {
2013-04-27 12:09:29 +00:00
+ if (php_stream_write(stream, (const char *)map->magic[i], len) != (ssize_t)len) {
2013-04-07 20:15:56 +00:00
file_error(ms, errno, "error writing `%s'", dbname);
goto out;
}
}
- if (fd != -1)
- (void)close(fd);
2013-04-07 20:15:56 +00:00
+ if (stream) {
+ php_stream_close(stream);
+ }
rv = 0;
out:
2016-11-24 13:58:54 +00:00
- apprentice_unmap(map);
- free(dbname);
+ efree(dbname);
return rv;
}
2017-10-11 16:18:55 +00:00
@@ -3146,16 +3158,18 @@
q++;
/* Compatibility with old code that looked in .mime */
if (ms->flags & MAGIC_MIME) {
2013-04-07 20:15:56 +00:00
- if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
- return NULL;
- if (access(buf, R_OK) != -1) {
+ spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", (int)(q - fn), fn, ext);
+#ifdef PHP_WIN32
+ if (VCWD_ACCESS(buf, R_OK) == 0) {
+#else
+ if (VCWD_ACCESS(buf, R_OK) != -1) {
+#endif
ms->flags &= MAGIC_MIME_TYPE;
return buf;
}
- free(buf);
+ efree(buf);
}
2013-04-07 20:15:56 +00:00
- if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
- return NULL;
+ spprintf(&buf, MAXPATHLEN, "%.*s%s", (int)(q - fn), fn, ext);
/* Compatibility with old code that looked in .mime */
2016-11-24 13:58:54 +00:00
if (strstr(fn, ".mime") != NULL)
2017-10-11 16:18:55 +00:00
@@ -3245,7 +3259,7 @@
m->offset = swap4((uint32_t)m->offset);
m->in_offset = swap4((uint32_t)m->in_offset);
m->lineno = swap4((uint32_t)m->lineno);
- if (IS_STRING(m->type)) {
+ if (IS_LIBMAGIC_STRING(m->type)) {
m->str_range = swap4(m->str_range);
m->str_flags = swap4(m->str_flags);
}
diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/ascmagic.c 2016-06-27 22:56:25.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/ascmagic.c 2017-10-18 12:52:13.745336900 +0200
2016-11-24 13:58:54 +00:00
@@ -133,7 +133,7 @@
/* malloc size is a conservative overestimate; could be
improved, or at least realloced after conversion. */
mlen = ulen * 6;
- if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
2013-04-07 20:15:56 +00:00
+ if ((utf8_buf = CAST(unsigned char *, emalloc(mlen))) == NULL) {
file_oomem(ms, mlen);
goto done;
2013-04-07 20:15:56 +00:00
}
2015-03-05 14:32:04 +00:00
@@ -298,7 +298,8 @@
}
rv = 1;
done:
- free(utf8_buf);
+ if (utf8_buf)
+ efree(utf8_buf);
return rv;
}
diff -u libmagic.orig/cdf.c libmagic/cdf.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/cdf.c 2017-05-08 20:10:13.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/cdf.c 2017-10-18 14:05:31.846245300 +0200
@@ -43,7 +43,17 @@
#include <err.h>
#endif
#include <stdlib.h>
+
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
#include <unistd.h>
+#endif
+
+#ifndef UINT32_MAX
+# define UINT32_MAX (0xffffffff)
+#endif
+
#include <string.h>
#include <time.h>
#include <ctype.h>
2017-10-11 16:18:55 +00:00
@@ -86,24 +96,21 @@
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 *
-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 *
-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 @@
2014-07-01 08:28:43 +00:00
}
2014-10-25 10:06:17 +00:00
static ssize_t
-cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
+cdf_read(const cdf_info_t *info, zend_off_t off, void *buf, size_t len)
{
size_t siz = (size_t)off + len;
2016-11-24 13:58:54 +00:00
- if ((off_t)(off + len) != (off_t)siz)
2014-10-25 10:06:17 +00:00
+ if ((zend_off_t)(off + len) != (zend_off_t)siz) {
2016-11-24 13:58:54 +00:00
goto out;
+ }
if (info->i_buf != NULL && info->i_len >= siz) {
(void)memcpy(buf, &info->i_buf[off], len);
2017-10-11 16:18:55 +00:00
@@ -348,7 +356,10 @@
2012-04-02 15:27:23 +00:00
if (info->i_fd == -1)
2016-11-24 13:58:54 +00:00
goto out;
2012-04-02 15:27:23 +00:00
2013-04-07 20:15:56 +00:00
- if (pread(info->i_fd, buf, len, off) != (ssize_t)len)
2014-10-25 10:06:17 +00:00
+ if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (zend_off_t)-1)
2013-04-07 20:15:56 +00:00
+ return -1;
+
2012-04-02 15:27:23 +00:00
+ if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len)
return -1;
return (ssize_t)len;
2017-10-11 16:18:55 +00:00
@@ -363,7 +374,7 @@
2014-10-25 10:06:17 +00:00
char buf[512];
(void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
- if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
+ if (cdf_read(info, (zend_off_t)0, buf, sizeof(buf)) == -1)
return -1;
cdf_unpack_header(h, buf);
cdf_swap_header(h);
2017-10-11 16:18:55 +00:00
@@ -397,7 +408,7 @@
2014-10-25 10:06:17 +00:00
size_t ss = CDF_SEC_SIZE(h);
size_t pos = CDF_SEC_POS(h, id);
assert(ss == len);
- return cdf_read(info, (off_t)pos, ((char *)buf) + offs, len);
+ return cdf_read(info, (zend_off_t)pos, ((char *)buf) + offs, len);
}
ssize_t
2017-10-11 16:18:55 +00:00
@@ -1380,7 +1391,7 @@
cdf_directory_t *d;
char name[__arraycount(d->d_name)];
cdf_stream_t scn;
- struct timespec ts;
+ struct timeval ts;
static const char *types[] = { "empty", "user storage",
"user stream", "lockbytes", "property", "root storage" };
2017-10-11 16:18:55 +00:00
@@ -1435,7 +1446,7 @@
cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
{
cdf_timestamp_t tp;
- struct timespec ts;
+ struct timeval ts;
char buf[64];
size_t i, j;
2017-10-11 16:18:55 +00:00
@@ -1557,10 +1568,7 @@
2015-03-29 16:22:42 +00:00
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;
diff -u libmagic.orig/cdf.h libmagic/cdf.h
2017-10-11 16:18:55 +00:00
--- libmagic.orig/cdf.h 2017-03-16 16:06:24.000000000 +0100
2017-10-23 17:18:25 +00:00
+++ libmagic/cdf.h 2017-10-18 14:05:31.846245300 +0200
2013-04-07 20:15:56 +00:00
@@ -35,10 +35,12 @@
#ifndef _H_CDF_
#define _H_CDF_
-#ifdef WIN32
+#ifdef PHP_WIN32
#include <winsock2.h>
#define timespec timeval
#define tv_nsec tv_usec
2013-04-07 20:15:56 +00:00
+#define asctime_r php_asctime_r
+#define ctime_r php_ctime_r
#endif
#ifdef __DJGPP__
#define timespec timeval
2016-11-24 13:58:54 +00:00
@@ -281,9 +283,9 @@
cdf_catalog_entry_t cat_e[1];
2015-03-05 14:32:04 +00:00
} cdf_catalog_t;
-struct timespec;
-int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
-int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
+struct timeval;
+int cdf_timestamp_to_timespec(struct timeval *, cdf_timestamp_t);
+int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timeval *);
int cdf_read_header(const cdf_info_t *, cdf_header_t *);
void cdf_swap_header(cdf_header_t *);
void cdf_unpack_header(cdf_header_t *, char *);
diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/cdf_time.c 2017-03-29 17:57:48.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/cdf_time.c 2017-10-18 14:05:31.846245300 +0200
@@ -96,7 +96,7 @@
}
int
-cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
+cdf_timestamp_to_timespec(struct timeval *ts, cdf_timestamp_t t)
{
struct tm tm;
#ifdef HAVE_STRUCT_TM_TM_ZONE
2013-04-07 20:15:56 +00:00
@@ -104,8 +104,9 @@
#endif
int rdays;
- /* Unit is 100's of nanoseconds */
- ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
2013-04-07 20:15:56 +00:00
+ /* XXX 5.14 at least introdced 100 ns intervals, this is to do */
+ /* Time interval, in microseconds */
+ ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC;
t /= CDF_TIME_PREC;
tm.tm_sec = (int)(t % 60);
2013-04-07 20:15:56 +00:00
@@ -144,7 +145,7 @@
int
/*ARGSUSED*/
-cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
+cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timeval *ts)
{
#ifndef __lint__
(void)&t;
2013-04-07 20:15:56 +00:00
@@ -156,7 +157,7 @@
errno = EINVAL;
return -1;
}
- *t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
+ *t = (ts->ts_usec / CDF_TIME_PREC) * CDF_TIME_PREC;
*t = tm.tm_sec;
*t += tm.tm_min * 60;
*t += tm.tm_hour * 60 * 60;
2015-03-05 14:32:04 +00:00
@@ -181,7 +182,7 @@
int
main(int argc, char *argv[])
{
- struct timespec ts;
+ struct timeval ts;
2013-04-07 20:15:56 +00:00
char buf[25];
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
2017-10-11 16:18:55 +00:00
--- libmagic.orig/compress.c 2017-03-29 17:57:48.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/compress.c 2017-10-18 14:05:31.861865200 +0200
2016-11-24 13:58:54 +00:00
@@ -45,15 +45,13 @@
2015-03-29 16:22:42 +00:00
#endif
2016-11-24 13:58:54 +00:00
#include <string.h>
#include <errno.h>
-#include <ctype.h>
-#include <stdarg.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
# ifndef HAVE_SIG_T
2015-03-29 16:22:42 +00:00
typedef void (*sig_t)(int);
# endif /* HAVE_SIG_T */
#endif
2015-03-05 14:32:04 +00:00
-#if !defined(__MINGW32__) && !defined(WIN32)
+#ifndef PHP_WIN32
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_WAIT_H
2017-10-11 16:18:55 +00:00
@@ -62,51 +60,12 @@
2017-01-05 22:32:30 +00:00
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
2017-10-11 16:18:55 +00:00
-#if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT)
2017-01-05 22:32:30 +00:00
+#if defined(HAVE_ZLIB_H) && defined(PHP_FILEINFO_UNCOMPRESS)
2016-11-24 13:58:54 +00:00
#define BUILTIN_DECOMPRESS
#include <zlib.h>
#endif
2016-11-24 13:58:54 +00:00
-#ifdef DEBUG
-int tty = -1;
-#define DPRINTF(...) do { \
- if (tty == -1) \
- tty = open("/dev/tty", O_RDWR); \
- if (tty == -1) \
- abort(); \
- dprintf(tty, __VA_ARGS__); \
-} while (/*CONSTCOND*/0)
-#else
-#define DPRINTF(...)
-#endif
2016-11-24 13:58:54 +00:00
-#ifdef ZLIBSUPPORT
-/*
- * The following python code is not really used because ZLIBSUPPORT is only
- * defined if we have a built-in zlib, and the built-in zlib handles that.
2017-10-11 16:18:55 +00:00
- * That is not true for android where we have zlib.h and not -lz.
2016-11-24 13:58:54 +00:00
- */
-static const char zlibcode[] =
- "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)
-{
- unsigned short x = 1;
2017-10-11 16:18:55 +00:00
- unsigned char *s = CAST(unsigned char *, CAST(void *, &x));
2016-11-24 13:58:54 +00:00
-
- if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
- return 0;
- if (s[0] != 1) /* endianness test */
- x = buf[0] | (buf[1] << 8);
- else
- x = buf[1] | (buf[0] << 8);
- if (x % 31)
- return 0;
- return 1;
-}
-#endif
+#undef FIONREAD
2016-11-24 13:58:54 +00:00
#define gzip_flags "-cd"
#define lrzip_flags "-do"
2017-10-11 16:18:55 +00:00
@@ -169,7 +128,7 @@
2016-11-24 13:58:54 +00:00
#define ERRDATA 2
private ssize_t swrite(int, const void *, size_t);
-#if HAVE_FORK
+#ifdef PHP_FILEINFO_UNCOMPRESS
2016-11-24 13:58:54 +00:00
private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
private int uncompressbuf(int, size_t, size_t, const unsigned char *,
unsigned char **, size_t *);
2017-10-11 16:18:55 +00:00
@@ -179,8 +138,7 @@
2016-11-24 13:58:54 +00:00
private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
size_t *);
#endif
-static int makeerror(unsigned char **, size_t *, const char *, ...)
- __attribute__((__format__(__printf__, 3, 4)));
+static int makeerror(unsigned char **, size_t *, const char *, ...);
private const char *methodname(size_t);
protected int
2017-10-11 16:18:55 +00:00
@@ -275,7 +233,8 @@
2015-03-29 16:22:42 +00:00
#ifdef HAVE_SIGNAL_H
2015-03-05 14:32:04 +00:00
(void)signal(SIGPIPE, osigpipe);
2015-03-29 16:22:42 +00:00
#endif
- free(newbuf);
+ if (newbuf)
+ efree(newbuf);
ms->flags |= MAGIC_COMPRESS;
2016-11-24 13:58:54 +00:00
DPRINTF("Zmagic returns %d\n", rv);
return rv;
2017-10-11 16:18:55 +00:00
@@ -310,7 +269,7 @@
* `safe' read for sockets and pipes.
*/
protected ssize_t
2013-04-07 20:15:56 +00:00
-sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
+sread(int fd, void *buf, size_t n, int canbepipe)
{
ssize_t rv;
#ifdef FIONREAD
2017-10-11 16:18:55 +00:00
@@ -358,7 +317,7 @@
2012-04-02 15:27:23 +00:00
nocheck:
do
- switch ((rv = read(fd, buf, n))) {
+ switch ((rv = FINFO_READ_FUNC(fd, buf, n))) {
case -1:
if (errno == EINTR)
continue;
2017-10-11 16:18:55 +00:00
@@ -435,13 +394,14 @@
2012-04-02 15:27:23 +00:00
return -1;
}
(void)close(tfd);
- if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
2014-10-25 10:06:17 +00:00
+ if (FINFO_LSEEK_FUNC(fd, (zend_off_t)0, SEEK_SET) == (zend_off_t)-1) {
2012-04-02 15:27:23 +00:00
file_badseek(ms);
return -1;
}
return fd;
}
-#if HAVE_FORK
+
+#ifdef PHP_FILEINFO_UNCOMPRESS
#ifdef BUILTIN_DECOMPRESS
#define FHCRC (1 << 1)
2017-10-11 16:18:55 +00:00
@@ -736,27 +696,5 @@
2016-11-24 13:58:54 +00:00
rv = makeerror(newch, n, "No data");
goto err;
}
2016-11-24 13:58:54 +00:00
-
- *n = r;
- /* NUL terminate, as every buffer is handled here. */
- (*newch)[*n] = '\0';
-err:
- closefd(fdp[STDIN_FILENO], 1);
- closefd(fdp[STDOUT_FILENO], 0);
- closefd(fdp[STDERR_FILENO], 0);
- if (wait(&status) == -1) {
- free(*newch);
- rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
- DPRINTF("Child wait return %#x\n", status);
- } else if (!WIFEXITED(status)) {
2017-10-11 16:18:55 +00:00
- DPRINTF("Child not exited (%#x)\n", status);
2016-11-24 13:58:54 +00:00
- } else if (WEXITSTATUS(status) != 0) {
2017-10-11 16:18:55 +00:00
- DPRINTF("Child exited (%#u)\n", WEXITSTATUS(status));
2016-11-24 13:58:54 +00:00
- }
-
- closefd(fdp[STDIN_FILENO], 0);
- DPRINTF("Returning %p n=%zu rv=%d\n", *newch, *n, rv);
-
- return rv;
}
-#endif
+#endif /* if PHP_FILEINFO_UNCOMPRESS */
2016-11-24 13:58:54 +00:00
diff -u libmagic.orig/der.c libmagic/der.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/der.c 2017-03-07 23:20:58.000000000 +0100
2017-10-23 17:18:25 +00:00
+++ libmagic/der.c 2017-10-18 14:05:31.861865200 +0200
2016-11-24 13:58:54 +00:00
@@ -51,7 +51,9 @@
#include "magic.h"
#include "der.h"
#else
+#ifndef PHP_WIN32
#include <sys/mman.h>
+#endif
#include <sys/stat.h>
#include <err.h>
#endif
2017-10-11 16:18:55 +00:00
@@ -219,6 +221,7 @@
2016-11-24 13:58:54 +00:00
der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
{
const uint8_t *d = CAST(const uint8_t *, q);
+ uint32_t i;
switch (tag) {
case DER_TAG_PRINTABLE_STRING:
case DER_TAG_UTF8_STRING:
2017-10-11 16:18:55 +00:00
@@ -229,7 +232,7 @@
2016-11-24 13:58:54 +00:00
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]);
2014-10-25 10:06:17 +00:00
diff -u libmagic.orig/elfclass.h libmagic/elfclass.h
2017-10-11 16:18:55 +00:00
--- libmagic.orig/elfclass.h 2014-12-16 23:23:50.000000000 +0100
+++ libmagic/elfclass.h 2017-10-11 15:25:46.389495700 +0200
2015-03-05 14:32:04 +00:00
@@ -41,7 +41,7 @@
return toomany(ms, "program headers", phnum);
2014-10-25 10:06:17 +00:00
flags |= FLAGS_IS_CORE;
if (dophn_core(ms, clazz, swap, fd,
2015-03-05 14:32:04 +00:00
- (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
+ (zend_off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
2014-10-25 10:06:17 +00:00
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
2015-03-05 14:32:04 +00:00
fsize, &flags, &notecount) == -1)
return -1;
@@ -56,7 +56,7 @@
if (shnum > ms->elf_shnum_max)
return toomany(ms, "section", shnum);
2014-10-25 10:06:17 +00:00
if (dophn_exec(ms, clazz, swap, fd,
2015-03-05 14:32:04 +00:00
- (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
+ (zend_off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
2014-10-25 10:06:17 +00:00
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
2015-03-05 14:32:04 +00:00
fsize, shnum, &flags, &notecount) == -1)
return -1;
@@ -66,7 +66,7 @@
if (shnum > ms->elf_shnum_max)
return toomany(ms, "section headers", shnum);
2014-10-25 10:06:17 +00:00
if (doshn(ms, clazz, swap, fd,
2015-03-05 14:32:04 +00:00
- (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
+ (zend_off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
2014-10-25 10:06:17 +00:00
(size_t)elf_getu16(swap, elfhdr.e_shentsize),
2015-03-05 14:32:04 +00:00
fsize, elf_getu16(swap, elfhdr.e_machine),
(int)elf_getu16(swap, elfhdr.e_shstrndx),
diff -u libmagic.orig/file.h libmagic/file.h
2017-10-11 16:18:55 +00:00
--- libmagic.orig/file.h 2017-05-08 20:10:13.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/file.h 2017-10-18 14:05:31.861865200 +0200
2017-10-11 16:18:55 +00:00
@@ -33,15 +33,9 @@
#ifndef __file_h__
#define __file_h__
-#ifdef HAVE_CONFIG_H
2017-10-23 17:18:25 +00:00
-#include <config.h>
2017-10-11 16:18:55 +00:00
-#endif
-#ifdef HAVE_STDINT_H
-#ifndef __STDC_LIMIT_MACROS
-#define __STDC_LIMIT_MACROS
-#endif
2017-10-23 17:18:25 +00:00
+#include "config.h"
-#ifdef WIN32
+#ifdef PHP_WIN32
#ifdef _WIN64
#define SIZE_T_FORMAT "I64"
#else
2017-10-11 16:18:55 +00:00
@@ -54,19 +48,31 @@
#define INT64_T_FORMAT "ll"
#define INTMAX_T_FORMAT "j"
#endif
-#include <stdint.h>
-#endif
#include <stdio.h> /* Include that here, to make sure __P gets defined */
#include <errno.h>
#include <fcntl.h> /* For open and flags */
+#ifdef HAVE_STDINT_H
+#ifndef __STDC_LIMIT_MACROS
+#define __STDC_LIMIT_MACROS
+#endif
+#include <stdint.h>
+#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
-#include <regex.h>
2013-04-07 20:15:56 +00:00
-#include <time.h>
+#ifdef PHP_WIN32
+#include "win32/php_stdint.h"
+#endif
+
+#include "php.h"
+#include "ext/standard/php_string.h"
+#include "ext/pcre/php_pcre.h"
+
#include <sys/types.h>
2015-03-05 14:32:04 +00:00
-#ifndef WIN32
+#ifdef PHP_WIN32
+#include "win32/param.h"
+#else
#include <sys/param.h>
2015-03-05 14:32:04 +00:00
#endif
/* Do this here and now, because struct stat gets re-defined on solaris */
2016-11-24 13:58:54 +00:00
@@ -79,7 +85,7 @@
#define MAGIC "/etc/magic"
#endif
-#if defined(__EMX__) || defined (WIN32)
+#if defined(__EMX__) || defined(PHP_WIN32)
#define PATHSEP ';'
#else
#define PATHSEP ':'
2016-11-24 13:58:54 +00:00
@@ -113,12 +119,6 @@
#endif
#endif
-#ifndef __GNUC__
-#ifndef __attribute__
-#define __attribute__(a)
-#endif
-#endif
-
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
2016-11-24 13:58:54 +00:00
@@ -230,7 +230,7 @@
#define FILE_DER 48
#define FILE_NAMES_SIZE 49 /* size of array to contain all names */
-#define IS_STRING(t) \
+#define IS_LIBMAGIC_STRING(t) \
((t) == FILE_STRING || \
(t) == FILE_PSTRING || \
(t) == FILE_BESTRING16 || \
2016-11-24 13:58:54 +00:00
@@ -431,28 +431,22 @@
2014-10-25 10:06:17 +00:00
/* Type for Unicode characters */
typedef unsigned long unichar;
-struct stat;
#define FILE_T_LOCAL 1
#define FILE_T_WINDOWS 2
2013-04-07 20:15:56 +00:00
protected const char *file_fmttime(uint64_t, int, char *);
protected struct magic_set *file_ms_alloc(int);
protected void file_ms_free(struct magic_set *);
-protected int file_buffer(struct magic_set *, int, const char *, const void *,
+protected int file_buffer(struct magic_set *, php_stream *, const char *, const void *,
size_t);
-protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
2014-10-25 10:06:17 +00:00
+protected int file_fsmagic(struct magic_set *, const char *, zend_stat_t *, php_stream *);
protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
2014-02-19 10:20:24 +00:00
-protected int file_vprintf(struct magic_set *, const char *, va_list)
- __attribute__((__format__(__printf__, 2, 0)));
2015-03-08 16:24:44 +00:00
protected size_t file_printedlen(const struct magic_set *);
protected int file_replace(struct magic_set *, const char *, const char *);
-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 *);
2014-12-30 19:28:13 +00:00
-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);
-#if HAVE_FORK
2014-02-19 10:20:24 +00:00
+#ifdef PHP_FILEINFO_UNCOMPRESS
protected int file_zmagic(struct magic_set *, int, const char *,
const unsigned char *, size_t);
#endif
2016-11-24 13:58:54 +00:00
@@ -472,16 +466,13 @@
2013-04-07 20:15:56 +00:00
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);
protected void file_badread(struct magic_set *);
protected void file_badseek(struct magic_set *);
protected void file_oomem(struct magic_set *, size_t);
-protected void file_error(struct magic_set *, int, const char *, ...)
- __attribute__((__format__(__printf__, 3, 4)));
-protected void file_magerror(struct magic_set *, const char *, ...)
- __attribute__((__format__(__printf__, 2, 3)));
-protected void file_magwarn(struct magic_set *, const char *, ...)
- __attribute__((__format__(__printf__, 2, 3)));
-protected void file_mdump(struct magic *);
+protected void file_error(struct magic_set *, int, const char *, ...);
+protected void file_magerror(struct magic_set *, const char *, ...);
+protected void file_magwarn(struct magic_set *, const char *, ...);
protected void file_showstr(FILE *, const char *, size_t);
protected size_t file_mbswidth(const char *);
protected const char *file_getbuffer(struct magic_set *);
2016-11-24 13:58:54 +00:00
@@ -497,32 +488,6 @@
size_t);
#endif /* __EMX__ */
2015-03-05 14:32:04 +00:00
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
-#endif
-#if defined(HAVE_XLOCALE_H)
-#include <xlocale.h>
-#endif
-
-typedef struct {
- const char *pat;
-#if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
-#define USE_C_LOCALE
- locale_t old_lc_ctype;
- locale_t c_lc_ctype;
2016-11-24 13:58:54 +00:00
-#else
- char *old_lc_ctype;
2015-03-05 14:32:04 +00:00
-#endif
- int rc;
- regex_t rx;
-} file_regex_t;
-
-protected int file_regcomp(file_regex_t *, const char *, int);
-protected int file_regexec(file_regex_t *, const char *, size_t, regmatch_t *,
- int);
-protected void file_regfree(file_regex_t *);
-protected void file_regerror(file_regex_t *, int, struct magic_set *);
-
2015-03-05 14:32:04 +00:00
typedef struct {
char *buf;
uint32_t offset;
2016-11-24 13:58:54 +00:00
@@ -531,10 +496,8 @@
2015-03-05 14:32:04 +00:00
protected file_pushbuf_t *file_push_buffer(struct magic_set *);
protected char *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
-#ifndef COMPILE_ONLY
extern const char *file_names[];
extern const size_t file_nnames;
-#endif
#ifndef HAVE_STRERROR
extern int sys_nerr;
2016-11-24 13:58:54 +00:00
@@ -547,23 +510,10 @@
#define strtoul(a, b, c) strtol(a, b, c)
#endif
2013-04-07 20:15:56 +00:00
-#ifndef HAVE_PREAD
-ssize_t pread(int, void *, size_t, off_t);
-#endif
-#ifndef HAVE_VASPRINTF
-int vasprintf(char **, const char *, va_list);
-#endif
-#ifndef HAVE_ASPRINTF
2014-02-19 10:20:24 +00:00
-int asprintf(char **, const char *, ...);
-#endif
2016-11-24 13:58:54 +00:00
-#ifndef HAVE_DPRINTF
-int dprintf(int, const char *, ...);
-#endif
-
-#ifndef HAVE_STRLCPY
+#ifndef strlcpy
2014-02-19 10:20:24 +00:00
size_t strlcpy(char *, const char *, size_t);
#endif
-#ifndef HAVE_STRLCAT
+#ifndef strlcat
2014-02-19 10:20:24 +00:00
size_t strlcat(char *, const char *, size_t);
#endif
2014-02-19 10:20:24 +00:00
#ifndef HAVE_STRCASESTR
2016-11-24 13:58:54 +00:00
@@ -579,16 +529,6 @@
2015-03-05 14:32:04 +00:00
#ifndef HAVE_ASCTIME_R
char *asctime_r(const struct tm *, char *);
2013-04-07 20:15:56 +00:00
#endif
2015-03-05 14:32:04 +00:00
-#ifndef HAVE_GMTIME_R
-struct tm *gmtime_r(const time_t *, struct tm *);
-#endif
-#ifndef HAVE_LOCALTIME_R
-struct tm *localtime_r(const time_t *, struct tm *);
-#endif
-#ifndef HAVE_FMTCHECK
-const char *fmtcheck(const char *, const char *)
- __attribute__((__format_arg__(2)));
-#endif
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
#define QUICK
2017-10-11 16:18:55 +00:00
@@ -611,6 +551,18 @@
2014-02-18 18:08:16 +00:00
#else
#define FILE_RCSID(id)
2016-01-25 03:45:20 +00:00
#endif
2014-02-18 18:08:16 +00:00
+
2012-04-02 15:27:23 +00:00
+#ifdef PHP_WIN32
2017-10-11 16:18:55 +00:00
+#ifdef _WIN64
+#define FINFO_LSEEK_FUNC _lseeki64
+#else
2012-04-02 15:27:23 +00:00
+#define FINFO_LSEEK_FUNC _lseek
2017-10-11 16:18:55 +00:00
+#endif
2012-04-02 15:27:23 +00:00
+#define FINFO_READ_FUNC _read
+#else
+#define FINFO_LSEEK_FUNC lseek
+#define FINFO_READ_FUNC read
2016-01-25 03:45:20 +00:00
+#endif
2015-03-05 14:32:04 +00:00
#ifndef __RCSID
#define __RCSID(a)
2016-01-25 03:45:20 +00:00
#endif
diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/fsmagic.c 2016-05-03 17:48:37.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/fsmagic.c 2017-10-18 12:52:13.745336900 +0200
2015-03-05 14:32:04 +00:00
@@ -63,27 +63,21 @@
# define minor(dev) ((dev) & 0xff)
#endif
#undef HAVE_MAJOR
-#ifdef S_IFLNK
-private int
-bad_link(struct magic_set *ms, int err, char *buf)
-{
- int mime = ms->flags & MAGIC_MIME;
- if ((mime & MAGIC_MIME_TYPE) &&
- file_printf(ms, "inode/symlink")
- == -1)
- return -1;
- else if (!mime) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, err,
2015-03-05 14:32:04 +00:00
- "broken symbolic link to %s", buf);
- return -1;
- }
2015-03-05 14:32:04 +00:00
- if (file_printf(ms, "broken symbolic link to %s", buf) == -1)
- return -1;
- }
- return 1;
-}
+
+#ifdef PHP_WIN32
+
+# undef S_IFIFO
2014-02-19 10:20:24 +00:00
#endif
+
+
+#ifndef S_ISDIR
+#define S_ISDIR(mode) ((mode) & _S_IFDIR)
+#endif
+
+#ifndef S_ISREG
+#define S_ISREG(mode) ((mode) & _S_IFREG)
2014-02-19 10:20:24 +00:00
+#endif
+
private int
handle_mime(struct magic_set *ms, int mime, const char *str)
{
2016-11-24 13:58:54 +00:00
@@ -100,71 +94,38 @@
}
protected int
-file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
2014-10-25 10:06:17 +00:00
+file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb, php_stream *stream)
{
2013-04-07 20:15:56 +00:00
int ret, did = 0;
int mime = ms->flags & MAGIC_MIME;
-#ifdef S_IFLNK
- char buf[BUFSIZ+4];
- ssize_t nch;
- struct stat tstatbuf;
-#endif
2014-12-30 19:28:13 +00:00
2016-11-24 13:58:54 +00:00
if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
return 0;
- if (fn == NULL)
+
2013-04-07 20:15:56 +00:00
+ if (fn == NULL && !stream) {
return 0;
+ }
2013-04-07 20:15:56 +00:00
#define COMMA (did++ ? ", " : "")
- /*
- * Fstat is cheaper but fails for files you don't have read perms on.
- * On 4.2BSD and similar systems, use lstat() to identify symlinks.
- */
-#ifdef S_IFLNK
- if ((ms->flags & MAGIC_SYMLINK) == 0)
- ret = lstat(fn, sb);
- else
-#endif
- ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
-
2015-03-05 14:32:04 +00:00
-#ifdef WIN32
- {
- HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE |
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
- NULL);
- if (hFile != INVALID_HANDLE_VALUE) {
- /*
- * Stat failed, but we can still open it - assume it's
- * a block device, if nothing else.
- */
- if (ret) {
- sb->st_mode = S_IFBLK;
- ret = 0;
2016-11-24 13:58:54 +00:00
- }
2015-03-05 14:32:04 +00:00
- switch (GetFileType(hFile)) {
- case FILE_TYPE_CHAR:
- sb->st_mode |= S_IFCHR;
- sb->st_mode &= ~S_IFREG;
- break;
- case FILE_TYPE_PIPE:
- sb->st_mode |= S_IFIFO;
- sb->st_mode &= ~S_IFREG;
- break;
2016-11-24 13:58:54 +00:00
+
+ if (stream) {
+ php_stream_statbuf ssb;
+ if (php_stream_stat(stream, &ssb) < 0) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno, "cannot stat `%s'", fn);
+ return -1;
2015-03-05 14:32:04 +00:00
}
- CloseHandle(hFile);
2014-02-19 10:20:24 +00:00
+ return 0;
}
2016-11-24 13:58:54 +00:00
- }
2015-03-05 14:32:04 +00:00
-#endif
-
- if (ret) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, errno, "cannot stat `%s'", fn);
- return -1;
2016-11-24 13:58:54 +00:00
+ memcpy(sb, &ssb.sb, sizeof(struct stat));
+ } else {
+ if (php_sys_stat(fn, sb) != 0) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno, "cannot stat `%s'", fn);
+ return -1;
+ }
+ return 0;
}
- if (file_printf(ms, "cannot open `%s' (%s)",
- fn, strerror(errno)) == -1)
- return -1;
2014-02-19 10:20:24 +00:00
- return 0;
2016-11-24 13:58:54 +00:00
}
2013-04-07 20:15:56 +00:00
ret = 1;
2015-03-05 14:32:04 +00:00
@@ -187,30 +148,24 @@
}
switch (sb->st_mode & S_IFMT) {
- case S_IFDIR:
- if (mime) {
- if (handle_mime(ms, mime, "directory") == -1)
- return -1;
2013-04-07 20:15:56 +00:00
- } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
- return -1;
2013-04-07 20:15:56 +00:00
- break;
-#ifdef S_IFCHR
- case S_IFCHR:
- /*
- * If -s has been specified, treat character special files
- * like ordinary files. Otherwise, just report that they
- * are block special files and go on to the next file.
- */
2013-04-07 20:15:56 +00:00
- if ((ms->flags & MAGIC_DEVICES) != 0) {
- ret = 0;
- break;
2013-04-07 20:15:56 +00:00
- }
- if (mime) {
- if (handle_mime(ms, mime, "chardevice") == -1)
- return -1;
- } else {
2015-03-05 14:32:04 +00:00
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
2013-04-07 20:15:56 +00:00
+#ifndef PHP_WIN32
+# ifdef S_IFCHR
+ case S_IFCHR:
2015-03-05 14:32:04 +00:00
+ /*
2013-04-07 20:15:56 +00:00
+ * If -s has been specified, treat character special files
+ * like ordinary files. Otherwise, just report that they
+ * are block special files and go on to the next file.
+ */
+ if ((ms->flags & MAGIC_DEVICES) != 0) {
+ ret = 0;
+ break;
+ }
+ if (mime) {
2014-02-19 10:20:24 +00:00
+ if (handle_mime(ms, mime, "chardevice") == -1)
2013-04-07 20:15:56 +00:00
+ return -1;
+ } else {
+# ifdef HAVE_STAT_ST_RDEV
+# ifdef dv_unit
if (file_printf(ms, "%scharacter special (%d/%d/%d)",
COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
2014-02-19 10:20:24 +00:00
dv_subunit(sb->st_rdev)) == -1)
2015-03-05 14:32:04 +00:00
@@ -225,44 +180,11 @@
2013-04-07 20:15:56 +00:00
if (file_printf(ms, "%scharacter special", COMMA) == -1)
2014-02-19 10:20:24 +00:00
return -1;
#endif
- }
2013-04-07 20:15:56 +00:00
- break;
-#endif
-#ifdef S_IFBLK
- case S_IFBLK:
- /*
- * If -s has been specified, treat block special files
- * like ordinary files. Otherwise, just report that they
- * are block special files and go on to the next file.
- */
2013-04-07 20:15:56 +00:00
- if ((ms->flags & MAGIC_DEVICES) != 0) {
- ret = 0;
- break;
2013-04-07 20:15:56 +00:00
- }
- if (mime) {
- if (handle_mime(ms, mime, "blockdevice") == -1)
- return -1;
- } else {
2015-03-05 14:32:04 +00:00
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
2013-04-07 20:15:56 +00:00
- if (file_printf(ms, "%sblock special (%d/%d/%d)",
- COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
- dv_subunit(sb->st_rdev)) == -1)
- return -1;
-# else
2013-04-07 20:15:56 +00:00
- if (file_printf(ms, "%sblock special (%ld/%ld)",
- COMMA, (long)major(sb->st_rdev),
- (long)minor(sb->st_rdev)) == -1)
- return -1;
2014-02-19 10:20:24 +00:00
+ }
+ return 1;
# endif
-#else
2013-04-07 20:15:56 +00:00
- if (file_printf(ms, "%sblock special", COMMA) == -1)
- return -1;
2016-11-24 13:58:54 +00:00
-#endif
- }
2013-04-07 20:15:56 +00:00
- break;
2016-11-24 13:58:54 +00:00
#endif
- /* TODO add code to handle V7 MUX and Blit MUX files */
+
#ifdef S_IFIFO
case S_IFIFO:
if((ms->flags & MAGIC_DEVICES) != 0)
2015-03-05 14:32:04 +00:00
@@ -285,79 +207,14 @@
#endif
#ifdef S_IFLNK
case S_IFLNK:
- if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
+ /* stat is used, if it made here then the link is broken */
if (ms->flags & MAGIC_ERROR) {
- file_error(ms, errno, "unreadable symlink `%s'",
- fn);
+ file_error(ms, errno, "unreadable symlink `%s'", fn);
return -1;
}
- if (mime) {
- if (handle_mime(ms, mime, "symlink") == -1)
- return -1;
- } else if (file_printf(ms,
2013-04-07 20:15:56 +00:00
- "%sunreadable symlink `%s' (%s)", COMMA, fn,
- strerror(errno)) == -1)
- return -1;
2013-04-07 20:15:56 +00:00
- break;
- }
- buf[nch] = '\0'; /* readlink(2) does not do this */
-
- /* If broken symlink, say so and quit early. */
- if (*buf == '/') {
- if (stat(buf, &tstatbuf) < 0)
- return bad_link(ms, errno, buf);
- } else {
- char *tmp;
- char buf2[BUFSIZ+BUFSIZ+4];
-
- if ((tmp = strrchr(fn, '/')) == NULL) {
- tmp = buf; /* in current directory anyway */
- } else {
- if (tmp - fn + 1 > BUFSIZ) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, 0,
- "path too long: `%s'", buf);
- return -1;
- }
- if (mime) {
- if (handle_mime(ms, mime,
- "x-path-too-long") == -1)
- return -1;
- } else if (file_printf(ms,
2013-04-07 20:15:56 +00:00
- "%spath too long: `%s'", COMMA,
- fn) == -1)
- return -1;
2013-04-07 20:15:56 +00:00
- break;
- }
- /* take dir part */
- (void)strlcpy(buf2, fn, sizeof buf2);
- buf2[tmp - fn + 1] = '\0';
- /* plus (rel) link */
- (void)strlcat(buf2, buf, sizeof buf2);
- tmp = buf2;
- }
- if (stat(tmp, &tstatbuf) < 0)
- return bad_link(ms, errno, buf);
- }
-
- /* Otherwise, handle it. */
- if ((ms->flags & MAGIC_SYMLINK) != 0) {
- const char *p;
- ms->flags &= MAGIC_SYMLINK;
- p = magic_file(ms, buf);
- ms->flags |= MAGIC_SYMLINK;
2013-04-07 20:15:56 +00:00
- if (p == NULL)
- return -1;
- } else { /* just print what it points to */
- if (mime) {
- if (handle_mime(ms, mime, "symlink") == -1)
- return -1;
2015-03-05 14:32:04 +00:00
- } else if (file_printf(ms, "%ssymbolic link to %s",
2013-04-07 20:15:56 +00:00
- COMMA, buf) == -1)
- return -1;
- }
2013-04-07 20:15:56 +00:00
- break;
+ return 1;
#endif
+
#ifdef S_IFSOCK
#ifndef __COHERENT__
case S_IFSOCK:
diff -u libmagic.orig/funcs.c libmagic/funcs.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/funcs.c 2017-05-08 20:10:13.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/funcs.c 2017-10-18 14:05:31.861865200 +0200
2015-03-05 14:32:04 +00:00
@@ -31,7 +31,6 @@
2014-10-25 10:06:17 +00:00
#endif /* lint */
#include "magic.h"
2015-03-05 14:32:04 +00:00
-#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
2017-10-11 16:18:55 +00:00
@@ -42,78 +41,79 @@
#if defined(HAVE_WCTYPE_H)
#include <wctype.h>
#endif
-#if defined(HAVE_LIMITS_H)
-#include <limits.h>
2015-03-05 14:32:04 +00:00
+#if defined(HAVE_LOCALE_H)
+#include <locale.h>
#endif
2014-02-19 10:20:24 +00:00
#ifndef SIZE_MAX
2015-03-05 14:32:04 +00:00
#define SIZE_MAX ((size_t)~0)
#endif
2014-02-19 10:20:24 +00:00
-/*
- * Like printf, only we append to a buffer.
- */
-protected int
-file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
2014-02-19 10:20:24 +00:00
-{
- int len;
- char *buf, *newstr;
2014-03-31 15:24:15 +00:00
+#include "php.h"
+#include "main/php_network.h"
2014-02-19 10:20:24 +00:00
- if (ms->event_flags & EVENT_HAD_ERR)
- return 0;
- len = vasprintf(&buf, fmt, ap);
- if (len < 0)
- goto out;
2014-03-31 15:24:15 +00:00
+#ifndef PREG_OFFSET_CAPTURE
+# define PREG_OFFSET_CAPTURE (1<<8)
+#endif
2014-02-19 10:20:24 +00:00
- if (ms->o.buf != NULL) {
- len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
- free(buf);
- if (len < 0)
- goto out;
- free(ms->o.buf);
- buf = newstr;
2014-02-19 10:20:24 +00:00
- }
- ms->o.buf = buf;
2014-02-19 10:20:24 +00:00
- return 0;
-out:
2017-10-11 16:18:55 +00:00
- fprintf(stderr, "vasprintf failed (%s)", strerror(errno));
- return -1;
-}
2014-10-25 10:06:17 +00:00
+extern public void convert_libmagic_pattern(zval *pattern, char *val, int len, int options);
2014-02-19 10:20:24 +00:00
protected int
file_printf(struct magic_set *ms, const char *fmt, ...)
{
2017-10-11 16:18:55 +00:00
- int rv;
2014-02-19 10:20:24 +00:00
va_list ap;
+ int len;
+ char *buf = NULL, *newstr;
va_start(ap, fmt);
- rv = file_vprintf(ms, fmt, ap);
2014-02-19 10:20:24 +00:00
+ len = vspprintf(&buf, 0, fmt, ap);
va_end(ap);
- return rv;
2014-02-19 10:20:24 +00:00
+
+ if (ms->o.buf != NULL) {
+ len = spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
+ if (buf) {
+ efree(buf);
+ }
+ efree(ms->o.buf);
+ ms->o.buf = newstr;
+ } else {
+ ms->o.buf = buf;
+ }
+ return 0;
}
/*
2014-02-19 10:20:24 +00:00
* error - print best error message possible
*/
/*VARARGS*/
-__attribute__((__format__(__printf__, 3, 0)))
private void
file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
size_t lineno)
{
+ char *buf = NULL;
2015-03-05 14:32:04 +00:00
+
/* Only the first error is ok */
if (ms->event_flags & EVENT_HAD_ERR)
return;
if (lineno != 0) {
- free(ms->o.buf);
+ efree(ms->o.buf);
ms->o.buf = NULL;
2016-11-24 13:58:54 +00:00
file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
}
2016-11-24 13:58:54 +00:00
- if (ms->o.buf && *ms->o.buf)
- file_printf(ms, " ");
- file_vprintf(ms, f, va);
- if (error > 0)
- file_printf(ms, " (%s)", strerror(error));
+
+ vspprintf(&buf, 0, f, va);
+ va_end(va);
2015-03-05 14:32:04 +00:00
+
+ if (error > 0) {
+ file_printf(ms, "%s (%s)", (*buf ? buf : ""), strerror(error));
+ } else if (*buf) {
+ file_printf(ms, "%s", buf);
+ }
2015-03-05 14:32:04 +00:00
+
+ if (buf) {
+ efree(buf);
+ }
+
ms->event_flags |= EVENT_HAD_ERR;
ms->error = error;
}
2017-10-11 16:18:55 +00:00
@@ -160,7 +160,6 @@
file_error(ms, errno, "error reading");
}
-#ifndef COMPILE_ONLY
2016-11-24 13:58:54 +00:00
static int
checkdone(struct magic_set *ms, int *rv)
2017-10-11 16:18:55 +00:00
@@ -174,8 +173,8 @@
2016-11-24 13:58:54 +00:00
/*ARGSUSED*/
protected int
2015-03-05 14:32:04 +00:00
-file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
- const void *buf, size_t nb)
+file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const void *buf,
+ size_t nb)
{
int m = 0, rv = 0, looks_text = 0;
2016-11-24 13:58:54 +00:00
const unsigned char *ubuf = CAST(const unsigned char *, buf);
2017-10-11 16:18:55 +00:00
@@ -216,10 +215,10 @@
}
}
#endif
-#if HAVE_FORK
- /* try compression stuff */
+
+#if PHP_FILEINFO_UNCOMPRESS
2016-11-24 13:58:54 +00:00
if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
- m = file_zmagic(ms, fd, inname, ubuf, nb);
+ m = file_zmagic(ms, stream, inname, ubuf, nb);
if ((ms->flags & MAGIC_DEBUG) != 0)
(void)fprintf(stderr, "[try zmagic %d]\n", m);
if (m) {
2017-10-11 16:18:55 +00:00
@@ -240,12 +239,15 @@
/* Check if we have a CDF file */
2016-11-24 13:58:54 +00:00
if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
2014-03-31 15:24:15 +00:00
+ php_socket_t fd;
2016-11-24 13:58:54 +00:00
+ 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;
+ }
}
2016-11-24 13:58:54 +00:00
}
2017-10-11 16:18:55 +00:00
@@ -316,7 +318,7 @@
2015-03-05 14:32:04 +00:00
if (file_printf(ms, "%s", code_mime) == -1)
rv = -1;
}
-#if HAVE_FORK
2016-01-25 03:45:20 +00:00
+#if PHP_FILEINFO_UNCOMPRESS
2015-03-05 14:32:04 +00:00
done_encoding:
2016-01-25 03:45:20 +00:00
#endif
2015-03-05 14:32:04 +00:00
free(u8buf);
2017-10-11 16:18:55 +00:00
@@ -325,7 +327,6 @@
return m;
}
-#endif
protected int
file_reset(struct magic_set *ms)
2017-10-11 16:18:55 +00:00
@@ -335,11 +336,11 @@
return -1;
}
if (ms->o.buf) {
- free(ms->o.buf);
+ efree(ms->o.buf);
ms->o.buf = NULL;
}
if (ms->o.pbuf) {
- free(ms->o.pbuf);
+ efree(ms->o.pbuf);
ms->o.pbuf = NULL;
}
ms->event_flags &= ~EVENT_HAD_ERR;
2017-10-11 16:18:55 +00:00
@@ -377,7 +378,7 @@
return NULL;
}
psize = len * 4 + 1;
- if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
2016-11-24 13:58:54 +00:00
+ if ((pbuf = CAST(char *, erealloc(ms->o.pbuf, psize))) == NULL) {
2013-04-07 20:15:56 +00:00
file_oomem(ms, psize);
return NULL;
}
2017-10-11 16:18:55 +00:00
@@ -441,8 +442,8 @@
if (level >= ms->c.len) {
2016-11-24 13:58:54 +00:00
len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
2013-04-07 20:15:56 +00:00
ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
- malloc(len) :
- realloc(ms->c.li, len));
2013-04-07 20:15:56 +00:00
+ emalloc(len) :
+ erealloc(ms->c.li, len));
if (ms->c.li == NULL) {
file_oomem(ms, len);
return -1;
2017-10-11 16:18:55 +00:00
@@ -465,76 +466,41 @@
2015-03-05 14:32:04 +00:00
protected int
2012-03-28 10:06:09 +00:00
file_replace(struct magic_set *ms, const char *pat, const char *rep)
{
2015-03-05 14:32:04 +00:00
- file_regex_t rx;
2014-02-19 10:20:24 +00:00
- int rc, rv = -1;
2015-03-05 14:32:04 +00:00
-
- rc = file_regcomp(&rx, pat, REG_EXTENDED);
2014-02-19 10:20:24 +00:00
- if (rc) {
2015-03-05 14:32:04 +00:00
- file_regerror(&rx, rc, ms);
2014-02-19 10:20:24 +00:00
- } else {
- regmatch_t rm;
- int nm = 0;
2015-03-05 14:32:04 +00:00
- while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
2014-02-19 10:20:24 +00:00
- ms->o.buf[rm.rm_so] = '\0';
- if (file_printf(ms, "%s%s", rep,
- rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
- goto out;
- nm++;
- }
- rv = nm;
2015-03-05 14:32:04 +00:00
+ zval patt;
+ int opts = 0;
+ pcre_cache_entry *pce;
+ zend_string *res;
2017-10-11 16:18:55 +00:00
+ zend_string *repl;
2015-03-05 14:32:04 +00:00
+ int rep_cnt = 0;
+
+ (void)setlocale(LC_CTYPE, "C");
+
+ opts |= PCRE_MULTILINE;
2016-01-25 03:45:20 +00:00
+ convert_libmagic_pattern(&patt, (char*)pat, strlen(pat), opts);
2014-12-13 22:06:14 +00:00
+ if ((pce = pcre_get_compiled_regex_cache(Z_STR(patt))) == NULL) {
2014-10-25 10:06:17 +00:00
+ zval_ptr_dtor(&patt);
2014-02-19 10:20:24 +00:00
+ rep_cnt = -1;
+ goto out;
}
2015-03-05 14:32:04 +00:00
-out:
- file_regfree(&rx);
- return rv;
-}
2014-10-25 10:06:17 +00:00
+ zval_ptr_dtor(&patt);
2015-03-05 14:32:04 +00:00
-protected int
-file_regcomp(file_regex_t *rx, const char *pat, int flags)
-{
-#ifdef USE_C_LOCALE
- rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
- assert(rx->c_lc_ctype != NULL);
- rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
- assert(rx->old_lc_ctype != NULL);
2016-11-24 13:58:54 +00:00
-#else
- rx->old_lc_ctype = setlocale(LC_CTYPE, "C");
2015-03-05 14:32:04 +00:00
-#endif
- rx->pat = pat;
2017-10-11 16:18:55 +00:00
+ repl = zend_string_init(rep, strlen(rep), 0);
+ res = php_pcre_replace_impl(pce, NULL, ms->o.buf, strlen(ms->o.buf), repl, -1, &rep_cnt);
2015-03-05 14:32:04 +00:00
- return rx->rc = regcomp(&rx->rx, pat, flags);
-}
2017-10-11 16:18:55 +00:00
+ zend_string_release(repl);
2016-11-24 13:58:54 +00:00
+ if (NULL == res) {
+ rep_cnt = -1;
+ goto out;
+ }
2015-03-05 14:32:04 +00:00
-protected int
-file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
- regmatch_t* pmatch, int eflags)
-{
- assert(rx->rc == 0);
2017-10-11 16:18:55 +00:00
- /* XXX: force initialization because glibc does not always do this */
- memset(pmatch, 0, nmatch * sizeof(*pmatch));
2015-03-05 14:32:04 +00:00
- return regexec(&rx->rx, str, nmatch, pmatch, eflags);
-}
2016-11-24 13:58:54 +00:00
+ strncpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res));
+ ms->o.buf[ZSTR_LEN(res)] = '\0';
2015-03-05 14:32:04 +00:00
-protected void
-file_regfree(file_regex_t *rx)
-{
- if (rx->rc == 0)
- regfree(&rx->rx);
-#ifdef USE_C_LOCALE
- (void)uselocale(rx->old_lc_ctype);
- freelocale(rx->c_lc_ctype);
2016-11-24 13:58:54 +00:00
-#else
- (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
2015-03-05 14:32:04 +00:00
-#endif
-}
2016-11-24 13:58:54 +00:00
-
2015-03-05 14:32:04 +00:00
-protected void
-file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
-{
- char errmsg[512];
2014-10-25 10:06:17 +00:00
+ zend_string_release(res);
2015-03-05 14:32:04 +00:00
- (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
- file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
- errmsg);
+out:
+ (void)setlocale(LC_CTYPE, "");
+ return rep_cnt;
}
2015-03-05 14:32:04 +00:00
protected file_pushbuf_t *
2017-10-11 16:18:55 +00:00
@@ -545,7 +511,7 @@
2015-03-05 14:32:04 +00:00
if (ms->event_flags & EVENT_HAD_ERR)
return NULL;
- if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
+ if ((pb = (CAST(file_pushbuf_t *, emalloc(sizeof(*pb))))) == NULL)
return NULL;
pb->buf = ms->o.buf;
2017-10-11 16:18:55 +00:00
@@ -563,8 +529,8 @@
2015-03-05 14:32:04 +00:00
char *rbuf;
if (ms->event_flags & EVENT_HAD_ERR) {
- free(pb->buf);
- free(pb);
+ efree(pb->buf);
+ efree(pb);
return NULL;
}
2017-10-11 16:18:55 +00:00
@@ -573,7 +539,7 @@
2015-03-05 14:32:04 +00:00
ms->o.buf = pb->buf;
ms->offset = pb->offset;
- free(pb);
+ efree(pb);
return rbuf;
}
diff -u libmagic.orig/magic.c libmagic/magic.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/magic.c 2016-07-18 13:43:05.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/magic.c 2017-10-18 14:05:31.861865200 +0200
@@ -25,11 +25,6 @@
* SUCH DAMAGE.
*/
-#ifdef WIN32
-#include <windows.h>
-#include <shlwapi.h>
-#endif
-
#include "file.h"
#ifndef lint
2017-10-11 16:18:55 +00:00
@@ -39,14 +34,19 @@
#include "magic.h"
#include <stdlib.h>
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
#include <unistd.h>
+#endif
#include <string.h>
-#ifdef QUICK
-#include <sys/mman.h>
2017-10-11 16:18:55 +00:00
+#include "config.h"
+
+#ifdef PHP_WIN32
+#include <shlwapi.h>
#endif
2017-10-11 16:18:55 +00:00
-#ifdef HAVE_LIMITS_H
+
2017-10-11 16:18:55 +00:00
#include <limits.h> /* for PIPE_BUF */
-#endif
#if defined(HAVE_UTIMES)
# include <sys/time.h>
2017-10-11 16:18:55 +00:00
@@ -71,194 +71,23 @@
#endif
#endif
+#ifdef PHP_WIN32
+# undef S_IFLNK
+# undef S_IFIFO
+#endif
+
private void close_and_restore(const struct magic_set *, const char *, int,
2014-10-25 10:06:17 +00:00
- const struct stat *);
+ const zend_stat_t *);
private int unreadable_info(struct magic_set *, mode_t, const char *);
2013-04-27 12:09:29 +00:00
+#if 0
private const char* get_default_magic(void);
-#ifndef COMPILE_ONLY
-private const char *file_or_fd(struct magic_set *, const char *, int);
2013-04-27 12:09:29 +00:00
#endif
+private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
2016-11-24 13:58:54 +00:00
-#ifdef WIN32
-/* HINSTANCE of this shared library. Needed for get_default_magic() */
-static HINSTANCE _w32_dll_instance = NULL;
-
-static void
-_w32_append_path(char **hmagicpath, const char *fmt, ...)
-{
- char *tmppath;
- char *newpath;
- va_list ap;
-
- va_start(ap, fmt);
- if (vasprintf(&tmppath, fmt, ap) < 0) {
- va_end(ap);
- return;
- }
- va_end(ap);
-
- if (access(tmppath, R_OK) == -1)
- goto out;
-
- if (*hmagicpath == NULL) {
- *hmagicpath = tmppath;
- return;
- }
-
- if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0)
- goto out;
-
- free(*hmagicpath);
- free(tmppath);
- *hmagicpath = newpath;
- return;
-out:
- free(tmppath);
-}
-
-static void
-_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
-{
- static const char *trypaths[] = {
- "%s/share/misc/magic.mgc",
- "%s/magic.mgc",
- };
- LPSTR dllpath;
- size_t sp;
-
- dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath));
-
- if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
- goto out;
-
- PathRemoveFileSpecA(dllpath);
-
- if (module) {
- char exepath[MAX_PATH];
- GetModuleFileNameA(NULL, exepath, MAX_PATH);
- PathRemoveFileSpecA(exepath);
- if (stricmp(exepath, dllpath) == 0)
- goto out;
- }
-
- sp = strlen(dllpath);
- if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) {
- _w32_append_path(hmagicpath,
- "%s/../share/misc/magic.mgc", dllpath);
- goto out;
- }
-
- for (sp = 0; sp < __arraycount(trypaths); sp++)
- _w32_append_path(hmagicpath, trypaths[sp], dllpath);
-out:
- free(dllpath);
-}
-
-/* Placate GCC by offering a sacrificial previous prototype */
-BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
-
-BOOL WINAPI
-DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
- LPVOID lpvReserved __attribute__((__unused__)))
-{
- if (fdwReason == DLL_PROCESS_ATTACH)
- _w32_dll_instance = hinstDLL;
- return TRUE;
-}
-#endif
-
-private const char *
-get_default_magic(void)
-{
- static const char hmagic[] = "/.magic/magic.mgc";
- static char *default_magic;
- char *home, *hmagicpath;
-
-#ifndef WIN32
2016-11-24 13:58:54 +00:00
- struct stat st;
-
- if (default_magic) {
- free(default_magic);
- default_magic = NULL;
- }
- if ((home = getenv("HOME")) == NULL)
- return MAGIC;
-
- if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
- return MAGIC;
- if (stat(hmagicpath, &st) == -1) {
- free(hmagicpath);
2013-04-07 20:15:56 +00:00
- if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
2016-11-24 13:58:54 +00:00
- return MAGIC;
2013-04-07 20:15:56 +00:00
- if (stat(hmagicpath, &st) == -1)
2016-11-24 13:58:54 +00:00
- goto out;
2013-04-07 20:15:56 +00:00
- if (S_ISDIR(st.st_mode)) {
- free(hmagicpath);
- if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
- return MAGIC;
- if (access(hmagicpath, R_OK) == -1)
- goto out;
- }
2016-11-24 13:58:54 +00:00
- }
-
- if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
- goto out;
- free(hmagicpath);
- return default_magic;
-out:
- default_magic = NULL;
- free(hmagicpath);
- return MAGIC;
-#else
2015-03-05 14:32:04 +00:00
- hmagicpath = NULL;
2016-11-24 13:58:54 +00:00
-
- if (default_magic) {
- free(default_magic);
- default_magic = NULL;
- }
-
- /* First, try to get a magic file from user-application data */
- if ((home = getenv("LOCALAPPDATA")) != NULL)
- _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
-
- /* Second, try to get a magic file from the user profile data */
- if ((home = getenv("USERPROFILE")) != NULL)
- _w32_append_path(&hmagicpath,
- "%s/Local Settings/Application Data%s", home, hmagic);
-
- /* Third, try to get a magic file from Common Files */
- if ((home = getenv("COMMONPROGRAMFILES")) != NULL)
- _w32_append_path(&hmagicpath, "%s%s", home, hmagic);
-
- /* Fourth, try to get magic file relative to exe location */
- _w32_get_magic_relative_to(&hmagicpath, NULL);
-
- /* Fifth, try to get magic file relative to dll location */
- _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
-
- /* Avoid MAGIC constant - it likely points to a file within MSys tree */
- default_magic = hmagicpath;
- return default_magic;
-#endif
-}
-
-public const char *
-magic_getpath(const char *magicfile, int action)
-{
- if (magicfile != NULL)
- return magicfile;
-
- magicfile = getenv("MAGIC");
- if (magicfile != NULL)
- return magicfile;
-
- return action == FILE_LOAD ? get_default_magic() : MAGIC;
-}
-
2012-03-28 10:06:09 +00:00
public struct magic_set *
magic_open(int flags)
2016-11-24 13:58:54 +00:00
{
2017-10-11 16:18:55 +00:00
@@ -304,20 +133,6 @@
2015-03-05 14:32:04 +00:00
return file_apprentice(ms, magicfile, FILE_LOAD);
}
-#ifndef COMPILE_ONLY
2015-03-08 16:24:44 +00:00
-/*
- * Install a set of compiled magic buffers.
- */
-public int
-magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
- size_t nbufs)
-{
- if (ms == NULL)
- return -1;
- return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
-}
2015-03-05 14:32:04 +00:00
-#endif
2015-03-08 16:24:44 +00:00
-
2015-03-05 14:32:04 +00:00
public int
magic_compile(struct magic_set *ms, const char *magicfile)
2015-03-08 16:24:44 +00:00
{
2017-10-11 16:18:55 +00:00
@@ -326,13 +141,6 @@
2013-04-07 20:15:56 +00:00
return file_apprentice(ms, magicfile, FILE_COMPILE);
}
-public int
-magic_check(struct magic_set *ms, const char *magicfile)
-{
2013-04-07 20:15:56 +00:00
- if (ms == NULL)
- return -1;
- return file_apprentice(ms, magicfile, FILE_CHECK);
-}
public int
magic_list(struct magic_set *ms, const char *magicfile)
2017-10-11 16:18:55 +00:00
@@ -344,11 +152,8 @@
2014-10-25 10:06:17 +00:00
private void
close_and_restore(const struct magic_set *ms, const char *name, int fd,
2014-10-25 10:06:17 +00:00
- const struct stat *sb)
+ const zend_stat_t *sb)
{
2014-02-19 10:20:24 +00:00
- if (fd == STDIN_FILENO || name == NULL)
- return;
- (void) close(fd);
if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
/*
2017-10-11 16:18:55 +00:00
@@ -375,7 +180,6 @@
}
}
-#ifndef COMPILE_ONLY
/*
* find type of descriptor
2017-10-11 16:18:55 +00:00
@@ -385,7 +189,7 @@
{
2013-04-07 20:15:56 +00:00
if (ms == NULL)
return NULL;
- return file_or_fd(ms, NULL, fd);
+ return file_or_stream(ms, NULL, NULL);
}
/*
2017-10-11 16:18:55 +00:00
@@ -396,31 +200,42 @@
{
2013-04-07 20:15:56 +00:00
if (ms == NULL)
return NULL;
- return file_or_fd(ms, inname, STDIN_FILENO);
+ return file_or_stream(ms, inname, NULL);
+}
+
+public const char *
+magic_stream(struct magic_set *ms, php_stream *stream)
+{
2013-04-07 20:15:56 +00:00
+ if (ms == NULL)
+ return NULL;
+ return file_or_stream(ms, NULL, stream);
}
private const char *
-file_or_fd(struct magic_set *ms, const char *inname, int fd)
+file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
{
int rv = -1;
unsigned char *buf;
2014-10-25 10:06:17 +00:00
- struct stat sb;
+ zend_stat_t sb;
ssize_t nbytes = 0; /* number of bytes read from a datafile */
- int ispipe = 0;
2014-02-19 10:20:24 +00:00
- off_t pos = (off_t)-1;
+ int no_in_stream = 0;
2015-03-05 14:32:04 +00:00
2016-11-24 13:58:54 +00:00
if (file_reset(ms) == -1)
goto out;
+ if (!inname && !stream) {
+ return NULL;
+ }
2016-11-24 13:58:54 +00:00
+
/*
* one extra for terminating '\0', and
* some overlapping space for matches near EOF
*/
#define SLOP (1 + sizeof(union VALUETYPE))
2016-11-24 13:58:54 +00:00
- if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
+ if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
return NULL;
- switch (file_fsmagic(ms, inname, &sb)) {
+ switch (file_fsmagic(ms, inname, &sb, stream)) {
case -1: /* error */
goto done;
case 0: /* nothing found */
2017-10-11 16:18:55 +00:00
@@ -430,103 +245,41 @@
goto done;
}
2015-03-05 14:32:04 +00:00
-#ifdef WIN32
- /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */
- if (fd == STDIN_FILENO)
- _setmode(STDIN_FILENO, O_BINARY);
-#endif
2016-11-24 13:58:54 +00:00
-
- if (inname == NULL) {
- if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
- ispipe = 1;
2014-02-19 10:20:24 +00:00
- else
- pos = lseek(fd, (off_t)0, SEEK_CUR);
- } else {
- int flags = O_RDONLY|O_BINARY;
2014-02-19 10:20:24 +00:00
- int okstat = stat(inname, &sb) == 0;
2016-11-24 13:58:54 +00:00
+ errno = 0;
2014-02-19 10:20:24 +00:00
- if (okstat && S_ISFIFO(sb.st_mode)) {
-#ifdef O_NONBLOCK
- flags |= O_NONBLOCK;
2014-02-19 10:20:24 +00:00
-#endif
- ispipe = 1;
- }
+ if (!stream && inname) {
+ no_in_stream = 1;
+ stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
+ }
- errno = 0;
- if ((fd = open(inname, flags)) < 0) {
2015-03-05 14:32:04 +00:00
-#ifdef WIN32
- /*
- * Can't stat, can't open. It may have been opened in
- * fsmagic, so if the user doesn't have read permission,
- * allow it to say so; otherwise an error was probably
- * displayed in fsmagic.
- */
- if (!okstat && errno == EACCES) {
- sb.st_mode = S_IFBLK;
- okstat = 1;
- }
-#endif
2014-02-19 10:20:24 +00:00
- if (okstat &&
- unreadable_info(ms, sb.st_mode, inname) == -1)
- goto done;
- rv = 0;
+ if (!stream) {
+ if (unreadable_info(ms, sb.st_mode, inname) == -1)
goto done;
- }
+ rv = 0;
+ goto done;
+ }
+
#ifdef O_NONBLOCK
- if ((flags = fcntl(fd, F_GETFL)) != -1) {
- flags &= ~O_NONBLOCK;
- (void)fcntl(fd, F_SETFL, flags);
- }
+/* we should be already be in non blocking mode for network socket */
#endif
- }
/*
2016-11-24 13:58:54 +00:00
* try looking at the first ms->bytes_max bytes
*/
- if (ispipe) {
- ssize_t r = 0;
-
- while ((r = sread(fd, (void *)&buf[nbytes],
2016-11-24 13:58:54 +00:00
- (size_t)(ms->bytes_max - nbytes), 1)) > 0) {
- nbytes += r;
- if (r < PIPE_BUF) break;
- }
-
2016-11-24 13:58:54 +00:00
- if (nbytes == 0 && inname) {
- /* We can not read it, but we were able to stat it. */
- if (unreadable_info(ms, sb.st_mode, inname) == -1)
- goto done;
- rv = 0;
- goto done;
- }
-
- } else {
2015-03-05 14:32:04 +00:00
- /* Windows refuses to read from a big console buffer. */
- size_t howmany =
2016-11-24 13:58:54 +00:00
-#if defined(WIN32)
2015-03-05 14:32:04 +00:00
- _isatty(fd) ? 8 * 1024 :
-#endif
2016-11-24 13:58:54 +00:00
- ms->bytes_max;
2015-03-05 14:32:04 +00:00
- if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
- if (inname == NULL && fd != STDIN_FILENO)
- file_error(ms, errno, "cannot read fd %d", fd);
- else
- file_error(ms, errno, "cannot read `%s'",
- inname == NULL ? "/dev/stdin" : inname);
- goto done;
- }
2016-11-24 13:58:54 +00:00
+ if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
+ file_error(ms, errno, "cannot read `%s'", inname);
+ goto done;
}
(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
- if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
+ if (file_buffer(ms, stream, inname, buf, (size_t)nbytes) == -1)
goto done;
rv = 0;
done:
- free(buf);
2016-11-24 13:58:54 +00:00
- if (fd != -1) {
- if (pos != (off_t)-1)
- (void)lseek(fd, pos, SEEK_SET);
- close_and_restore(ms, inname, fd, &sb);
+ efree(buf);
+
+ if (no_in_stream && stream) {
+ php_stream_close(stream);
2016-11-24 13:58:54 +00:00
}
out:
return rv == 0 ? file_getbuffer(ms) : NULL;
2017-10-11 16:18:55 +00:00
@@ -542,14 +295,13 @@
return NULL;
/*
* 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.
*/
- if (file_buffer(ms, -1, NULL, buf, nb) == -1) {
+ if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
return NULL;
}
return file_getbuffer(ms);
}
-#endif
public const char *
magic_error(struct magic_set *ms)
diff -u libmagic.orig/print.c libmagic/print.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/print.c 2017-03-07 23:20:58.000000000 +0100
2017-10-23 17:18:25 +00:00
+++ libmagic/print.c 2017-10-18 14:05:31.861865200 +0200
2016-11-24 13:58:54 +00:00
@@ -28,6 +28,8 @@
2014-02-19 10:20:24 +00:00
/*
* print.c - debugging printout routines
*/
2012-07-15 10:25:58 +00:00
+#define _GNU_SOURCE
+#include "php.h"
2014-02-19 10:20:24 +00:00
#include "file.h"
2016-11-24 13:58:54 +00:00
@@ -43,202 +45,44 @@
2013-04-07 20:15:56 +00:00
#endif
#include <time.h>
2016-11-24 13:58:54 +00:00
+#ifdef PHP_WIN32
+# define asctime_r php_asctime_r
+# define ctime_r php_ctime_r
+#endif
+
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
#include "cdf.h"
-#ifndef COMPILE_ONLY
-protected void
-file_mdump(struct magic *m)
-{
2013-04-07 20:15:56 +00:00
- 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,",
2013-04-07 20:15:56 +00:00
- /* 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),",
2013-04-07 20:15:56 +00:00
- ((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" : "",
2013-04-07 20:15:56 +00:00
- /* 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:
2013-04-07 20:15:56 +00:00
- 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,",
2016-11-24 13:58:54 +00:00
- file_fmttime(m->value.l, 0, tbuf));
- break;
- case FILE_LDATE:
- case FILE_LELDATE:
- case FILE_BELDATE:
- case FILE_MELDATE:
- (void)fprintf(stderr, "%s,",
2016-11-24 13:58:54 +00:00
- file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
2015-03-05 14:32:04 +00:00
- break;
- case FILE_QDATE:
- case FILE_LEQDATE:
- case FILE_BEQDATE:
- (void)fprintf(stderr, "%s,",
2016-11-24 13:58:54 +00:00
- file_fmttime(m->value.q, 0, tbuf));
- break;
- case FILE_QLDATE:
- case FILE_LEQLDATE:
- case FILE_BEQLDATE:
- (void)fprintf(stderr, "%s,",
2016-11-24 13:58:54 +00:00
- file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
2013-04-07 20:15:56 +00:00
- 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;
2013-04-07 20:15:56 +00:00
- case FILE_USE:
- case FILE_NAME:
2016-11-24 13:58:54 +00:00
- case FILE_DER:
2013-04-07 20:15:56 +00:00
- (void) fprintf(stderr, "'%s'", m->value.s);
- break;
- default:
2013-04-07 20:15:56 +00:00
- (void) fprintf(stderr, "*bad type %d*", m->type);
- break;
- }
- }
- (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
-}
2016-11-24 13:58:54 +00:00
-#endif
-
/*VARARGS*/
protected void
file_magwarn(struct magic_set *ms, const char *f, ...)
{
va_list va;
2014-12-30 19:28:13 +00:00
+ char *expanded_format = NULL;
+ int expanded_len;
- /* cuz we use stdout for most, stderr here */
- (void) fflush(stdout);
-
- if (ms->file)
- (void) fprintf(stderr, "%s, %lu: ", ms->file,
- (unsigned long)ms->line);
- (void) fprintf(stderr, "Warning: ");
va_start(va, f);
- (void) vfprintf(stderr, f, va);
2014-12-30 19:28:13 +00:00
+ expanded_len = vasprintf(&expanded_format, f, va);
va_end(va);
- (void) fputc('\n', stderr);
2015-03-05 14:32:04 +00:00
+
2014-12-30 19:28:13 +00:00
+ if (expanded_len >= 0 && expanded_format) {
+ php_error_docref(NULL, E_NOTICE, "Warning: %s", expanded_format);
+
2014-12-30 19:28:13 +00:00
+ free(expanded_format);
+ }
}
protected const char *
2015-03-05 14:32:04 +00:00
file_fmttime(uint64_t v, int flags, char *buf)
{
char *pp;
- time_t t;
- struct tm *tm, tmz;
+ time_t t = (time_t)v;
2015-03-29 16:22:42 +00:00
+ struct tm *tm = NULL;
2013-04-07 20:15:56 +00:00
if (flags & FILE_T_WINDOWS) {
- struct timespec ts;
2017-10-11 16:18:55 +00:00
- cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
2013-04-07 20:15:56 +00:00
+ struct timeval ts;
2015-03-05 14:32:04 +00:00
+ cdf_timestamp_to_timespec(&ts, t);
2013-04-07 20:15:56 +00:00
t = ts.tv_sec;
2015-03-05 14:32:04 +00:00
} else {
// XXX: perhaps detect and print something if overflow
2016-11-24 13:58:54 +00:00
@@ -247,9 +91,29 @@
2015-03-05 14:32:04 +00:00
}
if (flags & FILE_T_LOCAL) {
- tm = localtime_r(&t, &tmz);
+ pp = ctime_r(&t, buf);
} else {
- tm = gmtime_r(&t, &tmz);
+#ifndef HAVE_DAYLIGHT
+ private int daylight = 0;
+#ifdef HAVE_TM_ISDST
+ private time_t now = (time_t)0;
+
+ if (now == (time_t)0) {
+ struct tm *tm1;
+ (void)time(&now);
+ tm1 = localtime(&now);
+ if (tm1 == NULL)
+ goto out;
+ daylight = tm1->tm_isdst;
+ }
+#endif /* HAVE_TM_ISDST */
+#endif /* HAVE_DAYLIGHT */
+ if (daylight)
+ t += 3600;
+ tm = gmtime(&t);
+ if (tm == NULL)
+ goto out;
+ pp = asctime_r(tm, buf);
2013-04-07 20:15:56 +00:00
}
2015-03-05 14:32:04 +00:00
if (tm == NULL)
goto out;
diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/readcdf.c 2017-05-08 20:10:13.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/readcdf.c 2017-10-18 14:05:31.861865200 +0200
2016-11-24 13:58:54 +00:00
@@ -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
@@ -31,7 +31,11 @@
2016-11-24 13:58:54 +00:00
#include <assert.h>
#include <stdlib.h>
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
#include <unistd.h>
+#endif
#include <string.h>
#include <time.h>
#include <ctype.h>
2016-11-24 13:58:54 +00:00
@@ -75,6 +79,10 @@
2014-02-19 10:20:24 +00:00
{ NULL, NULL, },
};
+#ifdef PHP_WIN32
+# define strcasestr strstr
+#endif
2014-07-01 08:28:43 +00:00
+
2015-03-05 14:32:04 +00:00
static const struct cv {
uint64_t clsid[2];
const char *mime;
2016-11-24 13:58:54 +00:00
@@ -104,10 +112,6 @@
2015-03-29 16:22:42 +00:00
if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
return cv[i].mime;
}
-#ifdef CDF_DEBUG
- fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0],
- clsid[1]);
-#endif
return NULL;
}
2016-11-24 13:58:54 +00:00
@@ -116,30 +120,14 @@
2014-02-19 10:20:24 +00:00
{
2015-03-05 14:32:04 +00:00
size_t i;
const char *rv = NULL;
-#ifdef USE_C_LOCALE
- locale_t old_lc_ctype, c_lc_ctype;
- c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
- assert(c_lc_ctype != NULL);
- old_lc_ctype = uselocale(c_lc_ctype);
- assert(old_lc_ctype != NULL);
2016-11-24 13:58:54 +00:00
-#else
- char *old_lc_ctype = setlocale(LC_CTYPE, "C");
2015-03-05 14:32:04 +00:00
-#endif
+ (void)setlocale(LC_CTYPE, "C");
for (i = 0; nv[i].pattern != NULL; i++)
if (strcasestr(vbuf, nv[i].pattern) != NULL) {
rv = nv[i].mime;
break;
}
2015-03-29 16:22:42 +00:00
-#ifdef CDF_DEBUG
- fprintf(stderr, "unknown app %s\n", vbuf);
-#endif
2015-03-05 14:32:04 +00:00
-#ifdef USE_C_LOCALE
- (void)uselocale(old_lc_ctype);
- freelocale(c_lc_ctype);
2016-11-24 13:58:54 +00:00
-#else
- setlocale(LC_CTYPE, old_lc_ctype);
2015-03-05 14:32:04 +00:00
-#endif
+ (void)setlocale(LC_CTYPE, "");
return rv;
}
2014-07-01 08:28:43 +00:00
2016-11-24 13:58:54 +00:00
@@ -149,12 +137,14 @@
{
size_t i;
cdf_timestamp_t tp;
- struct timespec ts;
+ struct timeval ts;
char buf[64];
const char *str = NULL;
2017-10-11 16:18:55 +00:00
const char *s, *e;
2014-04-24 18:16:06 +00:00
int len;
+ memset(&ts, 0, sizeof(ts));
2014-07-01 08:28:43 +00:00
+
2015-03-05 14:32:04 +00:00
if (!NOTMIME(ms) && root_storage)
str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
clsid2mime);
2017-10-11 16:18:55 +00:00
@@ -234,8 +224,11 @@
return -1;
} else {
char *c, *ec;
- cdf_timestamp_to_timespec(&ts, tp);
2014-02-19 10:20:24 +00:00
- c = cdf_ctime(&ts.tv_sec, tbuf);
+ const time_t sec = ts.tv_sec;
+ if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
+ return -1;
+ }
2014-02-19 10:20:24 +00:00
+ c = cdf_ctime(&sec, tbuf);
if (c != NULL &&
(ec = strchr(c, '\n')) != NULL)
*ec = '\0';
diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/softmagic.c 2017-05-08 20:10:13.000000000 +0200
2017-10-23 17:18:25 +00:00
+++ libmagic/softmagic.c 2017-10-18 14:05:31.861865200 +0200
2016-11-24 13:58:54 +00:00
@@ -43,6 +43,10 @@
2015-03-05 14:32:04 +00:00
#include <time.h>
2016-11-24 13:58:54 +00:00
#include "der.h"
+#ifndef PREG_OFFSET_CAPTURE
+# define PREG_OFFSET_CAPTURE (1<<8)
+#endif
+
private int match(struct magic_set *, struct magic *, uint32_t,
2016-11-24 13:58:54 +00:00
const unsigned char *, size_t, size_t, int, int, int, uint16_t *,
uint16_t *, int *, int *, int *);
@@ -113,8 +117,8 @@
2015-03-05 14:32:04 +00:00
return 0;
}
-#define FILE_FMTDEBUG
-#ifdef FILE_FMTDEBUG
2014-03-10 13:17:47 +00:00
+
2015-03-05 14:32:04 +00:00
+#if defined(FILE_FMTDEBUG) && defined(HAVE_FMTCHECK)
#define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
private const char * __attribute__((__format_arg__(3)))
2016-11-24 13:58:54 +00:00
@@ -128,8 +132,10 @@
2015-03-05 14:32:04 +00:00
" with `%s'", file, line, m->desc, def);
return ptr;
}
-#else
+#elif defined(HAVE_FMTCHECK)
#define F(a, b, c) fmtcheck((b)->desc, (c))
+#else
2015-03-08 16:24:44 +00:00
+#define F(a, b, c) ((b)->desc)
2015-03-05 14:32:04 +00:00
#endif
2014-02-19 10:20:24 +00:00
/*
2016-11-24 13:58:54 +00:00
@@ -182,7 +188,7 @@
struct magic *m = &magic[magindex];
2013-04-07 20:15:56 +00:00
if (m->type != FILE_NAME)
- if ((IS_STRING(m->type) &&
+ if ((IS_LIBMAGIC_STRING(m->type) &&
2013-04-07 20:15:56 +00:00
#define FLT (STRING_BINTEST | STRING_TEXTTEST)
((text && (m->str_flags & FLT) == STRING_BINTEST) ||
(!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
2017-10-11 16:18:55 +00:00
@@ -406,42 +412,26 @@
private int
check_fmt(struct magic_set *ms, struct magic *m)
{
2015-03-05 14:32:04 +00:00
- file_regex_t rx;
2014-02-19 10:20:24 +00:00
- int rc, rv = -1;
+ pcre *pce;
2014-02-19 10:20:24 +00:00
+ int re_options, rv = -1;
+ pcre_extra *re_extra;
2014-10-25 10:06:17 +00:00
+ zend_string *pattern;
2015-03-05 14:32:04 +00:00
2014-02-19 10:20:24 +00:00
if (strchr(m->desc, '%') == NULL)
return 0;
2014-02-19 10:20:24 +00:00
2015-03-05 14:32:04 +00:00
- rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
- if (rc) {
2015-03-05 14:32:04 +00:00
- file_regerror(&rx, rc, ms);
+ (void)setlocale(LC_CTYPE, "C");
2014-10-25 10:06:17 +00:00
+ pattern = zend_string_init("~%[-0-9.]*s~", sizeof("~%[-0-9.]*s~") - 1, 0);
2014-12-13 22:06:14 +00:00
+ if ((pce = pcre_get_compiled_regex(pattern, &re_extra, &re_options)) == NULL) {
2014-02-19 10:20:24 +00:00
+ rv = -1;
} else {
2015-03-05 14:32:04 +00:00
- rc = file_regexec(&rx, m->desc, 0, 0, 0);
2014-02-19 10:20:24 +00:00
- rv = !rc;
+ rv = !pcre_exec(pce, re_extra, m->desc, strlen(m->desc), 0, re_options, NULL, 0);
}
2015-03-05 14:32:04 +00:00
- file_regfree(&rx);
2014-10-25 10:06:17 +00:00
+ zend_string_release(pattern);
2015-03-05 14:32:04 +00:00
+ (void)setlocale(LC_CTYPE, "");
2014-02-19 10:20:24 +00:00
return rv;
}
-#ifndef HAVE_STRNDUP
-char * strndup(const char *, size_t);
-
-char *
-strndup(const char *str, size_t n)
-{
- size_t len;
- char *copy;
-
- for (len = 0; len < n && str[len]; len++)
- continue;
- if ((copy = malloc(len + 1)) == NULL)
- return NULL;
- (void)memcpy(copy, str, len);
- copy[len] = '\0';
- return copy;
-}
-#endif /* HAVE_STRNDUP */
-
private int32_t
mprint(struct magic_set *ms, struct magic *m)
{
2017-10-11 16:18:55 +00:00
@@ -667,19 +657,18 @@
2016-01-25 03:45:20 +00:00
t = ms->offset + sizeof(double);
break;
- case FILE_SEARCH:
case FILE_REGEX: {
char *cp;
int rval;
- cp = strndup((const char *)ms->search.s, ms->search.rm_len);
2015-03-08 16:24:44 +00:00
+ cp = estrndup((const char *)ms->search.s, ms->search.rm_len);
2013-04-07 20:15:56 +00:00
if (cp == NULL) {
file_oomem(ms, ms->search.rm_len);
return -1;
2015-03-08 16:24:44 +00:00
}
rval = file_printf(ms, F(ms, m, "%s"),
file_printable(sbuf, sizeof(sbuf), cp));
- free(cp);
+ efree(cp);
2015-03-08 16:24:44 +00:00
if (rval == -1)
return -1;
2017-10-11 16:18:55 +00:00
@@ -691,6 +680,15 @@
2016-01-25 03:45:20 +00:00
break;
}
+ case FILE_SEARCH:
+ if (file_printf(ms, F(ms, m, "%s"), m->value.s) == -1)
+ return -1;
+ if ((m->str_flags & REGEX_OFFSET_START))
+ t = ms->search.offset;
+ else
+ t = ms->search.offset + m->vallen;
+ break;
+
case FILE_DEFAULT:
case FILE_CLEAR:
if (file_printf(ms, "%s", m->desc) == -1)
2017-10-11 16:18:55 +00:00
@@ -1205,21 +1203,28 @@
2015-03-29 16:22:42 +00:00
return 0;
}
- if (m->str_flags & REGEX_LINE_COUNT) {
- linecnt = m->str_range;
- bytecnt = linecnt * 80;
- } else {
- linecnt = 0;
- bytecnt = m->str_range;
2016-01-25 03:45:20 +00:00
- }
2015-03-29 16:22:42 +00:00
+ /* bytecnt checks are to be kept for PHP, see cve-2014-3538.
+ PCRE might get stuck if the input buffer is too big. */
+ linecnt = m->str_range;
+ bytecnt = linecnt * 80;
2015-03-08 16:24:44 +00:00
- if (bytecnt == 0 || bytecnt > nbytes - offset)
- bytecnt = nbytes - offset;
2016-11-24 13:58:54 +00:00
- if (bytecnt > ms->regex_max)
- bytecnt = ms->regex_max;
2016-01-25 03:45:20 +00:00
+ if (bytecnt == 0) {
+ bytecnt = 1 << 14;
+ }
2015-03-29 16:22:42 +00:00
+ if (bytecnt > nbytes) {
+ bytecnt = nbytes;
+ }
2016-01-25 03:45:20 +00:00
+ if (offset > bytecnt) {
+ offset = bytecnt;
+ }
+ if (s == NULL) {
+ ms->search.s_len = 0;
+ ms->search.s = NULL;
+ return 0;
+ }
2015-03-08 16:24:44 +00:00
buf = RCAST(const char *, s) + offset;
2016-11-24 13:58:54 +00:00
- end = last = RCAST(const char *, s) + bytecnt + offset;
+ end = last = RCAST(const char *, s) + bytecnt;
2016-01-25 03:45:20 +00:00
/* mget() guarantees buf <= last */
2016-11-24 13:58:54 +00:00
for (lines = linecnt, b = buf; lines && b < end &&
((b = CAST(const char *,
2017-10-11 16:18:55 +00:00
@@ -1373,9 +1378,6 @@
2015-03-05 14:32:04 +00:00
m->type, m->flag, offset, o, nbytes,
2016-11-24 13:58:54 +00:00
*indir_count, *name_count);
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
-#ifndef COMPILE_ONLY
- file_mdump(m);
-#endif
}
if (m->flag & INDIR) {
2017-10-11 16:18:55 +00:00
@@ -1488,9 +1490,6 @@
if ((ms->flags & MAGIC_DEBUG) != 0) {
mdebug(offset, (char *)(void *)p,
sizeof(union VALUETYPE));
-#ifndef COMPILE_ONLY
- file_mdump(m);
-#endif
}
}
2017-10-11 16:18:55 +00:00
@@ -1572,15 +1571,15 @@
2014-03-10 13:17:47 +00:00
if (rv == 1) {
2016-11-24 13:58:54 +00:00
if ((ms->flags & MAGIC_NODESC) == 0 &&
2015-03-05 14:32:04 +00:00
file_printf(ms, F(ms, m, "%u"), offset) == -1) {
- free(rbuf);
+ if (rbuf) efree(rbuf);
2014-02-19 10:20:24 +00:00
return -1;
2015-03-05 14:32:04 +00:00
}
if (file_printf(ms, "%s", rbuf) == -1) {
- free(rbuf);
2015-03-08 16:24:44 +00:00
+ if (rbuf) efree(rbuf);
2013-04-07 20:15:56 +00:00
return -1;
2015-03-05 14:32:04 +00:00
}
}
2015-03-05 14:32:04 +00:00
- free(rbuf);
2015-03-08 16:24:44 +00:00
+ if (rbuf) efree(rbuf);
return rv;
2015-03-05 14:32:04 +00:00
case FILE_USE:
2017-10-11 16:18:55 +00:00
@@ -1703,6 +1702,41 @@
return file_strncmp(a, b, len, flags);
}
+public void
2014-10-25 10:06:17 +00:00
+convert_libmagic_pattern(zval *pattern, char *val, int len, int options)
+{
2014-10-25 10:06:17 +00:00
+ int i, j=0;
+ zend_string *t;
+
2014-10-25 10:06:17 +00:00
+ t = zend_string_alloc(len * 2 + 4, 0);
+
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j++] = '~';
2014-10-25 10:06:17 +00:00
+
+ for (i = 0; i < len; i++, j++) {
+ switch (val[i]) {
+ case '~':
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j++] = '\\';
+ ZSTR_VAL(t)[j] = '~';
2014-10-25 10:06:17 +00:00
+ break;
+ default:
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j] = val[i];
2014-10-25 10:06:17 +00:00
+ break;
+ }
2014-10-25 10:06:17 +00:00
+ }
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j++] = '~';
+
2014-10-25 10:06:17 +00:00
+ if (options & PCRE_CASELESS)
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j++] = 'i';
2014-10-25 10:06:17 +00:00
+
+ if (options & PCRE_MULTILINE)
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j++] = 'm';
+
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j]='\0';
+ ZSTR_LEN(t) = j;
2014-10-25 10:06:17 +00:00
+
+ ZVAL_NEW_STR(pattern, t);
+}
+
private int
magiccheck(struct magic_set *ms, struct magic *m)
{
2017-10-11 16:18:55 +00:00
@@ -1863,65 +1897,77 @@
break;
}
case FILE_REGEX: {
- int rc;
2015-03-05 14:32:04 +00:00
- file_regex_t rx;
- const char *search;
2014-10-25 10:06:17 +00:00
+ zval pattern;
+ int options = 0;
+ pcre_cache_entry *pce;
2015-03-05 14:32:04 +00:00
- if (ms->search.s == NULL)
- return 0;
+ options |= PCRE_MULTILINE;
2015-03-05 14:32:04 +00:00
- l = 0;
- rc = file_regcomp(&rx, m->value.s,
- REG_EXTENDED|REG_NEWLINE|
- ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
- if (rc) {
- file_regerror(&rx, rc, ms);
- v = (uint64_t)-1;
+ if (m->str_flags & STRING_IGNORE_CASE) {
+ options |= PCRE_CASELESS;
+ }
2015-03-05 14:32:04 +00:00
+
2014-10-25 10:06:17 +00:00
+ convert_libmagic_pattern(&pattern, (char *)m->value.s, m->vallen, options);
2015-03-05 14:32:04 +00:00
+
+ l = v = 0;
2014-12-13 22:06:14 +00:00
+ if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
2014-10-25 10:06:17 +00:00
+ zval_ptr_dtor(&pattern);
+ return -1;
2015-03-05 14:32:04 +00:00
} else {
2016-11-24 13:58:54 +00:00
- regmatch_t pmatch;
2015-03-05 14:32:04 +00:00
- size_t slen = ms->search.s_len;
- char *copy;
- if (slen != 0) {
2016-11-24 13:58:54 +00:00
- copy = CAST(char *, malloc(slen));
2015-03-05 14:32:04 +00:00
- if (copy == NULL) {
2016-11-24 13:58:54 +00:00
- file_regfree(&rx);
2015-03-05 14:32:04 +00:00
- file_error(ms, errno,
- "can't allocate %" SIZE_T_FORMAT "u bytes",
- slen);
- return -1;
- }
- memcpy(copy, ms->search.s, slen);
- copy[--slen] = '\0';
- search = copy;
+ /* pce now contains the compiled regex */
2014-10-25 10:06:17 +00:00
+ zval retval;
+ zval subpats;
+ char *haystack;
2015-03-05 14:32:04 +00:00
+
2014-10-25 10:06:17 +00:00
+ ZVAL_NULL(&retval);
+ ZVAL_NULL(&subpats);
+
+ /* Cut the search len from haystack, equals to REG_STARTEND */
+ haystack = estrndup(ms->search.s, ms->search.s_len);
+
+ /* match v = 0, no match v = 1 */
2016-01-25 03:45:20 +00:00
+ php_pcre_match_impl(pce, haystack, ms->search.s_len, &retval, &subpats, 0, 1, PREG_OFFSET_CAPTURE, 0);
+ /* Free haystack */
+ efree(haystack);
2015-03-05 14:32:04 +00:00
+
2014-10-25 10:06:17 +00:00
+ if (Z_LVAL(retval) < 0) {
+ zval_ptr_dtor(&subpats);
2014-10-25 10:06:17 +00:00
+ zval_ptr_dtor(&pattern);
+ return -1;
2014-10-25 10:06:17 +00:00
+ } else if ((Z_LVAL(retval) > 0) && (Z_TYPE(subpats) == IS_ARRAY)) {
+ /* Need to fetch global match which equals pmatch[0] */
2014-10-25 10:06:17 +00:00
+ zval *pzval;
+ HashTable *ht = Z_ARRVAL(subpats);
2016-01-25 03:45:20 +00:00
+ if ((pzval = zend_hash_index_find(ht, 0)) != NULL && Z_TYPE_P(pzval) == IS_ARRAY) {
2014-10-25 10:06:17 +00:00
+ /* If everything goes according to the master plan
+ tmpcopy now contains two elements:
+ 0 = the match
+ 1 = starting position of the match */
2016-01-25 03:45:20 +00:00
+ zval *match, *offset;
+ if ((match = zend_hash_index_find(Z_ARRVAL_P(pzval), 0)) &&
+ (offset = zend_hash_index_find(Z_ARRVAL_P(pzval), 1))) {
+ if (Z_TYPE_P(match) != IS_STRING && Z_TYPE_P(offset) != IS_LONG) {
+ goto error_out;
2014-10-25 10:06:17 +00:00
+ }
2016-01-25 03:45:20 +00:00
+ ms->search.s += Z_LVAL_P(offset); /* this is where the match starts */
+ ms->search.offset += Z_LVAL_P(offset); /* this is where the match starts as size_t */
+ ms->search.rm_len = Z_STRLEN_P(match) /* This is the length of the matched pattern */;
2014-10-25 10:06:17 +00:00
+ v = 0;
+ } else {
2016-01-25 03:45:20 +00:00
+ goto error_out;
2014-10-25 10:06:17 +00:00
+ }
2016-01-25 03:45:20 +00:00
+ } else {
+error_out:
+ zval_ptr_dtor(&subpats);
+ zval_ptr_dtor(&pattern);
+ return -1;
+ }
} else {
2017-10-11 16:18:55 +00:00
- search = CCAST(char *, "");
2016-01-25 03:45:20 +00:00
- copy = NULL;
- }
- rc = file_regexec(&rx, (const char *)search,
2016-11-24 13:58:54 +00:00
- 1, &pmatch, 0);
2016-01-25 03:45:20 +00:00
- free(copy);
- switch (rc) {
- case 0:
2016-11-24 13:58:54 +00:00
- ms->search.s += (int)pmatch.rm_so;
- ms->search.offset += (size_t)pmatch.rm_so;
2016-01-25 03:45:20 +00:00
- ms->search.rm_len =
2016-11-24 13:58:54 +00:00
- (size_t)(pmatch.rm_eo - pmatch.rm_so);
2016-01-25 03:45:20 +00:00
- v = 0;
- break;
-
- case REG_NOMATCH:
v = 1;
- break;
-
- default:
- file_regerror(&rx, rc, ms);
- v = (uint64_t)-1;
- break;
}
+ zval_ptr_dtor(&subpats);
2014-10-25 10:06:17 +00:00
+ zval_ptr_dtor(&pattern);
}
2015-03-05 14:32:04 +00:00
- file_regfree(&rx);
- if (v == (uint64_t)-1)
- return -1;
2014-02-19 10:20:24 +00:00
break;
}
case FILE_INDIRECT:
2014-05-04 19:34:17 +00:00
diff -u libmagic.orig/strcasestr.c libmagic/strcasestr.c
2017-10-11 16:18:55 +00:00
--- libmagic.orig/strcasestr.c 2014-09-11 17:05:33.000000000 +0200
+++ libmagic/strcasestr.c 2017-10-11 15:25:46.558395300 +0200
2015-03-05 14:32:04 +00:00
@@ -39,6 +39,8 @@
#include "file.h"
2014-05-04 19:34:17 +00:00
+#include "php_stdint.h"
+
#include <assert.h>
#include <ctype.h>
#include <string.h>