--- embedaddon/php/ext/fileinfo/libmagic.patch 2012/02/21 23:47:56 1.1.1.1 +++ embedaddon/php/ext/fileinfo/libmagic.patch 2012/05/29 12:34:39 1.1.1.2 @@ -1,6 +1,6 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c ---- libmagic.orig/apprentice.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/apprentice.c 2011-09-15 23:29:29.000000000 +0800 +--- libmagic.orig/apprentice.c 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/apprentice.c 2012-03-28 01:53:04.283305402 +0200 @@ -29,6 +29,8 @@ * apprentice - make one pass through /etc/magic, learning its secrets. */ @@ -10,9 +10,11 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice #include "file.h" #ifndef lint -@@ -38,17 +40,32 @@ +@@ -36,18 +38,34 @@ + #endif /* lint */ + #include "magic.h" - #include "patchlevel.h" ++#include "patchlevel.h" #include -#ifdef HAVE_UNISTD_H + @@ -47,7 +49,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice #define EATAB {while (isascii((unsigned char) *l) && \ isspace((unsigned char) *l)) ++l;} -@@ -116,12 +133,10 @@ +@@ -112,12 +130,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 *); @@ -60,7 +62,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice private struct { const char *name; size_t len; -@@ -135,38 +150,7 @@ +@@ -131,38 +147,7 @@ { NULL, 0, NULL } }; @@ -100,7 +102,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice static const struct type_tbl_s { const char name[16]; -@@ -222,6 +206,10 @@ +@@ -218,6 +203,10 @@ # undef XX_NULL }; @@ -111,7 +113,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice private int get_type(const char *l, const char **t) { -@@ -279,15 +267,17 @@ +@@ -275,15 +264,17 @@ if (rv != 0) return -1; rv = apprentice_compile(ms, &magic, &nmagic, fn); @@ -134,7 +136,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice if (rv != 0) return -1; } -@@ -299,11 +289,7 @@ +@@ -295,11 +286,7 @@ return -1; } @@ -147,15 +149,15 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice ml->magic = magic; ml->nmagic = nmagic; -@@ -315,7 +301,6 @@ - mlist->prev = ml; - +@@ -318,7 +305,6 @@ + } + return 0; -#endif /* COMPILE_ONLY */ } protected void -@@ -324,22 +309,18 @@ +@@ -327,22 +313,18 @@ if (p == NULL) return; switch (type) { @@ -184,16 +186,24 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice default: abort(); } -@@ -357,20 +338,17 @@ - - if (fn == NULL) - fn = getenv("MAGIC"); -- if (fn == NULL) -- fn = MAGIC; +@@ -355,23 +337,27 @@ + char *p, *mfn; + int file_err, errs = -1; + struct mlist *mlist; - ++/* XXX disabling default magic loading so the compiled in data is used */ ++#if 0 + if ((fn = magic_getpath(fn, action)) == NULL) + return NULL; ++#endif + + init_file_tables(); + - if ((mfn = strdup(fn)) == NULL) { - file_oomem(ms, strlen(fn)); - return NULL; ++ if (fn == NULL) ++ fn = getenv("MAGIC"); + if (fn == NULL) { + mlist = emalloc(sizeof(*mlist)); + mlist->next = mlist->prev = mlist; @@ -213,7 +223,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice mlist->next = mlist->prev = mlist; while (fn) { -@@ -384,13 +362,13 @@ +@@ -385,13 +371,13 @@ fn = p; } if (errs == -1) { @@ -230,7 +240,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice return mlist; } -@@ -523,6 +501,7 @@ +@@ -524,6 +510,7 @@ abort(); } @@ -238,7 +248,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice /* * Magic entries with no description get a bonus because they depend * on subsequent magic entries to print something. -@@ -538,8 +517,8 @@ +@@ -539,8 +526,8 @@ private int apprentice_sort(const void *a, const void *b) { @@ -249,17 +259,16 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice size_t sa = apprentice_magic_strength(ma->mp); size_t sb = apprentice_magic_strength(mb->mp); if (sa == sb) -@@ -617,34 +596,51 @@ +@@ -671,12 +658,22 @@ load_1(struct magic_set *ms, int action, const char *fn, int *errs, struct magic_entry **marray, uint32_t *marraycount) { -- char line[BUFSIZ]; +- size_t lineno = 0, llen = 0; + char buffer[BUFSIZ + 1]; -+ char *line; -+ size_t line_len; - size_t lineno = 0; -- FILE *f = fopen(ms->file = fn, "r"); -- if (f == NULL) { + char *line = NULL; +- ssize_t len; ++ size_t len; ++ size_t lineno = 0; + + php_stream *stream; + @@ -270,71 +279,50 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice +#else + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL); +#endif -+ + +- FILE *f = fopen(ms->file = fn, "r"); +- if (f == NULL) { + if (stream == NULL) { if (errno != ENOENT) file_error(ms, errno, "cannot read magic file `%s'", fn); - (*errs)++; - } else { -+ - /* read and parse this file */ -- for (ms->line = 1; fgets(line, sizeof(line), f) != NULL; ms->line++) { -- size_t len; -- len = strlen(line); -- if (len == 0) /* null line, garbage, etc */ +@@ -684,9 +681,12 @@ + return; + } + +- /* read and parse this file */ +- for (ms->line = 1; (len = getline(&line, &llen, f)) != -1; +- ms->line++) { ++ /* read and parse this file */ +#if (PHP_MAJOR_VERSION < 6) -+ for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &line_len)) != NULL; ms->line++) { ++ for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &len)) != NULL; ms->line++) { +#else -+ for (ms->line = 1; (line = php_stream_get_line(stream, ZSTR(buffer), BUFSIZ, &line_len)) != NULL; ms->line++) { ++ for (ms->line = 1; (line = php_stream_get_line(stream, ZSTR(buffer), BUFSIZ, &len)) != NULL; ms->line++) { +#endif -+ if (line_len == 0) /* null line, garbage, etc */ - continue; -- if (line[len - 1] == '\n') { -+ -+ if (line[line_len - 1] == '\n') { - lineno++; -- line[len - 1] = '\0'; /* delete newline */ -+ line[line_len - 1] = '\0'; /* delete newline */ - } - if (line[0] == '\0') /* empty, do not parse */ - continue; - if (line[0] == '#') /* comment, do not parse */ - continue; -+ - if (line[0] == '!' && line[1] == ':') { - size_t i; - - for (i = 0; bang[i].name != NULL; i++) { -- if (len - 2 > bang[i].len && -+ if (line_len - 2 > bang[i].len && - memcmp(bang[i].name, line + 2, - bang[i].len) == 0) - break; -@@ -670,12 +666,11 @@ - } - continue; - } -- if (parse(ms, marray, marraycount, line, lineno, -- action) != 0) -+ if (parse(ms, marray, marraycount, line, lineno, action) != 0) - (*errs)++; + if (len == 0) /* null line, garbage, etc */ + continue; + if (line[len - 1] == '\n') { +@@ -736,8 +736,7 @@ + break; } - -- (void)fclose(f); -+ php_stream_close(stream); } +- free(line); +- (void)fclose(f); ++ php_stream_close(stream); } -@@ -690,7 +685,6 @@ + /* +@@ -757,20 +756,16 @@ int errs = 0; struct magic_entry *marray; uint32_t marraycount, i, mentrycount = 0, starttest; -- size_t slen; - char subfn[MAXPATHLEN]; +- size_t slen, files = 0, maxfiles = 0; +- char **filearr = NULL, *mfn; ++ size_t files = 0, maxfiles = 0; ++ char **filearr = NULL, mfn[MAXPATHLEN]; struct stat st; DIR *dir; -@@ -698,12 +692,8 @@ + struct dirent *d; ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */ @@ -349,24 +337,42 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice marraycount = 0; /* print silly verbose header for USG compat. */ -@@ -713,14 +703,14 @@ +@@ -778,14 +773,16 @@ + (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 (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) { ++ /* 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)) { dir = opendir(fn); - if (dir) { - while ((d = readdir(dir)) != NULL) { - snprintf(subfn, sizeof(subfn), "%s/%s", - fn, d->d_name); - if (stat(subfn, &st) == 0 && -- S_ISREG(st.st_mode)) { -+ S_ISREG(st.st_mode)) { - load_1(ms, action, subfn, &errs, - &marray, &marraycount); - } -@@ -790,12 +780,7 @@ + if (!dir) { + errs++; + goto out; + } + 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) { + file_oomem(ms, + strlen(fn) + strlen(d->d_name) + 2); + errs++; +@@ -793,7 +790,6 @@ + goto out; + } + if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { +- free(mfn); + continue; + } + if (files >= maxfiles) { +@@ -803,7 +799,6 @@ + if ((filearr = CAST(char **, + realloc(filearr, mlen))) == NULL) { + file_oomem(ms, mlen); +- free(mfn); + closedir(dir); + errs++; + goto out; +@@ -882,12 +877,7 @@ for (i = 0; i < marraycount; i++) mentrycount += marray[i].cont_count; @@ -380,7 +386,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice mentrycount = 0; for (i = 0; i < marraycount; i++) { -@@ -805,8 +790,8 @@ +@@ -897,8 +887,8 @@ } out: for (i = 0; i < marraycount; i++) @@ -391,7 +397,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice if (errs) { *magicp = NULL; *nmagicp = 0; -@@ -1081,11 +1066,7 @@ +@@ -1178,11 +1168,7 @@ if (me->cont_count == me->max_count) { struct magic *nm; size_t cnt = me->max_count + ALLOC_CHUNK; @@ -402,9 +408,9 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice - } + nm = erealloc(me->mp, sizeof(*nm) * cnt); me->mp = m = nm; - me->max_count = cnt; + me->max_count = CAST(uint32_t, cnt); } -@@ -1097,23 +1078,13 @@ +@@ -1194,23 +1180,13 @@ struct magic_entry *mp; maxmagic += ALLOC_INCR; @@ -431,7 +437,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice me->mp = m; me->max_count = ALLOC_CHUNK; } else -@@ -1264,7 +1235,7 @@ +@@ -1361,7 +1337,7 @@ m->mask_op = 0; if (*l == '~') { @@ -440,16 +446,16 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice m->mask_op |= FILE_OPINVERSE; else if (ms->flags & MAGIC_CHECK) file_magwarn(ms, "'~' invalid for string types"); -@@ -1274,7 +1245,7 @@ - m->str_flags = 0; - m->num_mask = 0; +@@ -1370,7 +1346,7 @@ + m->str_range = 0; + m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0; if ((op = get_op(*l)) != -1) { - if (!IS_STRING(m->type)) { + if (!IS_LIBMAGIC_STRING(m->type)) { uint64_t val; ++l; m->mask_op |= op; -@@ -1423,11 +1394,6 @@ +@@ -1558,11 +1534,6 @@ if (check_format(ms, m) == -1) return -1; } @@ -461,7 +467,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice m->mimetype[0] = '\0'; /* initialise MIME type to none */ if (m->cont_level == 0) ++(*nmentryp); /* make room for next */ -@@ -2053,56 +2019,68 @@ +@@ -2195,56 +2166,68 @@ /* * handle a compiled file. @@ -553,23 +559,14 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice ptr = (uint32_t *)(void *)*magicp; if (*ptr != MAGICNO) { if (swap4(*ptr) != MAGICNO) { -@@ -2110,42 +2088,65 @@ - goto error1; - } - needsbyteswap = 1; -- } else -+ } else { - needsbyteswap = 0; -+ } -+ - if (needsbyteswap) - version = swap4(ptr[1]); +@@ -2259,35 +2242,55 @@ else version = ptr[1]; -+ if (version != VERSIONNO) { - file_error(ms, 0, "File %d.%d supports only version %d magic " - "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel, +- 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); goto error1; } @@ -634,24 +631,22 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice return -1; } -@@ -2159,41 +2160,50 @@ +@@ -2301,42 +2304,49 @@ apprentice_compile(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, const char *fn) { -- int fd; +- int fd = -1; char *dbname; int rv = -1; + php_stream *stream; - dbname = mkdbname(ms, fn, 1); + TSRMLS_FETCH(); - -- if (dbname == NULL) -+ dbname = mkdbname(ms, fn, 0); + -+ if (dbname == NULL) { ++ dbname = mkdbname(ms, fn, 0); + + if (dbname == NULL) goto out; -+ } - if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) { +/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */ @@ -686,7 +681,8 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice goto out; } -- (void)close(fd); +- if (fd != -1) +- (void)close(fd); + php_stream_close(stream); + rv = 0; @@ -696,7 +692,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice return rv; } -@@ -2206,6 +2216,7 @@ +@@ -2349,6 +2359,7 @@ { const char *p, *q; char *buf; @@ -704,7 +700,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice if (strip) { if ((p = strrchr(fn, '/')) != NULL) -@@ -2227,14 +2238,14 @@ +@@ -2370,14 +2381,14 @@ q++; /* Compatibility with old code that looked in .mime */ if (ms->flags & MAGIC_MIME) { @@ -723,7 +719,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice /* Compatibility with old code that looked in .mime */ if (strstr(p, ".mime") != NULL) -@@ -2324,7 +2335,7 @@ +@@ -2467,7 +2478,7 @@ m->offset = swap4((uint32_t)m->offset); m->in_offset = swap4((uint32_t)m->in_offset); m->lineno = swap4((uint32_t)m->lineno); @@ -733,34 +729,36 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice m->str_flags = swap4(m->str_flags); } diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c ---- libmagic.orig/ascmagic.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/ascmagic.c 2011-09-15 23:28:26.000000000 +0800 -@@ -144,10 +144,8 @@ - /* 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) { -- file_oomem(ms, mlen); -- goto done; -- } -+ utf8_buf = emalloc(mlen); +--- libmagic.orig/ascmagic.c 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/ascmagic.c 2012-03-28 01:53:04.287303117 +0200 +@@ -139,10 +139,8 @@ + /* 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) { +- file_oomem(ms, mlen); +- goto done; +- } ++ utf8_buf = emalloc(mlen); + - if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL) - goto done; - if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf), -@@ -310,7 +308,7 @@ + if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) + == NULL) + goto done; +@@ -296,7 +294,8 @@ + } rv = 1; done: - if (utf8_buf) -- free(utf8_buf); +- free(utf8_buf); ++ if (utf8_buf) + efree(utf8_buf); return rv; } +Only in libmagic.orig: asprintf.c diff -u libmagic.orig/cdf.c libmagic/cdf.c ---- libmagic.orig/cdf.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/cdf.c 2011-09-15 23:29:29.000000000 +0800 -@@ -40,7 +40,17 @@ +--- libmagic.orig/cdf.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/cdf.c 2012-03-28 01:53:04.299331601 +0200 +@@ -43,7 +43,17 @@ #include #endif #include @@ -778,7 +776,7 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c #include #include #include -@@ -1042,7 +1052,7 @@ +@@ -1135,7 +1145,7 @@ cdf_directory_t *d; char name[__arraycount(d->d_name)]; cdf_stream_t scn; @@ -787,23 +785,16 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c static const char *types[] = { "empty", "user storage", "user stream", "lockbytes", "property", "root storage" }; -@@ -1094,13 +1104,13 @@ +@@ -1188,7 +1198,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; + size_t i, j; - for (i = 0; i < count; i++) { - cdf_print_property_name(buf, sizeof(buf), info[i].pi_id); -- (void)fprintf(stderr, "%zu) %s: ", i, buf); -+ (void)fprintf(stderr, "%zu) %s: ", i, buf); - switch (info[i].pi_type) { - case CDF_SIGNED16: - (void)fprintf(stderr, "signed 16 [%hd]\n", -@@ -1121,13 +1131,17 @@ +@@ -1232,7 +1242,11 @@ break; case CDF_FILETIME: tp = info[i].pi_tp; @@ -815,18 +806,20 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c cdf_print_elapsed_time(buf, sizeof(buf), tp); (void)fprintf(stderr, "timestamp %s\n", buf); } else { - cdf_timestamp_to_timespec(&ts, tp); - (void)fprintf(stderr, "timestamp %s", -- ctime(&ts.tv_sec)); -+ ctime(&ts.tv_sec)); - } - break; - case CDF_CLIPBOARD: diff -u libmagic.orig/cdf.h libmagic/cdf.h ---- libmagic.orig/cdf.h 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/cdf.h 2011-09-15 23:29:29.000000000 +0800 -@@ -42,7 +42,11 @@ +--- libmagic.orig/cdf.h 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/cdf.h 2012-03-28 01:53:04.299331601 +0200 +@@ -35,7 +35,7 @@ + #ifndef _H_CDF_ + #define _H_CDF_ +-#ifdef WIN32 ++#ifdef PHP_WIN32 + #include + #define timespec timeval + #define tv_nsec tv_usec +@@ -57,7 +57,11 @@ + typedef struct { uint64_t h_magic; -#define CDF_MAGIC 0xE11AB1A1E011CFD0LL @@ -838,7 +831,7 @@ diff -u libmagic.orig/cdf.h libmagic/cdf.h uint64_t h_uuid[2]; uint16_t h_revision; uint16_t h_version; -@@ -248,9 +252,9 @@ +@@ -267,9 +271,9 @@ size_t i_len; } cdf_info_t; @@ -852,8 +845,8 @@ diff -u libmagic.orig/cdf.h libmagic/cdf.h void cdf_swap_header(cdf_header_t *); void cdf_unpack_header(cdf_header_t *, char *); diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c ---- libmagic.orig/cdf_time.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/cdf_time.c 2011-10-19 22:51:40.000000000 +0800 +--- libmagic.orig/cdf_time.c 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/cdf_time.c 2012-03-28 01:53:04.299331601 +0200 @@ -96,7 +96,7 @@ } @@ -873,26 +866,26 @@ diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c + ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC; t /= CDF_TIME_PREC; - tm.tm_sec = t % 60; + tm.tm_sec = (int)(t % 60); @@ -117,7 +117,7 @@ - tm.tm_hour = t % 24; + tm.tm_hour = (int)(t % 24); t /= 24; - // XXX: Approx + /* XXX: Approx */ - tm.tm_year = CDF_BASE_YEAR + (t / 365); + tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365)); rdays = cdf_getdays(tm.tm_year); -@@ -143,7 +143,7 @@ - } +@@ -144,7 +144,7 @@ int + /*ARGSUSED*/ -cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts) +cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timeval *ts) { + #ifndef __lint__ (void)&t; - (void)&ts; -@@ -153,7 +153,7 @@ +@@ -156,7 +156,7 @@ errno = EINVAL; return -1; } @@ -901,7 +894,7 @@ diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c *t = tm.tm_sec; *t += tm.tm_min * 60; *t += tm.tm_hour * 60 * 60; -@@ -167,7 +167,7 @@ +@@ -182,7 +182,7 @@ int main(int argc, char *argv[]) { @@ -911,8 +904,8 @@ diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c static const char *ref = "Sat Apr 23 01:30:00 1977"; char *p, *q; diff -u libmagic.orig/compress.c libmagic/compress.c ---- libmagic.orig/compress.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/compress.c 2011-09-15 23:28:26.000000000 +0800 +--- libmagic.orig/compress.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/compress.c 2012-03-28 01:53:04.299331601 +0200 @@ -32,6 +32,7 @@ * uncompress(method, old, n, newch) - uncompress old into new, * using method, return sizeof new @@ -921,18 +914,17 @@ diff -u libmagic.orig/compress.c libmagic/compress.c #include "file.h" #ifndef lint -@@ -45,7 +46,10 @@ +@@ -45,7 +46,8 @@ #endif #include #include +-#ifndef __MINGW32__ +#include +#ifndef PHP_WIN32 #include -+#endif - #ifdef HAVE_SYS_WAIT_H - #include #endif -@@ -57,6 +61,9 @@ + #ifdef HAVE_SYS_WAIT_H +@@ -59,6 +61,9 @@ #include #endif @@ -942,21 +934,17 @@ diff -u libmagic.orig/compress.c libmagic/compress.c private const struct { const char magic[8]; size_t maglen; -@@ -79,12 +86,10 @@ - { "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */ - }; - --private size_t ncompr = sizeof(compr) / sizeof(compr[0]); -- +@@ -85,8 +90,7 @@ #define NODATA ((size_t)~0) -- private ssize_t swrite(int, const void *, size_t); +-#if HAVE_FORK +-private size_t ncompr = sizeof(compr) / sizeof(compr[0]); +#ifdef PHP_FILEINFO_UNCOMPRESS private size_t uncompressbuf(struct magic_set *, int, size_t, const unsigned char *, unsigned char **, size_t); #ifdef BUILTIN_DECOMPRESS -@@ -100,10 +105,13 @@ +@@ -102,10 +106,13 @@ size_t i, nsz; int rv = 0; int mime = ms->flags & MAGIC_MIME; @@ -970,29 +958,31 @@ diff -u libmagic.orig/compress.c libmagic/compress.c for (i = 0; i < ncompr; i++) { if (nbytes < compr[i].maglen) continue; -@@ -133,10 +141,11 @@ +@@ -134,7 +141,8 @@ + } } error: - if (newbuf) -- free(newbuf); +- free(newbuf); ++ if (newbuf) + efree(newbuf); ms->flags |= MAGIC_COMPRESS; return rv; } -+#endif - - /* - * `safe' write for sockets and pipes. -@@ -169,7 +178,7 @@ +@@ -168,12 +176,9 @@ + * `safe' read for sockets and pipes. + */ protected ssize_t - sread(int fd, void *buf, size_t n, int canbepipe) +-sread(int fd, void *buf, size_t n, int canbepipe __attribute__ ((unused))) ++sread(int fd, void *buf, size_t n, int canbepipe) { -- int rv, cnt; -+ int rv; + ssize_t rv; +-#ifdef FD_ZERO +- ssize_t cnt; +-#endif #ifdef FIONREAD int t = 0; #endif -@@ -181,6 +190,7 @@ +@@ -185,6 +190,7 @@ #ifdef FIONREAD if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) { #ifdef FD_ZERO @@ -1000,24 +990,18 @@ diff -u libmagic.orig/compress.c libmagic/compress.c for (cnt = 0;; cnt++) { fd_set check; struct timeval tout = {0, 100 * 1000}; -@@ -294,6 +304,7 @@ +@@ -301,7 +307,8 @@ + } return fd; } - +-#if HAVE_FORK ++ +#ifdef PHP_FILEINFO_UNCOMPRESS #ifdef BUILTIN_DECOMPRESS #define FHCRC (1 << 1) -@@ -301,6 +312,7 @@ - #define FNAME (1 << 3) - #define FCOMMENT (1 << 4) +@@ -338,9 +345,7 @@ -+ - private size_t - uncompressgzipped(struct magic_set *ms, const unsigned char *old, - unsigned char **newch, size_t n) -@@ -330,9 +342,7 @@ - if (data_start >= n) return 0; - if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) { @@ -1027,7 +1011,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c /* XXX: const castaway, via strchr */ z.next_in = (Bytef *)strchr((const char *)old + data_start, -@@ -455,20 +465,14 @@ +@@ -465,20 +470,14 @@ fdin[1] = -1; } @@ -1051,15 +1035,18 @@ diff -u libmagic.orig/compress.c libmagic/compress.c n = 0; newch[0] = '\0'; goto err; -@@ -492,3 +496,4 @@ +@@ -502,4 +501,4 @@ return n; } } +-#endif +#endif /* if PHP_FILEINFO_UNCOMPRESS */ +Only in libmagic: config.h +Only in libmagic.orig: file.c diff -u libmagic.orig/file.h libmagic/file.h ---- libmagic.orig/file.h 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/file.h 2011-09-15 23:28:26.000000000 +0800 -@@ -33,9 +33,7 @@ +--- libmagic.orig/file.h 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/file.h 2012-03-28 01:53:04.304322598 +0200 +@@ -33,11 +33,9 @@ #ifndef __file_h__ #define __file_h__ @@ -1068,9 +1055,12 @@ diff -u libmagic.orig/file.h libmagic/file.h -#endif +#include "config.h" - #include /* Include that here, to make sure __P gets defined */ - #include -@@ -46,9 +44,20 @@ +-#ifdef WIN32 ++#ifdef PHP_WIN32 + #ifdef _WIN64 + #define SIZE_T_FORMAT "I64" + #else +@@ -61,9 +59,20 @@ #ifdef HAVE_INTTYPES_H #include #endif @@ -1092,16 +1082,16 @@ diff -u libmagic.orig/file.h libmagic/file.h /* Do this here and now, because struct stat gets re-defined on solaris */ #include #include -@@ -59,7 +68,7 @@ +@@ -74,7 +83,7 @@ #define MAGIC "/etc/magic" #endif --#ifdef __EMX__ +-#if defined(__EMX__) || defined (WIN32) +#if defined(__EMX__) || defined(PHP_WIN32) #define PATHSEP ';' #else #define PATHSEP ':' -@@ -81,12 +90,6 @@ +@@ -100,12 +109,6 @@ #endif #endif @@ -1114,7 +1104,7 @@ diff -u libmagic.orig/file.h libmagic/file.h #ifndef MIN #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #endif -@@ -189,7 +192,7 @@ +@@ -209,7 +212,7 @@ #define FILE_INDIRECT 41 #define FILE_NAMES_SIZE 42/* size of array to contain all names */ @@ -1123,7 +1113,7 @@ diff -u libmagic.orig/file.h libmagic/file.h ((t) == FILE_STRING || \ (t) == FILE_PSTRING || \ (t) == FILE_BESTRING16 || \ -@@ -211,7 +214,7 @@ +@@ -231,7 +234,7 @@ #ifdef ENABLE_CONDITIONALS uint8_t cond; /* conditional type */ #else @@ -1132,7 +1122,7 @@ diff -u libmagic.orig/file.h libmagic/file.h #endif uint8_t factor_op; #define FILE_FACTOR_OP_PLUS '+' -@@ -345,20 +348,20 @@ +@@ -387,21 +390,18 @@ struct stat; protected const char *file_fmttime(uint32_t, int); @@ -1143,6 +1133,8 @@ diff -u libmagic.orig/file.h libmagic/file.h +protected int file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream); protected int file_pipe2file(struct magic_set *, int, const void *, size_t); -protected int file_vprintf(struct magic_set *, const char *, va_list); +-protected size_t file_printedlen(const struct magic_set *); + protected int file_replace(struct magic_set *, const char *, const char *); -protected int file_printf(struct magic_set *, const char *, ...) - __attribute__((__format__(__printf__, 2, 3))); +protected int file_printf(struct magic_set *, const char *, ...); @@ -1151,14 +1143,12 @@ diff -u libmagic.orig/file.h libmagic/file.h size_t); protected int file_trycdf(struct magic_set *, int, const unsigned char *, size_t); +-#if HAVE_FORK +#ifdef PHP_FILEINFO_UNCOMPRESS protected int file_zmagic(struct magic_set *, int, const char *, const unsigned char *, size_t); -+#endif - protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t); - protected int file_ascmagic_with_encoding(struct magic_set *, - const unsigned char *, size_t, unichar *, size_t, const char *, -@@ -375,13 +378,9 @@ + #endif +@@ -422,13 +422,9 @@ protected void file_badread(struct magic_set *); protected void file_badseek(struct magic_set *); protected void file_oomem(struct magic_set *, size_t); @@ -1175,7 +1165,7 @@ diff -u libmagic.orig/file.h libmagic/file.h protected void file_showstr(FILE *, const char *, size_t); protected size_t file_mbswidth(const char *); protected const char *file_getbuffer(struct magic_set *); -@@ -394,11 +393,8 @@ +@@ -443,11 +439,8 @@ size_t); #endif /* __EMX__ */ @@ -1187,7 +1177,7 @@ diff -u libmagic.orig/file.h libmagic/file.h #ifndef HAVE_STRERROR extern int sys_nerr; -@@ -411,20 +407,14 @@ +@@ -460,17 +453,10 @@ #define strtoul(a, b, c) strtol(a, b, c) #endif @@ -1206,45 +1196,40 @@ diff -u libmagic.orig/file.h libmagic/file.h +#ifndef strlcat size_t strlcat(char *dst, const char *src, size_t siz); #endif - -+ - #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) - #define QUICK - #endif + #ifndef HAVE_GETLINE +Only in libmagic.orig: file_opts.h diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c ---- libmagic.orig/fsmagic.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/fsmagic.c 2011-09-15 23:28:26.000000000 +0800 -@@ -60,29 +60,19 @@ +--- libmagic.orig/fsmagic.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/fsmagic.c 2012-03-28 01:53:04.304322598 +0200 +@@ -59,27 +59,21 @@ + # define minor(dev) ((dev) & 0xff) #endif #undef HAVE_MAJOR - +-#ifdef S_IFLNK -private int -bad_link(struct magic_set *ms, int err, char *buf) -{ -- const char *errfmt; - int mime = ms->flags & MAGIC_MIME; - if ((mime & MAGIC_MIME_TYPE) && -- file_printf(ms, "application/x-symlink") +- file_printf(ms, "inode/symlink") - == -1) - return -1; - else if (!mime) { -- if (err == ELOOP) -- errfmt = "symbolic link in a loop"; -- else -- errfmt = "broken symbolic link to `%s'"; - if (ms->flags & MAGIC_ERROR) { -- file_error(ms, err, errfmt, buf); +- file_error(ms, err, +- "broken symbolic link to `%s'", buf); - return -1; - } -- if (file_printf(ms, errfmt, buf) == -1) +- if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1) - return -1; - } - return 1; -} ++ +#ifdef PHP_WIN32 + +# undef S_IFIFO -+#endif + #endif + + +#ifndef S_ISDIR @@ -1254,10 +1239,11 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c +#ifndef S_ISREG +#define S_ISREG(mode) ((mode) & _S_IFREG) +#endif - ++ private int handle_mime(struct magic_set *ms, int mime, const char *str) -@@ -100,41 +90,36 @@ + { +@@ -96,42 +90,36 @@ } protected int @@ -1268,7 +1254,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c int mime = ms->flags & MAGIC_MIME; -#ifdef S_IFLNK - char buf[BUFSIZ+4]; -- int nch; +- ssize_t nch; - struct stat tstatbuf; -#endif + TSRMLS_FETCH(); @@ -1317,20 +1303,18 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - if (file_printf(ms, "cannot open `%s' (%s)", - fn, strerror(errno)) == -1) - return -1; -- return 1; +- ms->event_flags |= EVENT_HAD_ERR; +- return -1; } if (!mime) { -@@ -154,172 +139,75 @@ - return -1; - #endif +@@ -153,77 +141,42 @@ } -- -+ + switch (sb->st_mode & S_IFMT) { - case S_IFDIR: - if (mime) { -- if (handle_mime(ms, mime, "x-directory") == -1) +- if (handle_mime(ms, mime, "directory") == -1) - return -1; - } else if (file_printf(ms, "directory") == -1) - return -1; @@ -1345,7 +1329,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - if ((ms->flags & MAGIC_DEVICES) != 0) - break; - if (mime) { -- if (handle_mime(ms, mime, "x-character-device") == -1) +- if (handle_mime(ms, mime, "chardevice") == -1) - return -1; - } else { -#ifdef HAVE_STAT_ST_RDEV @@ -1377,7 +1361,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - if ((ms->flags & MAGIC_DEVICES) != 0) - break; - if (mime) { -- if (handle_mime(ms, mime, "x-block-device") == -1) +- if (handle_mime(ms, mime, "blockdevice") == -1) - return -1; - } else { -#ifdef HAVE_STAT_ST_RDEV @@ -1390,42 +1374,6 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - if (file_printf(ms, "block special (%ld/%ld)", - (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1) - return -1; --# endif --#else -- if (file_printf(ms, "block special") == -1) -- return -1; --#endif -- } -- return 1; --#endif -- /* TODO add code to handle V7 MUX and Blit MUX files */ --#ifdef S_IFIFO -- case S_IFIFO: -- if((ms->flags & MAGIC_DEVICES) != 0) -- break; -- if (mime) { -- if (handle_mime(ms, mime, "x-fifo") == -1) -- return -1; -- } else if (file_printf(ms, "fifo (named pipe)") == -1) -- return -1; -- return 1; --#endif --#ifdef S_IFDOOR -- case S_IFDOOR: -- if (mime) { -- if (handle_mime(ms, mime, "x-door") == -1) -- return -1; -- } else if (file_printf(ms, "door") == -1) -- return -1; -- return 1; --#endif --#ifdef S_IFLNK -- case S_IFLNK: -- if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { -- if (ms->flags & MAGIC_ERROR) { -- file_error(ms, errno, "unreadable symlink `%s'", -- fn); -- return -1; +#ifndef PHP_WIN32 +# ifdef S_IFCHR + case S_IFCHR: @@ -1436,15 +1384,10 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c + */ + if ((ms->flags & MAGIC_DEVICES) != 0) { + break; - } - if (mime) { -- if (handle_mime(ms, mime, "x-symlink") == -1) ++ } ++ if (mime) { + if (handle_mime(ms, mime, "x-character-device") == -1) - return -1; -- } else if (file_printf(ms, -- "unreadable symlink `%s' (%s)", fn, -- strerror(errno)) == -1) -- return -1; ++ return -1; + } else { +# ifdef HAVE_STAT_ST_RDEV +# ifdef dv_unit @@ -1463,8 +1406,41 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c + return -1; +# endif + } - return 1; ++ return 1; + # endif +-#else +- if (file_printf(ms, "block special") == -1) +- return -1; +-#endif - } +- return 1; + #endif +- /* TODO add code to handle V7 MUX and Blit MUX files */ ++ + #ifdef S_IFIFO + case S_IFIFO: + if((ms->flags & MAGIC_DEVICES) != 0) +@@ -246,77 +199,14 @@ + #endif + #ifdef S_IFLNK + case S_IFLNK: +- if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { ++ /* stat is used, if it made here then the link is broken */ + if (ms->flags & MAGIC_ERROR) { +- file_error(ms, errno, "unreadable symlink `%s'", +- fn); ++ file_error(ms, errno, "unreadable symlink `%s'", fn); + return -1; + } +- if (mime) { +- if (handle_mime(ms, mime, "symlink") == -1) +- return -1; +- } else if (file_printf(ms, +- "unreadable symlink `%s' (%s)", fn, +- strerror(errno)) == -1) +- return -1; +- return 1; +- } - buf[nch] = '\0'; /* readlink(2) does not do this */ - - /* If broken symlink, say so and quit early. */ @@ -1474,9 +1450,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - } else { - char *tmp; - char buf2[BUFSIZ+BUFSIZ+4]; -+# endif -+#endif - +- - if ((tmp = strrchr(fn, '/')) == NULL) { - tmp = buf; /* in current directory anyway */ - } else { @@ -1484,30 +1458,16 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - if (ms->flags & MAGIC_ERROR) { - file_error(ms, 0, - "path too long: `%s'", buf); -+#ifdef S_IFIFO -+ case S_IFIFO: -+ if((ms->flags & MAGIC_DEVICES) != 0) -+ break; -+ if (mime) { -+ if (handle_mime(ms, mime, "x-fifo") == -1) - return -1; +- return -1; - } -+ } else if (file_printf(ms, "fifo (named pipe)") == -1) -+ return -1; -+ return 1; -+#endif -+#ifdef S_IFDOOR -+ case S_IFDOOR: - if (mime) { +- if (mime) { - if (handle_mime(ms, mime, - "x-path-too-long") == -1) -+ if (handle_mime(ms, mime, "x-door") == -1) - return -1; +- return -1; - } else if (file_printf(ms, - "path too long: `%s'", fn) == -1) -+ } else if (file_printf(ms, "door") == -1) - return -1; - return 1; +- return -1; +- return 1; - } - /* take dir part */ - (void)strlcpy(buf2, fn, sizeof buf2); @@ -1519,8 +1479,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - if (stat(tmp, &tstatbuf) < 0) - return bad_link(ms, errno, buf); - } -+#endif - +- - /* Otherwise, handle it. */ - if ((ms->flags & MAGIC_SYMLINK) != 0) { - const char *p; @@ -1530,27 +1489,20 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c - return p != NULL ? 1 : -1; - } else { /* just print what it points to */ - if (mime) { -- if (handle_mime(ms, mime, "x-symlink") == -1) +- if (handle_mime(ms, mime, "symlink") == -1) - return -1; - } else if (file_printf(ms, "symbolic link to `%s'", - buf) == -1) - return -1; - } - return 1; -+#ifdef S_IFLNK -+ case S_IFLNK: -+ /* 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); -+ return -1; -+ } + return 1; #endif + #ifdef S_IFSOCK #ifndef __COHERENT__ case S_IFSOCK: -@@ -331,12 +219,14 @@ +@@ -328,12 +218,14 @@ return 1; #endif #endif @@ -1572,22 +1524,27 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c /* diff -u libmagic.orig/funcs.c libmagic/funcs.c ---- libmagic.orig/funcs.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/funcs.c 2011-09-15 23:28:26.000000000 +0800 -@@ -41,52 +41,36 @@ +--- libmagic.orig/funcs.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/funcs.c 2012-03-28 11:49:56.089607390 +0200 +@@ -41,52 +41,42 @@ #if defined(HAVE_WCTYPE_H) #include #endif -#if defined(HAVE_LIMITS_H) -#include --#endif ++ ++#ifndef SIZE_MAX ++# define SIZE_MAX ((size_t) -1) + #endif -#ifndef SIZE_MAX -#define SIZE_MAX ((size_t)~0) -+#ifndef SIZE_MAX -+# define SIZE_MAX ((size_t) -1) ++#ifndef PREG_OFFSET_CAPTURE ++# define PREG_OFFSET_CAPTURE (1<<8) #endif ++extern public void convert_libmagic_pattern(zval *pattern, int options); ++ /* * Like printf, only we append to a buffer. */ @@ -1643,23 +1600,20 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c } /* -@@ -97,17 +81,32 @@ +@@ -97,17 +87,30 @@ file_error_core(struct magic_set *ms, int error, const char *f, va_list va, - uint32_t lineno) + size_t lineno) { + char *buf = NULL; + /* Only the first error is ok */ -- if (ms->event_flags & EVENT_HAD_ERR) -+ if (ms->event_flags & EVENT_HAD_ERR) { + if (ms->event_flags & EVENT_HAD_ERR) return; -+ } -+ if (lineno != 0) { - free(ms->o.buf); + efree(ms->o.buf); ms->o.buf = NULL; - file_printf(ms, "line %u: ", lineno); + file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno); } - file_vprintf(ms, f, va); - if (error > 0) @@ -1681,24 +1635,20 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c ms->event_flags |= EVENT_HAD_ERR; ms->error = error; } -@@ -153,14 +152,13 @@ +@@ -154,10 +157,9 @@ file_error(ms, errno, "error reading"); } -#ifndef COMPILE_ONLY protected int --file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf, +-file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)), +- const void *buf, size_t nb) +file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const void *buf, - size_t nb) ++ size_t nb) { int m = 0, rv = 0, looks_text = 0; int mime = ms->flags & MAGIC_MIME; -- const unsigned char *ubuf = CAST(const unsigned char *, buf); -+ const unsigned char *ubuf = buf; - unichar *u8buf = NULL; - size_t ulen; - const char *code = NULL; -@@ -188,7 +186,7 @@ +@@ -189,7 +191,7 @@ &code, &code_mime, &type); } @@ -1707,11 +1657,13 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { switch (file_os2_apptype(ms, inname, buf, nb)) { case -1: -@@ -201,13 +199,14 @@ +@@ -201,10 +203,10 @@ + } } #endif - +-#if HAVE_FORK - /* try compression stuff */ ++ +#if PHP_FILEINFO_UNCOMPRESS if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) - if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) { @@ -1719,13 +1671,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "zmagic %d\n", m); goto done; -- } -+ } -+#endif - - /* Check if we have a tar file */ - if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) -@@ -218,12 +217,17 @@ +@@ -219,12 +221,17 @@ } /* Check if we have a CDF file */ @@ -1748,7 +1694,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c /* try soft magic tests */ if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) -@@ -294,7 +298,6 @@ +@@ -296,7 +303,6 @@ return m; } @@ -1756,7 +1702,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c protected int file_reset(struct magic_set *ms) -@@ -304,11 +307,11 @@ +@@ -306,11 +312,11 @@ return -1; } if (ms->o.buf) { @@ -1770,7 +1716,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c ms->o.pbuf = NULL; } ms->event_flags &= ~EVENT_HAD_ERR; -@@ -342,14 +345,10 @@ +@@ -344,14 +350,10 @@ /* * 4 is for octal representation, + 1 is for NUL */ len = strlen(ms->o.buf); if (len > (SIZE_MAX - 1) / 4) { @@ -1786,7 +1732,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c ms->o.pbuf = pbuf; #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) -@@ -409,13 +408,7 @@ +@@ -411,13 +413,7 @@ if (level >= ms->c.len) { len = (ms->c.len += 20) * sizeof(*ms->c.li); @@ -1801,10 +1747,97 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c } ms->c.li[level].got_match = 0; #ifdef ENABLE_CONDITIONALS +@@ -433,29 +429,51 @@ + return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); + } + +-protected int ++ ++protected int + file_replace(struct magic_set *ms, const char *pat, const char *rep) + { +- regex_t rx; +- int rc; ++ zval *patt; ++ int opts = 0; ++ pcre_cache_entry *pce; ++ char *res; ++ zval *repl; ++ int res_len, rep_cnt; ++ TSRMLS_FETCH(); ++ ++ MAKE_STD_ZVAL(patt); ++ ZVAL_STRINGL(patt, pat, strlen(pat), 0); ++ opts |= PCRE_MULTILINE; ++ convert_libmagic_pattern(patt, opts); ++#if (PHP_MAJOR_VERSION < 6) ++ if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) { ++#else ++ if ((pce = pcre_get_compiled_regex_cache(IS_STRING, Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) { ++#endif ++ zval_dtor(patt); ++ FREE_ZVAL(patt); ++ return -1; ++ } ++ ++ MAKE_STD_ZVAL(repl); ++ ZVAL_STRINGL(repl, rep, strlen(rep), 0); + +- rc = regcomp(&rx, pat, REG_EXTENDED); +- if (rc) { +- char errmsg[512]; +- (void)regerror(rc, &rx, errmsg, sizeof(errmsg)); +- file_magerror(ms, "regex error %d, (%s)", rc, errmsg); ++ res = php_pcre_replace_impl(pce, ms->o.buf, strlen(ms->o.buf), repl, ++ 0, &res_len, -1, &rep_cnt TSRMLS_CC); ++ ++ FREE_ZVAL(repl); ++ zval_dtor(patt); ++ FREE_ZVAL(patt); ++ ++ if (NULL == res) { + return -1; +- } else { +- regmatch_t rm; +- int nm = 0; +- while (regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) { +- 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) +- return -1; +- nm++; +- } +- regfree(&rx); +- return nm; + } ++ ++ strncpy(ms->o.buf, res, res_len); ++ ms->o.buf[res_len] = '\0'; ++ ++ efree(res); ++ ++ return rep_cnt; + } ++ +Only in libmagic.orig: getline.c +Only in libmagic.orig: getopt_long.c +Only in libmagic: LICENSE diff -u libmagic.orig/magic.c libmagic/magic.c ---- libmagic.orig/magic.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/magic.c 2011-09-15 23:29:29.000000000 +0800 -@@ -34,14 +34,19 @@ +--- libmagic.orig/magic.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/magic.c 2012-03-28 11:42:50.404750579 +0200 +@@ -25,11 +25,6 @@ + * SUCH DAMAGE. + */ + +-#ifdef WIN32 +-#include +-#include +-#endif +- + #include "file.h" + + #ifndef lint +@@ -39,15 +34,24 @@ #include "magic.h" #include @@ -1822,26 +1855,20 @@ diff -u libmagic.orig/magic.c libmagic/magic.c +# include "php_config.h" #endif -#ifdef HAVE_LIMITS_H +-#include /* for PIPE_BUF */ + - #include /* for PIPE_BUF */ --#endif ++#ifdef PHP_WIN32 ++#include + #endif ++#include /* for PIPE_BUF */ ++ #if defined(HAVE_UTIMES) # include -@@ -57,7 +62,9 @@ - #include /* for read() */ + #elif defined(HAVE_UTIME) +@@ -71,19 +75,24 @@ #endif - --#include /* for byte swapping */ -+#ifndef PHP_WIN32 -+# include /* for byte swapping */ -+#endif - - #include "patchlevel.h" - -@@ -70,13 +77,16 @@ #endif - #endif +#ifdef PHP_WIN32 +# undef S_IFLNK @@ -1852,6 +1879,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c 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); -#endif @@ -1859,20 +1887,59 @@ diff -u libmagic.orig/magic.c libmagic/magic.c #ifndef STDIN_FILENO #define STDIN_FILENO 0 -@@ -86,11 +96,8 @@ + #endif + ++/* XXX this functionality is excluded in php, enable it in apprentice.c:340 */ ++#if 0 + private const char * + get_default_magic(void) + { +@@ -91,7 +100,7 @@ + static char *default_magic; + char *home, *hmagicpath; + +-#ifndef WIN32 ++#ifndef PHP_WIN32 + struct stat st; + + if (default_magic) { +@@ -124,6 +133,7 @@ + #else + char *hmagicp = hmagicpath; + char *tmppath = NULL; ++ LPTSTR dllpath; + + #define APPENDPATH() \ + do { \ +@@ -168,7 +178,7 @@ + } + + /* Third, try to get magic file relative to dll location */ +- LPTSTR dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1)); ++ dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1)); + dllpath[MAX_PATH] = 0; /* just in case long path gets truncated and not null terminated */ + if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){ + PathRemoveFileSpecA(dllpath); +@@ -206,16 +216,14 @@ + + return action == FILE_LOAD ? get_default_magic() : MAGIC; + } ++#endif + + public struct magic_set * magic_open(int flags) { struct magic_set *ms; - size_t len; -- if ((ms = CAST(magic_set *, calloc((size_t)1, +- if ((ms = CAST(struct magic_set *, calloc((size_t)1, - sizeof(struct magic_set)))) == NULL) - return NULL; + ms = ecalloc((size_t)1, sizeof(struct magic_set)); if (magic_setflags(ms, flags) == -1) { errno = EINVAL; -@@ -98,11 +105,9 @@ +@@ -223,11 +231,9 @@ } ms->o.buf = ms->o.pbuf = NULL; @@ -1886,7 +1953,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c ms->event_flags = 0; ms->error = -1; ms->mlist = NULL; -@@ -110,7 +115,7 @@ +@@ -235,7 +241,7 @@ ms->line = 0; return ms; free: @@ -1895,7 +1962,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c return NULL; } -@@ -126,10 +131,10 @@ +@@ -251,10 +257,10 @@ struct mlist *next = ml->next; struct magic *mg = ml->magic; file_delmagic(mg, ml->mapped, ml->nmagic); @@ -1908,7 +1975,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c } private int -@@ -153,11 +158,19 @@ +@@ -278,11 +284,19 @@ public void magic_close(struct magic_set *ms) { @@ -1933,7 +2000,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c } /* -@@ -183,21 +196,10 @@ +@@ -308,13 +322,6 @@ return ml ? 0 : -1; } @@ -1944,8 +2011,10 @@ diff -u libmagic.orig/magic.c libmagic/magic.c - free_mlist(ml); - return ml ? 0 : -1; -} -- - private void + + public int + magic_list(struct magic_set *ms, const char *magicfile) +@@ -328,9 +335,6 @@ close_and_restore(const struct magic_set *ms, const char *name, int fd, const struct stat *sb) { @@ -1955,7 +2024,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { /* -@@ -224,7 +226,6 @@ +@@ -357,7 +361,6 @@ } } @@ -1963,7 +2032,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c /* * find type of descriptor -@@ -232,7 +233,7 @@ +@@ -365,7 +368,7 @@ public const char * magic_descriptor(struct magic_set *ms, int fd) { @@ -1972,7 +2041,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c } /* -@@ -241,103 +242,95 @@ +@@ -374,30 +377,40 @@ public const char * magic_file(struct magic_set *ms, const char *inname) { @@ -2011,43 +2080,16 @@ diff -u libmagic.orig/magic.c libmagic/magic.c - return NULL; + buf = emalloc(HOWMANY + SLOP); -- if (file_reset(ms) == -1) -+ if (file_reset(ms) == -1) { + if (file_reset(ms) == -1) goto done; -+ } - switch (file_fsmagic(ms, inname, &sb)) { -- case -1: /* error */ -- goto done; -- case 0: /* nothing found */ -- break; -- default: /* matched it and printed type */ + switch (file_fsmagic(ms, inname, &sb, stream)) { -+ case -1: /* error */ -+ goto done; -+ case 0: /* nothing found */ -+ break; -+ default: /* matched it and printed type */ -+ rv = 0; -+ goto done; -+ } -+ -+ errno = 0; -+ -+ if (!stream && inname) { -+ no_in_stream = 1; -+#if PHP_API_VERSION < 20100412 -+ stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); -+#else -+ stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL); -+#endif -+ } -+ -+ if (!stream) { -+ if (unreadable_info(ms, sb.st_mode, inname) == -1) -+ goto done; - rv = 0; + case -1: /* error */ goto done; + case 0: /* nothing found */ +@@ -407,68 +420,48 @@ + goto done; } - if (inname == NULL) { @@ -2055,19 +2097,35 @@ diff -u libmagic.orig/magic.c libmagic/magic.c - ispipe = 1; - } else { - int flags = O_RDONLY|O_BINARY; -- ++ errno = 0; + - if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) { +-#ifdef O_NONBLOCK - flags |= O_NONBLOCK; ++ if (!stream && inname) { ++ no_in_stream = 1; ++#if PHP_API_VERSION < 20100412 ++ stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); ++#else ++ stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL); + #endif - ispipe = 1; - } -- ++ } + - errno = 0; - if ((fd = open(inname, flags)) < 0) { - if (unreadable_info(ms, sb.st_mode, inname) == -1) - goto done; - rv = 0; -- goto done; ++ if (!stream) { ++ if (unreadable_info(ms, sb.st_mode, inname) == -1) + goto done; - } ++ rv = 0; ++ goto done; ++ } ++ #ifdef O_NONBLOCK - if ((flags = fcntl(fd, F_GETFL)) != -1) { - flags &= ~O_NONBLOCK; @@ -2125,11 +2183,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c return rv == 0 ? file_getbuffer(ms) : NULL; } -- - public const char * - magic_buffer(struct magic_set *ms, const void *buf, size_t nb) - { -@@ -345,14 +338,13 @@ +@@ -480,14 +473,13 @@ return NULL; /* * The main work is done here! @@ -2147,91 +2201,44 @@ diff -u libmagic.orig/magic.c libmagic/magic.c public const char * magic_error(struct magic_set *ms) diff -u libmagic.orig/magic.h libmagic/magic.h ---- libmagic.orig/magic.h 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/magic.h 2011-09-15 23:28:26.000000000 +0800 -@@ -70,6 +70,7 @@ - void magic_close(magic_t); +--- libmagic.orig/magic.h 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/magic.h 2012-03-28 01:53:04.304322598 +0200 +@@ -85,6 +85,7 @@ + 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); -@@ -78,7 +79,6 @@ +@@ -93,7 +94,6 @@ int magic_load(magic_t, const char *); int magic_compile(magic_t, const char *); -int magic_check(magic_t, const char *); + int magic_list(magic_t, const char *); int magic_errno(magic_t); - #ifdef __cplusplus -diff -u libmagic.orig/patchlevel.h libmagic/patchlevel.h ---- libmagic.orig/patchlevel.h 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/patchlevel.h 2011-09-15 23:28:26.000000000 +0800 -@@ -3,23 +3,23 @@ - - /* - * Patchlevel file for Ian Darwin's MAGIC command. -- * $File: patchlevel.h,v 1.74 2009/05/06 20:32:48 christos Exp $ -+ * $File: patchlevel.h,v 1.68 2008/03/22 21:39:43 christos Exp $ - * -- * $Log: libmagic.patch,v $ -- * Revision 1.1.1.1 2012/02/21 23:47:56 misho -- * php -- * -- * Revision 1.74 2009/05/06 20:32:48 christos -- * welcome to 5.03 -+ * $Log: libmagic.patch,v $ -+ * Revision 1.1.1.1 2012/02/21 23:47:56 misho -+ * php -+ * -+ * Revision 1.4 2009/05/04 20:52:43 scottmac -+ * Update libmagic to 5.02 - * -- * Revision 1.73 2009/05/04 15:15:13 christos -- * 5.02... -+ * Revision 1.3 2009/03/15 23:02:35 scottmac -+ * Update fileinfo to libmagic 5.00 and remove dependency on dirent.h on Windows - * -- * Revision 1.72 2009/04/30 21:20:15 christos -- * 5.01 we are almost here. -+ * Revision 1.2 2008/11/02 16:09:27 scottmac -+ * Update libmagic to 4.26 and add support for v6 of the magic file format. - * -- * Revision 1.71 2009/01/21 19:09:42 christos -- * file 5.0 -+ * Revision 1.1 2008/07/11 14:13:50 derick -+ * - Move lib to libmagic - * -- * Revision 1.70 2008/08/30 10:01:01 christos -- * file 4.26 -+ * Revision 1.1 2008/07/11 14:10:50 derick -+ * - Step one for bundling the libmagic library. Some config.m4 issues left. - * - * Revision 1.69 2008/07/02 15:27:05 christos - * welcome to 4.25 +Only in libmagic.orig: Makefile.am +Only in libmagic.orig: Makefile.in +Only in libmagic.orig: mygetopt.h +Only in libmagic: names.h +Only in libmagic: patchlevel.h diff -u libmagic.orig/print.c libmagic/print.c ---- libmagic.orig/print.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/print.c 2011-09-15 23:29:29.000000000 +0800 -@@ -28,6 +28,8 @@ - /* +--- libmagic.orig/print.c 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/print.c 2012-03-28 01:53:04.304322598 +0200 +@@ -29,6 +29,9 @@ * print.c - debugging printout routines */ -+#define _GNU_SOURCE -+#include "php.h" ++#include "php.h" ++#include "main/snprintf.h" ++ #include "file.h" -@@ -35,6 +37,7 @@ - FILE_RCSID("@(#)$File: print.c,v 1.66 2009/02/03 20:27:51 christos Exp $") - #endif /* lint */ + #ifndef lint +@@ -45,174 +48,21 @@ -+#include - #include - #include - #include -@@ -45,157 +48,21 @@ - #define SZOF(a) (sizeof(a) / sizeof(a[0])) -#ifndef COMPILE_ONLY @@ -2240,9 +2247,8 @@ diff -u libmagic.orig/print.c libmagic/print.c -{ - private const char optyp[] = { FILE_OPS }; - -- (void) fprintf(stderr, "[%u", m->lineno); -- (void) fprintf(stderr, ">>>>>>>> %u" + 8 - (m->cont_level & 7), -- m->offset); +- (void) fprintf(stderr, "%u: %.*s %u", m->lineno, +- (m->cont_level & 7) + 1, ">>>>>>>>", m->offset); - - if (m->flag & INDIR) { - (void) fprintf(stderr, "(%s,", @@ -2266,10 +2272,10 @@ diff -u libmagic.orig/print.c libmagic/print.c - if (IS_STRING(m->type)) { - if (m->str_flags) { - (void) fputc('/', stderr); -- if (m->str_flags & STRING_COMPACT_BLANK) -- (void) fputc(CHAR_COMPACT_BLANK, stderr); -- if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK) -- (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK, +- 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); @@ -2277,6 +2283,24 @@ diff -u libmagic.orig/print.c libmagic/print.c - (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); @@ -2309,7 +2333,7 @@ diff -u libmagic.orig/print.c libmagic/print.c - case FILE_BEQUAD: - case FILE_LEQUAD: - case FILE_QUAD: -- (void) fprintf(stderr, "%lld", +- (void) fprintf(stderr, "%" INT64_T_FORMAT "d", - (unsigned long long)m->value.q); - break; - case FILE_PSTRING: @@ -2396,8 +2420,8 @@ diff -u libmagic.orig/print.c libmagic/print.c protected const char * diff -u libmagic.orig/readcdf.c libmagic/readcdf.c ---- libmagic.orig/readcdf.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/readcdf.c 2011-09-15 23:29:29.000000000 +0800 +--- libmagic.orig/readcdf.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/readcdf.c 2012-03-28 01:53:04.304322598 +0200 @@ -30,7 +30,11 @@ #endif @@ -2412,50 +2436,50 @@ diff -u libmagic.orig/readcdf.c libmagic/readcdf.c #include @@ -46,7 +50,7 @@ { - size_t i; - cdf_timestamp_t tp; -- struct timespec ts; -+ struct timeval ts; - char buf[64]; - const char *str = "vnd.ms-office"; - const char *s; -@@ -106,7 +110,11 @@ - case CDF_FILETIME: - tp = info[i].pi_tp; - if (tp != 0) { + size_t i; + cdf_timestamp_t tp; +- struct timespec ts; ++ struct timeval ts; + char buf[64]; + const char *str = NULL; + const char *s; +@@ -125,7 +129,11 @@ + case CDF_FILETIME: + tp = info[i].pi_tp; + if (tp != 0) { +- if (tp < 1000000000000000LL) { +#if defined(PHP_WIN32) && _MSC_VER <= 1500 -+ if (tp < 1000000000000000i64) { ++ if (tp < 1000000000000000i64) { +#else - if (tp < 1000000000000000LL) { ++ if (tp < 1000000000000000LL) { +#endif - char tbuf[64]; - cdf_print_elapsed_time(tbuf, - sizeof(tbuf), tp); -@@ -115,7 +123,10 @@ - return -1; - } else { - char *c, *ec; -- cdf_timestamp_to_timespec(&ts, tp); -+ -+ if (cdf_timestamp_to_timespec(&ts, tp) == -1) { -+ return -1; -+ } - c = ctime(&ts.tv_sec); - if ((ec = strchr(c, '\n')) != NULL) - *ec = '\0'; + char tbuf[64]; + cdf_print_elapsed_time(tbuf, + sizeof(tbuf), tp); +@@ -134,7 +142,9 @@ + return -1; + } else { + char *c, *ec; +- cdf_timestamp_to_timespec(&ts, tp); ++ if (cdf_timestamp_to_timespec(&ts, tp) == -1) { ++ return -1; ++ } + c = cdf_ctime(&ts.tv_sec); + if ((ec = strchr(c, '\n')) != NULL) + *ec = '\0'; diff -u libmagic.orig/readelf.c libmagic/readelf.c ---- libmagic.orig/readelf.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/readelf.c 2011-09-15 23:28:26.000000000 +0800 +--- libmagic.orig/readelf.c 2012-03-28 12:00:34.017709605 +0200 ++++ libmagic/readelf.c 2012-03-28 01:53:04.308322618 +0200 @@ -49,7 +49,7 @@ off_t, int *, int); - private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, int *, - int); + private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, + off_t, int *, int); -private size_t donote(struct magic_set *, void *, size_t, size_t, int, +private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int, int, size_t, int *); #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align) -@@ -364,7 +364,7 @@ +@@ -357,7 +357,7 @@ #endif private size_t @@ -2464,7 +2488,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c int clazz, int swap, size_t align, int *flags) { Elf32_Nhdr nh32; -@@ -374,7 +374,6 @@ +@@ -367,7 +367,6 @@ int os_style = -1; #endif uint32_t namesz, descsz; @@ -2472,10 +2496,10 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof); offset += xnh_sizeof; -@@ -855,20 +854,16 @@ - file_badread(ms); - return -1; - } +@@ -888,20 +887,16 @@ + /* Things we can determine when we seek */ + switch (xsh_type) { + case SHT_NOTE: - if ((nbuf = malloc((size_t)xsh_size)) == NULL) { - file_error(ms, errno, "Cannot allocate memory" - " for note"); @@ -2496,23 +2520,18 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c file_badread(ms); return -1; } -@@ -884,11 +879,11 @@ +@@ -916,7 +911,7 @@ + if (noff == 0) break; } - if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) { -- free(nbuf); -+ efree(nbuf); - file_badread(ms); - return -1; - } - free(nbuf); + efree(nbuf); break; case SHT_SUNW_cap: - { + if (lseek(fd, (off_t)xsh_offset, SEEK_SET) == diff -u libmagic.orig/softmagic.c libmagic/softmagic.c ---- libmagic.orig/softmagic.c 2011-09-15 23:28:13.000000000 +0800 -+++ libmagic/softmagic.c 2011-09-15 23:29:29.000000000 +0800 +--- libmagic.orig/softmagic.c 2012-03-28 12:00:34.012709598 +0200 ++++ libmagic/softmagic.c 2012-03-28 01:53:04.308322618 +0200 @@ -41,6 +41,11 @@ #include #include @@ -2524,10 +2543,16 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c + private int match(struct magic_set *, struct magic *, uint32_t, - const unsigned char *, size_t, int); -@@ -125,9 +130,9 @@ + const unsigned char *, size_t, int, int); +@@ -125,14 +130,14 @@ + int flush = 0; + struct magic *m = &magic[magindex]; - if ((m->flag & BINTEST) != mode) { +- if ((IS_STRING(m->type) && ++ if ((IS_LIBMAGIC_STRING(m->type) && + ((text && (m->str_flags & (STRING_BINTEST | STRING_TEXTTEST)) == STRING_BINTEST) || + (!text && (m->str_flags & (STRING_TEXTTEST | STRING_BINTEST)) == STRING_TEXTTEST))) || + (m->flag & mode) != mode) { /* Skip sub-tests */ - while (magic[magindex + 1].cont_level != 0 && - ++magindex < nmagic) @@ -2538,7 +2563,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c continue; /* Skip to next top-level test*/ } -@@ -162,9 +167,9 @@ +@@ -167,9 +172,9 @@ * main entry didn't match, * flush its continuations */ @@ -2550,7 +2575,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c continue; } -@@ -191,8 +196,8 @@ +@@ -196,8 +201,8 @@ if (file_check_mem(ms, ++cont_level) == -1) return -1; @@ -2561,7 +2586,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c m = &magic[magindex]; ms->line = m->lineno; /* for messages */ -@@ -207,8 +212,7 @@ +@@ -212,8 +217,7 @@ } ms->offset = m->offset; if (m->flag & OFFADD) { @@ -2571,7 +2596,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c } #ifdef ENABLE_CONDITIONALS -@@ -313,44 +317,22 @@ +@@ -318,44 +322,22 @@ private int check_fmt(struct magic_set *ms, struct magic *m) { @@ -2626,7 +2651,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c private int32_t mprint(struct magic_set *ms, struct magic *m) { -@@ -533,13 +515,10 @@ +@@ -538,13 +520,10 @@ char *cp; int rval; @@ -2643,7 +2668,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c if (rval == -1) return -1; -@@ -733,16 +712,16 @@ +@@ -738,16 +717,16 @@ if (m->num_mask) \ switch (m->mask_op & FILE_OPS_MASK) { \ case FILE_OPADD: \ @@ -2664,7 +2689,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c break; \ } \ -@@ -1033,16 +1012,13 @@ +@@ -1029,16 +1008,13 @@ if ((ms->flags & MAGIC_DEBUG) != 0) { mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE)); @@ -2683,7 +2708,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c switch (m->in_type) { case FILE_BYTE: off = q->b; -@@ -1522,9 +1498,6 @@ +@@ -1518,9 +1494,6 @@ if ((ms->flags & MAGIC_DEBUG) != 0) { mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE)); @@ -2693,11 +2718,11 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c } } -@@ -1672,6 +1645,65 @@ +@@ -1669,6 +1642,65 @@ return file_strncmp(a, b, len, flags); } -+private void ++public void +convert_libmagic_pattern(zval *pattern, int options) +{ + int i, j=0; @@ -2744,12 +2769,12 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c + t[j++] = '~'; + + if (options & PCRE_CASELESS) -+ t[j++] = 'm'; ++ t[j++] = 'i'; + + if (options & PCRE_MULTILINE) -+ t[j++] = 'i'; ++ t[j++] = 'm'; + -+ t[j]=0; ++ t[j]='\0'; + + Z_STRVAL_P(pattern) = t; + Z_STRLEN_P(pattern) = j; @@ -2759,7 +2784,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c private int magiccheck(struct magic_set *ms, struct magic *m) { -@@ -1828,67 +1860,162 @@ +@@ -1825,67 +1857,163 @@ break; } case FILE_REGEX: { @@ -2770,23 +2795,6 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c - 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'; + zval *pattern; + int options = 0; + pcre_cache_entry *pce; @@ -2803,6 +2811,23 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c + + convert_libmagic_pattern(pattern, options); + + 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'; +#if (PHP_MAJOR_VERSION < 6) + if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) { #else @@ -2974,87 +2999,6 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c default: file_magerror(ms, "invalid type %d in magiccheck()", m->type); return -1; -@@ -1900,7 +2027,7 @@ - case 'x': - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%llu == *any* = 1\n", -- (unsigned long long)v); -+ (uint64_t)v); - matched = 1; - break; - -@@ -1908,7 +2035,7 @@ - matched = v != l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%llu != %llu = %d\n", -- (unsigned long long)v, (unsigned long long)l, -+ (uint64_t)v, (uint64_t)l, - matched); - break; - -@@ -1916,7 +2043,7 @@ - matched = v == l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%llu == %llu = %d\n", -- (unsigned long long)v, (unsigned long long)l, -+ (uint64_t)v, (uint64_t)l, - matched); - break; - -@@ -1925,14 +2052,14 @@ - matched = v > l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%llu > %llu = %d\n", -- (unsigned long long)v, -- (unsigned long long)l, matched); -+ (uint64_t)v, -+ (uint64_t)l, matched); - } - else { - matched = (int64_t) v > (int64_t) l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%lld > %lld = %d\n", -- (long long)v, (long long)l, matched); -+ (uint64_t)v, (uint64_t)l, matched); - } - break; - -@@ -1941,14 +2068,14 @@ - matched = v < l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%llu < %llu = %d\n", -- (unsigned long long)v, -- (unsigned long long)l, matched); -+ (uint64_t)v, -+ (uint64_t)l, matched); - } - else { - matched = (int64_t) v < (int64_t) l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "%lld < %lld = %d\n", -- (long long)v, (long long)l, matched); -+ (int64_t)v, (int64_t)l, matched); - } - break; - -@@ -1956,16 +2083,16 @@ - matched = (v & l) == l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n", -- (unsigned long long)v, (unsigned long long)l, -- (unsigned long long)l, matched); -+ (uint64_t)v, (uint64_t)l, -+ (uint64_t)l, matched); - break; - - case '^': - matched = (v & l) != l; - if ((ms->flags & MAGIC_DEBUG) != 0) - (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n", -- (unsigned long long)v, (unsigned long long)l, -- (unsigned long long)l, matched); -+ (uint64_t)v, (uint64_t)l, -+ (uint64_t)l, matched); - break; - - default: +Only in libmagic.orig: strlcat.c +Only in libmagic.orig: strlcpy.c +Only in libmagic.orig: vasprintf.c