Diff for /embedaddon/php/ext/fileinfo/libmagic.patch between versions 1.1.1.2 and 1.1.1.3

version 1.1.1.2, 2012/05/29 12:34:39 version 1.1.1.3, 2013/07/22 01:31:50
Line 1 Line 1
 diff -u libmagic.orig/apprentice.c libmagic/apprentice.c  diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
--- libmagic.orig/apprentice.c  2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/apprentice.c  2013-03-21 18:45:14.000000000 +0100
+++ libmagic/apprentice.c       2012-03-28 01:53:04.283305402 +0200+++ libmagic/apprentice.c       2013-04-27 13:53:32.175250261 +0200
 @@ -29,6 +29,8 @@  @@ -29,6 +29,8 @@
   * apprentice - make one pass through /etc/magic, learning its secrets.    * apprentice - make one pass through /etc/magic, learning its secrets.
   */    */
Line 10  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 10  diff -u libmagic.orig/apprentice.c libmagic/apprentice
  #include "file.h"   #include "file.h"
     
  #ifndef        lint   #ifndef        lint
@@ -36,18 +38,34 @@@@ -36,18 +38,30 @@
  #endif /* lint */   #endif /* lint */
     
  #include "magic.h"   #include "magic.h"
Line 35  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 35  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 +#else  +#else
  #include <unistd.h>   #include <unistd.h>
  #endif   #endif
 +  
  #include <string.h>   #include <string.h>
  #include <assert.h>   #include <assert.h>
  #include <ctype.h>   #include <ctype.h>
Line 43  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 42  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 -#ifdef QUICK  -#ifdef QUICK
 -#include <sys/mman.h>  -#include <sys/mman.h>
 -#endif  -#endif
+#ifndef PHP_WIN32-#include <dirent.h>
 #include <dirent.h> 
+#endif 
     
  #define        EATAB {while (isascii((unsigned char) *l) && \   #define        EATAB {while (isascii((unsigned char) *l) && \
                       isspace((unsigned char) *l))  ++l;}                        isspace((unsigned char) *l))  ++l;}
@@ -112,12 +130,10 @@@@ -143,38 +157,7 @@
 private int parse_strength(struct magic_set *, struct magic_entry *, const char *); 
 private int parse_apple(struct magic_set *, struct magic_entry *, const char *); 
  
- 
 private size_t maxmagic = 0; 
 private size_t magicsize = sizeof(struct magic); 
  
 private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc"; 
- 
 private struct { 
        const char *name; 
        size_t len; 
@@ -131,38 +147,7 @@ 
         { NULL, 0, NULL }          { NULL, 0, NULL }
  };   };
     
Line 100  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 84  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 -#endif /* COMPILE_ONLY */  -#endif /* COMPILE_ONLY */
 +#include "../data_file.c"  +#include "../data_file.c"
     
 static const struct type_tbl_s { struct type_tbl_s {
         const char name[16];          const char name[16];
@@ -218,6 +203,10 @@@@ -255,6 +238,10 @@
  # undef XX
  # undef XX_NULL   # undef XX_NULL
  };  
     
 +#ifndef S_ISDIR  +#ifndef S_ISDIR
 +#define S_ISDIR(mode) ((mode) & _S_IFDIR)  +#define S_ISDIR(mode) ((mode) & _S_IFDIR)
 +#endif  +#endif
 +  +
  private int   private int
 get_type(const char *l, const char **t) get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
  {   {
@@ -275,15 +264,17 @@@@ -378,7 +365,7 @@
                if (rv != 0) {
                        return -1;        struct mlist *ml;
                rv = apprentice_compile(ms, &magic, &nmagic, fn); 
-               free(magic);-        if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL)
+               efree(magic);+       if ((ml = CAST(struct mlist *, emalloc(sizeof(*ml)))) == NULL)
                return rv;                return -1;
  
         ml->map = idx == 0 ? map : NULL;
 @@ -416,12 +403,13 @@
                 return apprentice_compile(ms, map, fn);
         }          }
     
 -#ifndef COMPILE_ONLY  -#ifndef COMPILE_ONLY
        if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {        map = apprentice_map(ms, fn);
         if (map == NULL) {
 -               if (ms->flags & MAGIC_CHECK)  -               if (ms->flags & MAGIC_CHECK)
 -                       file_magwarn(ms, "using regular magic file `%s'", fn);  -                       file_magwarn(ms, "using regular magic file `%s'", fn);
-               rv = apprentice_load(ms, &magic, &nmagic, fn, action);-               map = apprentice_load(ms, fn, action);
 +               if (fn) {  +               if (fn) {
 +                       if (ms->flags & MAGIC_CHECK)  +                       if (ms->flags & MAGIC_CHECK)
 +                               file_magwarn(ms, "using regular magic file `%s'", fn);  +                               file_magwarn(ms, "using regular magic file `%s'", fn);
+                       rv = apprentice_load(ms, &magic, &nmagic, fn, action);+                       map = apprentice_load(ms, fn, action);
 +               }  +               }
+                if (map == NULL)
                if (rv != 0) 
                         return -1;                          return -1;
         }          }
@@ -295,11 +286,7 @@@@ -444,7 +432,6 @@
                return -1; 
         }          }
    
 -       if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) {  
 -               file_delmagic(magic, mapped, nmagic);  
 -               file_oomem(ms, sizeof(*ml));  
 -               return -1;  
 -       }  
 +       ml = emalloc(sizeof(*ml));  
    
         ml->magic = magic;  
         ml->nmagic = nmagic;  
 @@ -318,7 +305,6 @@  
         }  
                   
         return 0;          return 0;
 -#endif /* COMPILE_ONLY */  -#endif /* COMPILE_ONLY */
  }   }
     
  protected void   protected void
@@ -327,22 +313,18 @@@@ -455,10 +442,16 @@
        if (p == NULL) 
                 return;                  return;
        switch (type) {        for (i = 0; i < MAGIC_SETS; i++)
-       case 2:                mlist_free(ms->mlist[i]);
 -       free(ms->o.pbuf);
 -       free(ms->o.buf);
 -       free(ms->c.li);
 -       free(ms);
 +       if (ms->o.pbuf) {
 +               efree(ms->o.pbuf);
 +       }
 +       if (ms->o.buf) {
 +               efree(ms->o.buf);
 +       }
 +       if (ms->c.li) {
 +               efree(ms->c.li);
 +       }
 +       efree(ms);
  }
  
  protected struct magic_set *
 @@ -467,7 +460,7 @@
         struct magic_set *ms;
         size_t i, len;
  
 -       if ((ms = CAST(struct magic_set *, calloc((size_t)1,
 +       if ((ms = CAST(struct magic_set *, ecalloc((size_t)1,
             sizeof(struct magic_set)))) == NULL)
                 return NULL;
  
 @@ -479,7 +472,7 @@
         ms->o.buf = ms->o.pbuf = NULL;
         len = (ms->c.len = 10) * sizeof(*ms->c.li);
  
 -       if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
 +       if ((ms->c.li = CAST(struct level_info *, emalloc(len))) == NULL)
                 goto free;
  
         ms->event_flags = 0;
 @@ -490,7 +483,7 @@
         ms->line = 0;
         return ms;
  free:
 -       free(ms);
 +       efree(ms);
         return NULL;
  }
  
 @@ -499,22 +492,24 @@
  {
         if (map == NULL)
                 return;
 -       if (map->p == NULL)
 -               return;
 -#ifdef QUICK  -#ifdef QUICK
-               p--;-       if (map->len)
-               (void)munmap((void *)p, sizeof(*p) * (entries + 1));-               (void)munmap(map->p, map->len);
+       case 3:-        else
+               /* Do nothing, it's part of the code segment */ 
                break; 
-#else 
-               (void)&entries; 
-               abort(); 
-               /*NOTREACHED*/ 
 -#endif  -#endif
+-               free(map->p);
        case 1:-       free(map);
                p--;+       if (map->p != php_magic_database) {
                /*FALLTHROUGH*/+               int j;
++               for (j = 0; j < MAGIC_SETS; j++) {
        case 0:+                       if (map->magic[j])
-               free(p);+                               efree(map->magic[j]);
+               efree(p);+               }
                break;+               if (map->p != NULL) {
++                       efree(map->p);
        default:+               }
                abort();+       }
 +       efree(map);
  }
  
  private struct mlist *
  mlist_alloc(void)
  {
         struct mlist *mlist;
 -       if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) {
 +       if ((mlist = CAST(struct mlist *, ecalloc(1, sizeof(*mlist)))) == NULL) {
                 return NULL;
         }          }
@@ -355,23 +337,27 @@        mlist->next = mlist->prev = mlist;
 @@ -533,10 +528,10 @@
                 struct mlist *next = ml->next;
                 if (ml->map)
                         apprentice_unmap(ml->map);
 -               free(ml);
 +               efree(ml);
                 ml = next;
         }
 -       free(ml);
 +       efree(ml);
  }
  
  /* const char *fn: list of magic files and directories */
 @@ -546,13 +541,28 @@
         char *p, *mfn;          char *p, *mfn;
         int file_err, errs = -1;          int file_err, errs = -1;
        struct mlist *mlist;        size_t i;
 -  -
 +/* XXX disabling default magic loading so the compiled in data is used */  +/* XXX disabling default magic loading so the compiled in data is used */
 +#if 0  +#if 0
         if ((fn = magic_getpath(fn, action)) == NULL)          if ((fn = magic_getpath(fn, action)) == NULL)
                return NULL;                return -1;
 +#endif  +#endif
     
         init_file_tables();          init_file_tables();
     
 -       if ((mfn = strdup(fn)) == NULL) {  -       if ((mfn = strdup(fn)) == NULL) {
 -               file_oomem(ms, strlen(fn));  
 -               return NULL;  
 +       if (fn == NULL)  +       if (fn == NULL)
 +               fn = getenv("MAGIC");  +               fn = getenv("MAGIC");
 +       if (fn == NULL) {  +       if (fn == NULL) {
+                mlist = emalloc(sizeof(*mlist));+               for (i = 0; i < MAGIC_SETS; i++) {
+               mlist->next = mlist->prev = mlist;+                       mlist_free(ms->mlist[i]);
+               apprentice_1(ms, fn, action, mlist);+                       if ((ms->mlist[i] = mlist_alloc()) == NULL) {
+               return mlist;+                               file_oomem(ms, sizeof(*ms->mlist[i]));
        }+                               return -1;
 +                       }
 +               }
 +               return apprentice_1(ms, fn, action);
 +       }
 +  +
+        mfn = estrdup(fn);+       if ((mfn = estrdup(fn)) == NULL) {
        fn = mfn;                file_oomem(ms, strlen(fn));
                 return -1;
-       if ((mlist = CAST(struct mlist *, malloc(sizeof(*mlist)))) == NULL) { 
-               free(mfn); 
-                file_oomem(ms, sizeof(*mlist)); 
-                return NULL; 
-       } 
+       mlist = emalloc(sizeof(*mlist)); 
        mlist->next = mlist->prev = mlist; 
  
        while (fn) { 
@@ -385,13 +371,13 @@ 
                fn = p; 
         }          }
        if (errs == -1) {@@ -567,7 +577,7 @@
-                free(mfn);                                        mlist_free(ms->mlist[i]);
-               free(mlist);                                while (i != 0);
+               efree(mfn);                        }
+               efree(mlist);-                        free(mfn);
                mlist = NULL;+                       efree(mfn);
                file_error(ms, 0, "could not find any magic files!");                        return -1;
                return NULL;                }
         }          }
   @@ -584,7 +594,7 @@
                   fn = p;
           }
    
 -       free(mfn);  -       free(mfn);
 +       efree(mfn);  +       efree(mfn);
         return mlist;  
  }  
     
@@ -524,6 +510,7 @@        if (errs == -1) {
                abort();                for (i = 0; i < MAGIC_SETS; i++) {
        }@@ -904,7 +914,7 @@
     
+                maxmagic[i] += ALLOC_INCR;
        /*                if ((mp = CAST(struct magic_entry *,
         * Magic entries with no description get a bonus because they depend-                   realloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
         * on subsequent magic entries to print something.+                   erealloc(mentry[i], sizeof(*mp) * maxmagic[i]))) ==
@@ -539,8 +526,8 @@                    NULL) {
 private int                        file_oomem(ms, sizeof(*mp) * maxmagic[i]);
 apprentice_sort(const void *a, const void *b)                        return -1;
 {@@ -925,13 +935,24 @@
-       const struct magic_entry *ma = CAST(const struct magic_entry *, a); 
-       const struct magic_entry *mb = CAST(const struct magic_entry *, b); 
+       const struct magic_entry *ma = a; 
+       const struct magic_entry *mb = b; 
        size_t sa = apprentice_magic_strength(ma->mp); 
        size_t sb = apprentice_magic_strength(mb->mp); 
        if (sa == sb) 
@@ -671,12 +658,22 @@ 
  load_1(struct magic_set *ms, int action, const char *fn, int *errs,   load_1(struct magic_set *ms, int action, const char *fn, int *errs,
    struct magic_entry **marray, uint32_t *marraycount)    struct magic_entry **mentry, uint32_t *mentrycount)
  {   {
 -       size_t lineno = 0, llen = 0;  -       size_t lineno = 0, llen = 0;
 +       char buffer[BUFSIZ + 1];  +       char buffer[BUFSIZ + 1];
Line 269  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 296  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 -       ssize_t len;  -       ssize_t len;
 +       size_t len;  +       size_t len;
 +       size_t lineno = 0;  +       size_t lineno = 0;
+        struct magic_entry me;
  
 -       FILE *f = fopen(ms->file = fn, "r");
 -       if (f == NULL) {
 +       php_stream *stream;  +       php_stream *stream;
 +  +
 +       TSRMLS_FETCH();  +       TSRMLS_FETCH();
 +  +
   +       ms->file = fn;
 +#if PHP_API_VERSION < 20100412  +#if PHP_API_VERSION < 20100412
 +       stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);  +       stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
 +#else  +#else
 +       stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);  +       stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL);
 +#endif  +#endif
 +
-       FILE *f = fopen(ms->file = fn, "r"); 
-       if (f == NULL) { 
 +       if (stream == NULL) {  +       if (stream == NULL) {
                 if (errno != ENOENT)                  if (errno != ENOENT)
                         file_error(ms, errno, "cannot read magic file `%s'",                          file_error(ms, errno, "cannot read magic file `%s'",
                                    fn);                                     fn);
@@ -684,9 +681,12 @@@@ -941,8 +962,7 @@
                return; 
        } 
     
-        /* read and parse this file */        memset(&me, 0, sizeof(me));
         /* read and parse this file */
 -       for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;  -       for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
 -           ms->line++) {  -           ms->line++) {
 +               /* read and parse this file */  
 +#if (PHP_MAJOR_VERSION < 6)  
 +       for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &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, &len)) != NULL; ms->line++) {  
 +#endif  
                 if (len == 0) /* null line, garbage, etc */                  if (len == 0) /* null line, garbage, etc */
                         continue;                          continue;
                 if (line[len - 1] == '\n') {                  if (line[len - 1] == '\n') {
@@ -736,8 +736,7 @@@@ -994,14 +1014,13 @@
                        break;                                goto again;
                         default:
                                 (*errs)++;
 -                                break;
 -                       }
 +                       break;
                 }                  }
         }          }
   +       }
           if (me.mp)
                   (void)addentry(ms, &me, mentry, mentrycount);
 -       free(line);  -       free(line);
 -       (void)fclose(f);  -       (void)fclose(f);
 +       php_stream_close(stream);  +       php_stream_close(stream);
  }   }
     
  /*   /*
@@ -757,20 +756,16 @@@@ -1080,7 +1099,7 @@
        int errs = 0;                mentrycount += me[i].cont_count;
        struct magic_entry *marray; 
        uint32_t marraycount, i, mentrycount = 0, starttest;        slen = sizeof(**ma) * mentrycount;
-        size_t slen, files = 0, maxfiles = 0;-       if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) {
 +       if ((*ma = CAST(struct magic *, emalloc(slen))) == NULL) {
                 file_oomem(ms, slen);
                 return -1;
         }
 @@ -1102,27 +1121,29 @@
         if (me == NULL)
                 return;
         for (i = 0; i < nme; i++)
 -               free(me[i].mp);
 -       free(me);
 +               efree(me[i].mp);
 +       efree(me);
  }
  
  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 };
         uint32_t i, j;
         size_t files = 0, maxfiles = 0;
 -       char **filearr = NULL, *mfn;  -       char **filearr = NULL, *mfn;
+       size_t files = 0, maxfiles = 0;+       char **filearr = NULL;
+       char **filearr = NULL, mfn[MAXPATHLEN]; 
         struct stat st;          struct stat st;
        DIR *dir;        struct magic_map *map;
        struct dirent *d;-        DIR *dir;
 -        struct dirent *d;
 +       php_stream *dir;
 +       php_stream_dirent d;
 +
 +       TSRMLS_FETCH();
     
         ms->flags |= MAGIC_CHECK;       /* Enable checks for parsed files */          ms->flags |= MAGIC_CHECK;       /* Enable checks for parsed files */
     
-        maxmagic = MAXMAGIS;-       if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
-       if ((marray = CAST(struct magic_entry *, calloc(maxmagic,+       if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
-           sizeof(*marray)))) == NULL) {                file_oomem(ms, sizeof(*map));
-                file_oomem(ms, maxmagic * sizeof(*marray));                return NULL;
-                return -1;        }
-       }@@ -1131,23 +1152,37 @@
+       maxmagic = MAXMAGIS;        if (action == FILE_CHECK)
+       marray = ecalloc(maxmagic, sizeof(*marray)); 
        marraycount = 0; 
  
        /* print silly verbose header for USG compat. */ 
@@ -778,14 +773,16 @@ 
                 (void)fprintf(stderr, "%s\n", usg_hdr);                  (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 */          /* load directory or file */
 -       if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {  -       if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
+        /* FIXME: Read file names and sort them to prevent-               dir = opendir(fn);
+           non-determinism. See Debian bug #488562. */+        /* 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)) {  +       if (php_sys_stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
                dir = opendir(fn);+               int mflen;
 +               char mfn[MAXPATHLEN];
 +
 +                dir = php_stream_opendir((char *)fn, REPORT_ERRORS, NULL);
                 if (!dir) {                  if (!dir) {
                         errs++;                          errs++;
                         goto out;                          goto out;
                 }                  }
                while ((d = readdir(dir)) != NULL) {-                while ((d = readdir(dir)) != NULL) {
 -                       if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {  -                       if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
+                       if (snprintf(mfn, sizeof(mfn), "%s/%s", fn, d->d_name) < 0) {+               while (php_stream_readdir(dir, &d)) {
 +                       if ((mflen = snprintf(mfn, sizeof(mfn), "%s/%s", fn, d.d_name)) < 0) {
                                 file_oomem(ms,                                  file_oomem(ms,
                                    strlen(fn) + strlen(d->d_name) + 2);-                                    strlen(fn) + strlen(d->d_name) + 2);
 +                               strlen(fn) + strlen(d.d_name) + 2);
                                 errs++;                                  errs++;
@@ -793,7 +790,6 @@-                               closedir(dir);
 +                               php_stream_closedir(dir);
                                 goto out;                                  goto out;
                         }                          }
                         if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {                          if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
Line 364  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 434  diff -u libmagic.orig/apprentice.c libmagic/apprentice
                                 continue;                                  continue;
                         }                          }
                         if (files >= maxfiles) {                          if (files >= maxfiles) {
@@ -803,7 +799,6 @@@@ -1155,24 +1190,23 @@
                                 maxfiles = (maxfiles + 1) * 2;
                                 mlen = maxfiles * sizeof(*filearr);
                                 if ((filearr = CAST(char **,                                  if ((filearr = CAST(char **,
                                    realloc(filearr, mlen))) == NULL) {-                                    realloc(filearr, mlen))) == NULL) {
 +                                   erealloc(filearr, mlen))) == NULL) {
                                         file_oomem(ms, mlen);                                          file_oomem(ms, mlen);
 -                                       free(mfn);  -                                       free(mfn);
                                        closedir(dir);-                                        closedir(dir);
 +                                       php_stream_closedir(dir);
                                         errs++;                                          errs++;
                                         goto out;                                          goto out;
@@ -882,12 +877,7 @@                                }
        for (i = 0; i < marraycount; i++)                        }
                mentrycount += marray[i].cont_count;-                       filearr[files++] = mfn;
 +                       filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen);
-       slen = sizeof(**magicp) * mentrycount;                }
-       if ((*magicp = CAST(struct magic *, malloc(slen))) == NULL) {-               closedir(dir);
-                file_oomem(ms, slen);+               php_stream_closedir(dir);
-               errs++;                qsort(filearr, files, sizeof(*filearr), cmpstrp);
-               goto out;                for (i = 0; i < files; i++) {
-       }                        load_1(ms, action, filearr[i], &errs, mentry,
+       *magicp = emalloc(sizeof(**magicp) * mentrycount);                            mentrycount);
 -                       free(filearr[i]);
        mentrycount = 0;+                       efree(filearr[i]);
        for (i = 0; i < marraycount; i++) {                }
@@ -897,8 +887,8 @@-               free(filearr);
        }+               efree(filearr);
 out:        } else
        for (i = 0; i < marraycount; i++)                load_1(ms, action, fn, &errs, mentry, mentrycount);
-               free(marray[i].mp);        if (errs)
-       free(marray);@@ -1211,9 +1245,9 @@
+               efree(marray[i].mp); 
+       efree(marray); 
         if (errs) {          if (errs) {
                *magicp = NULL;                for (j = 0; j < MAGIC_SETS; j++) {
                *nmagicp = 0;                        if (map->magic[j])
@@ -1178,11 +1168,7 @@-                               free(map->magic[j]);
 +                               efree(map->magic[j]);
                 }
 -               free(map);
 +               efree(map);
                 return NULL;
         }
         return map;
 @@ -1500,7 +1534,7 @@
                 if (me->cont_count == me->max_count) {                  if (me->cont_count == me->max_count) {
                         struct magic *nm;                          struct magic *nm;
                         size_t cnt = me->max_count + ALLOC_CHUNK;                          size_t cnt = me->max_count + ALLOC_CHUNK;
 -                       if ((nm = CAST(struct magic *, realloc(me->mp,  -                       if ((nm = CAST(struct magic *, realloc(me->mp,
-                            sizeof(*nm) * cnt))) == NULL) {+                       if ((nm = CAST(struct magic *, erealloc(me->mp,
-                                file_oomem(ms, sizeof(*nm) * cnt);                            sizeof(*nm) * cnt))) == NULL) {
-                                return -1;                                file_oomem(ms, sizeof(*nm) * cnt);
-                       }                                return -1;
+                       nm = erealloc(me->mp, sizeof(*nm) * cnt);@@ -1515,7 +1549,7 @@
                        me->mp = m = nm;                static const size_t len = sizeof(*m) * ALLOC_CHUNK;
                        me->max_count = CAST(uint32_t, cnt);                if (me->mp != NULL)
                         return 1;
 -               if ((m = CAST(struct magic *, malloc(len))) == NULL) {
 +               if ((m = CAST(struct magic *, emalloc(len))) == NULL) {
                         file_oomem(ms, len);
                         return -1;
                 }                  }
@@ -1194,23 +1180,13 @@@@ -1688,7 +1722,7 @@
                        struct magic_entry *mp;                                m->type = get_standard_integer_type(l, &l);
                         else if (*l == 's' && !isalpha((unsigned char)l[1])) {
                        maxmagic += ALLOC_INCR;                                m->type = FILE_STRING;
-                        if ((mp = CAST(struct magic_entry *,-                               ++l;
-                           realloc(*mentryp, sizeof(*mp) * maxmagic))) ==+               ++l;
-                           NULL) {                        }
-                               file_oomem(ms, sizeof(*mp) * maxmagic); 
-                               return -1; 
-                       } 
-                       (void)memset(&mp[*nmentryp], 0, sizeof(*mp) * 
-                           ALLOC_INCR); 
+                       mp = erealloc(*mentryp, sizeof(*mp) * maxmagic); 
+                       (void)memset(&mp[*nmentryp], 0, sizeof(*mp) * ALLOC_INCR); 
                        *mentryp = mp; 
                 }                  }
                me = &(*mentryp)[*nmentryp];        }
                if (me->mp == NULL) {@@ -1701,6 +1735,10 @@
-                       size_t len = sizeof(*m) * ALLOC_CHUNK;        if (m->type == FILE_INVALID) {
-                       if ((m = CAST(struct magic *, malloc(len))) == NULL) {                if (ms->flags & MAGIC_CHECK)
-                               file_oomem(ms, len);                        file_magwarn(ms, "type `%s' invalid", l);
-                                return -1;+                if (me->mp) {
-                       }+                       efree(me->mp);
+                       m = safe_emalloc(sizeof(*m), ALLOC_CHUNK, 0);+                       me->mp = NULL;
                        me->mp = m;+               }
                        me->max_count = ALLOC_CHUNK;                return -1;
                } else        }
@@ -1361,7 +1337,7 @@ 
     
   @@ -1709,7 +1747,7 @@
    
         m->mask_op = 0;          m->mask_op = 0;
         if (*l == '~') {          if (*l == '~') {
 -               if (!IS_STRING(m->type))  -               if (!IS_STRING(m->type))
Line 446  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 524  diff -u libmagic.orig/apprentice.c libmagic/apprentice
                         m->mask_op |= FILE_OPINVERSE;                          m->mask_op |= FILE_OPINVERSE;
                 else if (ms->flags & MAGIC_CHECK)                  else if (ms->flags & MAGIC_CHECK)
                         file_magwarn(ms, "'~' invalid for string types");                          file_magwarn(ms, "'~' invalid for string types");
@@ -1370,7 +1346,7 @@@@ -1718,7 +1756,7 @@
         m->str_range = 0;          m->str_range = 0;
         m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;          m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0;
         if ((op = get_op(*l)) != -1) {          if ((op = get_op(*l)) != -1) {
Line 455  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 533  diff -u libmagic.orig/apprentice.c libmagic/apprentice
                         uint64_t val;                          uint64_t val;
                         ++l;                          ++l;
                         m->mask_op |= op;                          m->mask_op |= op;
@@ -1558,11 +1534,6 @@@@ -1909,11 +1947,6 @@
                 if (check_format(ms, m) == -1)                  if (check_format(ms, m) == -1)
                         return -1;                          return -1;
         }          }
Line 465  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 543  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 -       }  -       }
 -#endif  -#endif
         m->mimetype[0] = '\0';          /* initialise MIME type to none */          m->mimetype[0] = '\0';          /* initialise MIME type to none */
        if (m->cont_level == 0)        return 0;
                ++(*nmentryp);          /* make room for next */ }
@@ -2195,56 +2166,68 @@@@ -2554,59 +2587,80 @@
  private struct magic_map *
 /* apprentice_map(struct magic_set *ms, const char *fn)
  * handle a compiled file. 
+ * return -1 = error 
+ * return 1  = memory structure you can free 
+ * return 3  = bundled library from PHP 
  */ 
 private int 
 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, 
     const char *fn) 
  {   {
 -       int fd;  -       int fd;
 -       struct stat st;  -       struct stat st;
         uint32_t *ptr;          uint32_t *ptr;
        uint32_t version;        uint32_t version, entries, nentries;
         int needsbyteswap;          int needsbyteswap;
         char *dbname = NULL;          char *dbname = NULL;
        void *mm = NULL;        struct magic_map *map;
+       int   ret = 0;        size_t i;
 +       php_stream *stream = NULL;  +       php_stream *stream = NULL;
 +       php_stream_statbuf st;  +       php_stream_statbuf st;
 +  +
+ 
 -       fd = -1;
 -       if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
 +       TSRMLS_FETCH();  +       TSRMLS_FETCH();
 +  +
   +       if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) {
                   file_oomem(ms, sizeof(*map));
   +               efree(map);
                   goto error;
           }
    
 +       if (fn == NULL) {  +       if (fn == NULL) {
+               mm = (void *)&php_magic_database;+               map->p = (void *)&php_magic_database;
+               ret = 3; 
 +               goto internal_loaded;  +               goto internal_loaded;
 +       }  +       }
 +
 +#ifdef PHP_WIN32
 +       /* Don't bother on windows with php_stream_open_wrapper,
 +       return to give apprentice_load() a chance. */
 +       if (php_stream_stat_path_ex((char *)fn, 0, &st, NULL) == SUCCESS) {
 +               if (st.sb.st_mode & S_IFDIR) {
 +                       goto error;
 +               }
 +       }
 +#endif
 +
         dbname = mkdbname(ms, fn, 0);          dbname = mkdbname(ms, fn, 0);
         if (dbname == NULL)          if (dbname == NULL)
                goto error2;                goto error;
     
 -       if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)  -       if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
 +#if PHP_API_VERSION < 20100412  +#if PHP_API_VERSION < 20100412
Line 511  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 598  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 +#endif  +#endif
 +  +
 +       if (!stream) {  +       if (!stream) {
                goto error2;                goto error;
 +       }  +       }
     
 -       if (fstat(fd, &st) == -1) {  -       if (fstat(fd, &st) == -1) {
 +       if (php_stream_stat(stream, &st) < 0) {  +       if (php_stream_stat(stream, &st) < 0) {
                 file_error(ms, errno, "cannot stat `%s'", dbname);                  file_error(ms, errno, "cannot stat `%s'", dbname);
                goto error1;                goto error;
         }          }
 -       if (st.st_size < 8) {  -       if (st.st_size < 8) {
 +  +
 +       if (st.sb.st_size < 8) {  +       if (st.sb.st_size < 8) {
                 file_error(ms, 0, "file `%s' is too small", dbname);                  file_error(ms, 0, "file `%s' is too small", dbname);
                goto error1;                goto error;
         }          }
     
   -       map->len = (size_t)st.st_size;
 -#ifdef QUICK  -#ifdef QUICK
-       if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,-       if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
 -           MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {  -           MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
 -               file_error(ms, errno, "cannot map `%s'", dbname);  -               file_error(ms, errno, "cannot map `%s'", dbname);
-               goto error1;-               goto error;
 -       }  -       }
 -#define RET    2  
 -#else  -#else
-       if ((mm = CAST(void *, malloc((size_t)st.st_size))) == NULL) {-       if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
-               file_oomem(ms, (size_t)st.st_size);+       map->len = (size_t)st.sb.st_size;
-               goto error1;+       if ((map->p = CAST(void *, emalloc(map->len))) == NULL) {
-       }                file_oomem(ms, map->len);
-       if (read(fd, mm, (size_t)st.st_size) != (ssize_t)st.st_size) {                goto error;
+       mm = emalloc((size_t)st.sb.st_size);        }
+       if (php_stream_read(stream, mm, (size_t)st.sb.st_size) != (size_t)st.sb.st_size) {-       if (read(fd, map->p, map->len) != (ssize_t)map->len) {
 +       if (php_stream_read(stream, map->p, (size_t)st.sb.st_size) != (size_t)st.sb.st_size) {
                 file_badread(ms);                  file_badread(ms);
                goto error1;                goto error;
         }          }
-#define RET     1        map->len = 0;
  #define RET    1
 -#endif  -#endif
 -       *magicp = CAST(struct magic *, mm);  
 -       (void)close(fd);  -       (void)close(fd);
 -       fd = -1;  -       fd = -1;
+       ret = 1;-       ptr = CAST(uint32_t *, map->p);
 +  +
 +       php_stream_close(stream);  +       php_stream_close(stream);
 +       stream = NULL;  +       stream = NULL;
 +  +
 +internal_loaded:  +internal_loaded:
+       *magicp = mm;+        ptr = (uint32_t *)(void *)map->p;
        ptr = (uint32_t *)(void *)*magicp; 
         if (*ptr != MAGICNO) {          if (*ptr != MAGICNO) {
                 if (swap4(*ptr) != MAGICNO) {                  if (swap4(*ptr) != MAGICNO) {
@@ -2259,35 +2242,55 @@                        file_error(ms, 0, "bad magic in `%s'", dbname);
 @@ -2620,17 +2674,29 @@
         else          else
                 version = ptr[1];                  version = ptr[1];
         if (version != VERSIONNO) {          if (version != VERSIONNO) {
Line 568  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 656  diff -u libmagic.orig/apprentice.c libmagic/apprentice
 +               file_error(ms, 0, "File %d.%d supports only version %d magic "  +               file_error(ms, 0, "File %d.%d supports only version %d magic "
 +                   "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,  +                   "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
                     VERSIONNO, dbname, version);                      VERSIONNO, dbname, version);
                goto error1;                goto error;
         }          }
-       *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic));-       entries = (uint32_t)(st.st_size / sizeof(struct magic));
-       if (*nmagicp > 0)-       if ((off_t)(entries * sizeof(struct magic)) != st.st_size) {
 -               file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
 -                   dbname, (unsigned long long)st.st_size,
 -                   sizeof(struct magic));
 -               goto error;
 +  +
 +       /* php_magic_database is a const, performing writes will segfault. This is for big-endian  +       /* php_magic_database is a const, performing writes will segfault. This is for big-endian
 +       machines only, PPC and Sparc specifically. Consider static variable or MINIT in  +       machines only, PPC and Sparc specifically. Consider static variable or MINIT in
 +       future. */  +       future. */
 +       if (needsbyteswap && fn == NULL) {  +       if (needsbyteswap && fn == NULL) {
+               mm = emalloc(sizeof(php_magic_database));+               map->p = emalloc(sizeof(php_magic_database));
+               mm = memcpy(mm, php_magic_database, sizeof(php_magic_database));+               map->p = memcpy(map->p, php_magic_database, sizeof(php_magic_database));
+               *magicp = mm; 
+               ret = 1; 
 +       }  +       }
 +  +
+       if (fn == NULL) {+       if (NULL != fn) {
+               *nmagicp = (sizeof(php_magic_database) / sizeof(struct magic));+               nentries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
+       } else {+               entries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
+               *nmagicp = (uint32_t)(st.sb.st_size / sizeof(struct magic));+               if ((off_t)(entries * sizeof(struct magic)) != st.sb.st_size) {
+       }+                       file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
+        if (*nmagicp > 0) {+                               dbname, (unsigned long long)st.sb.st_size,
                (*nmagicp)--;+                               sizeof(struct magic));
+       }+                       goto error;
        (*magicp)++;+               }
-        if (needsbyteswap)        }
+       if (needsbyteswap) {        map->magic[0] = CAST(struct magic *, map->p) + 1;
                byteswap(*magicp, *nmagicp);        nentries = 0;
 @@ -2643,22 +2709,29 @@
                         map->magic[i + 1] = map->magic[i] + map->nmagic[i];
                 nentries += map->nmagic[i];
         }
 -       if (entries != nentries + 1) {
 +       if (NULL != fn && entries != nentries + 1) {
                 file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
                     dbname, entries, nentries + 1);
                 goto error;
         }
 +
         if (needsbyteswap)
                 for (i = 0; i < MAGIC_SETS; i++)
                         byteswap(map->magic[i], map->nmagic[i]);
 -       free(dbname);  -       free(dbname);
 -       return RET;  
 +       }  
 +  +
 +       if (dbname) {  +       if (dbname) {
 +               efree(dbname);  +               efree(dbname);
 +       }  +       }
+        return ret;        return map;
     
 error1: error:
 -       if (fd != -1)  -       if (fd != -1)
 -               (void)close(fd);  -               (void)close(fd);
 -       if (mm) {  
 -#ifdef QUICK  
 -               (void)munmap((void *)mm, (size_t)st.st_size);  
 -#else  
 -               free(mm);  
 -#endif  
 +       if (stream) {  +       if (stream) {
 +               php_stream_close(stream);  +               php_stream_close(stream);
 +       }  +       }
+        apprentice_unmap(map);
+       if (mm && ret == 1) { 
+               efree(mm); 
        } else { 
                *magicp = NULL; 
                *nmagicp = 0; 
        } 
 error2: 
 -       free(dbname);  -       free(dbname);
 +       if (dbname) {  +       if (dbname) {
 +               efree(dbname);  +               efree(dbname);
 +       }  +       }
        return -1;        return NULL;
  }   }
     
@@ -2301,42 +2304,49 @@@@ -2679,14 +2752,23 @@
 apprentice_compile(struct magic_set *ms, struct magic **magicp, 
     uint32_t *nmagicp, const char *fn) 
 { 
-       int fd = -1; 
         char *dbname;          char *dbname;
         int rv = -1;          int rv = -1;
           uint32_t i;
 +       php_stream *stream;  +       php_stream *stream;
   +
   +       TSRMLS_FETCH();
     
 -       dbname = mkdbname(ms, fn, 1);  -       dbname = mkdbname(ms, fn, 1);
 +       TSRMLS_FETCH();  
 +  
 +       dbname = mkdbname(ms, fn, 0);  +       dbname = mkdbname(ms, fn, 0);
     
         if (dbname == NULL)           if (dbname == NULL) 
                 goto out;                  goto out;
     
-       if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {-       if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) 
 -       {
 +/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */  +/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */
 +#if PHP_API_VERSION < 20100412  +#if PHP_API_VERSION < 20100412
 +       stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);  +       stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
Line 660  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 747  diff -u libmagic.orig/apprentice.c libmagic/apprentice
                 file_error(ms, errno, "cannot open `%s'", dbname);                  file_error(ms, errno, "cannot open `%s'", dbname);
                 goto out;                  goto out;
         }          }
   @@ -2696,31 +2778,33 @@
                   goto out;
           }
     
-       if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {-       if (write(fd, map->nmagic, nm) != (ssize_t)nm) {
+       if (php_stream_write(stream, (char *)ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {+       if (php_stream_write(stream, (const char *)map->nmagic, nm) != (ssize_t)nm) {
                 file_error(ms, errno, "error writing `%s'", dbname);                  file_error(ms, errno, "error writing `%s'", dbname);
                 goto out;                  goto out;
         }          }
     
-       if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)        assert(nm + sizeof(ar) < m);
-           != sizeof(struct magic)) { 
 -       if (lseek(fd, (off_t)m, SEEK_SET) != (off_t)m) {
 +       if (php_stream_seek(stream,(off_t)sizeof(struct magic), SEEK_SET) != sizeof(struct magic)) {  +       if (php_stream_seek(stream,(off_t)sizeof(struct magic), SEEK_SET) != sizeof(struct magic)) {
                 file_error(ms, errno, "error seeking `%s'", dbname);                  file_error(ms, errno, "error seeking `%s'", dbname);
                 goto out;                  goto out;
         }          }
     
-        if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))         for (i = 0; i < MAGIC_SETS; i++) {
-           != (ssize_t)(sizeof(struct magic) * *nmagicp)) {                len = m * map->nmagic[i];
+       if (php_stream_write(stream, (char *)*magicp, (sizeof(struct magic) * *nmagicp) != (ssize_t)(sizeof(struct magic) * *nmagicp))) {              }
                file_error(ms, errno, "error writing `%s'", dbname);+               if (php_stream_write(stream, (const char *)map->magic[i], len) != (ssize_t)len) {
                goto out;                        file_error(ms, errno, "error writing `%s'", dbname);
                         goto out;
                 }
         }          }
     
 -       if (fd != -1)  -       if (fd != -1)
 -               (void)close(fd);  -               (void)close(fd);
+        php_stream_close(stream);+        if (stream) {
 +               php_stream_close(stream);
 +       }
 +  +
         rv = 0;          rv = 0;
  out:   out:
Line 692  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 787  diff -u libmagic.orig/apprentice.c libmagic/apprentice
         return rv;          return rv;
  }   }
     
@@ -2349,6 +2359,7 @@@@ -2733,6 +2817,7 @@
  {   {
         const char *p, *q;          const char *p, *q;
         char *buf;          char *buf;
Line 700  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 795  diff -u libmagic.orig/apprentice.c libmagic/apprentice
     
         if (strip) {          if (strip) {
                 if ((p = strrchr(fn, '/')) != NULL)                  if ((p = strrchr(fn, '/')) != NULL)
@@ -2370,14 +2381,14 @@@@ -2754,16 +2839,18 @@
         q++;          q++;
         /* Compatibility with old code that looked in .mime */          /* Compatibility with old code that looked in .mime */
         if (ms->flags & MAGIC_MIME) {          if (ms->flags & MAGIC_MIME) {
-                asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext);-               if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
 -                       return NULL;
 -               if (access(buf, R_OK) != -1) {  -               if (access(buf, R_OK) != -1) {
 +               spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", (int)(q - fn), fn, ext);  +               spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", (int)(q - fn), fn, ext);
   +#ifdef PHP_WIN32
   +               if (VCWD_ACCESS(buf, R_OK) == 0) {
   +#else
 +               if (VCWD_ACCESS(buf, R_OK) != -1) {  +               if (VCWD_ACCESS(buf, R_OK) != -1) {
   +#endif
                         ms->flags &= MAGIC_MIME_TYPE;                          ms->flags &= MAGIC_MIME_TYPE;
                         return buf;                          return buf;
                 }                  }
 -               free(buf);  -               free(buf);
 +               efree(buf);  +               efree(buf);
         }          }
-        asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext);-       if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
 -               return NULL;
 +       spprintf(&buf, MAXPATHLEN, "%.*s%s", (int)(q - fn), fn, ext);  +       spprintf(&buf, MAXPATHLEN, "%.*s%s", (int)(q - fn), fn, ext);
     
         /* Compatibility with old code that looked in .mime */          /* Compatibility with old code that looked in .mime */
         if (strstr(p, ".mime") != NULL)          if (strstr(p, ".mime") != NULL)
@@ -2467,7 +2478,7 @@@@ -2853,7 +2940,7 @@
         m->offset = swap4((uint32_t)m->offset);          m->offset = swap4((uint32_t)m->offset);
         m->in_offset = swap4((uint32_t)m->in_offset);          m->in_offset = swap4((uint32_t)m->in_offset);
         m->lineno = swap4((uint32_t)m->lineno);          m->lineno = swap4((uint32_t)m->lineno);
Line 729  diff -u libmagic.orig/apprentice.c libmagic/apprentice Line 830  diff -u libmagic.orig/apprentice.c libmagic/apprentice
                 m->str_flags = swap4(m->str_flags);                  m->str_flags = swap4(m->str_flags);
         }          }
 diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c  diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
--- libmagic.orig/ascmagic.c    2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/ascmagic.c    2012-10-31 18:03:01.000000000 +0100
+++ libmagic/ascmagic.c 2012-03-28 01:53:04.287303117 +0200+++ libmagic/ascmagic.c 2013-04-08 15:42:57.328298809 +0200
@@ -139,10 +139,8 @@@@ -139,7 +139,7 @@
                 /* malloc size is a conservative overestimate; could be                  /* malloc size is a conservative overestimate; could be
                    improved, or at least realloced after conversion. */                     improved, or at least realloced after conversion. */
                 mlen = ulen * 6;                  mlen = ulen * 6;
 -               if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {  -               if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
-                        file_oomem(ms, mlen);+               if ((utf8_buf = CAST(unsigned char *, emalloc(mlen))) == NULL) {
-                       goto done;                        file_oomem(ms, mlen);
-               } 
+               utf8_buf = emalloc(mlen); 
+ 
                if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) 
                    == NULL) 
                         goto done;                          goto done;
@@ -296,7 +294,8 @@                }
 @@ -211,6 +211,7 @@
                                 case 0:
                                         if (file_printf(ms, ", ") == -1)
                                                 goto done;
 +                                       break;
                                 case -1:
                                         goto done;
                                 default:
 @@ -296,7 +297,8 @@
         }          }
         rv = 1;          rv = 1;
  done:   done:
Line 754  diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c Line 859  diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
     
         return rv;          return rv;
  }   }
 Only in libmagic.orig: asprintf.c  
 diff -u libmagic.orig/cdf.c libmagic/cdf.c  diff -u libmagic.orig/cdf.c libmagic/cdf.c
--- libmagic.orig/cdf.c 2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/cdf.c 2013-03-21 18:45:14.000000000 +0100
+++ libmagic/cdf.c      2012-03-28 01:53:04.299331601 +0200+++ libmagic/cdf.c      2013-04-08 15:42:57.328298809 +0200
 @@ -43,7 +43,17 @@  @@ -43,7 +43,17 @@
  #include <err.h>   #include <err.h>
  #endif   #endif
Line 776  diff -u libmagic.orig/cdf.c libmagic/cdf.c Line 880  diff -u libmagic.orig/cdf.c libmagic/cdf.c
  #include <string.h>   #include <string.h>
  #include <time.h>   #include <time.h>
  #include <ctype.h>   #include <ctype.h>
@@ -1135,7 +1145,7 @@@@ -296,7 +306,10 @@
         if (info->i_fd == -1)
                 return -1;
  
 -       if (pread(info->i_fd, buf, len, off) != (ssize_t)len)
 +       if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (off_t)-1)
 +               return -1;
 +
 +       if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len)
                 return -1;
  
         return (ssize_t)len;
 @@ -1132,7 +1145,7 @@
         cdf_directory_t *d;          cdf_directory_t *d;
         char name[__arraycount(d->d_name)];          char name[__arraycount(d->d_name)];
         cdf_stream_t scn;          cdf_stream_t scn;
Line 785  diff -u libmagic.orig/cdf.c libmagic/cdf.c Line 901  diff -u libmagic.orig/cdf.c libmagic/cdf.c
     
         static const char *types[] = { "empty", "user storage",          static const char *types[] = { "empty", "user storage",
             "user stream", "lockbytes", "property", "root storage" };              "user stream", "lockbytes", "property", "root storage" };
@@ -1188,7 +1198,7 @@@@ -1185,7 +1198,7 @@
  cdf_dump_property_info(const cdf_property_info_t *info, size_t count)   cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
  {   {
         cdf_timestamp_t tp;          cdf_timestamp_t tp;
Line 794  diff -u libmagic.orig/cdf.c libmagic/cdf.c Line 910  diff -u libmagic.orig/cdf.c libmagic/cdf.c
         char buf[64];          char buf[64];
         size_t i, j;          size_t i, j;
     
@@ -1232,7 +1242,11 @@@@ -1229,7 +1242,11 @@
                         break;                          break;
                 case CDF_FILETIME:                  case CDF_FILETIME:
                         tp = info[i].pi_tp;                          tp = info[i].pi_tp;
Line 807  diff -u libmagic.orig/cdf.c libmagic/cdf.c Line 923  diff -u libmagic.orig/cdf.c libmagic/cdf.c
                                 (void)fprintf(stderr, "timestamp %s\n", buf);                                  (void)fprintf(stderr, "timestamp %s\n", buf);
                         } else {                          } else {
 diff -u libmagic.orig/cdf.h libmagic/cdf.h  diff -u libmagic.orig/cdf.h libmagic/cdf.h
--- libmagic.orig/cdf.h 2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/cdf.h 2012-10-31 18:03:01.000000000 +0100
+++ libmagic/cdf.h      2012-03-28 01:53:04.299331601 +0200+++ libmagic/cdf.h      2013-04-08 15:42:57.328298809 +0200
@@ -35,7 +35,7 @@@@ -35,10 +35,12 @@
  #ifndef _H_CDF_   #ifndef _H_CDF_
  #define _H_CDF_   #define _H_CDF_
     
Line 818  diff -u libmagic.orig/cdf.h libmagic/cdf.h Line 934  diff -u libmagic.orig/cdf.h libmagic/cdf.h
  #include <winsock2.h>   #include <winsock2.h>
  #define timespec timeval   #define timespec timeval
  #define tv_nsec tv_usec   #define tv_nsec tv_usec
@@ -57,7 +57,11 @@+#define asctime_r php_asctime_r
 +#define ctime_r php_ctime_r
  #endif
  #ifdef __DJGPP__
  #define timespec timeval
 @@ -57,7 +59,11 @@
     
  typedef struct {   typedef struct {
         uint64_t        h_magic;          uint64_t        h_magic;
Line 831  diff -u libmagic.orig/cdf.h libmagic/cdf.h Line 952  diff -u libmagic.orig/cdf.h libmagic/cdf.h
         uint64_t        h_uuid[2];          uint64_t        h_uuid[2];
         uint16_t        h_revision;          uint16_t        h_revision;
         uint16_t        h_version;          uint16_t        h_version;
@@ -267,9 +271,9 @@@@ -267,9 +273,9 @@
         size_t i_len;          size_t i_len;
  } cdf_info_t;   } cdf_info_t;
     
Line 845  diff -u libmagic.orig/cdf.h libmagic/cdf.h Line 966  diff -u libmagic.orig/cdf.h libmagic/cdf.h
  void cdf_swap_header(cdf_header_t *);   void cdf_swap_header(cdf_header_t *);
  void cdf_unpack_header(cdf_header_t *, char *);   void cdf_unpack_header(cdf_header_t *, char *);
 diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
--- libmagic.orig/cdf_time.c    2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/cdf_time.c    2012-10-31 18:03:01.000000000 +0100
+++ libmagic/cdf_time.c 2012-03-28 01:53:04.299331601 +0200+++ libmagic/cdf_time.c 2013-04-08 15:42:57.328298809 +0200
 @@ -96,7 +96,7 @@  @@ -96,7 +96,7 @@
  }   }
     
Line 856  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c Line 977  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
  {   {
         struct tm tm;          struct tm tm;
  #ifdef HAVE_STRUCT_TM_TM_ZONE   #ifdef HAVE_STRUCT_TM_TM_ZONE
@@ -104,8 +104,8 @@@@ -104,8 +104,9 @@
  #endif   #endif
         int rdays;          int rdays;
     
 -       /* Unit is 100's of nanoseconds */  -       /* Unit is 100's of nanoseconds */
 -       ts->tv_nsec = (t % CDF_TIME_PREC) * 100;  -       ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
   +       /* XXX 5.14 at least introdced 100 ns intervals, this is to do */
 +       /* Time interval, in microseconds */  +       /* Time interval, in microseconds */
 +       ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC;  +       ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC;
     
         t /= CDF_TIME_PREC;          t /= CDF_TIME_PREC;
         tm.tm_sec = (int)(t % 60);          tm.tm_sec = (int)(t % 60);
@@ -117,7 +117,7 @@@@ -117,7 +118,7 @@
         tm.tm_hour = (int)(t % 24);          tm.tm_hour = (int)(t % 24);
         t /= 24;          t /= 24;
     
Line 876  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c Line 998  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
         tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));          tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
     
         rdays = cdf_getdays(tm.tm_year);          rdays = cdf_getdays(tm.tm_year);
@@ -144,7 +144,7 @@@@ -144,7 +145,7 @@
     
  int   int
  /*ARGSUSED*/   /*ARGSUSED*/
Line 885  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c Line 1007  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
  {   {
  #ifndef __lint__   #ifndef __lint__
         (void)&t;          (void)&t;
@@ -156,7 +156,7 @@@@ -156,7 +157,7 @@
                 errno = EINVAL;                  errno = EINVAL;
                 return -1;                  return -1;
         }          }
Line 894  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c Line 1016  diff -u libmagic.orig/cdf_time.c libmagic/cdf_time.c
         *t = tm.tm_sec;          *t = tm.tm_sec;
         *t += tm.tm_min * 60;          *t += tm.tm_min * 60;
         *t += tm.tm_hour * 60 * 60;          *t += tm.tm_hour * 60 * 60;
@@ -182,7 +182,7 @@@@ -180,7 +181,7 @@
  int   int
  main(int argc, char *argv[])   main(int argc, char *argv[])
  {   {
 -       struct timespec ts;  -       struct timespec ts;
 +       struct timeval ts;  +       struct timeval ts;
           char buf[25];
         static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;          static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
         static const char *ref = "Sat Apr 23 01:30:00 1977";          static const char *ref = "Sat Apr 23 01:30:00 1977";
         char *p, *q;  
 diff -u libmagic.orig/compress.c libmagic/compress.c  diff -u libmagic.orig/compress.c libmagic/compress.c
--- libmagic.orig/compress.c    2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/compress.c    2013-01-06 21:35:43.000000000 +0100
+++ libmagic/compress.c 2012-03-28 01:53:04.299331601 +0200+++ libmagic/compress.c 2013-04-08 15:42:57.328298809 +0200
 @@ -32,6 +32,7 @@  @@ -32,6 +32,7 @@
   *     uncompress(method, old, n, newch) - uncompress old into new,     *     uncompress(method, old, n, newch) - uncompress old into new, 
   *                                         using method, return sizeof new    *                                         using method, return sizeof new
Line 968  diff -u libmagic.orig/compress.c libmagic/compress.c Line 1090  diff -u libmagic.orig/compress.c libmagic/compress.c
         ms->flags |= MAGIC_COMPRESS;          ms->flags |= MAGIC_COMPRESS;
         return rv;          return rv;
  }   }
@@ -168,12 +176,9 @@@@ -168,7 +176,7 @@
   * `safe' read for sockets and pipes.    * `safe' read for sockets and pipes.
   */    */
  protected ssize_t   protected ssize_t
-sread(int fd, void *buf, size_t n, int canbepipe __attribute__ ((unused)))-sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
 +sread(int fd, void *buf, size_t n, int canbepipe)  +sread(int fd, void *buf, size_t n, int canbepipe)
  {   {
         ssize_t rv;          ssize_t rv;
 -#ifdef FD_ZERO  
 -       ssize_t cnt;  
 -#endif  
  #ifdef FIONREAD   #ifdef FIONREAD
        int t = 0;@@ -216,7 +224,7 @@
 #endif 
@@ -185,6 +190,7 @@ nocheck:
 #ifdef FIONREAD        do
        if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {-               switch ((rv = read(fd, buf, n))) {
 #ifdef FD_ZERO+               switch ((rv = FINFO_READ_FUNC(fd, buf, n))) {
+               int cnt;                case -1:
                for (cnt = 0;; cnt++) {                        if (errno == EINTR)
                        fd_set check;                                continue;
                        struct timeval tout = {0, 100 * 1000};@@ -293,13 +301,14 @@
@@ -301,7 +307,8 @@                return -1;
         }          }
           (void)close(tfd);
   -       if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
   +       if (FINFO_LSEEK_FUNC(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
                   file_badseek(ms);
                   return -1;
           }
         return fd;          return fd;
  }   }
 -#if HAVE_FORK  -#if HAVE_FORK
Line 1000  diff -u libmagic.orig/compress.c libmagic/compress.c Line 1125  diff -u libmagic.orig/compress.c libmagic/compress.c
  #ifdef BUILTIN_DECOMPRESS   #ifdef BUILTIN_DECOMPRESS
     
  #define FHCRC          (1 << 1)   #define FHCRC          (1 << 1)
@@ -338,9 +345,7 @@@@ -336,7 +345,7 @@
     
         if (data_start >= n)          if (data_start >= n)
                 return 0;                  return 0;
 -       if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {  -       if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
-               return 0;+       if ((*newch = CAST(unsigned char *, emalloc(HOWMANY + 1))) == NULL) {
-       }                return 0;
+        *newch = (unsigned char *)emalloc(HOWMANY + 1));        }
                   
        /* XXX: const castaway, via strchr */@@ -397,19 +406,16 @@
        z.next_in = (Bytef *)strchr((const char *)old + data_start,        case 0: /* child */
@@ -465,20 +470,14 @@                (void) close(0);
                 if (fd != -1) {
 -                   if (dup(fd) == -1)
 -                       _exit(1);
 -                   (void) lseek(0, (off_t)0, SEEK_SET);
 +                   (void) dup(fd);
 +                   (void) FINFO_LSEEK_FUNC(0, (off_t)0, SEEK_SET);
                 } else {
 -                   if (dup(fdin[0]) == -1)
 -                       _exit(1);
 +                   (void) dup(fdin[0]);
                     (void) close(fdin[0]);
                     (void) close(fdin[1]);
                 }
  
                 (void) close(1);
 -               if (dup(fdout[1]) == -1)
 -                       _exit(1);
 +               (void) dup(fdout[1]);
                 (void) close(fdout[0]);
                 (void) close(fdout[1]);
  #ifndef DEBUG
 @@ -466,20 +472,14 @@
                         fdin[1] = -1;                          fdin[1] = -1;
                 }                  }
     
Line 1035  diff -u libmagic.orig/compress.c libmagic/compress.c Line 1182  diff -u libmagic.orig/compress.c libmagic/compress.c
                         n = 0;                          n = 0;
                         newch[0] = '\0';                          newch[0] = '\0';
                         goto err;                          goto err;
@@ -502,4 +501,4 @@@@ -503,4 +503,4 @@
                 return n;                  return n;
         }          }
  }   }
 -#endif  -#endif
 +#endif /* if PHP_FILEINFO_UNCOMPRESS */  +#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  diff -u libmagic.orig/file.h libmagic/file.h
--- libmagic.orig/file.h        2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/file.h        2013-02-18 16:40:59.000000000 +0100
+++ libmagic/file.h     2012-03-28 01:53:04.304322598 +0200+++ libmagic/file.h     2013-04-08 15:42:57.328298809 +0200
 @@ -33,11 +33,9 @@  @@ -33,11 +33,9 @@
  #ifndef __file_h__   #ifndef __file_h__
  #define __file_h__   #define __file_h__
Line 1060  diff -u libmagic.orig/file.h libmagic/file.h Line 1205  diff -u libmagic.orig/file.h libmagic/file.h
    #ifdef _WIN64     #ifdef _WIN64
      #define SIZE_T_FORMAT "I64"       #define SIZE_T_FORMAT "I64"
    #else     #else
@@ -61,9 +59,20 @@@@ -61,10 +59,20 @@
  #ifdef HAVE_INTTYPES_H   #ifdef HAVE_INTTYPES_H
  #include <inttypes.h>   #include <inttypes.h>
  #endif   #endif
 -#include <regex.h>  -#include <regex.h>
   -#include <time.h>
 +#ifdef PHP_WIN32  +#ifdef PHP_WIN32
 +#include "win32/php_stdint.h"  +#include "win32/php_stdint.h"
 +#endif  +#endif
Line 1082  diff -u libmagic.orig/file.h libmagic/file.h Line 1228  diff -u libmagic.orig/file.h libmagic/file.h
  /* Do this here and now, because struct stat gets re-defined on solaris */   /* Do this here and now, because struct stat gets re-defined on solaris */
  #include <sys/stat.h>   #include <sys/stat.h>
  #include <stdarg.h>   #include <stdarg.h>
@@ -74,7 +83,7 @@@@ -75,7 +83,7 @@
  #define MAGIC "/etc/magic"   #define MAGIC "/etc/magic"
  #endif   #endif
     
Line 1091  diff -u libmagic.orig/file.h libmagic/file.h Line 1237  diff -u libmagic.orig/file.h libmagic/file.h
  #define PATHSEP        ';'   #define PATHSEP        ';'
  #else   #else
  #define PATHSEP        ':'   #define PATHSEP        ':'
@@ -100,12 +109,6 @@@@ -109,12 +117,6 @@
  #endif   #endif
  #endif   #endif
     
Line 1104  diff -u libmagic.orig/file.h libmagic/file.h Line 1250  diff -u libmagic.orig/file.h libmagic/file.h
  #ifndef MIN   #ifndef MIN
  #define        MIN(a,b)        (((a) < (b)) ? (a) : (b))   #define        MIN(a,b)        (((a) < (b)) ? (a) : (b))
  #endif   #endif
@@ -209,7 +212,7 @@@@ -224,7 +226,7 @@
 #define                                FILE_INDIRECT        41 #define                                FILE_USE     46
 #define                                FILE_NAMES_SIZE 42/* size of array to contain all names */ #define                                FILE_NAMES_SIZE 47 /* size of array to contain all names */
     
 -#define IS_STRING(t) \  -#define IS_STRING(t) \
 +#define IS_LIBMAGIC_STRING(t) \  +#define IS_LIBMAGIC_STRING(t) \
         ((t) == FILE_STRING || \          ((t) == FILE_STRING || \
          (t) == FILE_PSTRING || \           (t) == FILE_PSTRING || \
          (t) == FILE_BESTRING16 || \           (t) == FILE_BESTRING16 || \
@@ -231,7 +234,7 @@@@ -248,7 +250,7 @@
  #ifdef ENABLE_CONDITIONALS   #ifdef ENABLE_CONDITIONALS
         uint8_t cond;           /* conditional type */          uint8_t cond;           /* conditional type */
  #else   #else
Line 1122  diff -u libmagic.orig/file.h libmagic/file.h Line 1268  diff -u libmagic.orig/file.h libmagic/file.h
  #endif   #endif
         uint8_t factor_op;          uint8_t factor_op;
  #define                FILE_FACTOR_OP_PLUS     '+'   #define                FILE_FACTOR_OP_PLUS     '+'
@@ -387,21 +390,18 @@@@ -347,7 +349,7 @@
  /* list of magic entries */
 struct stat; struct mlist {
 protected const char *file_fmttime(uint32_t, int);        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 @@
  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 *);
 -protected int file_buffer(struct magic_set *, int, const char *, const void *,  -protected int file_buffer(struct magic_set *, int, const char *, const void *,
 +protected int file_buffer(struct magic_set *, php_stream *, const char *, const void *,  +protected int file_buffer(struct magic_set *, php_stream *, const char *, const void *,
      size_t);       size_t);
 -protected int file_fsmagic(struct magic_set *, const char *, struct stat *);  -protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
+protected int file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream);+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_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);
 -protected size_t file_printedlen(const struct magic_set *);  -protected size_t file_printedlen(const struct magic_set *);
Line 1148  diff -u libmagic.orig/file.h libmagic/file.h Line 1303  diff -u libmagic.orig/file.h libmagic/file.h
  protected int file_zmagic(struct magic_set *, int, const char *,   protected int file_zmagic(struct magic_set *, int, const char *,
      const unsigned char *, size_t);       const unsigned char *, size_t);
  #endif   #endif
@@ -422,13 +422,9 @@@@ -443,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);
 +protected void file_delmagic(struct magic *, int type, size_t entries);
  protected void file_badread(struct magic_set *);   protected void file_badread(struct magic_set *);
  protected void file_badseek(struct magic_set *);   protected void file_badseek(struct magic_set *);
  protected void file_oomem(struct magic_set *, size_t);   protected void file_oomem(struct magic_set *, size_t);
Line 1165  diff -u libmagic.orig/file.h libmagic/file.h Line 1324  diff -u libmagic.orig/file.h libmagic/file.h
  protected void file_showstr(FILE *, const char *, size_t);   protected void file_showstr(FILE *, const char *, size_t);
  protected size_t file_mbswidth(const char *);   protected size_t file_mbswidth(const char *);
  protected const char *file_getbuffer(struct magic_set *);   protected const char *file_getbuffer(struct magic_set *);
@@ -443,11 +439,8 @@@@ -462,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 *);
 +protected size_t file_printedlen(const struct magic_set *ms);
  #ifdef __EMX__
  protected int file_os2_apptype(struct magic_set *, const char *, const void *,
      size_t);       size_t);
  #endif /* __EMX__ */   #endif /* __EMX__ */
     
Line 1177  diff -u libmagic.orig/file.h libmagic/file.h Line 1342  diff -u libmagic.orig/file.h libmagic/file.h
     
  #ifndef HAVE_STRERROR   #ifndef HAVE_STRERROR
  extern int sys_nerr;   extern int sys_nerr;
@@ -460,17 +453,10 @@@@ -484,32 +478,16 @@
  #define strtoul(a, b, c)       strtol(a, b, c)   #define strtoul(a, b, c)       strtol(a, b, c)
  #endif   #endif
     
   -#ifndef HAVE_PREAD
   -ssize_t pread(int, void *, size_t, off_t);
   -#endif
 -#ifndef HAVE_VASPRINTF  -#ifndef HAVE_VASPRINTF
 -int vasprintf(char **, const char *, va_list);  -int vasprintf(char **, const char *, va_list);
 -#endif  -#endif
Line 1197  diff -u libmagic.orig/file.h libmagic/file.h Line 1365  diff -u libmagic.orig/file.h libmagic/file.h
  size_t strlcat(char *dst, const char *src, size_t siz);   size_t strlcat(char *dst, const char *src, size_t siz);
  #endif   #endif
  #ifndef HAVE_GETLINE   #ifndef HAVE_GETLINE
Only in libmagic.orig: file_opts.h 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
 @@ -533,4 +511,12 @@
  #define FILE_RCSID(id)
  #endif
  
 +#ifdef PHP_WIN32
 +#define FINFO_LSEEK_FUNC _lseek
 +#define FINFO_READ_FUNC _read
 +#else
 +#define FINFO_LSEEK_FUNC lseek
 +#define FINFO_READ_FUNC read
 +#endif
 +
  #endif /* __file_h__ */
 diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
--- libmagic.orig/fsmagic.c     2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/fsmagic.c     2013-03-21 18:45:14.000000000 +0100
+++ libmagic/fsmagic.c  2012-03-28 01:53:04.304322598 +0200+++ libmagic/fsmagic.c  2013-04-08 15:42:57.328298809 +0200
 @@ -59,27 +59,21 @@  @@ -59,27 +59,21 @@
  # define minor(dev)  ((dev) & 0xff)   # define minor(dev)  ((dev) & 0xff)
  #endif   #endif
Line 1229  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1421  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +#ifdef PHP_WIN32  +#ifdef PHP_WIN32
 +  +
 +# undef S_IFIFO  +# undef S_IFIFO
 #endif+#endif
 +  +
 +  +
 +#ifndef S_ISDIR  +#ifndef S_ISDIR
Line 1238  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1430  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 +  +
 +#ifndef S_ISREG  +#ifndef S_ISREG
 +#define S_ISREG(mode) ((mode) & _S_IFREG)  +#define S_ISREG(mode) ((mode) & _S_IFREG)
+#endif #endif
 +  +
  private int   private int
  handle_mime(struct magic_set *ms, int mime, const char *str)   handle_mime(struct magic_set *ms, int mime, const char *str)
  {   {
@@ -96,42 +90,36 @@@@ -96,49 +90,45 @@
  }   }
     
  protected int   protected int
 -file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)  -file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
 +file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream)  +file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream)
  {   {
-        int ret = 0;        int ret, did = 0;
         int mime = ms->flags & MAGIC_MIME;          int mime = ms->flags & MAGIC_MIME;
 -#ifdef S_IFLNK  -#ifdef S_IFLNK
 -       char buf[BUFSIZ+4];  -       char buf[BUFSIZ+4];
Line 1263  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1455  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
                 return 0;                  return 0;
 -       if (fn == NULL)  -       if (fn == NULL)
 +  +
+       if (!fn && !stream) {+       if (fn == NULL && !stream) {
                 return 0;                  return 0;
 +       }  +       }
     
    #define COMMA  (did++ ? ", " : "")
 -       /*  -       /*
 -        * Fstat is cheaper but fails for files you don't have read perms on.  -        * Fstat is cheaper but fails for files you don't have read perms on.
 -        * On 4.2BSD and similar systems, use lstat() to identify symlinks.  -        * On 4.2BSD and similar systems, use lstat() to identify symlinks.
Line 1282  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1475  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -               if (ms->flags & MAGIC_ERROR) {  -               if (ms->flags & MAGIC_ERROR) {
 -                       file_error(ms, errno, "cannot stat `%s'", fn);  -                       file_error(ms, errno, "cannot stat `%s'", fn);
 -                       return -1;  -                       return -1;
   +
 +       if (stream) {  +       if (stream) {
 +               php_stream_statbuf ssb;  +               php_stream_statbuf ssb;
 +               if (php_stream_stat(stream, &ssb) < 0) {  +               if (php_stream_stat(stream, &ssb) < 0) {
Line 1307  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1501  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -               return -1;  -               return -1;
         }          }
     
           ret = 1;
         if (!mime) {          if (!mime) {
@@ -153,77 +141,42 @@ #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 @@
         }          }
                   
         switch (sb->st_mode & S_IFMT) {          switch (sb->st_mode & S_IFMT) {
Line 1316  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1517  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -               if (mime) {  -               if (mime) {
 -                       if (handle_mime(ms, mime, "directory") == -1)  -                       if (handle_mime(ms, mime, "directory") == -1)
 -                               return -1;  -                               return -1;
-               } else if (file_printf(ms, "directory") == -1)-               } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
 -                       return -1;  -                       return -1;
-               return 1;-               break;
 -#ifdef S_IFCHR  -#ifdef S_IFCHR
 -       case S_IFCHR:  -       case S_IFCHR:
 -               /*   -               /* 
Line 1326  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1527  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -                * like ordinary files.  Otherwise, just report that they  -                * like ordinary files.  Otherwise, just report that they
 -                * are block special files and go on to the next file.  -                * are block special files and go on to the next file.
 -                */  -                */
-               if ((ms->flags & MAGIC_DEVICES) != 0)-               if ((ms->flags & MAGIC_DEVICES) != 0) {
 -                       ret = 0;
 -                       break;  -                       break;
   -               }
 -               if (mime) {  -               if (mime) {
 -                       if (handle_mime(ms, mime, "chardevice") == -1)  -                       if (handle_mime(ms, mime, "chardevice") == -1)
 -                               return -1;  -                               return -1;
 -               } else {  -               } else {
 -#ifdef HAVE_STAT_ST_RDEV  -#ifdef HAVE_STAT_ST_RDEV
 -# ifdef dv_unit  -# ifdef dv_unit
-                        if (file_printf(ms, "character special (%d/%d/%d)",+#ifndef PHP_WIN32
-                           major(sb->st_rdev), dv_unit(sb->st_rdev),+# ifdef S_IFCHR
 +               case S_IFCHR:
 +                       /* 
 +                        * If -s has been specified, treat character special files
 +                        * like ordinary files.  Otherwise, just report that they
 +                        * are block special files and go on to the next file.
 +                        */
 +                       if ((ms->flags & MAGIC_DEVICES) != 0) {
 +                               ret = 0;
 +                               break;
 +                       }
 +                       if (mime) {
 +                               if (handle_mime(ms, mime, "x-character-device") == -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)  -                                       dv_subunit(sb->st_rdev)) == -1)
 -                               return -1;  -                               return -1;
 -# else  -# else
-                        if (file_printf(ms, "character special (%ld/%ld)",+                                               dv_subunit(sb->st_rdev)) == -1)
-                           (long)major(sb->st_rdev), (long)minor(sb->st_rdev))+                                       return -1;
-                           == -1)+#   else
                         if (file_printf(ms, "%scharacter special (%ld/%ld)",
                             COMMA, (long)major(sb->st_rdev),
                             (long)minor(sb->st_rdev)) == -1)
 -                               return -1;  -                               return -1;
 -# endif  -# endif
 -#else  -#else
-                        if (file_printf(ms, "character special") == -1)+                                       return -1;
 +#   endif
 +#  else
                         if (file_printf(ms, "%scharacter special", COMMA) == -1)
 -                               return -1;  -                               return -1;
 -#endif  -#endif
 -               }  -               }
-               return 1;-               break;
 -#endif  -#endif
 -#ifdef S_IFBLK  -#ifdef S_IFBLK
 -       case S_IFBLK:  -       case S_IFBLK:
Line 1358  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1585  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -                * like ordinary files.  Otherwise, just report that they  -                * like ordinary files.  Otherwise, just report that they
 -                * are block special files and go on to the next file.  -                * are block special files and go on to the next file.
 -                */  -                */
-               if ((ms->flags & MAGIC_DEVICES) != 0)-               if ((ms->flags & MAGIC_DEVICES) != 0) {
 -                       ret = 0;
 -                       break;  -                       break;
   -               }
 -               if (mime) {  -               if (mime) {
 -                       if (handle_mime(ms, mime, "blockdevice") == -1)  -                       if (handle_mime(ms, mime, "blockdevice") == -1)
 -                               return -1;  -                               return -1;
 -               } else {  -               } else {
 -#ifdef HAVE_STAT_ST_RDEV  -#ifdef HAVE_STAT_ST_RDEV
 -# ifdef dv_unit  -# ifdef dv_unit
-                       if (file_printf(ms, "block special (%d/%d/%d)",-                       if (file_printf(ms, "%sblock special (%d/%d/%d)",
-                                       major(sb->st_rdev), dv_unit(sb->st_rdev),-                           COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
-                                       dv_subunit(sb->st_rdev)) == -1)-                           dv_subunit(sb->st_rdev)) == -1)
 -                               return -1;  -                               return -1;
 -# else  -# else
-                       if (file_printf(ms, "block special (%ld/%ld)",-                       if (file_printf(ms, "%sblock special (%ld/%ld)",
-                                       (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)-                           COMMA, (long)major(sb->st_rdev),
 -                           (long)minor(sb->st_rdev)) == -1)
 -                               return -1;  -                               return -1;
 +#ifndef PHP_WIN32  
 +# ifdef S_IFCHR  
 +               case S_IFCHR:  
 +                       /*   
 +                        * If -s has been specified, treat character special files  
 +                        * like ordinary files.  Otherwise, just report that they  
 +                        * are block special files and go on to the next file.  
 +                        */  
 +                       if ((ms->flags & MAGIC_DEVICES) != 0) {  
 +                               break;  
 +                       }  
 +                       if (mime) {  
 +                               if (handle_mime(ms, mime, "x-character-device") == -1)  
 +                                       return -1;  +                                       return -1;
 +                       } else {  
 +#  ifdef HAVE_STAT_ST_RDEV  
 +#   ifdef dv_unit  
 +                               if (file_printf(ms, "character special (%d/%d/%d)",  
 +                                   major(sb->st_rdev), dv_unit(sb->st_rdev),  
 +                                               dv_subunit(sb->st_rdev)) == -1)  
 +                                       return -1;  
 +#   else  
 +                               if (file_printf(ms, "character special (%ld/%ld)",  
 +                                   (long)major(sb->st_rdev), (long)minor(sb->st_rdev))  
 +                                   == -1)  
 +                                       return -1;  
 +#   endif  
 +#  else  
 +                               if (file_printf(ms, "character special") == -1)  
 +                                       return -1;  
 +#  endif  +#  endif
 +                       }  +                       }
 +                       return 1;  +                       return 1;
  # endif   # endif
 -#else  -#else
-                       if (file_printf(ms, "block special") == -1)-                       if (file_printf(ms, "%sblock special", COMMA) == -1)
 -                               return -1;  -                               return -1;
 -#endif  
 -               }  
 -               return 1;  
  #endif   #endif
   -               }
   -               break;
   -#endif
 -       /* TODO add code to handle V7 MUX and Blit MUX files */  -       /* TODO add code to handle V7 MUX and Blit MUX files */
 +  +
  #ifdef S_IFIFO   #ifdef S_IFIFO
         case S_IFIFO:          case S_IFIFO:
                 if((ms->flags & MAGIC_DEVICES) != 0)                  if((ms->flags & MAGIC_DEVICES) != 0)
@@ -246,77 +199,14 @@@@ -253,79 +204,14 @@
  #endif   #endif
  #ifdef S_IFLNK   #ifdef S_IFLNK
         case S_IFLNK:          case S_IFLNK:
Line 1436  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1637  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -                               if (handle_mime(ms, mime, "symlink") == -1)  -                               if (handle_mime(ms, mime, "symlink") == -1)
 -                                       return -1;  -                                       return -1;
 -                       } else if (file_printf(ms,  -                       } else if (file_printf(ms,
-                           "unreadable symlink `%s' (%s)", fn,-                           "%sunreadable symlink `%s' (%s)", COMMA, fn,
 -                           strerror(errno)) == -1)  -                           strerror(errno)) == -1)
 -                               return -1;  -                               return -1;
-                       return 1;-                       break;
 -               }  -               }
 -               buf[nch] = '\0';        /* readlink(2) does not do this */  -               buf[nch] = '\0';        /* readlink(2) does not do this */
 -  -
Line 1465  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1666  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -                                                   "x-path-too-long") == -1)  -                                                   "x-path-too-long") == -1)
 -                                                       return -1;  -                                                       return -1;
 -                                       } else if (file_printf(ms,  -                                       } else if (file_printf(ms,
-                                           "path too long: `%s'", fn) == -1)-                                           "%spath too long: `%s'", COMMA,
 -                                            fn) == -1)
 -                                               return -1;  -                                               return -1;
-                                       return 1;-                                       break;
 -                               }  -                               }
 -                               /* take dir part */  -                               /* take dir part */
 -                               (void)strlcpy(buf2, fn, sizeof buf2);  -                               (void)strlcpy(buf2, fn, sizeof buf2);
Line 1486  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c Line 1688  diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
 -                       ms->flags &= MAGIC_SYMLINK;  -                       ms->flags &= MAGIC_SYMLINK;
 -                       p = magic_file(ms, buf);  -                       p = magic_file(ms, buf);
 -                       ms->flags |= MAGIC_SYMLINK;  -                       ms->flags |= MAGIC_SYMLINK;
-                        return p != NULL ? 1 : -1;-                        if (p == NULL)
 -                               return -1;
 -               } else { /* just print what it points to */  -               } else { /* just print what it points to */
 -                       if (mime) {  -                       if (mime) {
 -                               if (handle_mime(ms, mime, "symlink") == -1)  -                               if (handle_mime(ms, mime, "symlink") == -1)
 -                                       return -1;  -                                       return -1;
-                       } else if (file_printf(ms, "symbolic link to `%s'",-                       } else if (file_printf(ms, "%ssymbolic link to `%s'",
-                           buf) == -1)-                           COMMA, buf) == -1)
 -                               return -1;  -                               return -1;
 -               }  -               }
-               return 1;-               break;
 +       return 1;  +       return 1;
  #endif   #endif
 +  +
  #ifdef S_IFSOCK   #ifdef S_IFSOCK
  #ifndef __COHERENT__   #ifndef __COHERENT__
         case S_IFSOCK:          case S_IFSOCK:
@@ -328,12 +218,14 @@@@ -337,27 +223,27 @@
                return 1;                break;
  #endif   #endif
  #endif   #endif
 -       case S_IFREG:  -       case S_IFREG:
-               break;-               /*
-       default:-                * regular file, check next possibility
-               file_error(ms, 0, "invalid mode 0%o", sb->st_mode);-                *
-               return -1;-                * If stat() tells us the file has zero length, report here that
-               /*NOTREACHED*/-                * the file is empty, so we can skip all the work of opening and
+-                * reading the file.
 +               case S_IFREG:  +               case S_IFREG:
+                       break;+       /*
++        * regular file, check next possibility
+               default:+        *
+                        file_error(ms, 0, "invalid mode 0%o", sb->st_mode);+        * 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
                  * 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.)
 -                */
 -               if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
 -                       if (mime) {
 -                               if (handle_mime(ms, mime, "x-empty") == -1)
 -                                       return -1;
 -                       } else if (file_printf(ms, "%sempty", COMMA) == -1)
 +        */
 +       if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
 +               if (mime) {
 +                       if (handle_mime(ms, mime, "x-empty") == -1)
                                 return -1;
 +                       } else if (file_printf(ms, "%sempty", COMMA) == -1)
 +                       return -1;  +                       return -1;
+                        /*NOTREACHED*/                        break;
 -               }
 +       }
                 ret = 0;
                 break;
  
 @@ -367,9 +253,5 @@
                 /*NOTREACHED*/
         }          }
     
        /*-       if (!mime && did) {
 -           if (file_printf(ms, " ") == -1)
 -                   return -1;
 -       }
         return ret;
  }
 diff -u libmagic.orig/funcs.c libmagic/funcs.c  diff -u libmagic.orig/funcs.c libmagic/funcs.c
--- libmagic.orig/funcs.c       2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/funcs.c       2012-10-31 18:03:01.000000000 +0100
+++ libmagic/funcs.c    2012-03-28 11:49:56.089607390 +0200+++ libmagic/funcs.c    2013-04-08 15:42:57.328298809 +0200
 @@ -41,52 +41,42 @@  @@ -41,52 +41,42 @@
  #if defined(HAVE_WCTYPE_H)   #if defined(HAVE_WCTYPE_H)
  #include <wctype.h>   #include <wctype.h>
Line 1648  diff -u libmagic.orig/funcs.c libmagic/funcs.c Line 1882  diff -u libmagic.orig/funcs.c libmagic/funcs.c
  {   {
         int m = 0, rv = 0, looks_text = 0;          int m = 0, rv = 0, looks_text = 0;
         int mime = ms->flags & MAGIC_MIME;          int mime = ms->flags & MAGIC_MIME;
 @@ -189,7 +191,7 @@  
                     &code, &code_mime, &type);  
         }  
    
 -#ifdef __EMX__  
 +#if defined(__EMX__)  
         if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {  
                 switch (file_os2_apptype(ms, inname, buf, nb)) {  
                 case -1:  
 @@ -201,10 +203,10 @@  @@ -201,10 +203,10 @@
                 }                  }
         }          }
Line 1716  diff -u libmagic.orig/funcs.c libmagic/funcs.c Line 1941  diff -u libmagic.orig/funcs.c libmagic/funcs.c
                 ms->o.pbuf = NULL;                  ms->o.pbuf = NULL;
         }          }
         ms->event_flags &= ~EVENT_HAD_ERR;          ms->event_flags &= ~EVENT_HAD_ERR;
@@ -344,14 +350,10 @@@@ -329,7 +335,7 @@
  protected const char *
  file_getbuffer(struct magic_set *ms)
  {
 -       char *pbuf, *op, *np;
 +       char *op, *np;
         size_t psize, len;
  
         if (ms->event_flags & EVENT_HAD_ERR)
 @@ -344,15 +350,13 @@
         /* * 4 is for octal representation, + 1 is for NUL */          /* * 4 is for octal representation, + 1 is for NUL */
         len = strlen(ms->o.buf);          len = strlen(ms->o.buf);
         if (len > (SIZE_MAX - 1) / 4) {          if (len > (SIZE_MAX - 1) / 4) {
Line 1725  diff -u libmagic.orig/funcs.c libmagic/funcs.c Line 1959  diff -u libmagic.orig/funcs.c libmagic/funcs.c
         }          }
         psize = len * 4 + 1;          psize = len * 4 + 1;
 -       if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {  -       if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
-               file_oomem(ms, psize);+       if ((ms->o.pbuf = CAST(char *, erealloc(ms->o.pbuf, psize))) == NULL) {
-               return NULL;                file_oomem(ms, psize);
-       }                return NULL;
+        pbuf = erealloc(ms->o.pbuf, psize);        }
        ms->o.pbuf = pbuf;-        ms->o.pbuf = pbuf;
     
  #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)   #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
@@ -411,13 +413,7 @@        {
 @@ -412,8 +416,8 @@
         if (level >= ms->c.len) {          if (level >= ms->c.len) {
                 len = (ms->c.len += 20) * sizeof(*ms->c.li);                  len = (ms->c.len += 20) * sizeof(*ms->c.li);
-                ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?                ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
 -                   malloc(len) :  -                   malloc(len) :
 -                   realloc(ms->c.li, len));  -                   realloc(ms->c.li, len));
-               if (ms->c.li == NULL) {+                   emalloc(len) :
-                       file_oomem(ms, len);+                   erealloc(ms->c.li, len));
-                       return -1;                if (ms->c.li == NULL) {
-               }                        file_oomem(ms, len);
+               ms->c.li = (ms->c.li == NULL) ? emalloc(len) : erealloc(ms->c.li, len);                        return -1;
        }@@ -433,29 +437,47 @@
        ms->c.li[level].got_match = 0; 
 #ifdef ENABLE_CONDITIONALS 
@@ -433,29 +429,51 @@ 
         return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);          return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
  }   }
     
Line 1763  diff -u libmagic.orig/funcs.c libmagic/funcs.c Line 1994  diff -u libmagic.orig/funcs.c libmagic/funcs.c
 +       pcre_cache_entry *pce;  +       pcre_cache_entry *pce;
 +       char *res;  +       char *res;
 +       zval *repl;  +       zval *repl;
+       int res_len, rep_cnt;+       int res_len, rep_cnt = 0;
 +       TSRMLS_FETCH();  +       TSRMLS_FETCH();
 +  +
 +       MAKE_STD_ZVAL(patt);  +       MAKE_STD_ZVAL(patt);
 +       ZVAL_STRINGL(patt, pat, strlen(pat), 0);  +       ZVAL_STRINGL(patt, pat, strlen(pat), 0);
 +       opts |= PCRE_MULTILINE;  +       opts |= PCRE_MULTILINE;
 +       convert_libmagic_pattern(patt, opts);  +       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) {  +       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);  +               zval_dtor(patt);
 +               FREE_ZVAL(patt);  +               FREE_ZVAL(patt);
 +               return -1;  +               return -1;
Line 1782  diff -u libmagic.orig/funcs.c libmagic/funcs.c Line 2009  diff -u libmagic.orig/funcs.c libmagic/funcs.c
 +  +
 +       MAKE_STD_ZVAL(repl);  +       MAKE_STD_ZVAL(repl);
 +       ZVAL_STRINGL(repl, rep, strlen(rep), 0);  +       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);  -       rc = regcomp(&rx, pat, REG_EXTENDED);
 -       if (rc) {  -       if (rc) {
 -               char errmsg[512];  -               char errmsg[512];
 -               (void)regerror(rc, &rx, errmsg, sizeof(errmsg));  -               (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
 -               file_magerror(ms, "regex error %d, (%s)", rc, 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);  +       FREE_ZVAL(repl);
 +       zval_dtor(patt);  +       zval_dtor(patt);
 +       FREE_ZVAL(patt);  +       FREE_ZVAL(patt);
Line 1819  diff -u libmagic.orig/funcs.c libmagic/funcs.c Line 2046  diff -u libmagic.orig/funcs.c libmagic/funcs.c
 +       return rep_cnt;  +       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  diff -u libmagic.orig/magic.c libmagic/magic.c
--- libmagic.orig/magic.c       2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/magic.c       2013-01-11 17:43:09.000000000 +0100
+++ libmagic/magic.c    2012-03-28 11:42:50.404750579 +0200+++ libmagic/magic.c    2013-04-27 13:53:32.175250261 +0200
 @@ -25,11 +25,6 @@  @@ -25,11 +25,6 @@
   * SUCH DAMAGE.    * SUCH DAMAGE.
   */    */
Line 1866  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2090  diff -u libmagic.orig/magic.c libmagic/magic.c
  #if defined(HAVE_UTIMES)   #if defined(HAVE_UTIMES)
  # include <sys/time.h>   # include <sys/time.h>
  #elif defined(HAVE_UTIME)   #elif defined(HAVE_UTIME)
@@ -71,19 +75,24 @@@@ -71,18 +75,25 @@
  #endif   #endif
  #endif   #endif
     
Line 1875  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2099  diff -u libmagic.orig/magic.c libmagic/magic.c
 +# undef S_IFIFO  +# undef S_IFIFO
 +#endif  +#endif
 +  +
  private void free_mlist(struct mlist *);  
  private void close_and_restore(const struct magic_set *, const char *, int,   private void close_and_restore(const struct magic_set *, const char *, int,
      const struct stat *);       const struct stat *);
  private int unreadable_info(struct magic_set *, mode_t, const char *);   private int unreadable_info(struct magic_set *, mode_t, const char *);
   +#if 0
  private const char* get_default_magic(void);   private const char* get_default_magic(void);
 -#ifndef COMPILE_ONLY  -#ifndef COMPILE_ONLY
 -private const char *file_or_fd(struct magic_set *, const char *, int);  -private const char *file_or_fd(struct magic_set *, const char *, int);
-#endif #endif
 +private const char *file_or_stream(struct magic_set *, const char *, php_stream *);  +private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
     
  #ifndef        STDIN_FILENO   #ifndef        STDIN_FILENO
Line 1894  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2118  diff -u libmagic.orig/magic.c libmagic/magic.c
  private const char *   private const char *
  get_default_magic(void)   get_default_magic(void)
  {   {
@@ -91,7 +100,7 @@@@ -90,7 +101,7 @@
         static char *default_magic;          static char *default_magic;
         char *home, *hmagicpath;          char *home, *hmagicpath;
     
Line 1903  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2127  diff -u libmagic.orig/magic.c libmagic/magic.c
         struct stat st;          struct stat st;
     
         if (default_magic) {          if (default_magic) {
@@ -124,6 +133,7 @@@@ -104,17 +115,17 @@
                 return MAGIC;
         if (stat(hmagicpath, &st) == -1) {
                 free(hmagicpath);
 -               if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
 +       if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
 +               return MAGIC;
 +       if (stat(hmagicpath, &st) == -1)
 +               goto out;
 +       if (S_ISDIR(st.st_mode)) {
 +               free(hmagicpath);
 +               if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
                         return MAGIC;
 -               if (stat(hmagicpath, &st) == -1)
 +               if (access(hmagicpath, R_OK) == -1)
                         goto out;
 -               if (S_ISDIR(st.st_mode)) {
 -                       free(hmagicpath);
 -                       if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
 -                               return MAGIC;
 -                       if (access(hmagicpath, R_OK) == -1)
 -                               goto out;
 -               }
 +       }
         }
  
         if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
 @@ -128,6 +139,7 @@
  #else   #else
         char *hmagicp = hmagicpath;          char *hmagicp = hmagicpath;
         char *tmppath = NULL;          char *tmppath = NULL;
Line 1911  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2162  diff -u libmagic.orig/magic.c libmagic/magic.c
     
  #define APPENDPATH() \   #define APPENDPATH() \
         do { \          do { \
@@ -168,7 +178,7 @@@@ -172,7 +184,7 @@
         }          }
     
         /* Third, try to get magic file relative to dll location */          /* Third, try to get magic file relative to dll location */
Line 1920  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2171  diff -u libmagic.orig/magic.c libmagic/magic.c
         dllpath[MAX_PATH] = 0;  /* just in case long path gets truncated and not null terminated */          dllpath[MAX_PATH] = 0;  /* just in case long path gets truncated and not null terminated */
         if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){          if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){
                 PathRemoveFileSpecA(dllpath);                  PathRemoveFileSpecA(dllpath);
@@ -206,16 +216,14 @@@@ -210,6 +222,7 @@
     
         return action == FILE_LOAD ? get_default_magic() : MAGIC;          return action == FILE_LOAD ? get_default_magic() : MAGIC;
  }   }
Line 1928  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2179  diff -u libmagic.orig/magic.c libmagic/magic.c
     
  public struct magic_set *   public struct magic_set *
  magic_open(int flags)   magic_open(int flags)
   @@ -250,7 +263,7 @@
    magic_load(struct magic_set *ms, const char *magicfile)
  {   {
        struct magic_set *ms;        if (ms == NULL)
-       size_t len;-               return -1;
 +       return -1;
-        if ((ms = CAST(struct magic_set *, calloc((size_t)1,        return file_apprentice(ms, magicfile, FILE_LOAD);
-           sizeof(struct magic_set)))) == NULL) 
-               return NULL; 
+       ms = ecalloc((size_t)1, sizeof(struct magic_set)); 
  
        if (magic_setflags(ms, flags) == -1) { 
                errno = EINVAL; 
@@ -223,11 +231,9 @@ 
        } 
  
        ms->o.buf = ms->o.pbuf = NULL; 
-       len = (ms->c.len = 10) * sizeof(*ms->c.li); 
- 
-       if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL) 
-               goto free; 
  
+       ms->c.li = emalloc((ms->c.len = 10) * sizeof(*ms->c.li)); 
+        
        ms->event_flags = 0; 
        ms->error = -1; 
        ms->mlist = NULL; 
@@ -235,7 +241,7 @@ 
        ms->line = 0; 
        return ms; 
 free: 
-       free(ms); 
+       efree(ms); 
        return NULL; 
  }   }
     
@@ -251,10 +257,10 @@@@ -262,13 +275,6 @@
                struct mlist *next = ml->next;        return file_apprentice(ms, magicfile, FILE_COMPILE);
                struct magic *mg = ml->magic; 
                file_delmagic(mg, ml->mapped, ml->nmagic); 
-               free(ml); 
+               efree(ml); 
                ml = next; 
        } 
-       free(ml); 
+       efree(ml); 
  }   }
     
  private int  
 @@ -278,11 +284,19 @@  
  public void  
  magic_close(struct magic_set *ms)  
  {  
 -       free_mlist(ms->mlist);  
 -       free(ms->o.pbuf);  
 -       free(ms->o.buf);  
 -       free(ms->c.li);  
 -       free(ms);  
 +       if (ms->mlist) {  
 +               free_mlist(ms->mlist);  
 +       }  
 +       if (ms->o.pbuf) {  
 +               efree(ms->o.pbuf);  
 +       }  
 +       if (ms->o.buf) {  
 +               efree(ms->o.buf);  
 +       }  
 +       if (ms->c.li) {  
 +               efree(ms->c.li);  
 +       }  
 +       efree(ms);  
  }  
    
  /*  
 @@ -308,13 +322,6 @@  
         return ml ? 0 : -1;  
  }  
    
 -public int  -public int
 -magic_check(struct magic_set *ms, const char *magicfile)  -magic_check(struct magic_set *ms, const char *magicfile)
 -{  -{
-       struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);-       if (ms == NULL)
-       free_mlist(ml);-               return -1;
-       return ml ? 0 : -1;-       return file_apprentice(ms, magicfile, FILE_CHECK);
 -}  -}
     
  public int   public int
  magic_list(struct magic_set *ms, const char *magicfile)   magic_list(struct magic_set *ms, const char *magicfile)
@@ -328,9 +335,6 @@@@ -282,9 +288,6 @@
  close_and_restore(const struct magic_set *ms, const char *name, int fd,   close_and_restore(const struct magic_set *ms, const char *name, int fd,
      const struct stat *sb)       const struct stat *sb)
  {   {
Line 2024  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2212  diff -u libmagic.orig/magic.c libmagic/magic.c
     
         if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {          if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
                 /*                  /*
@@ -357,7 +361,6 @@@@ -311,7 +314,6 @@
         }          }
  }   }
     
Line 2032  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2220  diff -u libmagic.orig/magic.c libmagic/magic.c
     
  /*   /*
   * find type of descriptor    * find type of descriptor
@@ -365,7 +368,7 @@@@ -321,7 +323,7 @@
 public const char * 
 magic_descriptor(struct magic_set *ms, int fd) 
  {   {
           if (ms == NULL)
                   return NULL;
 -       return file_or_fd(ms, NULL, fd);  -       return file_or_fd(ms, NULL, fd);
 +       return file_or_stream(ms, NULL, NULL);  +       return file_or_stream(ms, NULL, NULL);
  }   }
     
  /*   /*
@@ -374,30 +377,40 @@@@ -332,30 +334,42 @@
 public const char * 
 magic_file(struct magic_set *ms, const char *inname) 
  {   {
           if (ms == NULL)
                   return NULL;
 -       return file_or_fd(ms, inname, STDIN_FILENO);  -       return file_or_fd(ms, inname, STDIN_FILENO);
 +       return file_or_stream(ms, inname, NULL);  +       return file_or_stream(ms, inname, NULL);
 +}  +}
Line 2052  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2240  diff -u libmagic.orig/magic.c libmagic/magic.c
 +public const char *  +public const char *
 +magic_stream(struct magic_set *ms, php_stream *stream)  +magic_stream(struct magic_set *ms, php_stream *stream)
 +{  +{
   +       if (ms == NULL)
   +               return NULL;
 +       return file_or_stream(ms, NULL, stream);  +       return file_or_stream(ms, NULL, stream);
  }   }
     
Line 2088  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2278  diff -u libmagic.orig/magic.c libmagic/magic.c
         case -1:                /* error */          case -1:                /* error */
                 goto done;                  goto done;
         case 0:                 /* nothing found */          case 0:                 /* nothing found */
@@ -407,68 +420,48 @@@@ -365,68 +379,48 @@
                 goto done;                  goto done;
         }          }
     
Line 2183  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2373  diff -u libmagic.orig/magic.c libmagic/magic.c
         return rv == 0 ? file_getbuffer(ms) : NULL;          return rv == 0 ? file_getbuffer(ms) : NULL;
  }   }
     
@@ -480,14 +473,13 @@@@ -440,14 +434,13 @@
                 return NULL;                  return NULL;
         /*          /*
          * The main work is done here!           * The main work is done here!
Line 2201  diff -u libmagic.orig/magic.c libmagic/magic.c Line 2391  diff -u libmagic.orig/magic.c libmagic/magic.c
  public const char *   public const char *
  magic_error(struct magic_set *ms)   magic_error(struct magic_set *ms)
 diff -u libmagic.orig/magic.h libmagic/magic.h  diff -u libmagic.orig/magic.h libmagic/magic.h
--- libmagic.orig/magic.h       2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/magic.h       2013-03-21 18:52:42.000000000 +0100
+++ libmagic/magic.h    2012-03-28 01:53:04.304322598 +0200+++ libmagic/magic.h    2013-04-08 15:42:57.328298809 +0200
@@ -85,6 +85,7 @@@@ -87,6 +87,7 @@
     
  const char *magic_getpath(const char *, int);   const char *magic_getpath(const char *, int);
  const char *magic_file(magic_t, const char *);   const char *magic_file(magic_t, const char *);
Line 2211  diff -u libmagic.orig/magic.h libmagic/magic.h Line 2401  diff -u libmagic.orig/magic.h libmagic/magic.h
  const char *magic_descriptor(magic_t, int);   const char *magic_descriptor(magic_t, int);
  const char *magic_buffer(magic_t, const void *, size_t);   const char *magic_buffer(magic_t, const void *, size_t);
     
@@ -93,7 +94,6 @@@@ -96,7 +97,6 @@
  int magic_version(void);
  int magic_load(magic_t, const char *);   int magic_load(magic_t, const char *);
  int magic_compile(magic_t, const char *);   int magic_compile(magic_t, const char *);
 -int magic_check(magic_t, const char *);  -int magic_check(magic_t, const char *);
  int magic_list(magic_t, const char *);   int magic_list(magic_t, const char *);
  int magic_errno(magic_t);   int magic_errno(magic_t);
     
 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  diff -u libmagic.orig/print.c libmagic/print.c
--- libmagic.orig/print.c       2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/print.c       2013-03-21 18:45:14.000000000 +0100
+++ libmagic/print.c    2012-03-28 01:53:04.304322598 +0200+++ libmagic/print.c    2013-04-27 13:53:32.175250261 +0200
@@ -29,6 +29,9 @@@@ -28,13 +28,17 @@
  /*
   * print.c - debugging printout routines    * print.c - debugging printout routines
   */    */
 +#define _GNU_SOURCE
 +#include "php.h"  +#include "php.h"
+#include "main/snprintf.h" 
+ 
  #include "file.h"   #include "file.h"
   +#include "cdf.h"
     
  #ifndef lint   #ifndef lint
@@ -45,174 +48,21 @@ FILE_RCSID("@(#)$File: print.c,v 1.76 2013/02/26 18:25:00 christos Exp $")
  #endif  /* lint */
     
 #define SZOF(a)        (sizeof(a) / sizeof(a[0]))+#include <stdio.h>
  #include <string.h>
  #include <stdarg.h>
  #include <stdlib.h>
 @@ -43,188 +47,28 @@
  #endif
  #include <time.h>
     
   -#define SZOF(a)        (sizeof(a) / sizeof(a[0]))
   -
   -#include "cdf.h"
   -
 -#ifndef COMPILE_ONLY  -#ifndef COMPILE_ONLY
 -protected void  -protected void
 -file_mdump(struct magic *m)  -file_mdump(struct magic *m)
 -{  -{
-       private const char optyp[] = { FILE_OPS };-       static const char optyp[] = { FILE_OPS };
 -       char tbuf[26];
 -  -
 -       (void) fprintf(stderr, "%u: %.*s %u", m->lineno,  -       (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
 -           (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);  -           (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
 -  -
 -       if (m->flag & INDIR) {  -       if (m->flag & INDIR) {
 -               (void) fprintf(stderr, "(%s,",  -               (void) fprintf(stderr, "(%s,",
-                              /* Note: type is unsigned */-                   /* Note: type is unsigned */
-                              (m->in_type < file_nnames) ? -                   (m->in_type < file_nnames) ? file_names[m->in_type] :
-                                       file_names[m->in_type] : "*bad*");-                   "*bad in_type*");
 -               if (m->in_op & FILE_OPINVERSE)  -               if (m->in_op & FILE_OPINVERSE)
 -                       (void) fputc('~', stderr);  -                       (void) fputc('~', stderr);
 -               (void) fprintf(stderr, "%c%u),",  -               (void) fprintf(stderr, "%c%u),",
-                              ((size_t)(m->in_op & FILE_OPS_MASK) <-                   ((size_t)(m->in_op & FILE_OPS_MASK) <
-                              SZOF(optyp)) ? -                   SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
-                                       optyp[m->in_op & FILE_OPS_MASK] : '?',-                   m->in_offset);
-                               m->in_offset); 
 -       }  -       }
 -       (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",  -       (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
-                      /* Note: type is unsigned */-           /* Note: type is unsigned */
-                      (m->type < file_nnames) ? file_names[m->type] : "*bad*");-           (m->type < file_nnames) ? file_names[m->type] : "*bad type");
 -       if (m->mask_op & FILE_OPINVERSE)  -       if (m->mask_op & FILE_OPINVERSE)
 -               (void) fputc('~', stderr);  -               (void) fputc('~', stderr);
 -  -
Line 2328  diff -u libmagic.orig/print.c libmagic/print.c Line 2525  diff -u libmagic.orig/print.c libmagic/print.c
 -               case FILE_MELONG:  -               case FILE_MELONG:
 -               case FILE_BESHORT:  -               case FILE_BESHORT:
 -               case FILE_BELONG:  -               case FILE_BELONG:
   -               case FILE_INDIRECT:
 -                       (void) fprintf(stderr, "%d", m->value.l);  -                       (void) fprintf(stderr, "%d", m->value.l);
 -                       break;  -                       break;
 -               case FILE_BEQUAD:  -               case FILE_BEQUAD:
Line 2349  diff -u libmagic.orig/print.c libmagic/print.c Line 2547  diff -u libmagic.orig/print.c libmagic/print.c
 -               case FILE_BEDATE:  -               case FILE_BEDATE:
 -               case FILE_MEDATE:  -               case FILE_MEDATE:
 -                       (void)fprintf(stderr, "%s,",  -                       (void)fprintf(stderr, "%s,",
-                           file_fmttime(m->value.l, 1));-                           file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
 -                       break;  -                       break;
 -               case FILE_LDATE:  -               case FILE_LDATE:
 -               case FILE_LELDATE:  -               case FILE_LELDATE:
 -               case FILE_BELDATE:  -               case FILE_BELDATE:
 -               case FILE_MELDATE:  -               case FILE_MELDATE:
 -                       (void)fprintf(stderr, "%s,",  -                       (void)fprintf(stderr, "%s,",
-                           file_fmttime(m->value.l, 0));-                           file_fmttime(m->value.l, 0, tbuf));
-                       break; 
 -               case FILE_QDATE:  -               case FILE_QDATE:
 -               case FILE_LEQDATE:  -               case FILE_LEQDATE:
 -               case FILE_BEQDATE:  -               case FILE_BEQDATE:
 -                       (void)fprintf(stderr, "%s,",  -                       (void)fprintf(stderr, "%s,",
-                           file_fmttime((uint32_t)m->value.q, 1));-                           file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
 -                       break;  -                       break;
 -               case FILE_QLDATE:  -               case FILE_QLDATE:
 -               case FILE_LEQLDATE:  -               case FILE_LEQLDATE:
 -               case FILE_BEQLDATE:  -               case FILE_BEQLDATE:
 -                       (void)fprintf(stderr, "%s,",  -                       (void)fprintf(stderr, "%s,",
-                           file_fmttime((uint32_t)m->value.q, 0));-                           file_fmttime(m->value.q, 0, tbuf));
 -                       break;  -                       break;
   -               case FILE_QWDATE:
   -               case FILE_LEQWDATE:
   -               case FILE_BEQWDATE:
   -                       (void)fprintf(stderr, "%s,",
   -                           file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
   -                       break;
 -               case FILE_FLOAT:  -               case FILE_FLOAT:
 -               case FILE_BEFLOAT:  -               case FILE_BEFLOAT:
 -               case FILE_LEFLOAT:  -               case FILE_LEFLOAT:
Line 2383  diff -u libmagic.orig/print.c libmagic/print.c Line 2586  diff -u libmagic.orig/print.c libmagic/print.c
 -               case FILE_DEFAULT:  -               case FILE_DEFAULT:
 -                       /* XXX - do anything here? */  -                       /* XXX - do anything here? */
 -                       break;  -                       break;
   -               case FILE_USE:
   -               case FILE_NAME:
   -                       (void) fprintf(stderr, "'%s'", m->value.s);
   -                       break;
 -               default:  -               default:
-                       (void) fputs("*bad*", stderr);-                       (void) fprintf(stderr, "*bad type %d*", m->type);
 -                       break;  -                       break;
 -               }  -               }
 -       }  -       }
 -       (void) fprintf(stderr, ",\"%s\"]\n", m->desc);  -       (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
 -}  -}
-#endif+#ifdef PHP_WIN32
-+# define asctime_r php_asctime_r
 +# define ctime_r php_ctime_r
  #endif
  
 +#define SZOF(a)        (sizeof(a) / sizeof(a[0]))
 +
  /*VARARGS*/   /*VARARGS*/
  protected void   protected void
  file_magwarn(struct magic_set *ms, const char *f, ...)   file_magwarn(struct magic_set *ms, const char *f, ...)
Line 2409  diff -u libmagic.orig/print.c libmagic/print.c Line 2621  diff -u libmagic.orig/print.c libmagic/print.c
 -       (void) fprintf(stderr, "Warning: ");  -       (void) fprintf(stderr, "Warning: ");
         va_start(va, f);          va_start(va, f);
 -       (void) vfprintf(stderr, f, va);  -       (void) vfprintf(stderr, f, va);
+        vasprintf(&expanded_format, f, va);+       if (vasprintf(&expanded_format, f, va)); /* silence */
         va_end(va);          va_end(va);
 -       (void) fputc('\n', stderr);  -       (void) fputc('\n', stderr);
 +         +       
Line 2419  diff -u libmagic.orig/print.c libmagic/print.c Line 2631  diff -u libmagic.orig/print.c libmagic/print.c
  }   }
     
  protected const char *   protected const char *
   @@ -235,7 +79,7 @@
           struct tm *tm;
    
           if (flags & FILE_T_WINDOWS) {
   -               struct timespec ts;
   +               struct timeval ts;
                   cdf_timestamp_to_timespec(&ts, t);
                   t = ts.tv_sec;
           }
 diff -u libmagic.orig/readcdf.c libmagic/readcdf.c  diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
--- libmagic.orig/readcdf.c     2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/readcdf.c     2012-10-31 18:03:01.000000000 +0100
+++ libmagic/readcdf.c  2012-03-28 01:53:04.304322598 +0200+++ libmagic/readcdf.c  2013-04-08 15:42:57.328298809 +0200
 @@ -30,7 +30,11 @@  @@ -30,7 +30,11 @@
  #endif   #endif
     
Line 2443  diff -u libmagic.orig/readcdf.c libmagic/readcdf.c Line 2664  diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
          char buf[64];           char buf[64];
          const char *str = NULL;           const char *str = NULL;
          const char *s;           const char *s;
@@ -125,7 +129,11 @@@@ -125,8 +129,12 @@
                  case CDF_FILETIME:                   case CDF_FILETIME:
                          tp = info[i].pi_tp;                           tp = info[i].pi_tp;
                          if (tp != 0) {                           if (tp != 0) {
   -                               char tbuf[64];
 -                                if (tp < 1000000000000000LL) {  -                                if (tp < 1000000000000000LL) {
   +                                                       char tbuf[64];
 +#if defined(PHP_WIN32) && _MSC_VER <= 1500  +#if defined(PHP_WIN32) && _MSC_VER <= 1500
 +                                                       if (tp < 1000000000000000i64) {  +                                                       if (tp < 1000000000000000i64) {
 +#else  +#else
 +                                                       if (tp < 1000000000000000LL) {  +                                                       if (tp < 1000000000000000LL) {
 +#endif  +#endif
                                          char tbuf[64];  
                                          cdf_print_elapsed_time(tbuf,                                           cdf_print_elapsed_time(tbuf,
                                              sizeof(tbuf), tp);                                               sizeof(tbuf), tp);
                                            if (NOTMIME(ms) && file_printf(ms,
 @@ -134,7 +142,9 @@  @@ -134,7 +142,9 @@
                                                  return -1;                                                   return -1;
                                  } else {                                   } else {
Line 2464  diff -u libmagic.orig/readcdf.c libmagic/readcdf.c Line 2687  diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
 +                                        if (cdf_timestamp_to_timespec(&ts, tp) == -1) {  +                                        if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
 +                                                                                       return -1;  +                                                                                       return -1;
 +                                                                               }  +                                                                               }
                                         c = cdf_ctime(&ts.tv_sec);                                         c = cdf_ctime(&ts.tv_sec, tbuf);
                                          if ((ec = strchr(c, '\n')) != NULL)                                           if ((ec = strchr(c, '\n')) != NULL)
                                                  *ec = '\0';                                                   *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;
 diff -u libmagic.orig/readelf.c libmagic/readelf.c  diff -u libmagic.orig/readelf.c libmagic/readelf.c
--- libmagic.orig/readelf.c     2012-03-28 12:00:34.017709605 +0200--- libmagic.orig/readelf.c     2013-03-21 18:45:14.000000000 +0100
+++ libmagic/readelf.c  2012-03-28 01:53:04.308322618 +0200+++ libmagic/readelf.c  2013-04-08 15:42:57.328298809 +0200
@@ -49,7 +49,7 @@@@ -48,8 +48,8 @@
  private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
      off_t, int *, int);       off_t, int *, int);
  private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,   private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
     off_t, int *, int);-    off_t, int *, int, int);
 -private size_t donote(struct magic_set *, void *, size_t, size_t, int,  -private size_t donote(struct magic_set *, void *, size_t, size_t, int,
   +    off_t, int *, int);
 +private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,  +private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
      int, size_t, int *);       int, size_t, int *);
     
  #define        ELF_ALIGN(a)    ((((a) + align - 1) / align) * align)   #define        ELF_ALIGN(a)    ((((a) + align - 1) / align) * align)
@@ -357,7 +357,7 @@@@ -127,11 +127,17 @@
 #endif 
     
 private size_t #define elf_getu16(swap, value) getu16(swap, value)
-donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size, #define elf_getu32(swap, value) getu32(swap, value)
+donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,-#define elf_getu64(swap, value) getu64(swap, value)
     int clazz, int swap, size_t align, int *flags)+#ifdef USE_ARRAY_FOR_64BIT_TYPES
 +# define elf_getu64(swap, array) \
 +       ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
 +        (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
 +#else
 +# define elf_getu64(swap, value) getu64(swap, value)
 +#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)
 @@ -292,7 +298,7 @@
  {   {
        Elf32_Nhdr nh32;        Elf32_Phdr ph32;
@@ -367,7 +367,6 @@        Elf64_Phdr ph64;
        int os_style = -1;-       size_t offset, len;
 #endif+       size_t offset;
        uint32_t namesz, descsz;        unsigned char nbuf[BUFSIZ];
-        unsigned char *nbuf = CAST(unsigned char *, vbuf);        ssize_t bufsize;
     
        (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);@@ -306,7 +312,11 @@
        offset += xnh_sizeof;         * Loop through all the program headers.
@@ -888,20 +887,16 @@         */
         for ( ; num; num--) {
 -               if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
 +               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) {
 +                       file_badseek(ms);
 +                       return -1;
 +               }
 +               if (FINFO_READ_FUNC(fd, xph_addr, xph_sizeof) == -1) {
                         file_badread(ms);
                         return -1;
                 }
 @@ -324,8 +334,13 @@
                  * This is a PT_NOTE section; loop through all the notes
                  * in the section.
                  */
 -               len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
 -               if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
 +               if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (off_t)-1) {
 +                       file_badseek(ms);
 +                       return -1;
 +               }
 +               bufsize = FINFO_READ_FUNC(fd, nbuf,
 +                   ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
 +               if (bufsize == -1) {
                         file_badread(ms);
                         return -1;
                 }
 @@ -852,24 +867,12 @@
                 return 0;
         }
  
 -       /* Read offset of name section to be able to read section names later */
 -       if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
 -               file_badread(ms);
 -               return -1;
 -       }
 -       name_off = xsh_offset;
 -
         for ( ; num; num--) {
 -               /* Read the name of this section. */
 -               if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
 -                       file_badread(ms);
 +               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) {
 +                       file_badseek(ms);
                         return -1;
                 }
 -               name[sizeof(name) - 1] = '\0';
 -               if (strcmp(name, ".debug_info") == 0)
 -                       stripped = 0;
 -
 -               if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
 +               if (FINFO_READ_FUNC(fd, xsh_addr, xsh_sizeof) == -1) {
                         file_badread(ms);
                         return -1;
                 }
 @@ -894,14 +897,17 @@
                 /* Things we can determine when we seek */                  /* Things we can determine when we seek */
                 switch (xsh_type) {                  switch (xsh_type) {
                 case SHT_NOTE:                  case SHT_NOTE:
-                       if ((nbuf = malloc((size_t)xsh_size)) == NULL) {-                       if ((nbuf = malloc(xsh_size)) == NULL) {
 -                               file_error(ms, errno, "Cannot allocate memory"  -                               file_error(ms, errno, "Cannot allocate memory"
 -                                   " for note");  -                                   " for note");
 -                               return -1;  
 -                       }  
 +                       nbuf = emalloc((size_t)xsh_size);  +                       nbuf = emalloc((size_t)xsh_size);
                        if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==+                        if ((noff = FINFO_LSEEK_FUNC(fd, (off_t)xsh_offset, SEEK_SET)) ==
                            (off_t)-1) {+                            (off_t)-1) {
                                file_badread(ms);+                                file_badread(ms);
-                               free(nbuf); 
 +                               efree(nbuf);  +                               efree(nbuf);
                                 return -1;                                  return -1;
                         }                          }
                        if (read(fd, nbuf, (size_t)xsh_size) !=-                        if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
                            (ssize_t)xsh_size) {+                       if (FINFO_READ_FUNC(fd, nbuf, (size_t)xsh_size) !=
-                               free(nbuf);+                            (ssize_t)xsh_size) {
 +                               efree(nbuf);  +                               efree(nbuf);
                                 file_badread(ms);                                  file_badread(ms);
   -                               free(nbuf);
                                 return -1;                                  return -1;
                         }                          }
@@ -916,7 +911,7 @@ 
 @@ -910,25 +916,16 @@
                                 if (noff >= (off_t)xsh_size)
                                         break;
                                 noff = donote(ms, nbuf, (size_t)noff,
 -                                   xsh_size, clazz, swap, 4, flags);
 +                                   (size_t)xsh_size, clazz, swap, 4,
 +                                   flags);
                                 if (noff == 0)                                  if (noff == 0)
                                         break;                                          break;
                         }                          }
Line 2528  diff -u libmagic.orig/readelf.c libmagic/readelf.c Line 2863  diff -u libmagic.orig/readelf.c libmagic/readelf.c
 +                       efree(nbuf);  +                       efree(nbuf);
                         break;                          break;
                 case SHT_SUNW_cap:                  case SHT_SUNW_cap:
                        if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==-                       switch (mach) {
 -                       case EM_SPARC:
 -                       case EM_SPARCV9:
 -                       case EM_IA_64:
 -                       case EM_386:
 -                       case EM_AMD64:
 -                               break;
 -                       default:
 -                               goto skip;
 -                       }
 -
 -                        if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
 +                       if (FINFO_LSEEK_FUNC(fd, (off_t)xsh_offset, SEEK_SET) ==
 +                           (off_t)-1) {
                                 file_badseek(ms);
                                 return -1;
                         }
 @@ -940,7 +937,7 @@
                                     MAX(sizeof cap32, sizeof cap64)];
                                 if ((coff += xcap_sizeof) > (off_t)xsh_size)
                                         break;
 -                               if (read(fd, cbuf, (size_t)xcap_sizeof) !=
 +                               if (FINFO_READ_FUNC(fd, cbuf, (size_t)xcap_sizeof) !=
                                     (ssize_t)xcap_sizeof) {
                                         file_badread(ms);
                                         return -1;
 @@ -966,13 +963,12 @@
                                         break;
                                 }
                         }
 -                       /*FALLTHROUGH*/
 -               skip:
 +                       break;
 +
                 default:
                         break;
                 }
         }
 -
         if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
                 return -1;
         if (cap_hw1) {
 @@ -1051,7 +1047,7 @@
         const char *shared_libraries = "";
         unsigned char nbuf[BUFSIZ];
         ssize_t bufsize;
 -       size_t offset, align, len;
 +       size_t offset, align;
         
         if (size != xph_sizeof) {
                 if (file_printf(ms, ", corrupted program header size") == -1)
 @@ -1060,8 +1056,13 @@
         }
  
         for ( ; num; num--) {
 -               if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
 -                       file_badread(ms);
 +               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) {
 +                       file_badseek(ms);
 +                       return -1;
 +               }
 +
 +               if (FINFO_READ_FUNC(fd, xph_addr, xph_sizeof) == -1) {
 +                       file_badread(ms);
                         return -1;
                 }
  
 @@ -1099,9 +1100,12 @@
                          * This is a PT_NOTE section; loop through all the notes
                          * in the section.
                          */
 -                       len = xph_filesz < sizeof(nbuf) ? xph_filesz
 -                           : sizeof(nbuf);
 -                       bufsize = pread(fd, nbuf, len, xph_offset);
 +                       if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (off_t)-1) {
 +                               file_badseek(ms);
 +                               return -1;
 +                       }
 +                       bufsize = FINFO_READ_FUNC(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
 +                           xph_filesz : sizeof(nbuf)));
                         if (bufsize == -1) {
                                 file_badread(ms);
                                 return -1;
 @@ -1162,7 +1166,7 @@
         /*
          * If we cannot seek, it must be a pipe, socket or fifo.
          */
 -       if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
 +       if((FINFO_LSEEK_FUNC(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
                 fd = file_pipe2file(ms, fd, buf, nbytes);
  
         if (fstat(fd, &st) == -1) {
 diff -u libmagic.orig/readelf.h libmagic/readelf.h
 --- libmagic.orig/readelf.h     2013-03-21 18:45:14.000000000 +0100
 +++ libmagic/readelf.h  2013-03-31 16:57:18.499744030 +0200
 @@ -44,9 +44,17 @@
  typedef uint32_t       Elf32_Word;
  typedef uint8_t                Elf32_Char;
  
 +#if SIZEOF_LONG_LONG != 8
 +#define        USE_ARRAY_FOR_64BIT_TYPES
 +typedef        uint32_t        Elf64_Addr[2];
 +typedef        uint32_t        Elf64_Off[2];
 +typedef uint32_t       Elf64_Xword[2];
 +#else
 +#undef USE_ARRAY_FOR_64BIT_TYPES
  typedef        uint64_t        Elf64_Addr;
  typedef        uint64_t        Elf64_Off;
  typedef uint64_t       Elf64_Xword;
 +#endif
  typedef uint16_t       Elf64_Half;
  typedef uint32_t       Elf64_Word;
  typedef uint8_t                Elf64_Char;
 diff -u libmagic.orig/softmagic.c libmagic/softmagic.c  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
--- libmagic.orig/softmagic.c   2012-03-28 12:00:34.012709598 +0200--- libmagic.orig/softmagic.c   2013-03-21 18:45:14.000000000 +0100
+++ libmagic/softmagic.c        2012-03-28 01:53:04.308322618 +0200+++ libmagic/softmagic.c        2013-05-14 11:00:07.044745939 +0200
 @@ -41,6 +41,11 @@  @@ -41,6 +41,11 @@
  #include <stdlib.h>   #include <stdlib.h>
  #include <time.h>   #include <time.h>
Line 2543  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 2990  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 +  +
     
  private int match(struct magic_set *, struct magic *, uint32_t,   private int match(struct magic_set *, struct magic *, uint32_t,
     const unsigned char *, size_t, int, int);     const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,
@@ -125,14 +130,14 @@@@ -132,7 +137,7 @@
                int flush = 0; 
                 struct magic *m = &magic[magindex];                  struct magic *m = &magic[magindex];
     
                   if (m->type != FILE_NAME)
 -               if ((IS_STRING(m->type) &&  -               if ((IS_STRING(m->type) &&
 +               if ((IS_LIBMAGIC_STRING(m->type) &&  +               if ((IS_LIBMAGIC_STRING(m->type) &&
                     ((text && (m->str_flags & (STRING_BINTEST | STRING_TEXTTEST)) == STRING_BINTEST) || #define FLT (STRING_BINTEST | STRING_TEXTTEST)
                      (!text && (m->str_flags & (STRING_TEXTTEST | STRING_BINTEST)) == STRING_TEXTTEST))) ||                     ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
                    (m->flag & mode) != mode) {                      (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
                        /* Skip sub-tests */@@ -209,8 +214,8 @@
-                       while (magic[magindex + 1].cont_level != 0 && 
-                              ++magindex < nmagic) 
-                               continue; 
+                       while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) { 
+                               magindex++; 
+                       } 
                        continue; /* Skip to next top-level test*/ 
                } 
  
@@ -167,9 +172,9 @@ 
                         * main entry didn't match, 
                         * flush its continuations 
                         */ 
-                       while (magindex < nmagic - 1 && 
-                           magic[magindex + 1].cont_level != 0) 
+                       while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) { 
                                magindex++; 
+                       } 
                        continue; 
                } 
  
@@ -196,8 +201,8 @@ 
                 if (file_check_mem(ms, ++cont_level) == -1)                  if (file_check_mem(ms, ++cont_level) == -1)
                         return -1;                          return -1;
     
 -               while (magic[magindex+1].cont_level != 0 &&  -               while (magic[magindex+1].cont_level != 0 &&
 -                   ++magindex < nmagic) {  -                   ++magindex < nmagic) {
+               while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) {+               while (magindex + 1 < nmagic && magic[magindex+1].cont_level != 0 &&
+                        magindex++;+                   ++magindex) {
                         m = &magic[magindex];                          m = &magic[magindex];
                         ms->line = m->lineno; /* for messages */                          ms->line = m->lineno; /* for messages */
     
@@ -212,8 +217,7 @@@@ -335,44 +340,22 @@
                        } 
                        ms->offset = m->offset; 
                        if (m->flag & OFFADD) { 
-                               ms->offset += 
-                                   ms->c.li[cont_level - 1].off; 
+                               ms->offset += ms->c.li[cont_level - 1].off; 
                        } 
  
 #ifdef ENABLE_CONDITIONALS 
@@ -318,44 +322,22 @@ 
  private int   private int
  check_fmt(struct magic_set *ms, struct magic *m)   check_fmt(struct magic_set *ms, struct magic *m)
  {   {
Line 2651  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3066  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
  private int32_t   private int32_t
  mprint(struct magic_set *ms, struct magic *m)   mprint(struct magic_set *ms, struct magic *m)
  {   {
@@ -538,13 +520,10 @@@@ -583,13 +566,13 @@
                 char *cp;                  char *cp;
                 int rval;                  int rval;
     
 -               cp = strndup((const char *)ms->search.s, ms->search.rm_len);  -               cp = strndup((const char *)ms->search.s, ms->search.rm_len);
 -               if (cp == NULL) {  
 -                       file_oomem(ms, ms->search.rm_len);  
 -                       return -1;  
 -               }  
 +               cp = estrndup((const char *)ms->search.s, ms->search.rm_len);  +               cp = estrndup((const char *)ms->search.s, ms->search.rm_len);
+                if (cp == NULL) {
                         file_oomem(ms, ms->search.rm_len);
                         return -1;
                 }
                 rval = file_printf(ms, m->desc, cp);                  rval = file_printf(ms, m->desc, cp);
 -               free(cp);  -               free(cp);
 +               efree(cp);  +               efree(cp);
     
                 if (rval == -1)                  if (rval == -1)
                         return -1;                          return -1;
@@ -738,16 +717,16 @@@@ -835,16 +818,16 @@
         if (m->num_mask) \          if (m->num_mask) \
                 switch (m->mask_op & FILE_OPS_MASK) { \                  switch (m->mask_op & FILE_OPS_MASK) { \
                 case FILE_OPADD: \                  case FILE_OPADD: \
Line 2689  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3103  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                         break; \                          break; \
                 } \                  } \
     
@@ -1029,16 +1008,13 @@@@ -1145,9 +1128,6 @@
                     "nbytes=%zu, count=%u)\n", m->type, m->flag, offset, o,
        if ((ms->flags & MAGIC_DEBUG) != 0) {                    nbytes, count);
                 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));                  mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
 -#ifndef COMPILE_ONLY  -#ifndef COMPILE_ONLY
 -               file_mdump(m);  -               file_mdump(m);
Line 2699  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3113  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
         }          }
     
         if (m->flag & INDIR) {          if (m->flag & INDIR) {
                int off = m->in_offset;@@ -1644,9 +1624,6 @@
                if (m->in_op & FILE_OPINDIRECT) { 
-                       const union VALUETYPE *q = CAST(const union VALUETYPE *, 
-                           ((const void *)(s + offset + off))); 
+                       const union VALUETYPE *q = 
+                           ((const void *)(s + offset + off)); 
                        switch (m->in_type) { 
                        case FILE_BYTE: 
                                off = q->b; 
@@ -1518,9 +1494,6 @@ 
                 if ((ms->flags & MAGIC_DEBUG) != 0) {                  if ((ms->flags & MAGIC_DEBUG) != 0) {
                         mdebug(offset, (char *)(void *)p,                          mdebug(offset, (char *)(void *)p,
                             sizeof(union VALUETYPE));                              sizeof(union VALUETYPE));
Line 2718  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3123  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
                 }                  }
         }          }
     
@@ -1669,6 +1642,65 @@@@ -1717,12 +1694,12 @@
                 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;
                         if (file_printf(ms, "%s", rbuf) == -1)
                                 return -1;
 -                       free(rbuf);
 +                       efree(rbuf);
                 }
                 return rv;
  
 @@ -1837,6 +1814,42 @@
         return file_strncmp(a, b, len, flags);          return file_strncmp(a, b, len, flags);
  }   }
     
Line 2734  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3155  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 +                 +               
 +               for (i=0; i<Z_STRLEN_P(pattern); i++, j++) {  +               for (i=0; i<Z_STRLEN_P(pattern); i++, j++) {
 +                       switch (Z_STRVAL_P(pattern)[i]) {  +                       switch (Z_STRVAL_P(pattern)[i]) {
 +                               case '?':  
 +                                       t[j] = '.';  
 +                                       break;  
 +                               case '*':  
 +                                       t[j++] = '.';  
 +                                       t[j] = '*';  
 +                                       break;  
 +                               case '.':  
 +                                       t[j++] = '\\';  
 +                                       t[j] = '.';  
 +                                       break;  
 +                               case '\\':  
 +                                       t[j++] = '\\';  
 +                                       t[j] = '\\';  
 +                                       break;  
 +                               case '(':  
 +                                       t[j++] = '\\';  
 +                                       t[j] = '(';  
 +                                       break;  
 +                               case ')':  
 +                                       t[j++] = '\\';  
 +                                       t[j] = ')';  
 +                                       break;  
 +                               case '~':  +                               case '~':
 +                                       t[j++] = '\\';  +                                       t[j++] = '\\';
 +                                       t[j] = '~';  +                                       t[j] = '~';
Line 2784  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3182  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
  private int   private int
  magiccheck(struct magic_set *ms, struct magic *m)   magiccheck(struct magic_set *ms, struct magic *m)
  {   {
@@ -1825,67 +1857,163 @@@@ -1996,69 +2009,157 @@
                 break;                  break;
         }          }
         case FILE_REGEX: {          case FILE_REGEX: {
Line 2795  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3193  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 -               if (ms->search.s == NULL)  -               if (ms->search.s == NULL)
 -                       return 0;  -                       return 0;
 -  -
+               zval *pattern;-                l = 0;
+               int options = 0; 
+               pcre_cache_entry *pce; 
+               TSRMLS_FETCH(); 
+                
+               MAKE_STD_ZVAL(pattern); 
+               ZVAL_STRINGL(pattern, (char *)m->value.s, m->vallen, 0); 
+        
+               options |= PCRE_MULTILINE; 
+                
+               if (m->str_flags & STRING_IGNORE_CASE) { 
+                       options |= PCRE_CASELESS; 
+               } 
+                
+               convert_libmagic_pattern(pattern, options); 
+                 
                l = 0; 
 -               rc = regcomp(&rx, m->value.s,  -               rc = regcomp(&rx, m->value.s,
 -                   REG_EXTENDED|REG_NEWLINE|  -                   REG_EXTENDED|REG_NEWLINE|
 -                   ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));  -                   ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
Line 2828  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3210  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 -                       size_t l = ms->search.s_len - 1;  -                       size_t l = ms->search.s_len - 1;
 -                       char c = ms->search.s[l];  -                       char c = ms->search.s[l];
 -                       ((char *)(intptr_t)ms->search.s)[l] = '\0';  -                       ((char *)(intptr_t)ms->search.s)[l] = '\0';
+#if (PHP_MAJOR_VERSION < 6)-#else
+               if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) { 
 #else 
 -                       pmatch[0].rm_so = 0;  -                       pmatch[0].rm_so = 0;
 -                       pmatch[0].rm_eo = ms->search.s_len;  -                       pmatch[0].rm_eo = ms->search.s_len;
+               if ((pce = pcre_get_compiled_regex_cache(IS_STRING, Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) {-#endif
 #endif 
 -                       rc = regexec(&rx, (const char *)ms->search.s,  -                       rc = regexec(&rx, (const char *)ms->search.s,
 -                           1, pmatch, REG_STARTEND);  -                           1, pmatch, REG_STARTEND);
 -#if REG_STARTEND == 0  -#if REG_STARTEND == 0
 -                       ((char *)(intptr_t)ms->search.s)[l] = c;  -                       ((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;
   +               TSRMLS_FETCH();
   +               
   +               MAKE_STD_ZVAL(pattern);
   +               ZVAL_STRINGL(pattern, (char *)m->value.s, m->vallen, 0);
   +       
   +               options |= PCRE_MULTILINE;
   +               
   +               if (m->str_flags & STRING_IGNORE_CASE) {
   +                       options |= PCRE_CASELESS;
   +               }
   +               
   +               convert_libmagic_pattern(pattern, options);
   +               
   +               l = v = 0;
   +               if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) {
 +                       zval_dtor(pattern);  +                       zval_dtor(pattern);
 +                       FREE_ZVAL(pattern);  +                       FREE_ZVAL(pattern);
 +                       return -1;  +                       return -1;
Line 2855  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3261  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 +                       haystack = estrndup(ms->search.s, ms->search.s_len);  +                       haystack = estrndup(ms->search.s, ms->search.s_len);
 +  +
 +                       /* match v = 0, no match v = 1 */  +                       /* match v = 0, no match v = 1 */
 +#if (PHP_MAJOR_VERSION < 6)  
 +                       php_pcre_match_impl(pce, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);  +                       php_pcre_match_impl(pce, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);
 +#else                    
 +                       php_pcre_match_impl(pce, IS_STRING, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);  
  #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;  
 +                       /* Free haystack */  +                       /* Free haystack */
 +                       efree(haystack);  +                       efree(haystack);
 +                         +                       
Line 2994  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c Line 3388  diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
 +               break;    +               break;  
         }          }
         case FILE_INDIRECT:          case FILE_INDIRECT:
           case FILE_USE:
           case FILE_NAME:
 -               return 1;  -               return 1;
 +               return 1;          +               return 1;        
         default:          default:
                 file_magerror(ms, "invalid type %d in magiccheck()", m->type);                  file_magerror(ms, "invalid type %d in magiccheck()", m->type);
                 return -1;                  return -1;
 Only in libmagic.orig: strlcat.c  
 Only in libmagic.orig: strlcpy.c  
 Only in libmagic.orig: vasprintf.c  

Removed from v.1.1.1.2  
changed lines
  Added in v.1.1.1.3


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>