diff -u libmagic.origin/apprentice.c libmagic/apprentice.c
---- libmagic.origin/apprentice.c 2012-07-15 18:17:24.802087661 +0800
-+++ libmagic/apprentice.c 2012-07-15 18:22:49.650087425 +0800
+--- libmagic.origin/apprentice.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/apprentice.c 2012-09-11 11:36:51.000000000 +0800
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
const char *fn, int action)
{
+- int errs = 0;
++ int errs = 0;
struct magic_entry *marray;
uint32_t marraycount, i, mentrycount = 0, starttest;
- size_t slen, files = 0, maxfiles = 0;
- char **filearr = NULL, *mfn;
+ size_t files = 0, maxfiles = 0;
-+ char **filearr = NULL, mfn[MAXPATHLEN];
++ char **filearr = NULL;
struct stat st;
DIR *dir;
struct dirent *d;
marraycount = 0;
/* print silly verbose header for USG compat. */
-@@ -778,14 +773,16 @@
+@@ -778,14 +773,18 @@
(void)fprintf(stderr, "%s\n", usg_hdr);
/* load directory or file */
+ /* 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];
dir = opendir(fn);
if (!dir) {
errs++;
}
while ((d = readdir(dir)) != NULL) {
- if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
-+ if (snprintf(mfn, sizeof(mfn), "%s/%s", fn, d->d_name) < 0) {
++ if ((mflen = snprintf(mfn, sizeof(mfn), "%s/%s", fn, d->d_name)) < 0) {
file_oomem(ms,
strlen(fn) + strlen(d->d_name) + 2);
errs++;
-@@ -793,7 +790,6 @@
+@@ -793,7 +792,6 @@
goto out;
}
if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
continue;
}
if (files >= maxfiles) {
-@@ -803,20 +799,19 @@
+@@ -803,20 +801,19 @@
if ((filearr = CAST(char **,
realloc(filearr, mlen))) == NULL) {
file_oomem(ms, mlen);
}
}
- filearr[files++] = mfn;
-+ filearr[files++] = estrndup(mfn);
++ filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen);
}
closedir(dir);
qsort(filearr, files, sizeof(*filearr), cmpstrp);
}
free(filearr);
} else
-@@ -882,12 +877,7 @@
+@@ -882,12 +879,7 @@
for (i = 0; i < marraycount; i++)
mentrycount += marray[i].cont_count;
mentrycount = 0;
for (i = 0; i < marraycount; i++) {
-@@ -896,9 +886,14 @@
+@@ -896,9 +888,14 @@
mentrycount += marray[i].cont_count;
}
out:
if (errs) {
*magicp = NULL;
*nmagicp = 0;
-@@ -1175,14 +1170,13 @@
+@@ -1175,14 +1172,13 @@
return -1;
}
me = &(*mentryp)[*nmentryp - 1];
me->mp = m = nm;
me->max_count = CAST(uint32_t, cnt);
}
-@@ -1194,23 +1188,13 @@
+@@ -1194,23 +1190,13 @@
struct magic_entry *mp;
maxmagic += ALLOC_INCR;
me->mp = m;
me->max_count = ALLOC_CHUNK;
} else
-@@ -1353,6 +1337,10 @@
+@@ -1353,6 +1339,10 @@
if (m->type == FILE_INVALID) {
if (ms->flags & MAGIC_CHECK)
file_magwarn(ms, "type `%s' invalid", l);
return -1;
}
-@@ -1361,7 +1349,7 @@
+@@ -1361,7 +1351,7 @@
m->mask_op = 0;
if (*l == '~') {
m->mask_op |= FILE_OPINVERSE;
else if (ms->flags & MAGIC_CHECK)
file_magwarn(ms, "'~' invalid for string types");
-@@ -1370,7 +1358,7 @@
+@@ -1370,7 +1360,7 @@
m->str_range = 0;
m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
if ((op = get_op(*l)) != -1) {
uint64_t val;
++l;
m->mask_op |= op;
-@@ -1558,11 +1546,6 @@
+@@ -1558,11 +1548,6 @@
if (check_format(ms, m) == -1)
return -1;
}
m->mimetype[0] = '\0'; /* initialise MIME type to none */
if (m->cont_level == 0)
++(*nmentryp); /* make room for next */
-@@ -2195,56 +2178,69 @@
+@@ -2195,56 +2180,69 @@
/*
* handle a compiled file.
ptr = (uint32_t *)(void *)*magicp;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
-@@ -2259,35 +2255,55 @@
+@@ -2259,35 +2257,55 @@
else
version = ptr[1];
if (version != VERSIONNO) {
return -1;
}
-@@ -2301,42 +2317,49 @@
+@@ -2301,42 +2319,49 @@
apprentice_compile(struct magic_set *ms, struct magic **magicp,
uint32_t *nmagicp, const char *fn)
{
return rv;
}
-@@ -2349,6 +2372,7 @@
+@@ -2349,6 +2374,7 @@
{
const char *p, *q;
char *buf;
if (strip) {
if ((p = strrchr(fn, '/')) != NULL)
-@@ -2370,14 +2394,14 @@
+@@ -2370,14 +2396,14 @@
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)
-@@ -2467,7 +2491,7 @@
+@@ -2467,7 +2493,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.origin/ascmagic.c libmagic/ascmagic.c
---- libmagic.origin/ascmagic.c 2012-07-15 18:17:24.762087659 +0800
-+++ libmagic/ascmagic.c 2012-07-15 18:20:42.730087520 +0800
+--- libmagic.origin/ascmagic.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/ascmagic.c 2012-09-11 11:33:55.000000000 +0800
@@ -139,10 +139,8 @@
/* malloc size is a conservative overestimate; could be
improved, or at least realloced after conversion. */
return rv;
}
diff -u libmagic.origin/cdf.c libmagic/cdf.c
---- libmagic.origin/cdf.c 2012-07-15 18:17:24.822087659 +0800
-+++ libmagic/cdf.c 2012-07-15 18:20:42.730087520 +0800
+--- libmagic.origin/cdf.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/cdf.c 2012-09-11 11:33:55.000000000 +0800
@@ -43,7 +43,17 @@
#include <err.h>
#endif
(void)fprintf(stderr, "timestamp %s\n", buf);
} else {
diff -u libmagic.origin/cdf.h libmagic/cdf.h
---- libmagic.origin/cdf.h 2012-07-15 18:17:25.046087660 +0800
-+++ libmagic/cdf.h 2012-07-15 18:20:42.730087520 +0800
+--- libmagic.origin/cdf.h 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/cdf.h 2012-09-11 11:33:55.000000000 +0800
@@ -35,7 +35,7 @@
#ifndef _H_CDF_
#define _H_CDF_
void cdf_swap_header(cdf_header_t *);
void cdf_unpack_header(cdf_header_t *, char *);
diff -u libmagic.origin/cdf_time.c libmagic/cdf_time.c
---- libmagic.origin/cdf_time.c 2012-07-15 18:17:24.734087660 +0800
-+++ libmagic/cdf_time.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/cdf_time.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/cdf_time.c 2012-09-11 11:33:55.000000000 +0800
@@ -96,7 +96,7 @@
}
static const char *ref = "Sat Apr 23 01:30:00 1977";
char *p, *q;
diff -u libmagic.origin/compress.c libmagic/compress.c
---- libmagic.origin/compress.c 2012-07-15 18:17:24.730087657 +0800
-+++ libmagic/compress.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/compress.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/compress.c 2012-09-11 11:33:55.000000000 +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 */
+Only in libmagic: diff
diff -u libmagic.origin/file.h libmagic/file.h
---- libmagic.origin/file.h 2012-07-15 18:17:25.046087660 +0800
-+++ libmagic/file.h 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/file.h 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/file.h 2012-09-11 11:33:55.000000000 +0800
@@ -33,11 +33,9 @@
#ifndef __file_h__
#define __file_h__
+
#endif /* __file_h__ */
diff -u libmagic.origin/fsmagic.c libmagic/fsmagic.c
---- libmagic.origin/fsmagic.c 2012-07-15 18:17:24.730087657 +0800
-+++ libmagic/fsmagic.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/fsmagic.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/fsmagic.c 2012-09-11 11:33:55.000000000 +0800
@@ -59,27 +59,21 @@
# define minor(dev) ((dev) & 0xff)
#endif
-#else
- if (file_printf(ms, "block special") == -1)
- return -1;
- #endif
+-#endif
- }
- return 1;
--#endif
+ #endif
- /* TODO add code to handle V7 MUX and Blit MUX files */
+
#ifdef S_IFIFO
/*
diff -u libmagic.origin/funcs.c libmagic/funcs.c
---- libmagic.origin/funcs.c 2012-07-15 18:17:25.046087660 +0800
-+++ libmagic/funcs.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/funcs.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/funcs.c 2012-09-11 11:33:55.000000000 +0800
@@ -41,52 +41,42 @@
#if defined(HAVE_WCTYPE_H)
#include <wctype.h>
+ return rep_cnt;
}
+
-Common subdirectories: libmagic.origin/.libs and libmagic/.libs
diff -u libmagic.origin/magic.c libmagic/magic.c
---- libmagic.origin/magic.c 2012-07-15 18:17:25.046087660 +0800
-+++ libmagic/magic.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/magic.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/magic.c 2012-09-11 11:33:55.000000000 +0800
@@ -25,11 +25,6 @@
* SUCH DAMAGE.
*/
public const char *
magic_error(struct magic_set *ms)
diff -u libmagic.origin/magic.h libmagic/magic.h
---- libmagic.origin/magic.h 2012-07-15 18:17:24.734087660 +0800
-+++ libmagic/magic.h 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/magic.h 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/magic.h 2012-09-11 11:33:55.000000000 +0800
@@ -85,6 +85,7 @@
const char *magic_getpath(const char *, int);
int magic_errno(magic_t);
diff -u libmagic.origin/print.c libmagic/print.c
---- libmagic.origin/print.c 2012-07-15 18:21:02.846087501 +0800
-+++ libmagic/print.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/print.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/print.c 2012-09-11 11:33:55.000000000 +0800
@@ -29,6 +29,9 @@
* print.c - debugging printout routines
*/
}
protected const char *
-diff -u libmagic.origin/print.c.rej libmagic/print.c.rej
---- libmagic.origin/print.c.rej 2012-07-15 18:17:24.762087659 +0800
-+++ libmagic/print.c.rej 2012-07-15 18:20:27.186087168 +0800
-@@ -1,12 +1,184 @@
----- libmagic/print.c 2012-05-29 14:34:03.770376387 +0200
--+++ libmagic.orig/print.c 2012-05-29 14:40:41.710960045 +0200
--@@ -29,9 +29,6 @@
-- * print.c - debugging printout routines
-- */
-+--- libmagic.orig/print.c 2012-05-29 14:40:41.710960045 +0200
-++++ libmagic/print.c 2012-05-29 14:34:03.770376387 +0200
-+@@ -48,174 +51,21 @@
-
---#include "php.h"
---#include "main/snprintf.h"
-+ #define SZOF(a) (sizeof(a) / sizeof(a[0]))
-+
-+-#ifndef COMPILE_ONLY
-+-protected void
-+-file_mdump(struct magic *m)
-+-{
-+- private const char optyp[] = { FILE_OPS };
-+-
-+- (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
-+- (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
-+-
-+- if (m->flag & INDIR) {
-+- (void) fprintf(stderr, "(%s,",
-+- /* Note: type is unsigned */
-+- (m->in_type < file_nnames) ?
-+- file_names[m->in_type] : "*bad*");
-+- if (m->in_op & FILE_OPINVERSE)
-+- (void) fputc('~', stderr);
-+- (void) fprintf(stderr, "%c%u),",
-+- ((size_t)(m->in_op & FILE_OPS_MASK) <
-+- SZOF(optyp)) ?
-+- optyp[m->in_op & FILE_OPS_MASK] : '?',
-+- m->in_offset);
-+- }
-+- (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
-+- /* Note: type is unsigned */
-+- (m->type < file_nnames) ? file_names[m->type] : "*bad*");
-+- if (m->mask_op & FILE_OPINVERSE)
-+- (void) fputc('~', stderr);
-+-
-+- if (IS_STRING(m->type)) {
-+- if (m->str_flags) {
-+- (void) fputc('/', stderr);
-+- if (m->str_flags & STRING_COMPACT_WHITESPACE)
-+- (void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
-+- if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
-+- (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
-+- stderr);
-+- if (m->str_flags & STRING_IGNORE_LOWERCASE)
-+- (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
-+- if (m->str_flags & STRING_IGNORE_UPPERCASE)
-+- (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
-+- if (m->str_flags & REGEX_OFFSET_START)
-+- (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
-+- if (m->str_flags & STRING_TEXTTEST)
-+- (void) fputc(CHAR_TEXTTEST, stderr);
-+- if (m->str_flags & STRING_BINTEST)
-+- (void) fputc(CHAR_BINTEST, stderr);
-+- if (m->str_flags & PSTRING_1_BE)
-+- (void) fputc(CHAR_PSTRING_1_BE, stderr);
-+- if (m->str_flags & PSTRING_2_BE)
-+- (void) fputc(CHAR_PSTRING_2_BE, stderr);
-+- if (m->str_flags & PSTRING_2_LE)
-+- (void) fputc(CHAR_PSTRING_2_LE, stderr);
-+- if (m->str_flags & PSTRING_4_BE)
-+- (void) fputc(CHAR_PSTRING_4_BE, stderr);
-+- if (m->str_flags & PSTRING_4_LE)
-+- (void) fputc(CHAR_PSTRING_4_LE, stderr);
-+- if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
-+- (void) fputc(
-+- CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
-+- stderr);
-+- }
-+- if (m->str_range)
-+- (void) fprintf(stderr, "/%u", m->str_range);
-+- }
-+- else {
-+- if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
-+- (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
-+- else
-+- (void) fputc('?', stderr);
-+-
-+- if (m->num_mask) {
-+- (void) fprintf(stderr, "%.8llx",
-+- (unsigned long long)m->num_mask);
-+- }
-+- }
-+- (void) fprintf(stderr, ",%c", m->reln);
-+-
-+- if (m->reln != 'x') {
-+- switch (m->type) {
-+- case FILE_BYTE:
-+- case FILE_SHORT:
-+- case FILE_LONG:
-+- case FILE_LESHORT:
-+- case FILE_LELONG:
-+- case FILE_MELONG:
-+- case FILE_BESHORT:
-+- case FILE_BELONG:
-+- (void) fprintf(stderr, "%d", m->value.l);
-+- break;
-+- case FILE_BEQUAD:
-+- case FILE_LEQUAD:
-+- case FILE_QUAD:
-+- (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
-+- (unsigned long long)m->value.q);
-+- break;
-+- case FILE_PSTRING:
-+- case FILE_STRING:
-+- case FILE_REGEX:
-+- case FILE_BESTRING16:
-+- case FILE_LESTRING16:
-+- case FILE_SEARCH:
-+- file_showstr(stderr, m->value.s, (size_t)m->vallen);
-+- break;
-+- case FILE_DATE:
-+- case FILE_LEDATE:
-+- case FILE_BEDATE:
-+- case FILE_MEDATE:
-+- (void)fprintf(stderr, "%s,",
-+- file_fmttime(m->value.l, 1));
-+- break;
-+- case FILE_LDATE:
-+- case FILE_LELDATE:
-+- case FILE_BELDATE:
-+- case FILE_MELDATE:
-+- (void)fprintf(stderr, "%s,",
-+- file_fmttime(m->value.l, 0));
-+- break;
-+- case FILE_QDATE:
-+- case FILE_LEQDATE:
-+- case FILE_BEQDATE:
-+- (void)fprintf(stderr, "%s,",
-+- file_fmttime((uint32_t)m->value.q, 1));
-+- break;
-+- case FILE_QLDATE:
-+- case FILE_LEQLDATE:
-+- case FILE_BEQLDATE:
-+- (void)fprintf(stderr, "%s,",
-+- file_fmttime((uint32_t)m->value.q, 0));
-+- break;
-+- case FILE_FLOAT:
-+- case FILE_BEFLOAT:
-+- case FILE_LEFLOAT:
-+- (void) fprintf(stderr, "%G", m->value.f);
-+- break;
-+- case FILE_DOUBLE:
-+- case FILE_BEDOUBLE:
-+- case FILE_LEDOUBLE:
-+- (void) fprintf(stderr, "%G", m->value.d);
-+- break;
-+- case FILE_DEFAULT:
-+- /* XXX - do anything here? */
-+- break;
-+- default:
-+- (void) fputs("*bad*", stderr);
-+- break;
-+- }
-+- }
-+- (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
-+-}
-+-#endif
-+-
-+ /*VARARGS*/
-+ protected void
-+ file_magwarn(struct magic_set *ms, const char *f, ...)
-+ {
-+ va_list va;
-++ char *expanded_format;
-++ TSRMLS_FETCH();
-+
-+- /* cuz we use stdout for most, stderr here */
-+- (void) fflush(stdout);
- -
-- #include "file.h"
-+- if (ms->file)
-+- (void) fprintf(stderr, "%s, %lu: ", ms->file,
-+- (unsigned long)ms->line);
-+- (void) fprintf(stderr, "Warning: ");
-+ va_start(va, f);
-+- (void) vfprintf(stderr, f, va);
-++ vasprintf(&expanded_format, f, va);
-+ va_end(va);
-+- (void) fputc('\n', stderr);
-++
-++ php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Warning: %s", expanded_format);
-++
-++ free(expanded_format);
-+ }
-
-- #ifndef lint
-+ protected const char *
diff -u libmagic.origin/readcdf.c libmagic/readcdf.c
---- libmagic.origin/readcdf.c 2012-07-15 18:17:24.734087660 +0800
-+++ libmagic/readcdf.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/readcdf.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/readcdf.c 2012-09-11 11:33:55.000000000 +0800
@@ -30,7 +30,11 @@
#endif
if ((ec = strchr(c, '\n')) != NULL)
*ec = '\0';
diff -u libmagic.origin/readelf.c libmagic/readelf.c
---- libmagic.origin/readelf.c 2012-07-15 18:17:25.046087660 +0800
-+++ libmagic/readelf.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/readelf.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/readelf.c 2012-09-11 11:33:55.000000000 +0800
@@ -49,7 +49,7 @@
off_t, int *, int);
private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
if (fstat(fd, &st) == -1) {
diff -u libmagic.origin/softmagic.c libmagic/softmagic.c
---- libmagic.origin/softmagic.c 2012-07-15 18:17:24.722087658 +0800
-+++ libmagic/softmagic.c 2012-07-15 18:20:42.734087519 +0800
+--- libmagic.origin/softmagic.c 2012-09-11 11:09:26.000000000 +0800
++++ libmagic/softmagic.c 2012-09-11 11:33:55.000000000 +0800
@@ -41,6 +41,11 @@
#include <stdlib.h>
#include <time.h>