]> granicus.if.org Git - php/commitdiff
updated libmagic.patch
authorAnatol Belski <ab@php.net>
Wed, 19 Feb 2014 10:20:24 +0000 (11:20 +0100)
committerAnatol Belski <ab@php.net>
Wed, 19 Feb 2014 10:20:24 +0000 (11:20 +0100)
ext/fileinfo/libmagic.patch

index d7fc532904042d175e4ea8a02d78f97f7e3b56d6..4130b079fdf0ae4d5e5fd81ab74788358f4836e5 100644 (file)
@@ -1,6 +1,6 @@
 diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
---- libmagic.orig/apprentice.c Thu Mar 21 18:45:14 2013
-+++ libmagic/apprentice.c      Fri May  3 15:19:35 2013
+--- libmagic.orig/apprentice.c Tue Nov 19 22:01:12 2013
++++ libmagic/apprentice.c      Wed Feb 19 10:56:29 2014
 @@ -29,6 +29,8 @@
   * apprentice - make one pass through /etc/magic, learning its secrets.
   */
@@ -46,7 +46,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  
  #define       EATAB {while (isascii((unsigned char) *l) && \
                      isspace((unsigned char) *l))  ++l;}
-@@ -143,38 +157,7 @@
+@@ -148,38 +162,7 @@
        { NULL, 0, NULL }
  };
  
@@ -86,7 +86,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  
  struct type_tbl_s {
        const char name[16];
-@@ -255,6 +238,10 @@
+@@ -261,6 +244,10 @@
  # undef XX
  # undef XX_NULL
  
@@ -97,7 +97,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  private int
  get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
  {
-@@ -378,7 +365,7 @@
+@@ -384,7 +371,7 @@
  {
        struct mlist *ml;
  
@@ -106,7 +106,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                return -1;
  
        ml->map = idx == 0 ? map : NULL;
-@@ -416,12 +403,13 @@
+@@ -422,12 +409,13 @@
                return apprentice_compile(ms, map, fn);
        }
  
@@ -124,7 +124,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                if (map == NULL)
                        return -1;
        }
-@@ -444,7 +432,6 @@
+@@ -450,7 +438,6 @@
        }
        
        return 0;
@@ -132,7 +132,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  }
  
  protected void
-@@ -455,10 +442,16 @@
+@@ -461,10 +448,16 @@
                return;
        for (i = 0; i < MAGIC_SETS; i++)
                mlist_free(ms->mlist[i]);
@@ -153,7 +153,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  }
  
  protected struct magic_set *
-@@ -467,7 +460,7 @@
+@@ -473,7 +466,7 @@
        struct magic_set *ms;
        size_t i, len;
  
@@ -162,7 +162,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
            sizeof(struct magic_set)))) == NULL)
                return NULL;
  
-@@ -479,7 +472,7 @@
+@@ -485,7 +478,7 @@
        ms->o.buf = ms->o.pbuf = NULL;
        len = (ms->c.len = 10) * sizeof(*ms->c.li);
  
@@ -171,7 +171,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                goto free;
  
        ms->event_flags = 0;
-@@ -490,7 +483,7 @@
+@@ -496,7 +489,7 @@
        ms->line = 0;
        return ms;
  free:
@@ -180,7 +180,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
        return NULL;
  }
  
-@@ -499,22 +492,24 @@
+@@ -505,22 +498,24 @@
  {
        if (map == NULL)
                return;
@@ -215,7 +215,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                return NULL;
        }
        mlist->next = mlist->prev = mlist;
-@@ -533,10 +528,10 @@
+@@ -539,10 +534,10 @@
                struct mlist *next = ml->next;
                if (ml->map)
                        apprentice_unmap(ml->map);
@@ -228,11 +228,10 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  }
  
  /* const char *fn: list of magic files and directories */
-@@ -546,13 +541,28 @@
-       char *p, *mfn;
-       int file_err, errs = -1;
-       size_t i;
--
+@@ -555,12 +550,28 @@
+       file_reset(ms);
 +/* XXX disabling default magic loading so the compiled in data is used */
 +#if 0
        if ((fn = magic_getpath(fn, action)) == NULL)
@@ -259,7 +258,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                file_oomem(ms, strlen(fn));
                return -1;
        }
-@@ -567,7 +577,7 @@
+@@ -575,7 +586,7 @@
                                        mlist_free(ms->mlist[i]);
                                while (i != 0);
                        }
@@ -268,7 +267,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                        return -1;
                }
        }
-@@ -584,7 +594,7 @@
+@@ -592,7 +603,7 @@
                fn = p;
        }
  
@@ -277,18 +276,18 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  
        if (errs == -1) {
                for (i = 0; i < MAGIC_SETS; i++) {
-@@ -904,7 +914,7 @@
+@@ -918,7 +929,7 @@
  
-               maxmagic[i] += ALLOC_INCR;
+               mset[i].max += ALLOC_INCR;
                if ((mp = CAST(struct magic_entry *,
--                  realloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
-+                  erealloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
+-                  realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
++                  erealloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
                    NULL) {
-                       file_oomem(ms, sizeof(*mp) * maxmagic[i]);
+                       file_oomem(ms, sizeof(*mp) * mset[i].max);
                        return -1;
-@@ -925,13 +935,24 @@
+@@ -939,13 +950,20 @@
  load_1(struct magic_set *ms, int action, const char *fn, int *errs,
-    struct magic_entry **mentry, uint32_t *mentrycount)
+    struct magic_entry_set *mset)
  {
 -      size_t lineno = 0, llen = 0;
 +      char buffer[BUFSIZ + 1];
@@ -305,17 +304,13 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
 +      TSRMLS_FETCH();
 +
 +      ms->file = fn;
-+#if PHP_API_VERSION < 20100412
-+      stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
-+#else
 +      stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
-+#endif
 +
 +      if (stream == NULL) {
                if (errno != ENOENT)
                        file_error(ms, errno, "cannot read magic file `%s'",
                                   fn);
-@@ -941,8 +962,7 @@
+@@ -955,8 +973,7 @@
  
        memset(&me, 0, sizeof(me));
        /* read and parse this file */
@@ -325,25 +320,17 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                if (len == 0) /* null line, garbage, etc */
                        continue;
                if (line[len - 1] == '\n') {
-@@ -994,14 +1014,13 @@
-                               goto again;
-                       default:
-                               (*errs)++;
--                              break;
--                      }
-+                      break;
-               }
+@@ -1014,8 +1031,7 @@
        }
-+      }
        if (me.mp)
-               (void)addentry(ms, &me, mentry, mentrycount);
+               (void)addentry(ms, &me, mset);
 -      free(line);
 -      (void)fclose(f);
 +      php_stream_close(stream);
  }
  
  /*
-@@ -1080,7 +1099,7 @@
+@@ -1094,7 +1110,7 @@
                mentrycount += me[i].cont_count;
  
        slen = sizeof(**ma) * mentrycount;
@@ -352,7 +339,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                file_oomem(ms, slen);
                return -1;
        }
-@@ -1102,27 +1121,29 @@
+@@ -1116,8 +1132,8 @@
        if (me == NULL)
                return;
        for (i = 0; i < nme; i++)
@@ -363,46 +350,34 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  }
  
  private struct magic_map *
- apprentice_load(struct magic_set *ms, const char *fn, int action)
- {
--      int errs = 0;
-+      int errs = 0; 
-       struct magic_entry *mentry[MAGIC_SETS] = { NULL };
-       uint32_t mentrycount[MAGIC_SETS] = { 0 };
+@@ -1126,18 +1142,20 @@
+       int errs = 0;
        uint32_t i, j;
        size_t files = 0, maxfiles = 0;
 -      char **filearr = NULL, *mfn;
 +      char **filearr = NULL;
        struct stat st;
        struct magic_map *map;
+       struct magic_entry_set mset[MAGIC_SETS];
 -      DIR *dir;
 -      struct dirent *d;
 +      php_stream *dir;
 +      php_stream_dirent d;
-+
++ 
 +      TSRMLS_FETCH();
  
+       memset(mset, 0, sizeof(mset));
        ms->flags |= MAGIC_CHECK;       /* Enable checks for parsed files */
  
--      if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
-+      if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
+-      if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL)
++      if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL)
+       {
                file_oomem(ms, sizeof(*map));
                return NULL;
-       }
-@@ -1131,23 +1152,37 @@
-       if (action == FILE_CHECK)
+@@ -1148,22 +1166,26 @@
                (void)fprintf(stderr, "%s\n", usg_hdr);
  
-+      {
-+              /* XXX the maxmagic has to be reset each time we load some new magic file.
-+              Where file commando is used it's not essential as the CLI process
-+              ends, multiple loading within the same process wouldn't work. */
-+              int k;
-+              for (k = 0; k < MAGIC_SETS; k++) {
-+                      maxmagic[k] = 0;
-+              }
-+      }
-+
        /* load directory or file */
 -      if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
 -              dir = opendir(fn);
@@ -434,7 +409,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                                continue;
                        }
                        if (files >= maxfiles) {
-@@ -1155,24 +1190,23 @@
+@@ -1171,23 +1193,23 @@
                                maxfiles = (maxfiles + 1) * 2;
                                mlen = maxfiles * sizeof(*filearr);
                                if ((filearr = CAST(char **,
@@ -443,6 +418,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                                        file_oomem(ms, mlen);
 -                                      free(mfn);
 -                                      closedir(dir);
++                                      efree(mfn);
 +                                      php_stream_closedir(dir);
                                        errs++;
                                        goto out;
@@ -455,17 +431,16 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
 +              php_stream_closedir(dir);
                qsort(filearr, files, sizeof(*filearr), cmpstrp);
                for (i = 0; i < files; i++) {
-                       load_1(ms, action, filearr[i], &errs, mentry,
-                           mentrycount);
+                       load_1(ms, action, filearr[i], &errs, mset);
 -                      free(filearr[i]);
 +                      efree(filearr[i]);
                }
 -              free(filearr);
 +              efree(filearr);
        } else
-               load_1(ms, action, fn, &errs, mentry, mentrycount);
+               load_1(ms, action, fn, &errs, mset);
        if (errs)
-@@ -1211,9 +1245,9 @@
+@@ -1226,9 +1248,9 @@
        if (errs) {
                for (j = 0; j < MAGIC_SETS; j++) {
                        if (map->magic[j])
@@ -477,7 +452,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                return NULL;
        }
        return map;
-@@ -1500,7 +1534,7 @@
+@@ -1516,7 +1538,7 @@
                if (me->cont_count == me->max_count) {
                        struct magic *nm;
                        size_t cnt = me->max_count + ALLOC_CHUNK;
@@ -486,7 +461,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                            sizeof(*nm) * cnt))) == NULL) {
                                file_oomem(ms, sizeof(*nm) * cnt);
                                return -1;
-@@ -1515,7 +1549,7 @@
+@@ -1531,7 +1553,7 @@
                static const size_t len = sizeof(*m) * ALLOC_CHUNK;
                if (me->mp != NULL)
                        return 1;
@@ -495,7 +470,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                        file_oomem(ms, len);
                        return -1;
                }
-@@ -1688,7 +1722,7 @@
+@@ -1704,7 +1726,7 @@
                                m->type = get_standard_integer_type(l, &l);
                        else if (*l == 's' && !isalpha((unsigned char)l[1])) {
                                m->type = FILE_STRING;
@@ -504,18 +479,18 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                        }
                }
        }
-@@ -1701,6 +1735,10 @@
+@@ -1717,6 +1739,10 @@
        if (m->type == FILE_INVALID) {
                if (ms->flags & MAGIC_CHECK)
                        file_magwarn(ms, "type `%s' invalid", l);
-+              if (me->mp) {
++              /*if (me->mp) {
 +                      efree(me->mp);
 +                      me->mp = NULL;
-+              }
++              }*/
                return -1;
        }
  
-@@ -1709,7 +1747,7 @@
+@@ -1725,7 +1751,7 @@
  
        m->mask_op = 0;
        if (*l == '~') {
@@ -524,7 +499,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                        m->mask_op |= FILE_OPINVERSE;
                else if (ms->flags & MAGIC_CHECK)
                        file_magwarn(ms, "'~' invalid for string types");
-@@ -1718,7 +1756,7 @@
+@@ -1734,7 +1760,7 @@
        m->str_range = 0;
        m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
        if ((op = get_op(*l)) != -1) {
@@ -533,7 +508,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                        uint64_t val;
                        ++l;
                        m->mask_op |= op;
-@@ -1909,11 +1947,6 @@
+@@ -1925,11 +1951,6 @@
                if (check_format(ms, m) == -1)
                        return -1;
        }
@@ -545,7 +520,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
        m->mimetype[0] = '\0';          /* initialise MIME type to none */
        return 0;
  }
-@@ -2554,59 +2587,80 @@
+@@ -2575,59 +2596,76 @@
  private struct magic_map *
  apprentice_map(struct magic_set *ms, const char *fn)
  {
@@ -560,11 +535,11 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
 +      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);
@@ -591,11 +566,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                goto error;
  
 -      if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
-+#if PHP_API_VERSION < 20100412
-+              stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
-+#else
 +              stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
-+#endif
 +
 +      if (!stream) {
                goto error;
@@ -647,7 +618,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
        if (*ptr != MAGICNO) {
                if (swap4(*ptr) != MAGICNO) {
                        file_error(ms, 0, "bad magic in `%s'", dbname);
-@@ -2620,17 +2674,29 @@
+@@ -2641,17 +2679,29 @@
        else
                version = ptr[1];
        if (version != VERSIONNO) {
@@ -685,7 +656,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
        }
        map->magic[0] = CAST(struct magic *, map->p) + 1;
        nentries = 0;
-@@ -2643,22 +2709,29 @@
+@@ -2664,22 +2714,29 @@
                        map->magic[i + 1] = map->magic[i] + map->nmagic[i];
                nentries += map->nmagic[i];
        }
@@ -720,7 +691,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
        return NULL;
  }
  
-@@ -2679,14 +2752,23 @@
+@@ -2700,14 +2757,19 @@
        char *dbname;
        int rv = -1;
        uint32_t i;
@@ -736,18 +707,14 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  
 -      if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) 
 -      {
-+/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */
-+#if PHP_API_VERSION < 20100412
-+      stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
-+#else
++      /* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */
 +      stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS, NULL);
-+#endif
 +
 +      if (!stream) {
                file_error(ms, errno, "cannot open `%s'", dbname);
                goto out;
        }
-@@ -2696,31 +2778,33 @@
+@@ -2717,31 +2779,33 @@
                goto out;
        }
  
@@ -787,7 +754,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
        return rv;
  }
  
-@@ -2733,6 +2817,7 @@
+@@ -2754,6 +2818,7 @@
  {
        const char *p, *q;
        char *buf;
@@ -795,7 +762,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  
        if (strip) {
                if ((p = strrchr(fn, '/')) != NULL)
-@@ -2754,16 +2839,18 @@
+@@ -2775,16 +2840,18 @@
        q++;
        /* Compatibility with old code that looked in .mime */
        if (ms->flags & MAGIC_MIME) {
@@ -820,7 +787,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
  
        /* Compatibility with old code that looked in .mime */
        if (strstr(p, ".mime") != NULL)
-@@ -2853,7 +2940,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);
@@ -830,8 +797,8 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
                m->str_flags = swap4(m->str_flags);
        }
 diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
---- libmagic.orig/ascmagic.c   Wed Oct 31 18:03:01 2012
-+++ libmagic/ascmagic.c        Tue Feb 18 18:44:17 2014
+--- libmagic.orig/ascmagic.c   Thu Feb 13 00:20:53 2014
++++ libmagic/ascmagic.c        Wed Feb 19 10:10:11 2014
 @@ -139,7 +139,7 @@
                /* malloc size is a conservative overestimate; could be
                   improved, or at least realloced after conversion. */
@@ -841,24 +808,7 @@ diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
                        file_oomem(ms, mlen);
                        goto done;
                }
-@@ -147,7 +147,7 @@
-                   == NULL)
-                       goto done;
-               if ((rv = file_softmagic(ms, utf8_buf,
--                  (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0)
-+                  (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
-                       rv = -1;
-       }
-@@ -211,6 +211,7 @@
-                               case 0:
-                                       if (file_printf(ms, ", ") == -1)
-                                               goto done;
-+                                      break;
-                               case -1:
-                                       goto done;
-                               default:
-@@ -296,7 +297,8 @@
+@@ -297,7 +297,8 @@
        }
        rv = 1;
  done:
@@ -869,8 +819,8 @@ diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
        return rv;
  }
 diff -u libmagic.orig/cdf.c libmagic/cdf.c
---- libmagic.orig/cdf.c        Thu Mar 21 18:45:14 2013
-+++ libmagic/cdf.c     Sun Apr  7 22:30:22 2013
+--- libmagic.orig/cdf.c        Tue Feb 26 17:20:42 2013
++++ libmagic/cdf.c     Tue Feb 18 10:59:23 2014
 @@ -43,7 +43,17 @@
  #include <err.h>
  #endif
@@ -932,8 +882,8 @@ diff -u libmagic.orig/cdf.c libmagic/cdf.c
                                (void)fprintf(stderr, "timestamp %s\n", buf);
                        } else {
 diff -u libmagic.orig/cdf.h libmagic/cdf.h
---- libmagic.orig/cdf.h        Wed Oct 31 18:03:01 2012
-+++ libmagic/cdf.h     Sun Apr  7 22:30:22 2013
+--- libmagic.orig/cdf.h        Thu Jun 21 00:19:55 2012
++++ libmagic/cdf.h     Tue Feb 18 10:59:23 2014
 @@ -35,10 +35,12 @@
  #ifndef _H_CDF_
  #define _H_CDF_
@@ -975,8 +925,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   Wed Oct 31 18:03:01 2012
-+++ libmagic/cdf_time.c        Sun Apr  7 22:30:22 2013
+--- libmagic.orig/cdf_time.c   Thu Jun 21 00:18:33 2012
++++ libmagic/cdf_time.c        Tue Feb 18 10:59:23 2014
 @@ -96,7 +96,7 @@
  }
  
@@ -1035,8 +985,8 @@ diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
        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  6 21:35:43 2013
-+++ libmagic/compress.c        Sun Apr  7 22:30:22 2013
+--- libmagic.orig/compress.c   Sun Jan  5 16:55:21 2014
++++ libmagic/compress.c        Wed Feb 19 10:10:11 2014
 @@ -32,6 +32,7 @@
   *    uncompress(method, old, n, newch) - uncompress old into new, 
   *                                        using method, return sizeof new
@@ -1065,7 +1015,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
  private const struct {
        const char magic[8];
        size_t maglen;
-@@ -85,8 +90,7 @@
+@@ -86,8 +91,7 @@
  #define NODATA ((size_t)~0)
  
  private ssize_t swrite(int, const void *, size_t);
@@ -1075,7 +1025,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
  private size_t uncompressbuf(struct magic_set *, int, size_t,
      const unsigned char *, unsigned char **, size_t);
  #ifdef BUILTIN_DECOMPRESS
-@@ -102,10 +106,13 @@
+@@ -103,10 +107,13 @@
        size_t i, nsz;
        int rv = 0;
        int mime = ms->flags & MAGIC_MIME;
@@ -1089,7 +1039,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
        for (i = 0; i < ncompr; i++) {
                if (nbytes < compr[i].maglen)
                        continue;
-@@ -134,7 +141,8 @@
+@@ -133,7 +140,8 @@
                }
        }
  error:
@@ -1099,7 +1049,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
        ms->flags |= MAGIC_COMPRESS;
        return rv;
  }
-@@ -168,7 +176,7 @@
+@@ -167,7 +175,7 @@
   * `safe' read for sockets and pipes.
   */
  protected ssize_t
@@ -1108,7 +1058,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
  {
        ssize_t rv;
  #ifdef FIONREAD
-@@ -216,7 +224,7 @@
+@@ -215,7 +223,7 @@
  
  nocheck:
        do
@@ -1117,7 +1067,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
                case -1:
                        if (errno == EINTR)
                                continue;
-@@ -293,13 +301,14 @@
+@@ -292,13 +300,14 @@
                return -1;
        }
        (void)close(tfd);
@@ -1134,7 +1084,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
  #ifdef BUILTIN_DECOMPRESS
  
  #define FHCRC         (1 << 1)
-@@ -336,7 +345,7 @@
+@@ -335,7 +344,7 @@
  
        if (data_start >= n)
                return 0;
@@ -1143,7 +1093,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
                return 0;
        }
        
-@@ -397,19 +406,16 @@
+@@ -396,19 +405,16 @@
        case 0: /* child */
                (void) close(0);
                if (fd != -1) {
@@ -1167,7 +1117,7 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
                (void) close(fdout[0]);
                (void) close(fdout[1]);
  #ifndef DEBUG
-@@ -466,20 +472,14 @@
+@@ -465,20 +471,14 @@
                        fdin[1] = -1;
                }
  
@@ -1189,17 +1139,17 @@ diff -u libmagic.orig/compress.c libmagic/compress.c
 -                      free(*newch);
 +                      efree(*newch);
                        n = 0;
-                       newch[0] = '\0';
+                       *newch = NULL;
                        goto err;
-@@ -503,4 +503,4 @@
+@@ -502,4 +502,4 @@
                return n;
        }
  }
 -#endif
 +#endif /* if PHP_FILEINFO_UNCOMPRESS */
 diff -u libmagic.orig/file.h libmagic/file.h
---- libmagic.orig/file.h       Mon Feb 18 16:40:59 2013
-+++ libmagic/file.h    Tue Feb 18 18:44:17 2014
+--- libmagic.orig/file.h       Thu Feb 13 00:20:53 2014
++++ libmagic/file.h    Wed Feb 19 10:10:11 2014
 @@ -33,11 +33,9 @@
  #ifndef __file_h__
  #define __file_h__
@@ -1259,34 +1209,16 @@ diff -u libmagic.orig/file.h libmagic/file.h
  #ifndef MIN
  #define       MIN(a,b)        (((a) < (b)) ? (a) : (b))
  #endif
-@@ -224,7 +226,7 @@
- #define                               FILE_USE        46
- #define                               FILE_NAMES_SIZE 47 /* size of array to contain all names */
+@@ -225,7 +227,7 @@
+ #define                               FILE_CLEAR      47
+ #define                               FILE_NAMES_SIZE 48 /* size of array to contain all names */
  
 -#define IS_STRING(t) \
 +#define IS_LIBMAGIC_STRING(t) \
        ((t) == FILE_STRING || \
         (t) == FILE_PSTRING || \
         (t) == FILE_BESTRING16 || \
-@@ -248,7 +250,7 @@
- #ifdef ENABLE_CONDITIONALS
-       uint8_t cond;           /* conditional type */
- #else
--      uint8_t dummy;
-+      uint8_t dummy;  
- #endif
-       uint8_t factor_op;
- #define               FILE_FACTOR_OP_PLUS     '+'
-@@ -347,7 +349,7 @@
- /* list of magic entries */
- struct mlist {
-       struct magic *magic;            /* array of magic entries */
--      uint32_t nmagic;                /* number of entries in array */
-+      uint32_t nmagic;                        /* number of entries in array */
-       void *map;                      /* internal resources used by entry */
-       struct mlist *next, *prev;
- };
-@@ -411,21 +413,18 @@
+@@ -411,22 +413,18 @@
  protected const char *file_fmttime(uint64_t, int, char *);
  protected struct magic_set *file_ms_alloc(int);
  protected void file_ms_free(struct magic_set *);
@@ -1296,7 +1228,8 @@ diff -u libmagic.orig/file.h libmagic/file.h
 -protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
 +protected int file_fsmagic(struct magic_set *, const char *, struct stat *, php_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 int file_vprintf(struct magic_set *, const char *, va_list)
+-    __attribute__((__format__(__printf__, 2, 0)));
 -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 *, ...)
@@ -1308,17 +1241,11 @@ diff -u libmagic.orig/file.h libmagic/file.h
  protected int file_trycdf(struct magic_set *, int, const unsigned char *,
      size_t);
 -#if HAVE_FORK
-+#ifdef PHP_FILEINFO_UNCOMPRESS 
++#ifdef PHP_FILEINFO_UNCOMPRESS
  protected int file_zmagic(struct magic_set *, int, const char *,
      const unsigned char *, size_t);
  #endif
-@@ -438,21 +437,18 @@
-     unichar **, size_t *, const char **, const char **, const char **);
- protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
- protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
--    int, int);
-+    size_t, int, int);
- protected int file_apprentice(struct magic_set *, const char *, int);
+@@ -444,16 +442,13 @@
  protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
  protected uint64_t file_signextend(struct magic_set *, struct magic *,
      uint64_t);
@@ -1339,7 +1266,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 *);
-@@ -462,16 +458,14 @@
+@@ -463,16 +458,14 @@
      size_t *);
  protected size_t file_pstring_length_size(const struct magic *);
  protected size_t file_pstring_get_length(const struct magic *, const char *);
@@ -1357,7 +1284,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
  
  #ifndef HAVE_STRERROR
  extern int sys_nerr;
-@@ -484,32 +478,16 @@
+@@ -485,20 +478,10 @@
  #define strtoul(a, b, c)      strtol(a, b, c)
  #endif
  
@@ -1368,31 +1295,19 @@ diff -u libmagic.orig/file.h libmagic/file.h
 -int vasprintf(char **, const char *, va_list);
 -#endif
 -#ifndef HAVE_ASPRINTF
--int asprintf(char **ptr, const char *format_string, ...);
+-int asprintf(char **, const char *, ...);
 -#endif
 -
 -#ifndef HAVE_STRLCPY
 +#ifndef strlcpy
- size_t strlcpy(char *dst, const char *src, size_t siz);
+ size_t strlcpy(char *, const char *, size_t);
  #endif
 -#ifndef HAVE_STRLCAT
 +#ifndef strlcat
- size_t strlcat(char *dst, const char *src, size_t siz);
+ size_t strlcat(char *, const char *, size_t);
  #endif
- #ifndef HAVE_GETLINE
- ssize_t getline(char **dst, size_t *len, FILE *fp);
- ssize_t getdelim(char **dst, size_t *len, int delimiter, FILE *fp);
- #endif
--#ifndef HAVE_CTIME_R
--char   *ctime_r(const time_t *, char *);
--#endif
--#ifndef HAVE_ASCTIME_R
--char   *asctime_r(const struct tm *, char *);
--#endif
- #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
- #define QUICK
-@@ -531,6 +509,14 @@
+ #ifndef HAVE_STRCASESTR
+@@ -535,6 +518,14 @@
  #endif
  #else
  #define FILE_RCSID(id)
@@ -1408,8 +1323,8 @@ diff -u libmagic.orig/file.h libmagic/file.h
  
  #endif /* __file_h__ */
 diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
---- libmagic.orig/fsmagic.c    Thu Mar 21 18:45:14 2013
-+++ libmagic/fsmagic.c Sun Apr  7 22:30:22 2013
+--- libmagic.orig/fsmagic.c    Sun Dec  1 20:22:13 2013
++++ libmagic/fsmagic.c Wed Feb 19 10:10:11 2014
 @@ -59,27 +59,21 @@
  # define minor(dev)  ((dev) & 0xff)
  #endif
@@ -1438,7 +1353,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +#ifdef PHP_WIN32
 +
 +# undef S_IFIFO
-+#endif
+ #endif
 +
 +
 +#ifndef S_ISDIR
@@ -1447,12 +1362,12 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +
 +#ifndef S_ISREG
 +#define S_ISREG(mode) ((mode) & _S_IFREG)
- #endif
++#endif
 +
  private int
  handle_mime(struct magic_set *ms, int mime, const char *str)
  {
-@@ -96,49 +90,45 @@
+@@ -96,42 +90,39 @@
  }
  
  protected int
@@ -1500,7 +1415,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +                              file_error(ms, errno, "cannot stat `%s'", fn);
 +                              return -1;
 +                      }
-+                      return 1;
++                      return 0;
 +              }
 +              memcpy(sb, &ssb.sb, sizeof(struct stat));
 +      } else {
@@ -1509,24 +1424,16 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +                              file_error(ms, errno, "cannot stat `%s'", fn);
 +                              return -1;
 +                      }
-+                      return 1;
++                      return 0;
                }
 -              if (file_printf(ms, "cannot open `%s' (%s)",
 -                  fn, strerror(errno)) == -1)
 -                      return -1;
--              ms->event_flags |= EVENT_HAD_ERR;
--              return -1;
+-              return 0;
        }
  
        ret = 1;
-       if (!mime) {
- #ifdef S_ISUID
--              if (sb->st_mode & S_ISUID)
-+              if (sb->st_mode & S_ISUID) 
-                       if (file_printf(ms, "%ssetuid", COMMA) == -1)
-                               return -1;
- #endif
-@@ -155,82 +145,43 @@
+@@ -154,30 +145,24 @@
        }
        
        switch (sb->st_mode & S_IFMT) {
@@ -1567,31 +1474,18 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +                              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
 +#   ifdef dv_unit
                        if (file_printf(ms, "%scharacter special (%d/%d/%d)",
                            COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
--                                      dv_subunit(sb->st_rdev)) == -1)
--                              return -1;
--# else
-+                                              dv_subunit(sb->st_rdev)) == -1)
-+                                      return -1;
-+#   else
-                       if (file_printf(ms, "%scharacter special (%ld/%ld)",
-                           COMMA, (long)major(sb->st_rdev),
-                           (long)minor(sb->st_rdev)) == -1)
--                              return -1;
--# endif
--#else
-+                                      return -1;
-+#   endif
-+#  else
+                                       dv_subunit(sb->st_rdev)) == -1)
+@@ -192,44 +177,11 @@
                        if (file_printf(ms, "%scharacter special", COMMA) == -1)
--                              return -1;
--#endif
+                               return -1;
+ #endif
 -              }
 -              break;
 -#endif
@@ -1621,24 +1515,22 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -                          COMMA, (long)major(sb->st_rdev),
 -                          (long)minor(sb->st_rdev)) == -1)
 -                              return -1;
-+                                      return -1;
-+#  endif
-+                      }
++      }
 +                      return 1;
  # endif
 -#else
 -                      if (file_printf(ms, "%sblock special", COMMA) == -1)
 -                              return -1;
- #endif
+-#endif
 -              }
 -              break;
--#endif
+ #endif
 -      /* TODO add code to handle V7 MUX and Blit MUX files */
 +
  #ifdef        S_IFIFO
        case S_IFIFO:
                if((ms->flags & MAGIC_DEVICES) != 0)
-@@ -253,79 +204,14 @@
+@@ -252,79 +204,14 @@
  #endif
  #ifdef        S_IFLNK
        case S_IFLNK:
@@ -1722,26 +1614,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
  #ifdef        S_IFSOCK
  #ifndef __COHERENT__
        case S_IFSOCK:
-@@ -337,27 +223,27 @@
-               break;
- #endif
- #endif
--      case S_IFREG:
--              /*
--               * regular file, check next possibility
--               *
--               * If stat() tells us the file has zero length, report here that
--               * the file is empty, so we can skip all the work of opening and
--               * reading the file.
-+              case S_IFREG:
-+      /*
-+       * regular file, check next possibility
-+       *
-+       * If stat() tells us the file has zero length, report here that
-+       * the file is empty, so we can skip all the work of opening and 
-+       * reading the file.
-                * But if the -s option has been given, we skip this
-                * optimization, since on some systems, stat() reports zero
+@@ -348,15 +235,15 @@
                 * size for raw disk partitions. (If the block special device
                 * really has zero length, the fact that it is empty will be
                 * detected and reported correctly when we read the file.)
@@ -1764,94 +1637,102 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
                ret = 0;
                break;
  
-@@ -367,9 +253,5 @@
+@@ -366,9 +253,5 @@
                /*NOTREACHED*/
        }
  
--      if (!mime && did) {
+-      if (!mime && did && ret == 0) {
 -          if (file_printf(ms, " ") == -1)
 -                  return -1;
 -      }
        return ret;
  }
 diff -u libmagic.orig/funcs.c libmagic/funcs.c
---- libmagic.orig/funcs.c      Wed Oct 31 18:03:01 2012
-+++ libmagic/funcs.c   Tue Feb 18 18:44:17 2014
-@@ -41,52 +41,42 @@
+--- libmagic.orig/funcs.c      Thu Feb 13 00:20:53 2014
++++ libmagic/funcs.c   Wed Feb 19 10:10:11 2014
+@@ -41,79 +41,76 @@
  #if defined(HAVE_WCTYPE_H)
  #include <wctype.h>
  #endif
 -#if defined(HAVE_LIMITS_H)
 -#include <limits.h>
-+
-+#ifndef SIZE_MAX 
-+# define SIZE_MAX ((size_t) -1) 
+-#endif
+ #if defined(HAVE_LOCALE_H)
+ #include <locale.h>
  #endif
  
--#ifndef SIZE_MAX
+ #ifndef SIZE_MAX
 -#define SIZE_MAX      ((size_t)~0)
-+#ifndef PREG_OFFSET_CAPTURE
-+# define PREG_OFFSET_CAPTURE                 (1<<8)
++# define SIZE_MAX ((size_t) -1) 
  #endif
  
-+extern public void convert_libmagic_pattern(zval *pattern, int options);
-+
- /*
-  * Like printf, only we append to a buffer.
-  */
- protected int
+-/*
+- * Like printf, only we append to a buffer.
+- */
+-protected int
 -file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
-+file_printf(struct magic_set *ms, const char *fmt, ...)
- {
-+      va_list ap;
-       int len;
+-{
+-      int len;
 -      char *buf, *newstr;
-+      char *buf = NULL, *newstr;
++#ifndef PREG_OFFSET_CAPTURE
++# define PREG_OFFSET_CAPTURE                 (1<<8)
++#endif
  
+-      if (ms->event_flags & EVENT_HAD_ERR)
+-              return 0;
 -      len = vasprintf(&buf, fmt, ap);
 -      if (len < 0)
 -              goto out;
-+      va_start(ap, fmt);
-+      len = vspprintf(&buf, 0, fmt, ap);
-+      va_end(ap);
-       if (ms->o.buf != NULL) {
+-
+-      if (ms->o.buf != NULL) {
 -              len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
 -              free(buf);
 -              if (len < 0)
 -                      goto out;
 -              free(ms->o.buf);
 -              buf = newstr;
-+              len = spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
-+              if (buf) {
-+                      efree(buf);
-+              }
-+              efree(ms->o.buf);
-+              ms->o.buf = newstr;
-+      } else {
-+              ms->o.buf = buf;
-       }
+-      }
 -      ms->o.buf = buf;
-       return 0;
+-      return 0;
 -out:
 -      file_error(ms, errno, "vasprintf failed");
 -      return -1;
 -}
--
--protected int
--file_printf(struct magic_set *ms, const char *fmt, ...)
--{
++extern public void convert_libmagic_pattern(zval *pattern, int options);
+ protected int
+ file_printf(struct magic_set *ms, const char *fmt, ...)
+ {
 -      int rv;
--      va_list ap;
--
--      va_start(ap, fmt);
+       va_list ap;
++      int len;
++      char *buf = NULL, *newstr;
+       va_start(ap, fmt);
 -      rv = file_vprintf(ms, fmt, ap);
--      va_end(ap);
++      len = vspprintf(&buf, 0, fmt, ap);
+       va_end(ap);
 -      return rv;
++
++      if (ms->o.buf != NULL) {
++              len = spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
++              if (buf) {
++                      efree(buf);
++              }
++              efree(ms->o.buf);
++              ms->o.buf = newstr;
++      } else {
++              ms->o.buf = buf;
++      }
++      return 0;
  }
  
  /*
-@@ -97,17 +87,30 @@
+  * error - print best error message possible
+  */
+ /*VARARGS*/
+-__attribute__((__format__(__printf__, 3, 0)))
+ private void
  file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
      size_t lineno)
  {
@@ -1886,7 +1767,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
        ms->event_flags |= EVENT_HAD_ERR;
        ms->error = error;
  }
-@@ -154,10 +157,9 @@
+@@ -160,10 +157,9 @@
        file_error(ms, errno, "error reading");
  }
  
@@ -1899,7 +1780,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
  {
        int m = 0, rv = 0, looks_text = 0;
        int mime = ms->flags & MAGIC_MIME;
-@@ -201,10 +203,10 @@
+@@ -203,10 +199,10 @@
                }
        }
  #endif
@@ -1912,8 +1793,8 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
 +              if ((m = file_zmagic(ms, stream, inname, ubuf, nb)) != 0) {
                        if ((ms->flags & MAGIC_DEBUG) != 0)
                                (void)fprintf(stderr, "zmagic %d\n", m);
-                       goto done;
-@@ -219,16 +221,21 @@
+                       goto done_encoding;
+@@ -221,12 +217,17 @@
                }
  
        /* Check if we have a CDF file */
@@ -1936,12 +1817,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
  
        /* try soft magic tests */
        if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
--              if ((m = file_softmagic(ms, ubuf, nb, BINTEST,
-+              if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
-                   looks_text)) != 0) {
-                       if ((ms->flags & MAGIC_DEBUG) != 0)
-                               (void)fprintf(stderr, "softmagic %d\n", m);
-@@ -296,7 +303,6 @@
+@@ -300,7 +301,6 @@
  
        return m;
  }
@@ -1949,7 +1825,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
  
  protected int
  file_reset(struct magic_set *ms)
-@@ -306,11 +312,11 @@
+@@ -310,11 +310,11 @@
                return -1;
        }
        if (ms->o.buf) {
@@ -1963,7 +1839,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
                ms->o.pbuf = NULL;
        }
        ms->event_flags &= ~EVENT_HAD_ERR;
-@@ -329,7 +335,7 @@
+@@ -333,7 +333,7 @@
  protected const char *
  file_getbuffer(struct magic_set *ms)
  {
@@ -1972,7 +1848,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
        size_t psize, len;
  
        if (ms->event_flags & EVENT_HAD_ERR)
-@@ -344,15 +350,13 @@
+@@ -348,15 +348,13 @@
        /* * 4 is for octal representation, + 1 is for NUL */
        len = strlen(ms->o.buf);
        if (len > (SIZE_MAX - 1) / 4) {
@@ -1989,7 +1865,7 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
  
  #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
        {
-@@ -412,8 +416,8 @@
+@@ -416,8 +414,8 @@
        if (level >= ms->c.len) {
                len = (ms->c.len += 20) * sizeof(*ms->c.li);
                ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
@@ -2000,17 +1876,15 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
                if (ms->c.li == NULL) {
                        file_oomem(ms, len);
                        return -1;
-@@ -433,29 +437,47 @@
+@@ -437,32 +435,50 @@
        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;
+-      int rc, rv = -1;
 +      zval *patt;
 +      int opts = 0;
 +      pcre_cache_entry *pce;
@@ -2018,6 +1892,25 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
 +      zval *repl;
 +      int res_len, rep_cnt = 0;
 +      TSRMLS_FETCH();
+       (void)setlocale(LC_CTYPE, "C");
+-      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);
+-      } 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)
+-                              goto out;
+-                      nm++;
+-              }
+-              regfree(&rx);
+-              rv = nm;
 +
 +      MAKE_STD_ZVAL(patt);
 +      ZVAL_STRINGL(patt, pat, strlen(pat), 0);
@@ -2026,51 +1919,38 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
 +      if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) {
 +              zval_dtor(patt);
 +              FREE_ZVAL(patt);
-+              return -1;
-+      }
++              rep_cnt = -1;
++              goto out;
+       }
 +
 +      MAKE_STD_ZVAL(repl);
 +      ZVAL_STRINGL(repl, rep, strlen(rep), 0);
 +
 +      res = php_pcre_replace_impl(pce, ms->o.buf, strlen(ms->o.buf), repl,
 +                      0, &res_len, -1, &rep_cnt TSRMLS_CC);
--      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);
++
 +      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;
-       }
++              rep_cnt = -1;
++              goto out;
++      }
 +
 +      strncpy(ms->o.buf, res, res_len);
 +      ms->o.buf[res_len] = '\0';
 +
 +      efree(res);
 +
+ out:
+       (void)setlocale(LC_CTYPE, "");
+-      return rv;
 +      return rep_cnt;
  }
-+
 diff -u libmagic.orig/magic.c libmagic/magic.c
---- libmagic.orig/magic.c      Fri Jan 11 17:43:09 2013
-+++ libmagic/magic.c   Fri May  3 15:19:35 2013
+--- libmagic.orig/magic.c      Sun Dec  1 20:22:13 2013
++++ libmagic/magic.c   Wed Feb 19 10:10:11 2014
 @@ -25,11 +25,6 @@
   * SUCH DAMAGE.
   */
@@ -2201,15 +2081,6 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
  
  public struct magic_set *
  magic_open(int flags)
-@@ -250,7 +263,7 @@
- magic_load(struct magic_set *ms, const char *magicfile)
- {
-       if (ms == NULL)
--              return -1;
-+      return -1;
-       return file_apprentice(ms, magicfile, FILE_LOAD);
- }
 @@ -262,13 +275,6 @@
        return file_apprentice(ms, magicfile, FILE_COMPILE);
  }
@@ -2228,7 +2099,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
  close_and_restore(const struct magic_set *ms, const char *name, int fd,
      const struct stat *sb)
  {
--      if (fd == STDIN_FILENO)
+-      if (fd == STDIN_FILENO || name == NULL)
 -              return;
 -      (void) close(fd);
  
@@ -2251,7 +2122,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
  }
  
  /*
-@@ -332,30 +334,42 @@
+@@ -332,31 +334,42 @@
  {
        if (ms == NULL)
                return NULL;
@@ -2276,6 +2147,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
        struct stat     sb;
        ssize_t nbytes = 0;     /* number of bytes read from a datafile */
 -      int     ispipe = 0;
+-      off_t   pos = (off_t)-1;
 +      int no_in_stream = 0;
 +      TSRMLS_FETCH();
 +
@@ -2300,34 +2172,35 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
        case -1:                /* error */
                goto done;
        case 0:                 /* nothing found */
-@@ -365,68 +379,48 @@
+@@ -366,74 +379,44 @@
                goto done;
        }
  
 -      if (inname == NULL) {
 -              if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
 -                      ispipe = 1;
+-              else
+-                      pos = lseek(fd, (off_t)0, SEEK_CUR);
 -      } else {
 -              int flags = O_RDONLY|O_BINARY;
+-              int okstat = stat(inname, &sb) == 0;
 +      errno = 0;
  
--              if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
+-              if (okstat && S_ISFIFO(sb.st_mode)) {
 -#ifdef O_NONBLOCK
 -                      flags |= O_NONBLOCK;
+-#endif
+-                      ispipe = 1;
+-              }
 +      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)
+-                      if (okstat &&
+-                          unreadable_info(ms, sb.st_mode, inname) == -1)
 -                              goto done;
 -                      rv = 0;
 +      if (!stream) {
@@ -2384,6 +2257,8 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
        rv = 0;
  done:
 -      free(buf);
+-      if (pos != (off_t)-1)
+-              (void)lseek(fd, pos, SEEK_SET);
 -      close_and_restore(ms, inname, fd, &sb);
 +      efree(buf);
 +
@@ -2395,7 +2270,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
        return rv == 0 ? file_getbuffer(ms) : NULL;
  }
  
-@@ -440,14 +434,13 @@
+@@ -447,14 +430,13 @@
                return NULL;
        /*
         * The main work is done here!
@@ -2413,9 +2288,9 @@ 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      Thu Mar 21 18:52:42 2013
-+++ libmagic/magic.h   Sun Apr  7 22:30:22 2013
-@@ -87,6 +87,7 @@
+--- libmagic.orig/magic.h      Wed Feb 19 10:53:11 2014
++++ libmagic/magic.h   Wed Feb 19 10:46:54 2014
+@@ -88,6 +88,7 @@
  
  const char *magic_getpath(const char *, int);
  const char *magic_file(magic_t, const char *);
@@ -2423,7 +2298,7 @@ diff -u libmagic.orig/magic.h libmagic/magic.h
  const char *magic_descriptor(magic_t, int);
  const char *magic_buffer(magic_t, const void *, size_t);
  
-@@ -96,7 +97,6 @@
+@@ -97,7 +98,6 @@
  int magic_version(void);
  int magic_load(magic_t, const char *);
  int magic_compile(magic_t, const char *);
@@ -2432,15 +2307,15 @@ diff -u libmagic.orig/magic.h libmagic/magic.h
  int magic_errno(magic_t);
  
 diff -u libmagic.orig/print.c libmagic/print.c
---- libmagic.orig/print.c      Thu Mar 21 18:45:14 2013
-+++ libmagic/print.c   Mon Dec 16 23:09:24 2013
-@@ -29,12 +29,17 @@
+--- libmagic.orig/print.c      Tue Feb 26 19:25:00 2013
++++ libmagic/print.c   Tue Feb 18 10:59:23 2014
+@@ -28,13 +28,17 @@
+ /*
   * print.c - debugging printout routines
   */
 +#define _GNU_SOURCE
 +#include "php.h"
-+
  #include "file.h"
 +#include "cdf.h"
  
@@ -2452,7 +2327,7 @@ diff -u libmagic.orig/print.c libmagic/print.c
  #include <string.h>
  #include <stdarg.h>
  #include <stdlib.h>
-@@ -43,188 +48,28 @@
+@@ -43,188 +47,28 @@
  #endif
  #include <time.h>
  
@@ -2653,7 +2528,7 @@ diff -u libmagic.orig/print.c libmagic/print.c
  }
  
  protected const char *
-@@ -235,7 +80,7 @@
+@@ -235,7 +79,7 @@
        struct tm *tm;
  
        if (flags & FILE_T_WINDOWS) {
@@ -2663,8 +2538,8 @@ diff -u libmagic.orig/print.c libmagic/print.c
                t = ts.tv_sec;
        }
 diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
---- libmagic.orig/readcdf.c    Wed Oct 31 18:03:01 2012
-+++ libmagic/readcdf.c Sun Apr  7 22:30:22 2013
+--- libmagic.orig/readcdf.c    Tue Jan  7 04:13:42 2014
++++ libmagic/readcdf.c Wed Feb 19 10:10:11 2014
 @@ -30,7 +30,11 @@
  #endif
  
@@ -2677,7 +2552,18 @@ diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
  #include <string.h>
  #include <time.h>
  #include <ctype.h>
-@@ -46,7 +50,7 @@
+@@ -69,6 +73,10 @@
+       { NULL,                         NULL,                   },
+ };
++#ifdef PHP_WIN32
++# define strcasestr strstr
++#endif
++
+ private const char *
+ cdf_app_to_mime(const char *vbuf, const struct nv *nv)
+ {
+@@ -91,7 +99,7 @@
  {
          size_t i;
          cdf_timestamp_t tp;
@@ -2686,7 +2572,7 @@ diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
          char buf[64];
          const char *str = NULL;
          const char *s;
-@@ -125,8 +129,12 @@
+@@ -162,8 +170,12 @@
                  case CDF_FILETIME:
                          tp = info[i].pi_tp;
                          if (tp != 0) {
@@ -2701,33 +2587,23 @@ diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
                                          cdf_print_elapsed_time(tbuf,
                                              sizeof(tbuf), tp);
                                          if (NOTMIME(ms) && file_printf(ms,
-@@ -134,7 +142,9 @@
+@@ -171,8 +183,11 @@
                                                  return -1;
                                  } else {
                                          char *c, *ec;
 -                                        cdf_timestamp_to_timespec(&ts, tp);
+-                                        c = cdf_ctime(&ts.tv_sec, tbuf);
++                                      const time_t sec = ts.tv_sec;
 +                                        if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
 +                                                                                      return -1;
 +                                                                              }
-                                         c = cdf_ctime(&ts.tv_sec, tbuf);
-                                         if ((ec = strchr(c, '\n')) != NULL)
-                                                 *ec = '\0';
-@@ -314,9 +324,9 @@
-                       if (file_printf(ms,
-                           "Composite Document File V2 Document") == -1)
-                           return -1;
--                      if (*expn)
--                          if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
--                              return -1;
-+                if (*expn)
-+                        if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
-+                                return -1;
-                   } else {
-                       if (file_printf(ms, "application/CDFV2-corrupt") == -1)
-                           return -1;
++                                        c = cdf_ctime(&sec, tbuf);
+                                         if (c != NULL &&
+                                           (ec = strchr(c, '\n')) != NULL)
+                                               *ec = '\0';
 diff -u libmagic.orig/readelf.c libmagic/readelf.c
---- libmagic.orig/readelf.c    Thu Mar 21 18:45:14 2013
-+++ libmagic/readelf.c Sun Apr  7 22:30:22 2013
+--- libmagic.orig/readelf.c    Tue Nov  5 16:44:01 2013
++++ libmagic/readelf.c Wed Feb 19 10:10:11 2014
 @@ -48,8 +48,8 @@
  private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
      off_t, int *, int);
@@ -2739,7 +2615,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
      int, size_t, int *);
  
  #define       ELF_ALIGN(a)    ((((a) + align - 1) / align) * align)
-@@ -127,11 +127,17 @@
+@@ -127,7 +127,13 @@
  
  #define elf_getu16(swap, value) getu16(swap, value)
  #define elf_getu32(swap, value) getu32(swap, value)
@@ -2753,35 +2629,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
 +#endif
  
  #define xsh_addr      (clazz == ELFCLASS32                    \
--                       ? (void *)&sh32                        \
--                       : (void *)&sh64)
-+                       ? (void *) &sh32                       \
-+                       : (void *) &sh64)
- #define xsh_sizeof    (clazz == ELFCLASS32                    \
-                        ? sizeof(sh32)                         \
-                        : sizeof(sh64))
-@@ -168,8 +174,8 @@
-                        ? elf_getu32(swap, ph32.p_filesz)      \
-                        : elf_getu64(swap, ph64.p_filesz)))
- #define xnh_addr      (clazz == ELFCLASS32                    \
--                       ? (void *)&nh32                        \
--                       : (void *)&nh64)
-+                       ? (void *) &nh32                       \
-+                       : (void *) &nh64)
- #define xph_memsz     (size_t)((clazz == ELFCLASS32           \
-                        ? elf_getu32(swap, ph32.p_memsz)       \
-                        : elf_getu64(swap, ph64.p_memsz)))
-@@ -189,8 +195,8 @@
-                        ? prpsoffsets32[i]                     \
-                        : prpsoffsets64[i])
- #define xcap_addr     (clazz == ELFCLASS32                    \
--                       ? (void *)&cap32                       \
--                       : (void *)&cap64)
-+                       ? (void *) &cap32                      \
-+                       : (void *) &cap64)
- #define xcap_sizeof   (clazz == ELFCLASS32                    \
-                        ? sizeof cap32                         \
-                        : sizeof cap64)
+                        ? (void *)&sh32                        \
 @@ -292,7 +298,7 @@
  {
        Elf32_Phdr ph32;
@@ -2820,7 +2668,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
                        file_badread(ms);
                        return -1;
                }
-@@ -852,24 +867,12 @@
+@@ -913,24 +928,12 @@
                return 0;
        }
  
@@ -2848,7 +2696,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
                        file_badread(ms);
                        return -1;
                }
-@@ -894,14 +897,17 @@
+@@ -955,14 +958,17 @@
                /* Things we can determine when we seek */
                switch (xsh_type) {
                case SHT_NOTE:
@@ -2865,13 +2713,13 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
 -                      if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
 +                      if (FINFO_READ_FUNC(fd, nbuf, (size_t)xsh_size) !=
 +                          (ssize_t)xsh_size) {
-+                              efree(nbuf);
                                file_badread(ms);
 -                              free(nbuf);
++                              efree(nbuf);
                                return -1;
                        }
  
-@@ -910,25 +916,16 @@
+@@ -971,25 +977,16 @@
                                if (noff >= (off_t)xsh_size)
                                        break;
                                noff = donote(ms, nbuf, (size_t)noff,
@@ -2902,7 +2750,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
                                file_badseek(ms);
                                return -1;
                        }
-@@ -940,7 +937,7 @@
+@@ -1001,7 +998,7 @@
                                    MAX(sizeof cap32, sizeof cap64)];
                                if ((coff += xcap_sizeof) > (off_t)xsh_size)
                                        break;
@@ -2911,7 +2759,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
                                    (ssize_t)xcap_sizeof) {
                                        file_badread(ms);
                                        return -1;
-@@ -966,13 +963,12 @@
+@@ -1027,13 +1024,12 @@
                                        break;
                                }
                        }
@@ -2927,7 +2775,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
        if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
                return -1;
        if (cap_hw1) {
-@@ -1051,7 +1047,7 @@
+@@ -1112,7 +1108,7 @@
        const char *shared_libraries = "";
        unsigned char nbuf[BUFSIZ];
        ssize_t bufsize;
@@ -2936,7 +2784,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
        
        if (size != xph_sizeof) {
                if (file_printf(ms, ", corrupted program header size") == -1)
-@@ -1060,8 +1056,13 @@
+@@ -1121,8 +1117,13 @@
        }
  
        for ( ; num; num--) {
@@ -2952,7 +2800,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
                        return -1;
                }
  
-@@ -1099,9 +1100,12 @@
+@@ -1160,9 +1161,12 @@
                         * This is a PT_NOTE section; loop through all the notes
                         * in the section.
                         */
@@ -2968,7 +2816,7 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
                        if (bufsize == -1) {
                                file_badread(ms);
                                return -1;
-@@ -1162,7 +1166,7 @@
+@@ -1223,7 +1227,7 @@
        /*
         * If we cannot seek, it must be a pipe, socket or fifo.
         */
@@ -2978,8 +2826,8 @@ diff -u libmagic.orig/readelf.c libmagic/readelf.c
  
        if (fstat(fd, &st) == -1) {
 diff -u libmagic.orig/readelf.h libmagic/readelf.h
---- libmagic.orig/readelf.h    Thu Mar 21 18:45:14 2013
-+++ libmagic/readelf.h Wed Mar 28 15:35:26 2012
+--- libmagic.orig/readelf.h    Tue Nov  5 16:41:56 2013
++++ libmagic/readelf.h Wed Feb 19 10:10:11 2014
 @@ -44,9 +44,17 @@
  typedef uint32_t      Elf32_Word;
  typedef uint8_t               Elf32_Char;
@@ -2999,11 +2847,11 @@ diff -u libmagic.orig/readelf.h libmagic/readelf.h
  typedef uint32_t      Elf64_Word;
  typedef uint8_t               Elf64_Char;
 diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
---- libmagic.orig/softmagic.c  Thu Mar 21 18:45:14 2013
-+++ libmagic/softmagic.c       Tue Feb 18 18:44:17 2014
-@@ -41,6 +41,11 @@
- #include <stdlib.h>
- #include <time.h>
+--- libmagic.orig/softmagic.c  Thu Feb 13 00:20:53 2014
++++ libmagic/softmagic.c       Wed Feb 19 10:10:11 2014
+@@ -50,6 +50,11 @@
+ #include <locale.h>
+ #endif
  
 +#ifndef PREG_OFFSET_CAPTURE
 +# define PREG_OFFSET_CAPTURE                 (1<<8)
@@ -3013,23 +2861,17 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
  
  private int match(struct magic_set *, struct magic *, uint32_t,
      const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,
-@@ -69,13 +74,13 @@
- /*ARGSUSED1*/         /* nbytes passed for regularity, maybe need later */
- protected int
- file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
--    int mode, int text)
-+    size_t level, int mode, int text)
- {
-       struct mlist *ml;
-       int rv, printed_something = 0, need_separator = 0;
-       for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
-               if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode,
--                  text, 0, 0, &printed_something, &need_separator,
-+                  text, 0, level, &printed_something, &need_separator,
-                   NULL)) != 0)
-                       return rv;
-@@ -132,7 +137,7 @@
+@@ -71,7 +76,8 @@
+ private void cvt_32(union VALUETYPE *, const struct magic *);
+ private void cvt_64(union VALUETYPE *, const struct magic *);
+-#define OFFSET_OOB(n, o, i)   ((n) < (o) || (i) >= ((n) - (o)))
++/*#define OFFSET_OOB(n, o, i) ((n) < (o) || (i) >= ((n) - (o))) */
++#define OFFSET_OOB(n, o, i)   (n < o + i)
+ /*
+  * softmagic - lookup one file in parsed, in-memory copy of database
+  * Passed the name and FILE * of one file to be typed.
+@@ -142,7 +148,7 @@
                struct magic *m = &magic[magindex];
  
                if (m->type != FILE_NAME)
@@ -3038,7 +2880,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
  #define FLT (STRING_BINTEST | STRING_TEXTTEST)
                     ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
                      (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
-@@ -209,8 +214,8 @@
+@@ -221,8 +227,8 @@
                if (file_check_mem(ms, ++cont_level) == -1)
                        return -1;
  
@@ -3049,37 +2891,37 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                        m = &magic[magindex];
                        ms->line = m->lineno; /* for messages */
  
-@@ -335,44 +340,22 @@
+@@ -350,46 +356,24 @@
  private int
  check_fmt(struct magic_set *ms, struct magic *m)
  {
 -      regex_t rx;
--      int rc;
+-      int rc, rv = -1;
 -
--      if (strchr(m->desc, '%') == NULL)
 +      pcre *pce;
-+      int re_options;
++      int re_options, rv = -1;
 +      pcre_extra *re_extra;
 +      TSRMLS_FETCH();
 +      
-+      if (strchr(m->desc, '%') == NULL) {
+       if (strchr(m->desc, '%') == NULL)
                return 0;
--
+       (void)setlocale(LC_CTYPE, "C");
 -      rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
 -      if (rc) {
 -              char errmsg[512];
 -              (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
 -              file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
-+      }
-+      
 +      if ((pce = pcre_get_compiled_regex("~%[-0-9.]*s~", &re_extra, &re_options TSRMLS_CC)) == NULL) {
-               return -1;
++              rv = -1;
        } else {
 -              rc = regexec(&rx, m->desc, 0, 0, 0);
 -              regfree(&rx);
--              return !rc;
-+              return !pcre_exec(pce, re_extra, m->desc, strlen(m->desc), 0, re_options, NULL, 0);
+-              rv = !rc;
++              rv = !pcre_exec(pce, re_extra, m->desc, strlen(m->desc), 0, re_options, NULL, 0);
        }
+       (void)setlocale(LC_CTYPE, "");
+       return rv;
  }
  
 -#ifndef HAVE_STRNDUP
@@ -3104,7 +2946,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
  private int32_t
  mprint(struct magic_set *ms, struct magic *m)
  {
-@@ -583,13 +566,13 @@
+@@ -618,13 +602,13 @@
                char *cp;
                int rval;
  
@@ -3114,13 +2956,13 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                        file_oomem(ms, ms->search.rm_len);
                        return -1;
                }
-               rval = file_printf(ms, m->desc, cp);
+               rval = file_printf(ms, F(m->desc, "%s"), cp);
 -              free(cp);
 +              efree(cp);
  
                if (rval == -1)
                        return -1;
-@@ -835,16 +818,16 @@
+@@ -870,16 +854,16 @@
        if (m->num_mask) \
                switch (m->mask_op & FILE_OPS_MASK) { \
                case FILE_OPADD: \
@@ -3141,7 +2983,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                        break; \
                } \
  
-@@ -1145,9 +1128,6 @@
+@@ -1178,9 +1162,6 @@
                    "nbytes=%zu, count=%u)\n", m->type, m->flag, offset, o,
                    nbytes, count);
                mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
@@ -3151,7 +2993,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
        }
  
        if (m->flag & INDIR) {
-@@ -1644,16 +1624,13 @@
+@@ -1679,9 +1660,6 @@
                if ((ms->flags & MAGIC_DEBUG) != 0) {
                        mdebug(offset, (char *)(void *)p,
                            sizeof(union VALUETYPE));
@@ -3161,40 +3003,8 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                }
        }
  
-       /* Verify we have enough data to match magic type */
-       switch (m->type) {
-       case FILE_BYTE:
--              if (nbytes < (offset + 1)) /* should alway be true */
-+              if (nbytes < (offset + 1)) /* should always be true */
-                       return 0;
-               break;
-@@ -1703,6 +1680,8 @@
-               break;
-       case FILE_INDIRECT:
-+              if (offset == 0)
-+                      return 0;
-               if (nbytes < offset)
-                       return 0;
-               sbuf = ms->o.buf;
-@@ -1710,19 +1689,19 @@
-               ms->o.buf = NULL;
-               ms->offset = 0;
-               rv = file_softmagic(ms, s + offset, nbytes - offset,
--                  BINTEST, text);
-+                  recursion_level, BINTEST, text);
-               if ((ms->flags & MAGIC_DEBUG) != 0)
-                       fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
-               rbuf = ms->o.buf;
-               ms->o.buf = sbuf;
-               ms->offset = soffset;
-               if (rv == 1) {
--                      if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
-+              if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
-                           file_printf(ms, m->desc, offset) == -1)
--                              return -1;
-+                      return -1;
+@@ -1759,7 +1737,7 @@
+                               return -1;
                        if (file_printf(ms, "%s", rbuf) == -1)
                                return -1;
 -                      free(rbuf);
@@ -3202,7 +3012,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                }
                return rv;
  
-@@ -1837,6 +1816,42 @@
+@@ -1875,6 +1853,42 @@
        return file_strncmp(a, b, len, flags);
  }
  
@@ -3245,7 +3055,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
  private int
  magiccheck(struct magic_set *ms, struct magic *m)
  {
-@@ -1996,69 +2011,157 @@
+@@ -2035,63 +2049,151 @@
                break;
        }
        case FILE_REGEX: {
@@ -3255,41 +3065,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';
--#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;
 +              zval *pattern;
 +              int options = 0;
 +              pcre_cache_entry *pce;
@@ -3423,11 +3198,45 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 +                                              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;
@@ -3445,16 +3254,8 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                }
 -              if (v == (uint64_t)-1)
 -                      return -1;
--              break;
 +              zval_dtor(pattern);
 +              FREE_ZVAL(pattern);
-+              break;  
+               break;
        }
        case FILE_INDIRECT:
-       case FILE_USE:
-       case FILE_NAME:
--              return 1;
-+              return 1;        
-       default:
-               file_magerror(ms, "invalid type %d in magiccheck()", m->type);
-               return -1;