diff -u libmagic.origin/apprentice.c libmagic/apprentice.c
---- libmagic.origin/apprentice.c 2012-09-11 11:09:26.000000000 +0800
-+++ libmagic/apprentice.c 2012-09-11 11:36:51.000000000 +0800
+--- libmagic.origin/apprentice.c Sat Dec 17 18:17:18 2011
++++ libmagic/apprentice.c Tue Oct 16 10:21:49 2012
@@ -29,6 +29,8 @@
* apprentice - make one pass through /etc/magic, learning its secrets.
*/
#include "file.h"
#ifndef lint
-@@ -36,18 +38,34 @@
+@@ -36,18 +38,31 @@
#endif /* lint */
#include "magic.h"
-#ifdef QUICK
-#include <sys/mman.h>
-#endif
-+#ifndef PHP_WIN32
- #include <dirent.h>
-+#endif
+-#include <dirent.h>
#define EATAB {while (isascii((unsigned char) *l) && \
isspace((unsigned char) *l)) ++l;}
-@@ -112,12 +130,10 @@
+@@ -112,12 +127,10 @@
private int parse_strength(struct magic_set *, struct magic_entry *, const char *);
private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
private struct {
const char *name;
size_t len;
-@@ -131,38 +147,7 @@
+@@ -131,38 +144,7 @@
{ NULL, 0, NULL }
};
static const struct type_tbl_s {
const char name[16];
-@@ -218,6 +203,10 @@
+@@ -218,6 +200,10 @@
# undef XX_NULL
};
private int
get_type(const char *l, const char **t)
{
-@@ -275,15 +264,17 @@
+@@ -275,15 +261,17 @@
if (rv != 0)
return -1;
rv = apprentice_compile(ms, &magic, &nmagic, fn);
if (rv != 0)
return -1;
}
-@@ -295,11 +286,7 @@
+@@ -295,11 +283,7 @@
return -1;
}
ml->magic = magic;
ml->nmagic = nmagic;
-@@ -318,7 +305,6 @@
+@@ -318,7 +302,6 @@
}
return 0;
}
protected void
-@@ -327,22 +313,18 @@
+@@ -327,22 +310,18 @@
if (p == NULL)
return;
switch (type) {
default:
abort();
}
-@@ -355,23 +337,27 @@
+@@ -355,23 +334,27 @@
char *p, *mfn;
int file_err, errs = -1;
struct mlist *mlist;
mlist->next = mlist->prev = mlist;
while (fn) {
-@@ -385,13 +371,13 @@
+@@ -385,13 +368,13 @@
fn = p;
}
if (errs == -1) {
return mlist;
}
-@@ -524,6 +510,7 @@
+@@ -524,6 +507,7 @@
abort();
}
/*
* Magic entries with no description get a bonus because they depend
* on subsequent magic entries to print something.
-@@ -539,8 +526,8 @@
+@@ -539,8 +523,8 @@
private int
apprentice_sort(const void *a, const void *b)
{
size_t sa = apprentice_magic_strength(ma->mp);
size_t sb = apprentice_magic_strength(mb->mp);
if (sa == sb)
-@@ -671,12 +658,22 @@
+@@ -671,12 +655,22 @@
load_1(struct magic_set *ms, int action, const char *fn, int *errs,
struct magic_entry **marray, uint32_t *marraycount)
{
if (errno != ENOENT)
file_error(ms, errno, "cannot read magic file `%s'",
fn);
-@@ -684,9 +681,12 @@
+@@ -684,9 +678,12 @@
return;
}
if (len == 0) /* null line, garbage, etc */
continue;
if (line[len - 1] == '\n') {
-@@ -736,8 +736,7 @@
+@@ -736,8 +733,7 @@
break;
}
}
}
/*
-@@ -754,23 +753,19 @@
+@@ -754,23 +750,21 @@
apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
const char *fn, int action)
{
+ size_t files = 0, maxfiles = 0;
+ char **filearr = NULL;
struct stat st;
- DIR *dir;
- struct dirent *d;
+- DIR *dir;
+- struct dirent *d;
++ php_stream *dir;
++ php_stream_dirent d;
++
++ TSRMLS_FETCH();
ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
marraycount = 0;
/* print silly verbose header for USG compat. */
-@@ -778,14 +773,18 @@
+@@ -778,22 +772,26 @@
(void)fprintf(stderr, "%s\n", usg_hdr);
/* load directory or file */
- if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
+- dir = opendir(fn);
+ /* 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);
++ int mflen;
++ char mfn[MAXPATHLEN];
++
++ dir = php_stream_opendir(fn, REPORT_ERRORS, NULL);
if (!dir) {
errs++;
goto out;
}
- while ((d = readdir(dir)) != NULL) {
+- while ((d = readdir(dir)) != NULL) {
- if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
-+ if ((mflen = snprintf(mfn, sizeof(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);
++ strlen(fn) + strlen(d.d_name) + 2);
errs++;
-@@ -793,7 +792,6 @@
+- closedir(dir);
++ php_stream_closedir(dir);
goto out;
}
if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
realloc(filearr, mlen))) == NULL) {
file_oomem(ms, mlen);
- free(mfn);
- closedir(dir);
+- closedir(dir);
++ php_stream_closedir(dir);
errs++;
goto out;
}
- filearr[files++] = mfn;
+ filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen);
}
- closedir(dir);
+- closedir(dir);
++ php_stream_closedir(dir);
qsort(filearr, files, sizeof(*filearr), cmpstrp);
for (i = 0; i < files; i++) {
load_1(ms, action, filearr[i], &errs, &marray,
m->mimetype[0] = '\0'; /* initialise MIME type to none */
if (m->cont_level == 0)
++(*nmentryp); /* make room for next */
-@@ -2195,56 +2180,69 @@
+@@ -2195,56 +2180,79 @@
/*
* handle a compiled file.
+ ret = 3;
+ 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(fn, 0, &st, NULL) == SUCCESS) {
++ if (st.sb.st_mode & S_IFDIR) {
++ goto error2;
++ }
++ }
++#endif
dbname = mkdbname(ms, fn, 0);
if (dbname == NULL)
ptr = (uint32_t *)(void *)*magicp;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
-@@ -2259,35 +2257,55 @@
+@@ -2259,35 +2267,55 @@
else
version = ptr[1];
if (version != VERSIONNO) {
return -1;
}
-@@ -2301,42 +2319,49 @@
+@@ -2301,42 +2329,49 @@
apprentice_compile(struct magic_set *ms, struct magic **magicp,
uint32_t *nmagicp, const char *fn)
{
return rv;
}
-@@ -2349,6 +2374,7 @@
+@@ -2349,6 +2384,7 @@
{
const char *p, *q;
char *buf;
if (strip) {
if ((p = strrchr(fn, '/')) != NULL)
-@@ -2370,14 +2396,14 @@
+@@ -2370,14 +2406,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 +2493,7 @@
+@@ -2467,7 +2503,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-09-11 11:09:26.000000000 +0800
-+++ libmagic/ascmagic.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/ascmagic.c Sat Dec 17 18:17:18 2011
++++ libmagic/ascmagic.c Tue Apr 10 09:46:33 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/cdf.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/cdf.c Mon Feb 20 23:35:29 2012
++++ libmagic/cdf.c Tue Apr 10 09:46:33 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/cdf.h 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/cdf.h Fri Feb 17 06:28:31 2012
++++ libmagic/cdf.h Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/cdf_time.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/cdf_time.c Tue Dec 13 14:48:41 2011
++++ libmagic/cdf_time.c Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/compress.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/compress.c Sat Dec 17 18:17:18 2011
++++ libmagic/compress.c Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/file.h 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/file.h Tue Sep 20 17:30:14 2011
++++ libmagic/file.h Mon Apr 23 17:58:54 2012
@@ -33,11 +33,9 @@
#ifndef __file_h__
#define __file_h__
size_t strlcat(char *dst, const char *src, size_t siz);
#endif
#ifndef HAVE_GETLINE
-@@ -500,4 +487,12 @@
- #define FILE_RCSID(id)
+@@ -498,6 +485,14 @@
#endif
-
+ #else
+ #define FILE_RCSID(id)
++#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.origin/fsmagic.c libmagic/fsmagic.c
---- libmagic.origin/fsmagic.c 2012-09-11 11:09:26.000000000 +0800
-+++ libmagic/fsmagic.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/fsmagic.c Tue Aug 23 10:57:10 2011
++++ libmagic/fsmagic.c Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/funcs.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/funcs.c Sat Dec 17 18:17:18 2011
++++ libmagic/funcs.c Mon Apr 23 17:58:54 2012
@@ -41,52 +41,42 @@
#if defined(HAVE_WCTYPE_H)
#include <wctype.h>
}
+
diff -u libmagic.origin/magic.c libmagic/magic.c
---- libmagic.origin/magic.c 2012-09-11 11:09:26.000000000 +0800
-+++ libmagic/magic.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/magic.c Thu May 26 03:27:59 2011
++++ libmagic/magic.c Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/magic.h 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/magic.h Sun Dec 18 15:54:43 2011
++++ libmagic/magic.h Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/print.c 2012-09-11 11:33:55.000000000 +0800
-@@ -29,6 +29,9 @@
+--- libmagic.origin/print.c Tue Sep 20 17:28:09 2011
++++ libmagic/print.c Tue Oct 16 10:13:39 2012
+@@ -29,12 +29,16 @@
* print.c - debugging printout routines
*/
#include "file.h"
#ifndef lint
-@@ -46,174 +49,21 @@
+ FILE_RCSID("@(#)$File: print.c,v 1.71 2011/09/20 15:28:09 christos Exp $")
+ #endif /* lint */
+
++#include <stdio.h>
+ #include <string.h>
+ #include <stdarg.h>
+ #include <stdlib.h>
+@@ -45,174 +49,21 @@
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
protected const char *
diff -u libmagic.origin/readcdf.c libmagic/readcdf.c
---- libmagic.origin/readcdf.c 2012-09-11 11:09:26.000000000 +0800
-+++ libmagic/readcdf.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/readcdf.c Mon Feb 20 21:04:58 2012
++++ libmagic/readcdf.c Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/readelf.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/readelf.c Tue Aug 23 10:57:10 2011
++++ libmagic/readelf.c Tue Apr 10 09:46:34 2012
@@ -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-09-11 11:09:26.000000000 +0800
-+++ libmagic/softmagic.c 2012-09-11 11:33:55.000000000 +0800
+--- libmagic.origin/softmagic.c Sat Dec 17 18:17:18 2011
++++ libmagic/softmagic.c Fri May 25 09:59:25 2012
@@ -41,6 +41,11 @@
#include <stdlib.h>
#include <time.h>