diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
---- libmagic.orig/apprentice.c Tue Nov 19 22:01:12 2013
-+++ libmagic/apprentice.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/apprentice.c 2016-01-25 11:17:38.141978563 +0800
++++ libmagic/apprentice.c 2016-01-25 11:19:37.409347842 +0800
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
m->mimetype[0] = '\0'; /* initialise MIME type to none */
return 0;
}
-@@ -2575,59 +2597,76 @@
+@@ -2575,59 +2597,75 @@
private struct magic_map *
apprentice_map(struct magic_set *ms, const char *fn)
{
size_t i;
+ php_stream *stream = NULL;
+ php_stream_statbuf st;
-+
-+
-+ TSRMLS_FETCH();
- fd = -1;
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
++
++ TSRMLS_FETCH();
++
+ if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
file_oomem(ms, sizeof(*map));
-+ efree(map);
- goto error;
- }
-
+- goto error;
++ return NULL;
++ }
++
+ if (fn == NULL) {
+ map->p = (void *)&php_magic_database;
+ goto internal_loaded;
-+ }
-+
+ }
+
+#ifdef PHP_WIN32
+ /* Don't bother on windows with php_stream_open_wrapper,
+ return to give apprentice_load() a chance. */
+ if (php_stream_stat_path_ex((char *)fn, 0, &st, NULL) == SUCCESS) {
+ if (st.sb.st_mode & S_IFDIR) {
-+ goto error;
++ return NULL;
+ }
+ }
+#endif
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
file_error(ms, 0, "bad magic in `%s'", dbname);
-@@ -2641,17 +2680,29 @@
+@@ -2641,17 +2679,29 @@
else
version = ptr[1];
if (version != VERSIONNO) {
}
map->magic[0] = CAST(struct magic *, map->p) + 1;
nentries = 0;
-@@ -2664,22 +2715,29 @@
+@@ -2664,22 +2714,29 @@
map->magic[i + 1] = map->magic[i] + map->nmagic[i];
nentries += map->nmagic[i];
}
return NULL;
}
-@@ -2700,14 +2758,19 @@
+@@ -2700,14 +2757,19 @@
char *dbname;
int rv = -1;
uint32_t i;
file_error(ms, errno, "cannot open `%s'", dbname);
goto out;
}
-@@ -2717,31 +2780,33 @@
+@@ -2717,31 +2779,33 @@
goto out;
}
return rv;
}
-@@ -2754,6 +2819,7 @@
+@@ -2754,6 +2818,7 @@
{
const char *p, *q;
char *buf;
if (strip) {
if ((p = strrchr(fn, '/')) != NULL)
-@@ -2775,16 +2841,18 @@
+@@ -2775,16 +2840,18 @@
q++;
/* Compatibility with old code that looked in .mime */
if (ms->flags & MAGIC_MIME) {
/* Compatibility with old code that looked in .mime */
if (strstr(p, ".mime") != NULL)
-@@ -2874,7 +2942,7 @@
+@@ -2874,7 +2941,7 @@
m->offset = swap4((uint32_t)m->offset);
m->in_offset = swap4((uint32_t)m->in_offset);
m->lineno = swap4((uint32_t)m->lineno);
m->str_flags = swap4(m->str_flags);
}
diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
---- libmagic.orig/ascmagic.c Thu Feb 13 00:20:53 2014
-+++ libmagic/ascmagic.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/ascmagic.c 2016-01-25 11:17:38.161978563 +0800
++++ libmagic/ascmagic.c 2016-01-25 11:19:37.410347842 +0800
@@ -139,7 +139,7 @@
/* malloc size is a conservative overestimate; could be
improved, or at least realloced after conversion. */
return rv;
}
diff -u libmagic.orig/cdf.c libmagic/cdf.c
---- libmagic.orig/cdf.c Tue Feb 26 17:20:42 2013
-+++ libmagic/cdf.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/cdf.c 2016-01-25 11:17:38.140978563 +0800
++++ libmagic/cdf.c 2016-01-25 11:19:37.410347842 +0800
@@ -35,7 +35,7 @@
#include "file.h"
(void)fprintf(stderr, "timestamp %s\n", buf);
} else {
diff -u libmagic.orig/cdf.h libmagic/cdf.h
---- libmagic.orig/cdf.h Thu Jun 21 00:19:55 2012
-+++ libmagic/cdf.h Sun Jan 4 17:06:01 2015
+--- libmagic.orig/cdf.h 2016-01-25 11:17:38.160978563 +0800
++++ libmagic/cdf.h 2016-01-25 11:19:37.410347842 +0800
@@ -35,10 +35,12 @@
#ifndef _H_CDF_
#define _H_CDF_
cdf_property_info_t **, size_t *, size_t *);
int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
---- libmagic.orig/cdf_time.c Thu Jun 21 00:18:33 2012
-+++ libmagic/cdf_time.c Sun Nov 9 19:16:18 2014
+--- libmagic.orig/cdf_time.c 2016-01-25 11:17:38.160978563 +0800
++++ libmagic/cdf_time.c 2016-01-25 11:19:37.410347842 +0800
@@ -96,7 +96,7 @@
}
static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
static const char *ref = "Sat Apr 23 01:30:00 1977";
diff -u libmagic.orig/compress.c libmagic/compress.c
---- libmagic.orig/compress.c Sun Jan 5 16:55:21 2014
-+++ libmagic/compress.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/compress.c 2016-01-25 11:17:38.149978563 +0800
++++ libmagic/compress.c 2016-01-25 11:19:37.410347842 +0800
@@ -32,6 +32,7 @@
* uncompress(method, old, n, newch) - uncompress old into new,
* using method, return sizeof new
-#endif
+#endif /* if PHP_FILEINFO_UNCOMPRESS */
diff -u libmagic.orig/file.h libmagic/file.h
---- libmagic.orig/file.h Thu Feb 13 00:20:53 2014
-+++ libmagic/file.h Sun Jan 4 17:06:01 2015
+--- libmagic.orig/file.h 2016-01-25 11:17:38.140978563 +0800
++++ libmagic/file.h 2016-01-25 11:19:37.411347842 +0800
@@ -33,11 +33,9 @@
#ifndef __file_h__
#define __file_h__
size_t strlcat(char *, const char *, size_t);
#endif
#ifndef HAVE_STRCASESTR
-@@ -535,6 +516,14 @@
- #endif
- #else
+@@ -537,4 +518,12 @@
#define FILE_RCSID(id)
-+#endif
-+
+ #endif
+
+#ifdef PHP_WIN32
+#define FINFO_LSEEK_FUNC _lseek
+#define FINFO_READ_FUNC _read
+#else
+#define FINFO_LSEEK_FUNC lseek
+#define FINFO_READ_FUNC read
- #endif
-
++#endif
++
#endif /* __file_h__ */
diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
---- libmagic.orig/fsmagic.c Sun Dec 1 20:22:13 2013
-+++ libmagic/fsmagic.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/fsmagic.c 2016-01-25 11:17:38.139978563 +0800
++++ libmagic/fsmagic.c 2016-01-25 11:19:37.411347842 +0800
@@ -59,27 +59,21 @@
# define minor(dev) ((dev) & 0xff)
#endif
return ret;
}
diff -u libmagic.orig/funcs.c libmagic/funcs.c
---- libmagic.orig/funcs.c Thu Feb 13 00:20:53 2014
-+++ libmagic/funcs.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/funcs.c 2016-01-25 11:17:38.150978563 +0800
++++ libmagic/funcs.c 2016-01-25 11:19:37.411347842 +0800
@@ -27,7 +27,7 @@
#include "file.h"
- return rv;
+ return rep_cnt;
}
+Common subdirectories: libmagic.orig/.libs and libmagic/.libs
diff -u libmagic.orig/magic.c libmagic/magic.c
---- libmagic.orig/magic.c Sun Dec 1 20:22:13 2013
-+++ libmagic/magic.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/magic.c 2016-01-25 11:17:38.161978563 +0800
++++ libmagic/magic.c 2016-01-25 11:19:37.411347842 +0800
@@ -25,11 +25,6 @@
* SUCH DAMAGE.
*/
public const char *
magic_error(struct magic_set *ms)
diff -u libmagic.orig/magic.h libmagic/magic.h
---- libmagic.orig/magic.h Tue Feb 11 16:30:44 2014
-+++ libmagic/magic.h Sun Jan 4 17:06:01 2015
-@@ -75,7 +75,7 @@
- #define MAGIC_NO_CHECK_FORTRAN 0x000000 /* Don't check ascii/fortran */
- #define MAGIC_NO_CHECK_TROFF 0x000000 /* Don't check ascii/troff */
-
--#define MAGIC_VERSION 516 /* This implementation */
-+#define MAGIC_VERSION 517 /* This implementation */
-
-
- #ifdef __cplusplus
+--- libmagic.orig/magic.h 2016-01-25 11:17:38.139978563 +0800
++++ libmagic/magic.h 2016-01-25 11:19:37.412347842 +0800
@@ -88,6 +88,7 @@
const char *magic_getpath(const char *, int);
int magic_list(magic_t, const char *);
int magic_errno(magic_t);
+diff -u libmagic.orig/patchlevel.h libmagic/patchlevel.h
+--- libmagic.orig/patchlevel.h 2016-01-25 11:17:38.141978563 +0800
++++ libmagic/patchlevel.h 2016-01-25 11:19:37.412347842 +0800
+@@ -351,7 +351,7 @@
+ *
+ * Revision 1.8 93/02/19 15:01:26 ian
+ * Numerous changes from Guy Harris too numerous to mention but including
+- * byte-order independance, fixing "old-style masking", etc. etc. A bugfix
++ * byte-order independence, fixing "old-style masking", etc. etc. A bugfix
+ * for broken symlinks from martin@@d255s004.zfe.siemens.de.
+ *
+ * Revision 1.7 93/01/05 14:57:27 ian
diff -u libmagic.orig/print.c libmagic/print.c
---- libmagic.orig/print.c Tue Feb 26 19:25:00 2013
-+++ libmagic/print.c Sun Jan 4 17:04:32 2015
+--- libmagic.orig/print.c 2016-01-25 11:17:38.162978563 +0800
++++ libmagic/print.c 2016-01-25 11:19:37.412347842 +0800
@@ -28,13 +28,17 @@
/*
* print.c - debugging printout routines
t = ts.tv_sec;
}
diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
---- libmagic.orig/readcdf.c Tue Jan 7 04:13:42 2014
-+++ libmagic/readcdf.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/readcdf.c 2016-01-25 11:17:38.160978563 +0800
++++ libmagic/readcdf.c 2016-01-25 11:19:37.412347842 +0800
@@ -26,11 +26,15 @@
#include "file.h"
}
if (NOTMIME(ms)) {
if (str != NULL) {
+Only in libmagic.orig/: readelf.c
+Only in libmagic.orig/: readelf.h
diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
---- libmagic.orig/softmagic.c Thu Feb 13 00:20:53 2014
-+++ libmagic/softmagic.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/softmagic.c 2016-01-25 11:17:38.139978563 +0800
++++ libmagic/softmagic.c 2016-01-25 11:19:45.545347848 +0800
@@ -50,6 +50,11 @@
#include <locale.h>
#endif
{
/*
* Note: FILE_SEARCH and FILE_REGEX do not actually copy
-@@ -1066,15 +1061,24 @@
+@@ -1066,15 +1061,27 @@
const char *last; /* end of search region */
const char *buf; /* start of search region */
const char *end;
- size_t lines;
+ size_t lines, linecnt, bytecnt;
-
++
+ linecnt = m->str_range;
+ bytecnt = linecnt * 80;
-+
+
+ if (bytecnt == 0) {
+ bytecnt = 8192;
+ }
+ if (bytecnt > nbytes) {
+ bytecnt = nbytes;
++ }
++ if (offset > bytecnt) {
++ offset = bytecnt;
+ }
if (s == NULL) {
ms->search.s_len = 0;
/* mget() guarantees buf <= last */
for (lines = linecnt, b = buf; lines && b < end &&
((b = CAST(const char *,
-@@ -1087,7 +1091,7 @@
+@@ -1087,7 +1094,7 @@
b++;
}
if (lines)
ms->search.s = buf;
ms->search.s_len = last - buf;
-@@ -1158,7 +1162,6 @@
+@@ -1158,7 +1165,6 @@
int *need_separator, int *returnval)
{
uint32_t soffset, offset = ms->offset;
int rv, oneed_separator, in_type;
char *sbuf, *rbuf;
union VALUETYPE *p = &ms->ms_value;
-@@ -1170,17 +1173,13 @@
+@@ -1170,17 +1176,13 @@
}
if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
}
if (m->flag & INDIR) {
-@@ -1672,16 +1671,13 @@
+@@ -1672,16 +1674,13 @@
if ((ms->flags & MAGIC_DEBUG) != 0)
fprintf(stderr, "indirect +offs=%u\n", offset);
}
}
}
-@@ -1755,11 +1751,21 @@
+@@ -1755,11 +1754,21 @@
ms->offset = soffset;
if (rv == 1) {
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
}
return rv;
-@@ -1875,6 +1881,42 @@
+@@ -1875,6 +1884,42 @@
return file_strncmp(a, b, len, flags);
}
private int
magiccheck(struct magic_set *ms, struct magic *m)
{
-@@ -2035,63 +2077,151 @@
+@@ -2035,63 +2080,90 @@
break;
}
case FILE_REGEX: {
-
- if (ms->search.s == NULL)
- return 0;
+-
+- l = 0;
+- rc = regcomp(&rx, m->value.s,
+- REG_EXTENDED|REG_NEWLINE|
+- ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
+- if (rc) {
+- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
+- file_magerror(ms, "regex error %d, (%s)",
+- rc, errmsg);
+- v = (uint64_t)-1;
+- }
+- else {
+- regmatch_t pmatch[1];
+-#ifndef REG_STARTEND
+-#define REG_STARTEND 0
+- size_t l = ms->search.s_len - 1;
+- char c = ms->search.s[l];
+- ((char *)(intptr_t)ms->search.s)[l] = '\0';
+-#else
+- pmatch[0].rm_so = 0;
+- pmatch[0].rm_eo = ms->search.s_len;
+-#endif
+- rc = regexec(&rx, (const char *)ms->search.s,
+- 1, pmatch, REG_STARTEND);
+-#if REG_STARTEND == 0
+- ((char *)(intptr_t)ms->search.s)[l] = c;
+-#endif
+- switch (rc) {
+- case 0:
+- ms->search.s += (int)pmatch[0].rm_so;
+- ms->search.offset += (size_t)pmatch[0].rm_so;
+- ms->search.rm_len =
+- (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
+- v = 0;
+- break;
+-
+- case REG_NOMATCH:
+ zval *pattern;
+ int options = 0;
+ pcre_cache_entry *pce;
+ haystack = estrndup(ms->search.s, ms->search.s_len);
+
+ /* match v = 0, no match v = 1 */
-+ php_pcre_match_impl(pce, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);
++ php_pcre_match_impl(pce, haystack, ms->search.s_len, retval, subpats, 0, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);
+ /* Free haystack */
+ efree(haystack);
+
+ FREE_ZVAL(pattern);
+ return -1;
+ } else if ((Z_LVAL_P(retval) > 0) && (Z_TYPE_P(subpats) == IS_ARRAY)) {
-+
+ /* Need to fetch global match which equals pmatch[0] */
++ zval **ppzval;
+ HashTable *ht = Z_ARRVAL_P(subpats);
-+ HashPosition outer_pos;
-+ zval *pattern_match = NULL, *pattern_offset = NULL;
-+
-+ zend_hash_internal_pointer_reset_ex(ht, &outer_pos);
-+
-+ if (zend_hash_has_more_elements_ex(ht, &outer_pos) == SUCCESS &&
-+ zend_hash_move_forward_ex(ht, &outer_pos)) {
-+
-+ zval **ppzval;
-+
-+ /* The first element (should be) is the global match
-+ Need to move to the inner array to get the global match */
-+
-+ if (zend_hash_get_current_data_ex(ht, (void**)&ppzval, &outer_pos) != FAILURE) {
-+
-+ HashTable *inner_ht;
-+ HashPosition inner_pos;
-+ zval **match, **offset;
-+ zval tmpcopy = **ppzval, matchcopy, offsetcopy;
-+
-+ zval_copy_ctor(&tmpcopy);
-+ INIT_PZVAL(&tmpcopy);
-+
-+ inner_ht = Z_ARRVAL(tmpcopy);
-+
-+ /* If everything goes according to the master plan
-+ tmpcopy now contains two elements:
-+ 0 = the match
-+ 1 = starting position of the match */
-+ zend_hash_internal_pointer_reset_ex(inner_ht, &inner_pos);
-+
-+ if (zend_hash_has_more_elements_ex(inner_ht, &inner_pos) == SUCCESS &&
-+ zend_hash_move_forward_ex(inner_ht, &inner_pos)) {
-+
-+ if (zend_hash_get_current_data_ex(inner_ht, (void**)&match, &inner_pos) != FAILURE) {
-+
-+ matchcopy = **match;
-+ zval_copy_ctor(&matchcopy);
-+ INIT_PZVAL(&matchcopy);
-+ convert_to_string(&matchcopy);
-+
-+ MAKE_STD_ZVAL(pattern_match);
-+ Z_STRVAL_P(pattern_match) = (char *)Z_STRVAL(matchcopy);
-+ Z_STRLEN_P(pattern_match) = Z_STRLEN(matchcopy);
-+ Z_TYPE_P(pattern_match) = IS_STRING;
-+
-+ zval_dtor(&matchcopy);
-+ }
++ /* The first element (should be) is the global match
++ Need to move to the inner array to get the global match */
++ if (zend_hash_index_find(ht, 0, (void **)&ppzval) == SUCCESS) {
++ zval **match, **offset;
++ /* If everything goes according to the master plan
++ tmpcopy now contains two elements:
++ 0 = the match
++ 1 = starting position of the match */
++ if (zend_hash_index_find(Z_ARRVAL_PP(ppzval), 0, (void **)&match) == SUCCESS
++ && zend_hash_index_find(Z_ARRVAL_PP(ppzval), 1, (void **)&offset) == SUCCESS) {
++ if (Z_TYPE_PP(match) != IS_STRING || Z_TYPE_PP(offset) != IS_LONG) {
++ goto error_out;
+ }
-+
-+ if (zend_hash_has_more_elements_ex(inner_ht, &inner_pos) == SUCCESS &&
-+ zend_hash_move_forward_ex(inner_ht, &inner_pos)) {
-+
-+ if (zend_hash_get_current_data_ex(inner_ht, (void**)&offset, &inner_pos) != FAILURE) {
-+
-+ offsetcopy = **offset;
-+ zval_copy_ctor(&offsetcopy);
-+ INIT_PZVAL(&offsetcopy);
-+ convert_to_long(&offsetcopy);
-+
-+ MAKE_STD_ZVAL(pattern_offset);
-+ Z_LVAL_P(pattern_offset) = Z_LVAL(offsetcopy);
-+ Z_TYPE_P(pattern_offset) = IS_LONG;
-+
-+ zval_dtor(&offsetcopy);
-+ }
-+ }
-+ zval_dtor(&tmpcopy);
-+ }
-+
-+ if ((pattern_match != NULL) && (pattern_offset != NULL)) {
-+ ms->search.s += (int)Z_LVAL_P(pattern_offset); /* this is where the match starts */
-+ ms->search.offset += (size_t)Z_LVAL_P(pattern_offset); /* this is where the match starts as size_t */
-+ ms->search.rm_len = Z_STRLEN_P(pattern_match) /* This is the length of the matched pattern */;
++ ms->search.s += (int)Z_LVAL_PP(offset); /* this is where the match starts */
++ ms->search.offset += (size_t)Z_LVAL_PP(offset); /* this is where the match starts as size_t */
++ ms->search.rm_len = Z_STRLEN_PP(match) /* This is the length of the matched pattern */;
+ v = 0;
-+
-+ efree(pattern_match);
-+ efree(pattern_offset);
-+
+ } else {
-+ zval_ptr_dtor(&subpats);
-+ FREE_ZVAL(retval);
-+ zval_dtor(pattern);
-+ FREE_ZVAL(pattern);
-+ return -1;
++ goto error_out;
+ }
++ } else {
++error_out:
++ zval_ptr_dtor(&subpats);
++ FREE_ZVAL(retval);
++ zval_dtor(pattern);
++ FREE_ZVAL(pattern);
++ return -1;
+ }
-
-- l = 0;
-- rc = regcomp(&rx, m->value.s,
-- REG_EXTENDED|REG_NEWLINE|
-- ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
-- if (rc) {
-- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
-- file_magerror(ms, "regex error %d, (%s)",
-- rc, errmsg);
-- v = (uint64_t)-1;
-- }
-- else {
-- regmatch_t pmatch[1];
--#ifndef REG_STARTEND
--#define REG_STARTEND 0
-- size_t l = ms->search.s_len - 1;
-- char c = ms->search.s[l];
-- ((char *)(intptr_t)ms->search.s)[l] = '\0';
--#else
-- pmatch[0].rm_so = 0;
-- pmatch[0].rm_eo = ms->search.s_len;
--#endif
-- rc = regexec(&rx, (const char *)ms->search.s,
-- 1, pmatch, REG_STARTEND);
--#if REG_STARTEND == 0
-- ((char *)(intptr_t)ms->search.s)[l] = c;
--#endif
-- switch (rc) {
-- case 0:
-- ms->search.s += (int)pmatch[0].rm_so;
-- ms->search.offset += (size_t)pmatch[0].rm_so;
-- ms->search.rm_len =
-- (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
-- v = 0;
-- break;
-
-- case REG_NOMATCH:
+ } else {
v = 1;
- break;
}
case FILE_INDIRECT:
diff -u libmagic.orig/strcasestr.c libmagic/strcasestr.c
---- libmagic.orig/strcasestr.c Thu Dec 5 17:57:50 2013
-+++ libmagic/strcasestr.c Sun Jan 4 17:06:01 2015
+--- libmagic.orig/strcasestr.c 2016-01-25 11:17:38.160978563 +0800
++++ libmagic/strcasestr.c 2016-01-25 11:19:37.413347842 +0800
@@ -37,6 +37,8 @@
__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $");
#endif /* LIBC_SCCS and not lint */