php-src/ext/fileinfo/libmagic.patch

3697 lines
93 KiB
Diff
Raw Normal View History

diff -ur libmagic.orig/apprentice.c libmagic/apprentice.c
--- libmagic.orig/apprentice.c 2020-05-09 20:57:15.000000000 +0200
+++ libmagic/apprentice.c 2020-08-29 19:56:29.638061530 +0200
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
+#include "php.h"
+
#include "file.h"
#ifndef lint
@@ -36,20 +38,35 @@
#endif /* lint */
#include "magic.h"
+#include "patchlevel.h"
#include <stdlib.h>
+
+#if defined(__hpux) && !defined(HAVE_STRTOULL)
+#if SIZEOF_LONG == 8
+# define strtoull strtoul
+#else
+# define strtoull __strtoull
2019-05-30 12:09:00 +00:00
+#endif
2018-11-05 21:25:31 +00:00
+#endif
+
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#define strtoull _strtoui64
+#else
#ifdef HAVE_UNISTD_H
2019-05-30 12:09:00 +00:00
#include <unistd.h>
#endif
2019-05-30 12:09:00 +00:00
-#include <stddef.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>
2019-05-30 12:09:00 +00:00
+
+#ifndef SSIZE_MAX
+#define MAXMAGIC_SIZE ((ssize_t)0x7fffffff)
+#else
+#define MAXMAGIC_SIZE SSIZE_MAX
#endif
-#include <dirent.h>
2015-03-05 14:32:04 +00:00
-#include <limits.h>
2019-05-30 12:09:00 +00:00
#define EATAB {while (isascii(CAST(unsigned char, *l)) && \
@@ -66,6 +83,10 @@
#endif
#endif
+#ifndef offsetof
+#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
+#endif
+
#ifndef MAP_FAILED
#define MAP_FAILED (void *) -1
#endif
@@ -128,10 +149,7 @@
2019-04-11 13:20:05 +00:00
private uint32_t swap4(uint32_t);
private uint64_t swap8(uint64_t);
private char *mkdbname(struct magic_set *, const char *, int);
-private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
- size_t);
private struct magic_map *apprentice_map(struct magic_set *, const char *);
-private int check_buffer(struct magic_set *, struct magic_map *, const char *);
private void apprentice_unmap(struct magic_map *);
private int apprentice_compile(struct magic_set *, struct magic_map *,
const char *);
@@ -167,38 +185,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];
@@ -417,7 +404,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;
@@ -502,10 +489,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 *
@@ -514,7 +507,7 @@
2013-04-07 20:15:56 +00:00
struct magic_set *ms;
size_t i, len;
2019-05-30 12:09:00 +00:00
- if ((ms = CAST(struct magic_set *, calloc(CAST(size_t, 1u),
+ if ((ms = CAST(struct magic_set *, ecalloc(CAST(size_t, 1u),
2013-04-07 20:15:56 +00:00
sizeof(struct magic_set)))) == NULL)
return NULL;
@@ -527,7 +520,7 @@
ms->o.blen = 0;
2013-04-07 20:15:56 +00:00
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;
@@ -545,48 +538,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;
@@ -609,7 +589,7 @@
2019-05-30 12:09:00 +00:00
{
if (ml->map)
apprentice_unmap(CAST(struct magic_map *, ml->map));
- free(ml);
+ efree(ml);
}
private void
@@ -628,51 +608,6 @@
2019-05-30 12:09:00 +00:00
mlist_free_one(mlist);
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;
-
2018-04-29 13:49:56 +00:00
- (void)file_reset(ms, 0);
2015-03-08 16:24:44 +00:00
-
- init_file_tables();
-
- for (i = 0; i < MAGIC_SETS; i++) {
- mlist_free(ms->mlist[i]);
- if ((ms->mlist[i] = mlist_alloc()) == NULL) {
- file_oomem(ms, sizeof(*ms->mlist[i]));
- goto fail;
- }
- }
-
- for (i = 0; i < nbufs; i++) {
- map = apprentice_buf(ms, bufs[i], sizes[i]);
- if (map == NULL)
- goto fail;
-
- for (j = 0; j < MAGIC_SETS; j++) {
- if (add_mlist(ms->mlist[j], map, j) == -1) {
- file_oomem(ms, sizeof(*ml));
- goto fail;
- }
- }
- }
-
- return 0;
-fail:
- mlist_free_all(ms);
2015-03-08 16:24:44 +00:00
- 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)
@@ -683,12 +618,28 @@
2018-04-29 13:49:56 +00:00
2019-05-30 12:09:00 +00:00
(void)file_reset(ms, 0);
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;
}
@@ -701,7 +652,7 @@
mlist_free(ms->mlist[j]);
ms->mlist[j] = NULL;
2013-04-07 20:15:56 +00:00
}
- free(mfn);
+ efree(mfn);
return -1;
}
}
@@ -718,7 +669,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++) {
@@ -1158,7 +1109,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;
@@ -1179,13 +1130,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);
@@ -1195,8 +1152,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') {
@@ -1255,8 +1211,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);
+ efree(line);
+ php_stream_close(stream);
}
/*
@@ -1335,7 +1291,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;
}
@@ -1357,8 +1313,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 *
@@ -1367,18 +1323,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;
2018-04-29 13:49:56 +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;
@@ -1390,52 +1347,50 @@
(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) {
size_t mlen;
- char **nfilearr;
2013-04-07 20:15:56 +00:00
maxfiles = (maxfiles + 1) * 2;
mlen = maxfiles * sizeof(*filearr);
- if ((nfilearr = CAST(char **,
2013-04-07 20:15:56 +00:00
- realloc(filearr, mlen))) == NULL) {
+ if ((filearr = CAST(char **,
2013-04-07 20:15:56 +00:00
+ 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 = nfilearr;
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);
2019-05-30 12:09:00 +00:00
if (filearr) {
qsort(filearr, files, sizeof(*filearr), cmpstrp);
for (i = 0; i < files; i++) {
load_1(ms, action, filearr[i], &errs, mset);
- free(filearr[i]);
+ efree(filearr[i]);
}
- free(filearr);
- filearr = NULL;
2019-05-30 12:09:00 +00:00
+ 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);
@@ -1470,7 +1425,6 @@
}
out:
- free(filearr);
for (j = 0; j < MAGIC_SETS; j++)
magic_entry_free(mset[j].me, mset[j].count);
@@ -1906,7 +1860,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;
@@ -1921,7 +1875,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;
}
@@ -2137,7 +2091,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");
@@ -2146,7 +2100,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) {
@@ -2266,7 +2220,7 @@
*/
private int
parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line,
- size_t len __attribute__((__unused__)))
+ size_t len)
{
const char *l = line;
char *el;
@@ -2328,8 +2282,7 @@
2015-03-08 16:24:44 +00:00
private int
parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
- size_t llen, off_t off, size_t len, const char *name, const char *extra,
- int nt)
+ size_t llen, zend_off_t off, size_t len, const char *name, const char *extra, int nt)
2015-03-08 16:24:44 +00:00
{
size_t i;
const char *l = line;
@@ -2694,14 +2647,19 @@
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);
+ zend_string *pattern;
2015-03-08 16:24:44 +00:00
+ int options = 0;
+ pcre_cache_entry *pce;
+
+ pattern = convert_libmagic_pattern(m->value.s, strlen(m->value.s), options);
2015-03-08 16:24:44 +00:00
+
+ if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) {
+ zend_string_release(pattern);
2018-04-29 13:49:56 +00:00
+ 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;
+ zend_string_release(pattern);
2015-03-08 16:24:44 +00:00
+
2018-11-05 21:25:31 +00:00
+ return 0;
2015-03-08 16:24:44 +00:00
}
2015-03-05 14:32:04 +00:00
return 0;
2018-04-29 13:49:56 +00:00
default:
@@ -3057,120 +3015,83 @@
2019-04-11 13:20:05 +00:00
}
/*
- * handle a buffer containing a compiled file.
- */
-private struct magic_map *
-apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
-{
- struct magic_map *map;
-
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
- file_oomem(ms, sizeof(*map));
- return NULL;
- }
- map->len = len;
- map->p = buf;
- map->type = MAP_TYPE_USER;
- if (check_buffer(ms, map, "buffer") != 0) {
- apprentice_unmap(map);
- return NULL;
- }
- return map;
-}
-
-/*
* handle a compiled file.
2019-04-11 13:20:05 +00:00
*/
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 = 0, nentries;
2015-03-05 14:32:04 +00:00
+ 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;
+
+
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;
}
- 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) {
2019-05-30 12:09:00 +00:00
+ goto error;
+ }
+ }
+#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) {
2019-05-30 12:09:00 +00:00
+#ifndef PHP_WIN32
+ 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;
}
2019-05-30 12:09:00 +00:00
- if (st.st_size < 8 || st.st_size > maxoff_t()) {
+#endif
+ if (st.sb.st_size < 8 || st.sb.st_size > maxoff_t()) {
2015-03-05 14:32:04 +00:00
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;
}
2019-05-30 12:09:00 +00:00
- map->len = CAST(size_t, st.st_size);
-#ifdef QUICK
2016-11-24 13:58:54 +00:00
- map->type = MAP_TYPE_MMAP;
2019-05-30 12:09:00 +00:00
- if ((map->p = mmap(0, CAST(size_t, st.st_size), PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_FILE, fd, CAST(off_t, 0))) == MAP_FAILED) {
- file_error(ms, errno, "cannot map `%s'", dbname);
2013-04-07 20:15:56 +00:00
- goto error;
- }
-#else
2019-05-30 12:09:00 +00:00
map->type = MAP_TYPE_MALLOC;
2013-04-07 20:15:56 +00:00
- if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
2019-05-30 12:09:00 +00:00
- file_oomem(ms, map->len);
- goto error;
- }
2013-04-07 20:15:56 +00:00
- if (read(fd, map->p, map->len) != (ssize_t)map->len) {
- file_badread(ms);
- goto error;
- }
-#endif
- (void)close(fd);
- fd = -1;
2019-05-30 12:09:00 +00:00
+ map->len = CAST(size_t, st.sb.st_size);
+ map->p = CAST(void *, emalloc(map->len));
- if (check_buffer(ms, map, dbname) != 0) {
- goto error;
- }
-#ifdef QUICK
- if (mprotect(map->p, CAST(size_t, st.st_size), PROT_READ) == -1) {
- file_error(ms, errno, "cannot mprotect `%s'", dbname);
2013-04-07 20:15:56 +00:00
+ 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;
}
-#endif
-
- free(dbname);
- return map;
-
-error:
- if (fd != -1)
- (void)close(fd);
- apprentice_unmap(map);
- free(dbname);
- return rv;
-}
2015-03-05 14:32:04 +00:00
-private int
-check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
-{
- uint32_t *ptr;
- uint32_t entries, nentries;
- uint32_t version;
- int i, needsbyteswap;
+ php_stream_close(stream);
+ stream = NULL;
- ptr = CAST(uint32_t *, map->p);
+internal_loaded:
2013-04-07 20:15:56 +00:00
+ ptr = (uint32_t *)(void *)map->p;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
file_error(ms, 0, "bad magic in `%s'", dbname);
- return -1;
2015-03-05 14:32:04 +00:00
+ goto error;
}
needsbyteswap = 1;
} else
@@ -3180,17 +3101,29 @@
else
version = ptr[1];
if (version != VERSIONNO) {
- file_error(ms, 0, "File %s supports only version %d magic "
- "files. `%s' is version %d", VERSION,
+ file_error(ms, 0, "File %d.%d supports only version %d magic "
+ "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
VERSIONNO, dbname, version);
- return -1;
+ goto error;
2016-11-24 13:58:54 +00:00
}
- entries = CAST(uint32_t, map->len / sizeof(struct magic));
- if ((entries * sizeof(struct magic)) != map->len) {
- file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
- "a multiple of %" SIZE_T_FORMAT "u",
- dbname, map->len, sizeof(struct magic));
- return -1;
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;
+ }
}
map->magic[0] = CAST(struct magic *, map->p) + 1;
nentries = 0;
@@ -3203,15 +3136,29 @@
map->magic[i + 1] = map->magic[i] + map->nmagic[i];
nentries += map->nmagic[i];
}
- if (entries != nentries + 1) {
2013-04-07 20:15:56 +00:00
+ if (NULL != fn && entries != nentries + 1) {
file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
dbname, entries, nentries + 1);
- return -1;
+ goto error;
2016-11-24 13:58:54 +00:00
}
if (needsbyteswap)
for (i = 0; i < MAGIC_SETS; i++)
byteswap(map->magic[i], map->nmagic[i]);
- return 0;
+
+ if (dbname) {
+ efree(dbname);
+ }
+ return map;
+
+error:
+ if (stream) {
+ php_stream_close(stream);
+ }
+ apprentice_unmap(map);
+ if (dbname) {
+ efree(dbname);
+ }
2016-11-24 13:58:54 +00:00
+ return NULL;
}
2019-04-11 13:20:05 +00:00
/*
@@ -3222,7 +3169,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;
@@ -3231,14 +3177,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);
2019-05-30 12:09:00 +00:00
if (dbname == NULL)
goto out;
2019-05-30 12:09:00 +00:00
- if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
2013-04-07 20:15:56 +00:00
- {
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;
}
@@ -3247,26 +3196,25 @@
2015-03-08 16:24:44 +00:00
hdr.h[1] = VERSIONNO;
memcpy(hdr.h + 2, map->nmagic, nm);
2019-05-30 12:09:00 +00:00
- if (write(fd, &hdr, sizeof(hdr)) != CAST(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);
2018-04-29 13:49:56 +00:00
- goto out2;
+ goto out;
}
2013-04-07 20:15:56 +00:00
for (i = 0; i < MAGIC_SETS; i++) {
len = m * map->nmagic[i];
2019-05-30 12:09:00 +00:00
- if (write(fd, map->magic[i], len) != CAST(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);
2018-04-29 13:49:56 +00:00
- goto out2;
+ goto out;
2013-04-07 20:15:56 +00:00
}
}
2013-04-07 20:15:56 +00:00
+ if (stream) {
+ php_stream_close(stream);
+ }
rv = 0;
2018-04-29 13:49:56 +00:00
-out2:
- if (fd != -1)
- (void)close(fd);
out:
2016-11-24 13:58:54 +00:00
- apprentice_unmap(map);
- free(dbname);
+ efree(dbname);
return rv;
}
@@ -3300,17 +3248,18 @@
q++;
/* Compatibility with old code that looked in .mime */
if (ms->flags & MAGIC_MIME) {
2019-05-30 12:09:00 +00:00
- if (asprintf(&buf, "%.*s.mime%s", CAST(int, q - fn), fn, ext)
- < 0)
2013-04-07 20:15:56 +00:00
- return NULL;
- if (access(buf, R_OK) != -1) {
2019-05-30 12:09:00 +00:00
+ spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", CAST(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);
}
2019-05-30 12:09:00 +00:00
- if (asprintf(&buf, "%.*s%s", CAST(int, q - fn), fn, ext) < 0)
2013-04-07 20:15:56 +00:00
- return NULL;
2019-05-30 12:09:00 +00:00
+ spprintf(&buf, MAXPATHLEN, "%.*s%s", CAST(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)
@@ -3400,7 +3349,7 @@
2019-05-30 12:09:00 +00:00
m->offset = swap4(CAST(uint32_t, m->offset));
m->in_offset = swap4(CAST(uint32_t, m->in_offset));
m->lineno = swap4(CAST(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 -ur libmagic.orig/ascmagic.c libmagic/ascmagic.c
--- libmagic.orig/ascmagic.c 2020-06-15 02:01:01.000000000 +0200
+++ libmagic/ascmagic.c 2020-08-29 02:05:56.212049441 +0200
@@ -50,7 +50,7 @@
#define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
|| (x) == 0x85 || (x) == '\f')
-private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
+private unsigned char *encode_utf8(unsigned char *, size_t, unicodechar *, size_t);
private size_t trim_nuls(const unsigned char *, size_t);
/*
@@ -69,7 +69,7 @@
protected int
file_ascmagic(struct magic_set *ms, const struct buffer *b, int text)
{
- unichar *ubuf = NULL;
+ unicodechar *ubuf = NULL;
size_t ulen = 0;
int rv = 1;
struct buffer bb;
@@ -95,14 +95,14 @@
2018-04-29 13:49:56 +00:00
rv = file_ascmagic_with_encoding(ms, &bb,
ubuf, ulen, code, type, text);
- free(ubuf);
+ efree(ubuf);
return rv;
}
protected int
file_ascmagic_with_encoding(struct magic_set *ms,
- const struct buffer *b, unichar *ubuf, size_t ulen, const char *code,
+ const struct buffer *b, unicodechar *ubuf, size_t ulen, const char *code,
const char *type, int text)
{
struct buffer bb;
@@ -142,7 +142,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
}
@@ -324,7 +324,8 @@
}
rv = 1;
done:
- free(utf8_buf);
+ if (utf8_buf)
+ efree(utf8_buf);
return rv;
}
@@ -334,7 +335,7 @@
* after end of string, or NULL if an invalid character is found.
*/
private unsigned char *
-encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen)
+encode_utf8(unsigned char *buf, size_t len, unicodechar *ubuf, size_t ulen)
{
size_t i;
unsigned char *end = buf + len;
diff -ur libmagic.orig/buffer.c libmagic/buffer.c
--- libmagic.orig/buffer.c 2020-02-16 16:52:49.000000000 +0100
+++ libmagic/buffer.c 2020-08-29 02:05:56.212049441 +0200
2019-05-30 12:09:00 +00:00
@@ -31,19 +31,23 @@
2018-04-29 13:49:56 +00:00
#endif /* lint */
#include "magic.h"
+#ifdef PHP_WIN32
+#include "win32/unistd.h"
+#else
#include <unistd.h>
+#endif
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
2019-05-30 12:09:00 +00:00
void
-buffer_init(struct buffer *b, int fd, const struct stat *st, const void *data,
+buffer_init(struct buffer *b, int fd, const zend_stat_t *st, const void *data,
size_t len)
2018-04-29 13:49:56 +00:00
{
b->fd = fd;
2019-05-30 12:09:00 +00:00
if (st)
memcpy(&b->st, st, sizeof(b->st));
- else if (b->fd == -1 || fstat(b->fd, &b->st) == -1)
+ else if (b->fd == -1 || zend_fstat(b->fd, &b->st) == -1)
2018-04-29 13:49:56 +00:00
memset(&b->st, 0, sizeof(b->st));
b->fbuf = data;
b->flen = len;
2019-05-30 12:09:00 +00:00
@@ -55,7 +59,7 @@
2018-04-29 13:49:56 +00:00
void
buffer_fini(struct buffer *b)
{
- free(b->ebuf);
+ efree(b->ebuf);
2018-04-29 13:49:56 +00:00
}
int
@@ -71,12 +75,14 @@
2018-04-29 13:49:56 +00:00
2019-05-30 12:09:00 +00:00
b->elen = CAST(size_t, b->st.st_size) < b->flen ?
CAST(size_t, b->st.st_size) : b->flen;
2018-04-29 13:49:56 +00:00
- if ((b->ebuf = malloc(b->elen)) == NULL)
+ if ((b->ebuf = emalloc(b->elen)) == NULL)
goto out;
b->eoff = b->st.st_size - b->elen;
- if (pread(b->fd, b->ebuf, b->elen, b->eoff) == -1) {
- free(b->ebuf);
+ if (FINFO_LSEEK_FUNC(b->fd, b->eoff, SEEK_SET) == (zend_off_t)-1 ||
+ FINFO_READ_FUNC(b->fd, b->ebuf, b->elen) != (ssize_t)b->elen)
+ {
+ efree(b->ebuf);
b->ebuf = NULL;
2018-04-29 13:49:56 +00:00
goto out;
}
diff -ur libmagic.orig/cdf.c libmagic/cdf.c
--- libmagic.orig/cdf.c 2019-09-30 17:42:50.000000000 +0200
+++ libmagic/cdf.c 2020-08-29 02:05:56.212049441 +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>
@@ -85,40 +95,9 @@
2018-04-29 13:49:56 +00:00
CDF_TOLE8(CAST(uint64_t, x))))
#define CDF_GETUINT32(x, y) cdf_getuint32(x, y)
2017-10-11 16:18:55 +00:00
2018-04-29 13:49:56 +00:00
-#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
-#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
-#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
-
-
-/*ARGSUSED*/
-static void *
2017-10-11 16:18:55 +00:00
-cdf_malloc(const char *file __attribute__((__unused__)),
- size_t line __attribute__((__unused__)), size_t n)
2018-04-29 13:49:56 +00:00
-{
2019-05-30 12:09:00 +00:00
- DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
- file, line, __func__, n));
2018-04-29 13:49:56 +00:00
- return malloc(n);
-}
-
-/*ARGSUSED*/
-static void *
2017-10-11 16:18:55 +00:00
-cdf_realloc(const char *file __attribute__((__unused__)),
- size_t line __attribute__((__unused__)), void *p, size_t n)
2018-04-29 13:49:56 +00:00
-{
2019-05-30 12:09:00 +00:00
- DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
- file, line, __func__, n));
2018-04-29 13:49:56 +00:00
- return realloc(p, n);
-}
-
-/*ARGSUSED*/
-static void *
2017-10-11 16:18:55 +00:00
-cdf_calloc(const char *file __attribute__((__unused__)),
- size_t line __attribute__((__unused__)), size_t n, size_t u)
2018-04-29 13:49:56 +00:00
-{
2019-05-30 12:09:00 +00:00
- DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u %"
- SIZE_T_FORMAT "u\n", file, line, __func__, n, u));
2018-04-29 13:49:56 +00:00
- return calloc(n, u);
-}
+#define CDF_MALLOC(n) emalloc(n)
+#define CDF_REALLOC(p, n) erealloc(p, n)
+#define CDF_CALLOC(n, u) ecalloc(n, u)
/*
* swap a short
@@ -314,7 +293,7 @@
2018-04-29 13:49:56 +00:00
scn->sst_len = 0;
scn->sst_dirlen = 0;
scn->sst_ss = 0;
- free(scn->sst_tab);
+ efree(scn->sst_tab);
scn->sst_tab = NULL;
return -1;
}
@@ -322,9 +301,11 @@
static size_t
cdf_check_stream(const cdf_stream_t *sst, const cdf_header_t *h)
{
+#ifndef NDEBUG
size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
assert(ss == sst->sst_ss);
+#endif
return sst->sst_ss;
}
@@ -347,11 +328,11 @@
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)
{
2019-05-30 12:09:00 +00:00
size_t siz = CAST(size_t, off + len);
2014-10-25 10:06:17 +00:00
2019-05-30 12:09:00 +00:00
- if (CAST(off_t, off + len) != CAST(off_t, siz))
+ if (CAST(zend_off_t, off + len) != CAST(zend_off_t, siz))
2016-11-24 13:58:54 +00:00
goto out;
if (info->i_buf != NULL && info->i_len >= siz) {
@@ -362,7 +343,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
2019-05-30 12:09:00 +00:00
- if (pread(info->i_fd, buf, len, off) != CAST(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;
2019-05-30 12:09:00 +00:00
return CAST(ssize_t, len);
@@ -377,7 +361,7 @@
2014-10-25 10:06:17 +00:00
char buf[512];
(void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
2019-05-30 12:09:00 +00:00
- if (cdf_read(info, CAST(off_t, 0), buf, sizeof(buf)) == -1)
+ if (cdf_read(info, CAST(zend_off_t, 0), buf, sizeof(buf)) == -1)
2014-10-25 10:06:17 +00:00
return -1;
cdf_unpack_header(h, buf);
cdf_swap_header(h);
@@ -524,14 +508,14 @@
2018-04-29 13:49:56 +00:00
}
out:
sat->sat_len = i;
- free(msa);
+ efree(msa);
return 0;
out3:
errno = EFTYPE;
out2:
- free(msa);
+ efree(msa);
out1:
- free(sat->sat_tab);
+ efree(sat->sat_tab);
return -1;
}
@@ -699,7 +683,7 @@
2018-04-29 13:49:56 +00:00
return -1;
if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
- free(dir->dir_tab);
+ efree(dir->dir_tab);
return -1;
}
@@ -722,11 +706,11 @@
2018-04-29 13:49:56 +00:00
if (NEED_SWAP)
for (i = 0; i < dir->dir_len; i++)
cdf_swap_dir(&dir->dir_tab[i]);
- free(buf);
+ efree(buf);
return 0;
out:
- free(dir->dir_tab);
- free(buf);
+ efree(dir->dir_tab);
+ efree(buf);
errno = EFTYPE;
return -1;
}
@@ -771,7 +755,7 @@
2018-04-29 13:49:56 +00:00
out:
errno = EFTYPE;
out1:
- free(ssat->sat_tab);
+ efree(ssat->sat_tab);
return -1;
}
@@ -933,7 +917,7 @@
2018-04-29 13:49:56 +00:00
*maxcount = newcount;
return inp;
out:
- free(*info);
+ efree(*info);
*maxcount = 0;
*info = NULL;
return NULL;
@@ -1115,7 +1099,7 @@
2018-04-29 13:49:56 +00:00
}
return 0;
out:
- free(*info);
+ efree(*info);
*info = NULL;
*count = 0;
*maxcount = 0;
@@ -1407,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" };
@@ -1449,7 +1433,7 @@
2018-04-29 13:49:56 +00:00
break;
}
cdf_dump_stream(&scn);
- free(scn.sst_tab);
+ efree(scn.sst_tab);
break;
default:
break;
@@ -1462,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;
@@ -1547,7 +1531,7 @@
2018-04-29 13:49:56 +00:00
(void)fprintf(stderr, "Class %s\n", buf);
(void)fprintf(stderr, "Count %d\n", ssi.si_count);
cdf_dump_property_info(info, count);
- free(info);
+ efree(info);
}
@@ -1568,7 +1552,7 @@
2018-04-29 13:49:56 +00:00
cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
cdf_ctime(&ts.tv_sec, tbuf));
}
- free(cat);
+ efree(cat);
}
#endif
diff -ur libmagic.orig/cdf.h libmagic/cdf.h
--- libmagic.orig/cdf.h 2019-09-30 17:42:50.000000000 +0200
+++ libmagic/cdf.h 2020-07-04 12:40:36.663619335 +0200
2018-04-30 12:29:55 +00:00
@@ -35,10 +35,10 @@
#ifndef _H_CDF_
#define _H_CDF_
-#ifdef WIN32
+#ifdef PHP_WIN32
#include <winsock2.h>
2018-04-30 12:29:55 +00:00
-#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
diff -ur libmagic.orig/cdf_time.c libmagic/cdf_time.c
2019-05-30 12:09:00 +00:00
--- libmagic.orig/cdf_time.c 2019-03-12 21:43:05.000000000 +0100
+++ libmagic/cdf_time.c 2020-07-04 12:40:36.667619309 +0200
2019-03-08 20:55:15 +00:00
@@ -23,6 +23,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include "php.h"
#include "file.h"
@@ -152,7 +153,7 @@
#endif
#ifdef notyet
struct tm tm;
- if (gmtime_r(&ts->ts_sec, &tm) == NULL) {
+ if (php_gmtime_r(&ts->ts_sec, &tm) == NULL) {
errno = EINVAL;
return -1;
}
@@ -168,7 +169,7 @@
char *
cdf_ctime(const time_t *sec, char *buf)
{
- char *ptr = ctime_r(sec, buf);
+ char *ptr = php_ctime_r(sec, buf);
if (ptr != NULL)
return buf;
(void)snprintf(buf, 26, "*Bad* %#16.16" INT64_T_FORMAT "x\n",
diff -ur libmagic.orig/compress.c libmagic/compress.c
--- libmagic.orig/compress.c 2020-05-31 02:11:06.000000000 +0200
+++ libmagic/compress.c 2020-08-29 02:05:56.212049441 +0200
@@ -51,7 +51,7 @@
#ifndef HAVE_SIG_T
2015-03-29 16:22:42 +00:00
typedef void (*sig_t)(int);
#endif /* HAVE_SIG_T */
-#if !defined(__MINGW32__) && !defined(WIN32) && !defined(__MINGW64__)
+#ifndef PHP_WIN32
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_WAIT_H
@@ -60,13 +60,14 @@
2017-01-05 22:32:30 +00:00
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
2019-05-30 12:09:00 +00:00
-
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
2018-04-29 13:49:56 +00:00
-#if defined(HAVE_BZLIB_H) && defined(BZLIBSUPPORT)
+#undef FIONREAD
2019-05-30 12:09:00 +00:00
+
+#if defined(PHP_FILEINFO_UNCOMPRESS)
#define BUILTIN_BZLIB
#include <bzlib.h>
#endif
@@ -118,6 +119,8 @@
}
#endif
+#ifdef PHP_FILEINFO_UNCOMPRESS
+
static int
lzmacmp(const unsigned char *buf)
{
@@ -221,8 +224,7 @@
size_t *);
2016-11-24 13:58:54 +00:00
#endif
2019-05-30 12:09:00 +00:00
2016-11-24 13:58:54 +00:00
-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);
2019-05-30 12:09:00 +00:00
private int
@@ -294,7 +296,7 @@
2018-04-29 13:49:56 +00:00
if (urv == ERRDATA)
2019-05-30 12:09:00 +00:00
prv = format_decompression_error(ms, i, newbuf);
else
- prv = file_buffer(ms, -1, NULL, name, newbuf, nsz);
+ prv = file_buffer(ms, NULL, NULL, name, newbuf, nsz);
if (prv == -1)
2018-04-29 13:49:56 +00:00
goto error;
2019-05-30 12:09:00 +00:00
rv = 1;
@@ -311,17 +313,17 @@
2019-05-30 12:09:00 +00:00
* XXX: If file_buffer fails here, we overwrite
* the compressed text. FIXME.
*/
- if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1) {
+ if (file_buffer(ms, NULL, NULL, NULL, buf, nbytes) == -1) {
if (file_pop_buffer(ms, pb) != NULL)
abort();
goto error;
}
2018-04-29 13:49:56 +00:00
if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
if (file_printf(ms, "%s", rbuf) == -1) {
- free(rbuf);
+ efree(rbuf);
goto error;
}
- free(rbuf);
+ efree(rbuf);
}
if (!mime && file_printf(ms, ")") == -1)
goto error;
@@ -342,7 +344,8 @@
2019-05-30 12:09:00 +00:00
if (sa_saved && sig_act.sa_handler != SIG_IGN)
(void)sigaction(SIGPIPE, &sig_act, NULL);
- 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;
@@ -377,7 +380,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
@@ -425,7 +428,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;
@@ -504,13 +507,13 @@
2012-04-02 15:27:23 +00:00
return -1;
}
(void)close(tfd);
2019-05-30 12:09:00 +00:00
- if (lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(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)
@@ -561,7 +564,7 @@
2018-04-29 13:49:56 +00:00
int rc;
z_stream z;
2019-05-30 12:09:00 +00:00
- if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
2018-04-29 13:49:56 +00:00
+ if ((*newch = CAST(unsigned char *, emalloc(bytes_max + 1))) == NULL)
return makeerror(newch, n, "No buffer, %s", strerror(errno));
z.next_in = CCAST(Bytef *, old);
@@ -967,3 +970,4 @@
2019-05-30 12:09:00 +00:00
return rv;
}
2019-05-30 12:09:00 +00:00
#endif
+#endif
diff -ur libmagic.orig/der.c libmagic/der.c
--- libmagic.orig/der.c 2020-06-15 02:01:01.000000000 +0200
+++ libmagic/der.c 2020-08-29 11:56:12.303522747 +0200
@@ -54,7 +54,9 @@
2016-11-24 13:58:54 +00:00
#include "magic.h"
#include "der.h"
#else
+#ifndef PHP_WIN32
#include <sys/mman.h>
+#endif
#include <sys/stat.h>
#include <err.h>
#endif
diff -ur libmagic.orig/elfclass.h libmagic/elfclass.h
2019-05-30 12:09:00 +00:00
--- libmagic.orig/elfclass.h 2019-02-20 02:30:19.000000000 +0100
+++ libmagic/elfclass.h 2020-07-04 12:40:36.667619309 +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,
2019-05-30 12:09:00 +00:00
- CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
+ CAST(zend_off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
CAST(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,
2019-05-30 12:09:00 +00:00
- CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
+ CAST(zend_off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
CAST(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,
2019-05-30 12:09:00 +00:00
- CAST(off_t, elf_getu(swap, elfhdr.e_shoff)), shnum,
+ CAST(zend_off_t, elf_getu(swap, elfhdr.e_shoff)), shnum,
CAST(size_t, elf_getu16(swap, elfhdr.e_shentsize)),
2015-03-05 14:32:04 +00:00
fsize, elf_getu16(swap, elfhdr.e_machine),
2019-05-30 12:09:00 +00:00
CAST(int, elf_getu16(swap, elfhdr.e_shstrndx)),
diff -ur libmagic.orig/encoding.c libmagic/encoding.c
--- libmagic.orig/encoding.c 2019-06-10 23:34:41.000000000 +0200
+++ libmagic/encoding.c 2020-08-29 02:05:56.212049441 +0200
@@ -43,14 +43,14 @@
#include <stdlib.h>
-private int looks_ascii(const unsigned char *, size_t, unichar *, size_t *);
-private int looks_utf8_with_BOM(const unsigned char *, size_t, unichar *,
+private int looks_ascii(const unsigned char *, size_t, unicodechar *, size_t *);
+private int looks_utf8_with_BOM(const unsigned char *, size_t, unicodechar *,
size_t *);
-private int looks_utf7(const unsigned char *, size_t, unichar *, size_t *);
-private int looks_ucs16(const unsigned char *, size_t, unichar *, size_t *);
-private int looks_ucs32(const unsigned char *, size_t, unichar *, size_t *);
-private int looks_latin1(const unsigned char *, size_t, unichar *, size_t *);
-private int looks_extended(const unsigned char *, size_t, unichar *, size_t *);
+private int looks_utf7(const unsigned char *, size_t, unicodechar *, size_t *);
+private int looks_ucs16(const unsigned char *, size_t, unicodechar *, size_t *);
+private int looks_ucs32(const unsigned char *, size_t, unicodechar *, size_t *);
+private int looks_latin1(const unsigned char *, size_t, unicodechar *, size_t *);
+private int looks_extended(const unsigned char *, size_t, unicodechar *, size_t *);
private void from_ebcdic(const unsigned char *, size_t, unsigned char *);
#ifdef DEBUG_ENCODING
@@ -62,11 +62,11 @@
/*
* Try to determine whether text is in some character code we can
* identify. Each of these tests, if it succeeds, will leave
- * the text converted into one-unichar-per-character Unicode in
+ * the text converted into one-unicodechar-per-character Unicode in
* ubuf, and the number of characters converted in ulen.
*/
protected int
-file_encoding(struct magic_set *ms, const struct buffer *b, unichar **ubuf,
+file_encoding(struct magic_set *ms, const struct buffer *b, unicodechar **ubuf,
size_t *ulen, const char **code, const char **code_mime, const char **type)
{
const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
@@ -74,7 +74,7 @@
size_t mlen;
int rv = 1, ucs_type;
unsigned char *nbuf = NULL;
- unichar *udefbuf;
+ unicodechar *udefbuf;
size_t udeflen;
if (ubuf == NULL)
@@ -88,13 +88,13 @@
2018-04-29 13:49:56 +00:00
*code_mime = "binary";
mlen = (nbytes + 1) * sizeof((*ubuf)[0]);
2019-05-30 12:09:00 +00:00
- if ((*ubuf = CAST(unichar *, calloc(CAST(size_t, 1), mlen))) == NULL) {
+ if ((*ubuf = CAST(unicodechar *, ecalloc(CAST(size_t, 1), mlen))) == NULL) {
2018-04-29 13:49:56 +00:00
file_oomem(ms, mlen);
goto done;
}
mlen = (nbytes + 1) * sizeof(nbuf[0]);
2019-05-30 12:09:00 +00:00
if ((nbuf = CAST(unsigned char *,
- calloc(CAST(size_t, 1), mlen))) == NULL) {
+ ecalloc(CAST(size_t, 1), mlen))) == NULL) {
2018-04-29 13:49:56 +00:00
file_oomem(ms, mlen);
goto done;
}
@@ -163,9 +163,9 @@
2018-04-29 13:49:56 +00:00
}
done:
- free(nbuf);
+ efree(nbuf);
if (ubuf == &udefbuf)
- free(udefbuf);
+ efree(udefbuf);
return rv;
}
@@ -250,7 +250,7 @@
};
private int
-looks_ascii(const unsigned char *buf, size_t nbytes, unichar *ubuf,
+looks_ascii(const unsigned char *buf, size_t nbytes, unicodechar *ubuf,
size_t *ulen)
{
size_t i;
@@ -270,7 +270,7 @@
}
private int
-looks_latin1(const unsigned char *buf, size_t nbytes, unichar *ubuf, size_t *ulen)
+looks_latin1(const unsigned char *buf, size_t nbytes, unicodechar *ubuf, size_t *ulen)
{
size_t i;
@@ -289,7 +289,7 @@
}
private int
-looks_extended(const unsigned char *buf, size_t nbytes, unichar *ubuf,
+looks_extended(const unsigned char *buf, size_t nbytes, unicodechar *ubuf,
size_t *ulen)
{
size_t i;
@@ -320,11 +320,11 @@
* ubuf must be big enough!
*/
protected int
-file_looks_utf8(const unsigned char *buf, size_t nbytes, unichar *ubuf, size_t *ulen)
+file_looks_utf8(const unsigned char *buf, size_t nbytes, unicodechar *ubuf, size_t *ulen)
{
size_t i;
int n;
- unichar c;
+ unicodechar c;
int gotone = 0, ctrl = 0;
if (ubuf)
@@ -391,7 +391,7 @@
* rest of the text.
*/
private int
-looks_utf8_with_BOM(const unsigned char *buf, size_t nbytes, unichar *ubuf,
+looks_utf8_with_BOM(const unsigned char *buf, size_t nbytes, unicodechar *ubuf,
size_t *ulen)
{
if (nbytes > 3 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)
@@ -401,7 +401,7 @@
}
private int
-looks_utf7(const unsigned char *buf, size_t nbytes, unichar *ubuf, size_t *ulen)
+looks_utf7(const unsigned char *buf, size_t nbytes, unicodechar *ubuf, size_t *ulen)
{
if (nbytes > 4 && buf[0] == '+' && buf[1] == '/' && buf[2] == 'v')
switch (buf[3]) {
@@ -420,7 +420,7 @@
}
private int
-looks_ucs16(const unsigned char *bf, size_t nbytes, unichar *ubf,
+looks_ucs16(const unsigned char *bf, size_t nbytes, unicodechar *ubf,
size_t *ulen)
{
int bigend;
@@ -443,10 +443,10 @@
if (bigend)
ubf[(*ulen)++] = bf[i + 1]
- | (CAST(unichar, bf[i]) << 8);
+ | (CAST(unicodechar, bf[i]) << 8);
else
ubf[(*ulen)++] = bf[i]
- | (CAST(unichar, bf[i + 1]) << 8);
+ | (CAST(unicodechar, bf[i + 1]) << 8);
if (ubf[*ulen - 1] == 0xfffe)
return 0;
@@ -459,7 +459,7 @@
}
private int
-looks_ucs32(const unsigned char *bf, size_t nbytes, unichar *ubf,
+looks_ucs32(const unsigned char *bf, size_t nbytes, unicodechar *ubf,
size_t *ulen)
{
int bigend;
@@ -481,15 +481,15 @@
/* XXX fix to properly handle chars > 65536 */
if (bigend)
- ubf[(*ulen)++] = CAST(unichar, bf[i + 3])
- | (CAST(unichar, bf[i + 2]) << 8)
- | (CAST(unichar, bf[i + 1]) << 16)
- | (CAST(unichar, bf[i]) << 24);
+ ubf[(*ulen)++] = CAST(unicodechar, bf[i + 3])
+ | (CAST(unicodechar, bf[i + 2]) << 8)
+ | (CAST(unicodechar, bf[i + 1]) << 16)
+ | (CAST(unicodechar, bf[i]) << 24);
else
- ubf[(*ulen)++] = CAST(unichar, bf[i + 0])
- | (CAST(unichar, bf[i + 1]) << 8)
- | (CAST(unichar, bf[i + 2]) << 16)
- | (CAST(unichar, bf[i + 3]) << 24);
+ ubf[(*ulen)++] = CAST(unicodechar, bf[i + 0])
+ | (CAST(unicodechar, bf[i + 1]) << 8)
+ | (CAST(unicodechar, bf[i + 2]) << 16)
+ | (CAST(unicodechar, bf[i + 3]) << 24);
if (ubf[*ulen - 1] == 0xfffe)
return 0;
diff -ur libmagic.orig/file.h libmagic/file.h
--- libmagic.orig/file.h 2020-06-15 02:01:01.000000000 +0200
+++ libmagic/file.h 2020-08-29 11:56:12.303522747 +0200
@@ -33,17 +33,13 @@
#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
2017-10-23 17:18:25 +00:00
+#include "config.h"
-#ifdef HAVE_STDINT_H
2017-10-11 16:18:55 +00:00
-#include <stdint.h>
-#endif
+#include "ext/standard/php_string.h"
+#include "ext/pcre/php_pcre.h"
2017-10-11 16:18:55 +00:00
-#ifdef HAVE_INTTYPES_H
+#include <stdint.h>
#include <inttypes.h>
-#endif
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
@@ -79,10 +75,11 @@
2017-10-11 16:18:55 +00:00
#include <stdio.h> /* Include that here, to make sure __P gets defined */
#include <errno.h>
#include <fcntl.h> /* For open and flags */
-#include <regex.h>
2013-04-07 20:15:56 +00:00
-#include <time.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 */
@@ -95,7 +92,7 @@
#define MAGIC "/etc/magic"
#endif
-#if defined(__EMX__) || defined (WIN32)
+#if defined(__EMX__) || defined(PHP_WIN32)
#define PATHSEP ';'
#else
#define PATHSEP ':'
@@ -129,12 +126,6 @@
#endif
#endif
-#ifndef __GNUC__
-#ifndef __attribute__
-#define __attribute__(a)
-#endif
-#endif
-
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
@@ -161,10 +152,10 @@
2018-04-29 13:49:56 +00:00
struct buffer {
int fd;
- struct stat st;
+ zend_stat_t st;
const void *fbuf;
size_t flen;
- off_t eoff;
+ zend_off_t eoff;
void *ebuf;
size_t elen;
};
@@ -258,7 +249,7 @@
#define FILE_OFFSET 50
#define FILE_NAMES_SIZE 51 /* 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 || \
@@ -464,21 +455,17 @@
};
2014-10-25 10:06:17 +00:00
/* Type for Unicode characters */
-typedef unsigned long unichar;
+typedef unsigned long unicodechar;
2014-10-25 10:06:17 +00:00
-struct stat;
#define FILE_T_LOCAL 1
#define FILE_T_WINDOWS 2
protected const char *file_fmttime(char *, size_t, uint64_t, int);
2013-04-07 20:15:56 +00:00
protected struct magic_set *file_ms_alloc(int);
protected void file_ms_free(struct magic_set *);
2019-05-30 12:09:00 +00:00
-protected int file_default(struct magic_set *, size_t);
-protected int file_buffer(struct magic_set *, int, struct stat *, const char *,
- const void *, size_t);
-protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
2019-05-30 12:09:00 +00:00
+protected int file_buffer(struct magic_set *, php_stream *, zend_stat_t *, const char *, const void *,
+ size_t);
+protected int file_fsmagic(struct magic_set *, const char *, zend_stat_t *);
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)));
2019-05-30 12:09:00 +00:00
protected int file_separator(struct magic_set *);
protected char *file_copystr(char *, size_t, size_t, const char *);
protected int file_checkfmt(char *, size_t, const char *);
@@ -486,48 +473,42 @@
protected int file_print_guid(char *, size_t, const uint64_t *);
protected int file_parse_guid(const char *, uint64_t *);
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 *, ...);
2018-04-29 13:49:56 +00:00
protected int file_reset(struct magic_set *, int);
protected int file_tryelf(struct magic_set *, const struct buffer *);
protected int file_trycdf(struct magic_set *, const struct buffer *);
-#if HAVE_FORK
2014-02-19 10:20:24 +00:00
+#ifdef PHP_FILEINFO_UNCOMPRESS
2018-04-29 13:49:56 +00:00
protected int file_zmagic(struct magic_set *, const struct buffer *,
const char *);
#endif
protected int file_ascmagic(struct magic_set *, const struct buffer *,
int);
protected int file_ascmagic_with_encoding(struct magic_set *,
- const struct buffer *, unichar *, size_t, const char *, const char *, int);
+ const struct buffer *, unicodechar *, size_t, const char *, const char *, int);
protected int file_encoding(struct magic_set *, const struct buffer *,
- unichar **, size_t *, const char **, const char **, const char **);
+ unicodechar **, size_t *, const char **, const char **, const char **);
protected int file_is_json(struct magic_set *, const struct buffer *);
protected int file_is_csv(struct magic_set *, const struct buffer *, int);
protected int file_is_tar(struct magic_set *, const struct buffer *);
protected int file_softmagic(struct magic_set *, const struct buffer *,
uint16_t *, uint16_t *, int, int);
protected int file_apprentice(struct magic_set *, const char *, int);
-protected int buffer_apprentice(struct magic_set *, struct magic **,
- size_t *, size_t);
protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
protected uint64_t file_signextend(struct magic_set *, struct magic *,
uint64_t);
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_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_mdump(struct magic *);
protected void file_showstr(FILE *, const char *, size_t);
protected size_t file_mbswidth(const char *);
protected const char *file_getbuffer(struct magic_set *);
protected ssize_t sread(int, void *, size_t, int);
protected int file_check_mem(struct magic_set *, unsigned int);
-protected int file_looks_utf8(const unsigned char *, size_t, unichar *,
+protected int file_looks_utf8(const unsigned char *, size_t, unicodechar *,
size_t *);
protected size_t file_pstring_length_size(struct magic_set *,
const struct magic *);
@@ -539,34 +520,12 @@
2019-05-30 12:09:00 +00:00
size_t);
#endif /* __EMX__ */
-protected void buffer_init(struct buffer *, int, const struct stat *,
+protected void buffer_init(struct buffer *, int, const zend_stat_t *,
const void *, size_t);
2018-04-29 13:49:56 +00:00
protected void buffer_fini(struct buffer *);
protected int buffer_fill(const struct buffer *);
2015-03-05 14:32:04 +00:00
-#include <locale.h>
-#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 *);
+public zend_string* convert_libmagic_pattern(char *val, size_t len, uint32_t options);
2018-11-05 21:25:31 +00:00
2015-03-05 14:32:04 +00:00
typedef struct {
char *buf;
@@ -582,23 +541,10 @@
extern const size_t file_nnames;
#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
@@ -614,39 +560,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
2019-05-30 12:09:00 +00:00
-const char *fmtcheck(const char *, const char *)
2015-03-05 14:32:04 +00:00
- __attribute__((__format_arg__(2)));
-#endif
2018-04-29 13:49:56 +00:00
-
-#ifdef HAVE_LIBSECCOMP
2019-05-30 12:09:00 +00:00
-// basic filter
2018-04-29 13:49:56 +00:00
-// this mode should not interfere with normal operations
-// only some dangerous syscalls are blacklisted
-int enable_sandbox_basic(void);
-
2019-05-30 12:09:00 +00:00
-// enhanced filter
2018-04-29 13:49:56 +00:00
-// this mode allows only the necessary syscalls used during normal operation
-// extensive testing required !!!
-int enable_sandbox_full(void);
-#endif
-
-protected const char *file_getprogname(void);
-protected void file_setprogname(const char *);
-protected void file_err(int, const char *, ...)
2019-05-30 12:09:00 +00:00
- __attribute__((__format__(__printf__, 2, 3), __noreturn__));
2018-04-29 13:49:56 +00:00
-protected void file_errx(int, const char *, ...)
2019-05-30 12:09:00 +00:00
- __attribute__((__format__(__printf__, 2, 3), __noreturn__));
2018-04-29 13:49:56 +00:00
-protected void file_warn(const char *, ...)
- __attribute__((__format__(__printf__, 1, 2)));
-protected void file_warnx(const char *, ...)
- __attribute__((__format__(__printf__, 1, 2)));
2015-03-05 14:32:04 +00:00
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
#define QUICK
@@ -676,4 +589,16 @@
#define __RCSID(a)
2016-01-25 03:45:20 +00:00
#endif
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
+
#endif /* __file_h__ */
diff -ur libmagic.orig/fsmagic.c libmagic/fsmagic.c
--- libmagic.orig/fsmagic.c 2019-07-16 15:30:32.000000000 +0200
+++ libmagic/fsmagic.c 2020-08-29 02:05:56.212049441 +0200
2019-05-30 12:09:00 +00:00
@@ -66,26 +66,10 @@
# 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;
2019-05-30 12:09:00 +00:00
- }
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
private int
handle_mime(struct magic_set *ms, int mime, const char *str)
2019-05-30 12:09:00 +00:00
@@ -103,60 +87,17 @@
}
protected int
-file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
2019-05-30 12:09:00 +00:00
+file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb)
{
2013-04-07 20:15:56 +00:00
int ret, did = 0;
int mime = ms->flags & MAGIC_MIME;
2018-04-29 13:49:56 +00:00
int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
-#ifdef S_IFLNK
- char buf[BUFSIZ+4];
- ssize_t nch;
- struct stat tstatbuf;
-#endif
2014-12-30 19:28:13 +00:00
2019-05-30 12:09:00 +00:00
if (fn == NULL)
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 */
2019-05-30 12:09:00 +00:00
-
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;
2019-05-30 12:09:00 +00:00
- }
2015-03-05 14:32:04 +00:00
- CloseHandle(hFile);
2019-05-30 12:09:00 +00:00
- }
2016-11-24 13:58:54 +00:00
- }
2015-03-05 14:32:04 +00:00
-#endif
2019-05-30 12:09:00 +00:00
+ ret = php_sys_stat(fn, sb);
2019-05-30 12:09:00 +00:00
if (ret) {
if (ms->flags & MAGIC_ERROR) {
@@ -189,32 +130,24 @@
}
2019-05-30 12:09:00 +00:00
switch (sb->st_mode & S_IFMT) {
- case S_IFDIR:
- if (mime) {
- if (handle_mime(ms, mime, "directory") == -1)
- return -1;
2018-04-29 13:49:56 +00:00
- } else if (silent) {
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:
2019-05-30 12:09:00 +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.
- */
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;
2018-04-29 13:49:56 +00:00
- } else if (silent) {
- } 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)
2019-05-30 12:09:00 +00:00
@@ -229,45 +162,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:
2019-05-30 12:09:00 +00:00
- /*
- * 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;
2018-04-29 13:49:56 +00:00
- } else if (silent) {
- } 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;
2018-06-03 12:06:11 +00:00
#endif
- }
2013-04-07 20:15:56 +00:00
- break;
2018-06-03 12:06:11 +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)
2019-05-30 12:09:00 +00:00
@@ -292,92 +191,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;
2018-04-29 13:49:56 +00:00
- } else if (silent) {
- } 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. */
2019-05-30 12:09:00 +00:00
-#ifdef __linux__
- /*
- * linux procfs/devfs makes symlinks like pipe:[3515864880]
- * that we can't stat their readlink output, so stat the
- * original filename instead.
- */
- if (stat(fn, &tstatbuf) < 0)
- return bad_link(ms, errno, buf);
-#else
- 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) {
2019-05-30 12:09:00 +00:00
- 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;
2018-04-29 13:49:56 +00:00
- } else if (silent) {
- } 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);
- }
2019-05-30 12:09:00 +00:00
+ return 1;
#endif
- /* 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;
2018-04-29 13:49:56 +00:00
- } else if (silent) {
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;
2019-05-30 12:09:00 +00:00
-#endif
#ifdef S_IFSOCK
#ifndef __COHERENT__
case S_IFSOCK:
diff -ur libmagic.orig/funcs.c libmagic/funcs.c
--- libmagic.orig/funcs.c 2020-02-20 16:50:20.000000000 +0100
+++ libmagic/funcs.c 2020-08-29 11:56:12.303522747 +0200
@@ -48,6 +48,13 @@
2015-03-05 14:32:04 +00:00
#define SIZE_MAX ((size_t)~0)
#endif
+#include "php.h"
+#include "main/php_network.h"
+
+#ifndef PREG_OFFSET_CAPTURE
+# define PREG_OFFSET_CAPTURE (1<<8)
+#endif
+
protected char *
file_copystr(char *buf, size_t blen, size_t width, const char *str)
{
@@ -60,7 +67,7 @@
private void
file_clearbuf(struct magic_set *ms)
{
- free(ms->o.buf);
+ efree(ms->o.buf);
ms->o.buf = NULL;
ms->o.blen = 0;
}
@@ -119,85 +126,61 @@
return 0;
}
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;
- char tbuf[1024];
-
2014-02-19 10:20:24 +00:00
- if (ms->event_flags & EVENT_HAD_ERR)
- return 0;
-
- if (file_checkfmt(tbuf, sizeof(tbuf), fmt)) {
- file_clearbuf(ms);
- file_error(ms, 0, "Bad magic format `%s' (%s)", fmt, tbuf);
- return -1;
- }
-
- len = vasprintf(&buf, fmt, ap);
- if (len < 0 || (size_t)len > 1024 || len + ms->o.blen > 1024 * 1024) {
- size_t blen = ms->o.blen;
- free(buf);
- file_clearbuf(ms);
- file_error(ms, 0, "Output buffer space exceeded %d+%zu", len,
- blen);
- return -1;
- }
-
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;
- ms->o.blen = len;
2014-02-19 10:20:24 +00:00
- return 0;
-out:
- file_clearbuf(ms);
- file_error(ms, errno, "vasprintf failed");
- return -1;
-}
-
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;
+ char *buf = NULL, *newstr;
va_start(ap, fmt);
- rv = file_vprintf(ms, fmt, ap);
2019-03-08 12:23:07 +00:00
+ vspprintf(&buf, 0, fmt, ap);
2014-02-19 10:20:24 +00:00
va_end(ap);
- return rv;
2014-02-19 10:20:24 +00:00
+
+ if (ms->o.buf != NULL) {
2019-03-08 12:23:07 +00:00
+ spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
2014-02-19 10:20:24 +00:00
+ 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) {
- file_clearbuf(ms);
2019-05-30 12:09:00 +00:00
- (void)file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
+ efree(ms->o.buf);
+ ms->o.buf = NULL;
2019-05-30 12:09:00 +00:00
+ file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
+ }
+
+ 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);
}
- if (ms->o.buf && *ms->o.buf)
- (void)file_printf(ms, " ");
- (void)file_vprintf(ms, f, va);
- if (error > 0)
- (void)file_printf(ms, " (%s)", strerror(error));
2015-03-05 14:32:04 +00:00
+
+ if (buf) {
+ efree(buf);
+ }
+
ms->event_flags |= EVENT_HAD_ERR;
ms->error = error;
}
@@ -293,8 +276,8 @@
2019-05-30 12:09:00 +00:00
*/
2016-11-24 13:58:54 +00:00
/*ARGSUSED*/
protected int
2019-05-30 12:09:00 +00:00
-file_buffer(struct magic_set *ms, int fd, struct stat *st,
- const char *inname __attribute__ ((__unused__)),
+file_buffer(struct magic_set *ms, php_stream *stream, zend_stat_t *st,
+ const char *inname,
const void *buf, size_t nb)
{
int m = 0, rv = 0, looks_text = 0;
@@ -304,6 +287,19 @@
2018-04-29 13:49:56 +00:00
const char *ftype = NULL;
2019-05-30 12:09:00 +00:00
char *rbuf = NULL;
2018-04-29 13:49:56 +00:00
struct buffer b;
+ int fd = -1;
2019-05-30 12:09:00 +00:00
+
+ if (stream) {
2019-05-30 12:09:00 +00:00
+#ifdef _WIN64
+ php_socket_t _fd = fd;
+#else
+ int _fd;
+#endif
+ int _ret = php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&_fd, 0);
+ if (SUCCESS == _ret) {
+ fd = (int)_fd;
+ }
+ }
2018-04-29 13:49:56 +00:00
2019-05-30 12:09:00 +00:00
buffer_init(&b, fd, st, buf, nb);
ms->mode = b.st.st_mode;
@@ -336,7 +332,8 @@
}
}
#endif
-#if HAVE_FORK
+
+#if PHP_FILEINFO_UNCOMPRESS
/* try compression stuff */
2016-11-24 13:58:54 +00:00
if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
2018-04-29 13:49:56 +00:00
m = file_zmagic(ms, &b, inname);
@@ -460,10 +457,10 @@
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
2019-05-30 12:09:00 +00:00
- free(rbuf);
+ efree(rbuf);
2018-04-29 13:49:56 +00:00
buffer_fini(&b);
2019-05-30 12:09:00 +00:00
if (rv)
return rv;
@@ -481,7 +478,7 @@
}
file_clearbuf(ms);
if (ms->o.pbuf) {
- free(ms->o.pbuf);
+ efree(ms->o.pbuf);
ms->o.pbuf = NULL;
}
ms->event_flags &= ~EVENT_HAD_ERR;
@@ -519,7 +516,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;
}
@@ -583,8 +580,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;
@@ -601,88 +598,44 @@
protected size_t
file_printedlen(const struct magic_set *ms)
{
- return ms->o.blen;
+ return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
}
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;
+ zend_string *pattern;
2018-04-30 12:29:55 +00:00
+ uint32_t opts = 0;
2015-03-05 14:32:04 +00:00
+ pcre_cache_entry *pce;
+ zend_string *res;
2017-10-11 16:18:55 +00:00
+ zend_string *repl;
+ size_t rep_cnt = 0;
2015-03-05 14:32:04 +00:00
+
+ opts |= PCRE2_MULTILINE;
+ pattern = convert_libmagic_pattern((char*)pat, strlen(pat), opts);
+ if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) {
+ zend_string_release(pattern);
2014-02-19 10:20:24 +00:00
+ rep_cnt = -1;
+ goto out;
+ }
+ zend_string_release(pattern);
+
+ 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);
+
+ zend_string_release_ex(repl, 0);
+ if (NULL == res) {
+ rep_cnt = -1;
2014-02-19 10:20:24 +00:00
+ goto out;
}
2015-03-05 14:32:04 +00:00
-out:
- file_regfree(&rx);
- return rv;
-}
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, NULL);
- assert(rx->old_lc_ctype != NULL);
- rx->old_lc_ctype = strdup(rx->old_lc_ctype);
- assert(rx->old_lc_ctype != NULL);
- (void)setlocale(LC_CTYPE, "C");
2015-03-05 14:32:04 +00:00
-#endif
- rx->pat = pat;
-
2015-03-05 14:32:04 +00:00
- return rx->rc = regcomp(&rx->rx, pat, flags);
-}
-
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 */
- if (nmatch != 0)
- memset(pmatch, 0, nmatch * sizeof(*pmatch));
2015-03-05 14:32:04 +00:00
- return regexec(&rx->rx, str, nmatch, pmatch, eflags);
-}
-
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);
- free(rx->old_lc_ctype);
2015-03-05 14:32:04 +00:00
-#endif
-}
+ strncpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res));
+ ms->o.buf[ZSTR_LEN(res)] = '\0';
2018-04-29 13:49:56 +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];
+ zend_string_release_ex(res, 0);
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:
+ return rep_cnt;
}
2015-03-05 14:32:04 +00:00
protected file_pushbuf_t *
@@ -693,7 +646,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;
@@ -713,8 +666,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;
}
@@ -724,7 +677,7 @@
ms->o.blen = pb->blen;
2015-03-05 14:32:04 +00:00
ms->offset = pb->offset;
- free(pb);
+ efree(pb);
return rbuf;
}
diff -ur libmagic.orig/magic.c libmagic/magic.c
--- libmagic.orig/magic.c 2020-06-15 02:01:01.000000000 +0200
+++ libmagic/magic.c 2020-08-29 11:56:12.303522747 +0200
@@ -25,11 +25,6 @@
* SUCH DAMAGE.
*/
-#ifdef WIN32
-#include <windows.h>
-#include <shlwapi.h>
-#endif
-
#include "file.h"
#ifndef lint
2019-05-30 12:09:00 +00:00
@@ -39,10 +34,16 @@
#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
#include <limits.h> /* for PIPE_BUF */
@@ -69,194 +70,18 @@
#endif
#endif
2019-04-11 13:20:05 +00:00
-private void close_and_restore(const struct magic_set *, const char *, int,
- const struct stat *);
-private int unreadable_info(struct magic_set *, mode_t, const char *);
-private const char* get_default_magic(void);
-#ifndef COMPILE_ONLY
-private const char *file_or_fd(struct magic_set *, const char *, int);
+#ifdef PHP_WIN32
+# undef S_IFLNK
+# undef S_IFIFO
2013-04-27 12:09:29 +00:00
#endif
+private int unreadable_info(struct magic_set *, mode_t, const char *);
+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;
2018-04-29 13:49:56 +00:00
- return 1;
2016-11-24 13:58:54 +00:00
-}
-#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
{
@@ -302,21 +127,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;
2019-05-30 12:09:00 +00:00
- return buffer_apprentice(ms, RCAST(struct magic **, bufs),
- sizes, nbufs);
2015-03-08 16:24:44 +00:00
-}
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
{
@@ -341,39 +151,6 @@
2019-04-11 13:20:05 +00:00
return file_apprentice(ms, magicfile, FILE_LIST);
}
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)
-{
- if (fd == STDIN_FILENO || name == NULL)
- return;
- (void) close(fd);
-
- if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
- /*
- * Try to restore access, modification times if read it.
- * This is really *bad* because it will modify the status
- * time of the file... And of course this will affect
- * backup programs
- */
-#ifdef HAVE_UTIMES
- struct timeval utsbuf[2];
- (void)memset(utsbuf, 0, sizeof(utsbuf));
- utsbuf[0].tv_sec = sb->st_atime;
- utsbuf[1].tv_sec = sb->st_mtime;
-
- (void) utimes(name, utsbuf); /* don't care if loses */
-#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
- struct utimbuf utbuf;
-
- (void)memset(&utbuf, 0, sizeof(utbuf));
- utbuf.actime = sb->st_atime;
- utbuf.modtime = sb->st_mtime;
- (void) utime(name, &utbuf); /* don't care if loses */
-#endif
- }
-}
-
#ifndef COMPILE_ONLY
/*
@@ -384,7 +161,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);
}
/*
@@ -395,19 +172,25 @@
{
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;
2019-05-30 12:09:00 +00:00
- int okstat = 0;
- off_t pos = CAST(off_t, -1);
+ int no_in_stream = 0;
2015-03-05 14:32:04 +00:00
2018-04-29 13:49:56 +00:00
if (file_reset(ms, 1) == -1)
2016-11-24 13:58:54 +00:00
goto out;
@@ -417,7 +200,7 @@
* 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;
2019-05-30 12:09:00 +00:00
switch (file_fsmagic(ms, inname, &sb)) {
@@ -430,95 +213,46 @@
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
2019-05-30 12:09:00 +00:00
- if (inname != NULL) {
- int flags = O_RDONLY|O_BINARY|O_NONBLOCK;
- errno = 0;
- if ((fd = open(inname, flags)) < 0) {
2019-05-30 12:09:00 +00:00
- okstat = stat(inname, &sb) == 0;
- if (okstat && S_ISFIFO(sb.st_mode))
- ispipe = 1;
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)
2019-05-30 12:09:00 +00:00
+ errno = 0;
+
+ if (inname && !stream) {
+ no_in_stream = 1;
+ stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
+ if (!stream) {
+ if (unreadable_info(ms, sb.st_mode, inname) == -1)
goto done;
- rv = 0;
2019-05-30 12:09:00 +00:00
+ rv = -1;
goto done;
2019-05-30 12:09:00 +00:00
}
}
- if (fd != -1) {
- okstat = fstat(fd, &sb) == 0;
2019-05-30 12:09:00 +00:00
- if (okstat && S_ISFIFO(sb.st_mode))
- ispipe = 1;
- if (inname == NULL)
- pos = lseek(fd, CAST(off_t, 0), SEEK_CUR);
+ php_stream_statbuf ssb;
+ if (php_stream_stat(stream, &ssb) < 0) {
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, errno, "cannot stat `%s'", inname);
+ rv = -1;
+ goto done;
+ }
}
+ memcpy(&sb, &ssb.sb, sizeof(zend_stat_t));
/*
2016-11-24 13:58:54 +00:00
* try looking at the first ms->bytes_max bytes
*/
- if (ispipe) {
2019-05-30 12:09:00 +00:00
- if (fd != -1) {
- ssize_t r = 0;
-
2019-05-30 12:09:00 +00:00
- while ((r = sread(fd, RCAST(void *, &buf[nbytes]),
- CAST(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;
- }
-
2019-05-30 12:09:00 +00:00
- } else if (fd != -1) {
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)
2019-05-30 12:09:00 +00:00
- _isatty(fd) ? 8 * 1024 :
2015-03-05 14:32:04 +00:00
-#endif
2019-05-30 12:09:00 +00:00
- ms->bytes_max;
- if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) {
2015-03-05 14:32:04 +00:00
- 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 */
2019-05-30 12:09:00 +00:00
- if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1)
+ if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
goto done;
rv = 0;
done:
- free(buf);
2016-11-24 13:58:54 +00:00
- if (fd != -1) {
2019-05-30 12:09:00 +00:00
- if (pos != CAST(off_t, -1))
2016-11-24 13:58:54 +00:00
- (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;
@@ -536,7 +270,7 @@
* The main work is done here!
2018-04-29 13:49:56 +00:00
* We have the file name and/or the data buffer to be identified.
*/
2019-05-30 12:09:00 +00:00
- if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) {
+ if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
return NULL;
}
return file_getbuffer(ms);
diff -ur libmagic.orig/magic.h libmagic/magic.h
--- libmagic.orig/magic.h 2020-06-29 01:13:35.424557511 +0200
+++ libmagic/magic.h 2020-08-29 02:05:56.212049441 +0200
@@ -126,6 +126,7 @@
2019-05-30 12:09:00 +00:00
const char *magic_getpath(const char *, int);
const char *magic_file(magic_t, const char *);
+const char *magic_stream(magic_t, php_stream *);
const char *magic_descriptor(magic_t, int);
const char *magic_buffer(magic_t, const void *, size_t);
diff -ur libmagic.orig/print.c libmagic/print.c
--- libmagic.orig/print.c 2020-05-09 20:57:15.000000000 +0200
+++ libmagic/print.c 2020-08-29 11:56:12.303522747 +0200
2019-03-08 20:55:15 +00:00
@@ -28,6 +28,7 @@
2014-02-19 10:20:24 +00:00
/*
* print.c - debugging printout routines
*/
+#include "php.h"
2014-02-19 10:20:24 +00:00
#include "file.h"
@@ -73,7 +74,7 @@
2018-04-29 13:49:56 +00:00
if (m->mask_op & FILE_OPINVERSE)
(void) fputc('~', stderr);
2016-11-24 13:58:54 +00:00
- if (IS_STRING(m->type)) {
2018-04-29 13:49:56 +00:00
+ if (IS_LIBMAGIC_STRING(m->type)) {
if (m->str_flags) {
(void) fputc('/', stderr);
2019-05-30 12:09:00 +00:00
if (m->str_flags & STRING_COMPACT_WHITESPACE)
@@ -225,18 +226,18 @@
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 */
2019-05-30 12:09:00 +00:00
- (void) fflush(stdout);
-
- if (ms->file)
- (void) fprintf(stderr, "%s, %lu: ", ms->file,
2019-05-30 12:09:00 +00:00
- CAST(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_WARNING, "%s", expanded_format);
+
2014-12-30 19:28:13 +00:00
+ free(expanded_format);
+ }
}
protected const char *
@@ -257,13 +258,13 @@
2015-03-05 14:32:04 +00:00
}
if (flags & FILE_T_LOCAL) {
- tm = localtime_r(&t, &tmz);
2019-03-08 20:55:15 +00:00
+ tm = php_localtime_r(&t, &tmz);
2015-03-05 14:32:04 +00:00
} else {
- tm = gmtime_r(&t, &tmz);
2019-03-08 20:55:15 +00:00
+ tm = php_gmtime_r(&t, &tmz);
2013-04-07 20:15:56 +00:00
}
2015-03-05 14:32:04 +00:00
if (tm == NULL)
goto out;
2019-03-08 20:55:15 +00:00
- pp = asctime_r(tm, buf);
+ pp = php_asctime_r(tm, buf);
if (pp == NULL)
goto out;
diff -ur libmagic.orig/readcdf.c libmagic/readcdf.c
--- libmagic.orig/readcdf.c 2019-09-30 17:42:50.000000000 +0200
+++ libmagic/readcdf.c 2020-08-29 02:05:56.212049441 +0200
2016-11-24 13:58:54 +00:00
@@ -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>
2019-05-30 12:09:00 +00:00
@@ -100,10 +104,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;
}
@@ -112,35 +112,24 @@
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;
+ char *vbuf_lower;
2015-03-05 14:32:04 +00:00
- 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, NULL);
- assert(old_lc_ctype != NULL);
- old_lc_ctype = strdup(old_lc_ctype);
- assert(old_lc_ctype != NULL);
- (void)setlocale(LC_CTYPE, "C");
2015-03-05 14:32:04 +00:00
-#endif
- for (i = 0; nv[i].pattern != NULL; i++)
- if (strcasestr(vbuf, nv[i].pattern) != NULL) {
+ vbuf_lower = zend_str_tolower_dup(vbuf, strlen(vbuf));
+ for (i = 0; nv[i].pattern != NULL; i++) {
+ char *pattern_lower;
+ int found;
+
+ pattern_lower = zend_str_tolower_dup(nv[i].pattern, strlen(nv[i].pattern));
+ found = (strstr(vbuf_lower, pattern_lower) != NULL);
+ efree(pattern_lower);
+
+ if (found) {
2015-03-05 14:32:04 +00:00
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
- (void)setlocale(LC_CTYPE, old_lc_ctype);
- free(old_lc_ctype);
2015-03-05 14:32:04 +00:00
-#endif
+ }
+
+ efree(vbuf_lower);
2015-03-05 14:32:04 +00:00
return rv;
}
2014-07-01 08:28:43 +00:00
@@ -156,7 +145,9 @@
2019-05-30 12:09:00 +00:00
const char *s, *e;
int len;
2014-04-24 18:16:06 +00:00
2019-05-30 12:09:00 +00:00
- if (!NOTMIME(ms) && root_storage)
2014-04-24 18:16:06 +00:00
+ memset(&ts, 0, sizeof(ts));
2014-07-01 08:28:43 +00:00
+
2019-05-30 12:09:00 +00:00
+ if (!NOTMIME(ms) && root_storage)
2015-03-05 14:32:04 +00:00
str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
clsid2mime);
2019-05-30 12:09:00 +00:00
@@ -282,10 +273,10 @@
2018-04-29 13:49:56 +00:00
if (file_printf(ms, "%s%s",
cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
i == cat->cat_num - 1 ? "]" : ", ") == -1) {
- free(cat);
+ efree(cat);
return -1;
}
- free(cat);
+ efree(cat);
2019-05-30 12:09:00 +00:00
} else if (ms->flags & MAGIC_MIME_TYPE) {
2018-04-29 13:49:56 +00:00
if (file_printf(ms, "application/CDFV2") == -1)
return -1;
@@ -346,7 +337,7 @@
2018-04-29 13:49:56 +00:00
}
2019-05-30 12:09:00 +00:00
m = cdf_file_property_info(ms, info, count, root_storage);
- free(info);
+ efree(info);
2018-04-29 13:49:56 +00:00
2019-05-30 12:09:00 +00:00
return m == -1 ? -2 : m;
2018-04-29 13:49:56 +00:00
}
@@ -656,11 +647,11 @@
2018-04-29 13:49:56 +00:00
cdf_zero_stream(&scn);
cdf_zero_stream(&sst);
out3:
2019-05-30 12:09:00 +00:00
- free(dir.dir_tab);
+ efree(dir.dir_tab);
2018-04-29 13:49:56 +00:00
out2:
2019-05-30 12:09:00 +00:00
- free(ssat.sat_tab);
+ efree(ssat.sat_tab);
2018-04-29 13:49:56 +00:00
out1:
2019-05-30 12:09:00 +00:00
- free(sat.sat_tab);
+ efree(sat.sat_tab);
2018-04-29 13:49:56 +00:00
out0:
2019-05-30 12:09:00 +00:00
/* If we handled it already, return */
if (i != -1)
diff -ur libmagic.orig/softmagic.c libmagic/softmagic.c
--- libmagic.orig/softmagic.c 2020-06-15 02:01:01.000000000 +0200
+++ libmagic/softmagic.c 2020-08-29 11:56:13.219516523 +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,
2018-04-29 13:49:56 +00:00
const struct buffer *, size_t, int, int, int, uint16_t *,
2019-05-30 12:09:00 +00:00
uint16_t *, int *, int *, int *, int *);
@@ -139,8 +143,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)))
2019-05-30 12:09:00 +00:00
@@ -159,8 +163,10 @@
2018-04-29 13:49:56 +00:00
" with `%s'", file, line, desc, def);
2015-03-05 14:32:04 +00:00
return ptr;
}
-#else
+#elif defined(HAVE_FMTCHECK)
2018-04-29 13:49:56 +00:00
#define F(a, b, c) fmtcheck((b), (c))
2015-03-05 14:32:04 +00:00
+#else
2018-04-29 13:49:56 +00:00
+#define F(a, b, c) ((b))
2015-03-05 14:32:04 +00:00
#endif
2014-02-19 10:20:24 +00:00
/*
2019-05-30 12:09:00 +00:00
@@ -222,7 +228,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))) ||
@@ -470,45 +476,28 @@
private int
2018-04-29 13:49:56 +00:00
check_fmt(struct magic_set *ms, const char *fmt)
{
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_cache_entry *pce;
+ int rv = -1;
2014-10-25 10:06:17 +00:00
+ zend_string *pattern;
2015-03-05 14:32:04 +00:00
2018-04-29 13:49:56 +00:00
if (strchr(fmt, '%') == 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);
2018-11-10 21:02:44 +00:00
+ pattern = zend_string_init("~%[-0-9\\.]*s~", sizeof("~%[-0-9\\.]*s~") - 1, 0);
+ if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) {
2014-02-19 10:20:24 +00:00
+ rv = -1;
} else {
2018-04-29 13:49:56 +00:00
- rc = file_regexec(&rx, fmt, 0, 0, 0);
2014-02-19 10:20:24 +00:00
- rv = !rc;
+ pcre2_code *re = php_pcre_pce_re(pce);
+ pcre2_match_data *match_data = php_pcre_create_match_data(0, re);
+ if (match_data) {
+ rv = pcre2_match(re, (PCRE2_SPTR)fmt, strlen(fmt), 0, 0, match_data, php_pcre_mctx()) > 0;
+ php_pcre_free_match_data(match_data);
+ }
}
2015-03-05 14:32:04 +00:00
- file_regfree(&rx);
+ zend_string_release(pattern);
2014-02-19 10:20:24 +00:00
return rv;
}
2019-05-30 12:09:00 +00:00
-#if !defined(HAVE_STRNDUP) || defined(__aiws__)
-# ifdef __aiws__
-# define strndup aix_strndup /* aix is broken */
-# endif
-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 */
-
2018-04-29 13:49:56 +00:00
static int
2019-05-30 12:09:00 +00:00
varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
{
@@ -809,15 +798,11 @@
char *cp;
int rval;
2019-05-30 12:09:00 +00:00
- cp = strndup(RCAST(const char *, ms->search.s),
+ cp = estrndup(RCAST(const char *, ms->search.s),
ms->search.rm_len);
2018-06-03 12:06:11 +00:00
- if (cp == NULL) {
- file_oomem(ms, ms->search.rm_len);
- return -1;
- }
2018-04-29 13:49:56 +00:00
rval = file_printf(ms, F(ms, desc, "%s"),
file_printable(sbuf, sizeof(sbuf), cp, ms->search.rm_len));
2015-03-08 16:24:44 +00:00
- free(cp);
+ efree(cp);
2015-03-08 16:24:44 +00:00
if (rval == -1)
return -1;
@@ -1851,17 +1836,16 @@
2019-05-30 12:09:00 +00:00
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 &&
2019-05-30 12:09:00 +00:00
- file_printf(ms, F(ms, m->desc, "%u"), offset) == -1)
- {
2015-03-05 14:32:04 +00:00
- free(rbuf);
2019-05-30 12:09:00 +00:00
+ file_printf(ms, F(ms, m->desc, "%u"), offset) == -1) {
2015-03-05 14:32:04 +00:00
+ 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:
@@ -1950,10 +1934,13 @@
}
else if ((flags & STRING_COMPACT_WHITESPACE) &&
isspace(*a)) {
+ /* XXX Dirty. The data and the pattern is what is causing this.
+ Revert _i for the next port and see if it still matters. */
+ uint32_t _i = 0;
a++;
if (isspace(*b++)) {
if (!isspace(*a))
- while (b < eb && isspace(*b))
+ while (EXPECTED(_i++ < 2048) && b < eb && isspace(*b))
b++;
}
else {
@@ -1989,6 +1976,60 @@
return file_strncmp(a, b, len, maxlen, flags);
}
+public zend_string* convert_libmagic_pattern(char *val, size_t len, uint32_t options)
+{
+ int i, j;
2014-10-25 10:06:17 +00:00
+ zend_string *t;
+
+ for (i = j = 0; i < len; i++) {
+ switch (val[i]) {
+ case '~':
+ j += 2;
+ break;
+ case '\0':
+ j += 4;
+ break;
+ default:
+ j++;
+ break;
+ }
+ }
+ t = zend_string_alloc(j + 4, 0);
2014-10-25 10:06:17 +00:00
+
+ j = 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;
2019-05-30 12:09:00 +00:00
+ case '\0':
+ ZSTR_VAL(t)[j++] = '\\';
+ ZSTR_VAL(t)[j++] = 'x';
+ ZSTR_VAL(t)[j++] = '0';
+ ZSTR_VAL(t)[j] = '0';
+ break;
2014-10-25 10:06:17 +00:00
+ 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++] = '~';
+
2018-04-29 13:49:56 +00:00
+ if (options & PCRE2_CASELESS)
2016-01-25 03:45:20 +00:00
+ ZSTR_VAL(t)[j++] = 'i';
2014-10-25 10:06:17 +00:00
+
+ if (options & PCRE2_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
+
+ return t;
+}
+
private int
magiccheck(struct magic_set *ms, struct magic *m)
{
@@ -2168,65 +2209,77 @@
break;
}
case FILE_REGEX: {
- int rc;
2015-03-05 14:32:04 +00:00
- file_regex_t rx;
- const char *search;
+ zend_string *pattern;
2018-04-30 12:29:55 +00:00
+ uint32_t options = 0;
+ pcre_cache_entry *pce;
2015-03-05 14:32:04 +00:00
- if (ms->search.s == NULL)
- return 0;
+ options |= PCRE2_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);
2019-05-30 12:09:00 +00:00
- v = CAST(uint64_t, -1);
+ if (m->str_flags & STRING_IGNORE_CASE) {
+ options |= PCRE2_CASELESS;
+ }
2015-03-05 14:32:04 +00:00
+
+ pattern = convert_libmagic_pattern((char *)m->value.s, m->vallen, options);
2015-03-05 14:32:04 +00:00
+
+ l = v = 0;
+ if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) {
+ zend_string_release(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;
+ zend_string *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 = zend_string_init(ms->search.s, ms->search.s_len, 0);
+
+ /* match v = 0, no match v = 1 */
+ php_pcre_match_impl(pce, haystack, &retval, &subpats, 0, 1, PREG_OFFSET_CAPTURE, 0);
+ /* Free haystack */
+ zend_string_release(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);
+ zend_string_release(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);
+ zend_string_release(pattern);
2016-01-25 03:45:20 +00:00
+ return -1;
+ }
} else {
2017-10-11 16:18:55 +00:00
- search = CCAST(char *, "");
2016-01-25 03:45:20 +00:00
- copy = NULL;
- }
2019-05-30 12:09:00 +00:00
- rc = file_regexec(&rx, RCAST(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:
2019-05-30 12:09:00 +00:00
- ms->search.s += CAST(int, pmatch.rm_so);
- ms->search.offset += CAST(size_t, pmatch.rm_so);
- ms->search.rm_len = CAST(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);
2019-05-30 12:09:00 +00:00
- v = CAST(uint64_t, -1);
2016-01-25 03:45:20 +00:00
- break;
}
+ zval_ptr_dtor(&subpats);
+ zend_string_release(pattern);
}
2015-03-05 14:32:04 +00:00
- file_regfree(&rx);
2019-05-30 12:09:00 +00:00
- if (v == CAST(uint64_t, -1))
- return -1;
2014-02-19 10:20:24 +00:00
break;
}
case FILE_INDIRECT:
diff -ur libmagic.orig/strcasestr.c libmagic/strcasestr.c
2019-05-30 12:09:00 +00:00
--- libmagic.orig/strcasestr.c 2014-09-11 17:05:33.000000000 +0200
+++ libmagic/strcasestr.c 2020-07-04 12:40:36.675619260 +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>