]> granicus.if.org Git - php/commitdiff
ported ext/fileinfo
authorAnatol Belski <ab@php.net>
Tue, 19 Aug 2014 19:35:13 +0000 (21:35 +0200)
committerAnatol Belski <ab@php.net>
Tue, 19 Aug 2014 19:35:13 +0000 (21:35 +0200)
ext/fileinfo/fileinfo.c
ext/fileinfo/libmagic/apprentice.c
ext/fileinfo/libmagic/cdf.c
ext/fileinfo/libmagic/compress.c
ext/fileinfo/libmagic/elfclass.h
ext/fileinfo/libmagic/fsmagic.c
ext/fileinfo/libmagic/readelf.c

index dac7ccbce9beb3c60ecd5f9bd24532b8ed33e69e..e2030c621d5f0240b29fffc1c28461c03382a89d 100644 (file)
@@ -44,7 +44,7 @@
 
 /* {{{ macros and type definitions */
 typedef struct _php_fileinfo {
-       long options;
+       php_int_t options;
        struct magic_set *magic;
 } php_fileinfo;
 
@@ -295,14 +295,14 @@ PHP_MINFO_FUNCTION(fileinfo)
    Create a new fileinfo resource. */
 PHP_FUNCTION(finfo_open)
 {
-       long options = MAGIC_NONE;
+       php_int_t options = MAGIC_NONE;
        char *file = NULL;
        int file_len = 0;
        php_fileinfo *finfo;
        FILEINFO_DECLARE_INIT_OBJECT(object)
        char resolved_path[MAXPATHLEN];
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lp", &options, &file, &file_len) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ip", &options, &file, &file_len) == FAILURE) {
                FILEINFO_DESTROY_OBJECT(object);
                RETURN_FALSE;
        }
@@ -382,18 +382,18 @@ PHP_FUNCTION(finfo_close)
    Set libmagic configuration options. */
 PHP_FUNCTION(finfo_set_flags)
 {
-       long options;
+       php_int_t options;
        php_fileinfo *finfo;
        zval *zfinfo;
        FILEINFO_DECLARE_INIT_OBJECT(object)
 
        if (object) {
-               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &options) == FAILURE) {
+               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "i", &options) == FAILURE) {
                        RETURN_FALSE;
                }
                FILEINFO_FROM_OBJECT(finfo, object);
        } else {
-               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zfinfo, &options) == FAILURE) {
+               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ri", &zfinfo, &options) == FAILURE) {
                        RETURN_FALSE;
                }
                ZEND_FETCH_RESOURCE(finfo, php_fileinfo *, zfinfo, -1, "file_info", le_fileinfo);
@@ -412,7 +412,7 @@ PHP_FUNCTION(finfo_set_flags)
 
 static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mimetype_emu) /* {{{ */
 {
-       long options = 0;
+       php_int_t options = 0;
        char *ret_val = NULL, *buffer = NULL;
        int buffer_len;
        php_fileinfo *finfo = NULL;
@@ -452,13 +452,13 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
                        goto common;
                }
        } else if (object) {
-               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
+               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ir", &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
                        RETURN_FALSE;
                }
                FILEINFO_FROM_OBJECT(finfo, object);
                magic = finfo->magic;
        } else {
-               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|lr", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
+               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|ir", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
                        RETURN_FALSE;
                }
                ZEND_FETCH_RESOURCE(finfo, php_fileinfo *, zfinfo, -1, "file_info", le_fileinfo);
@@ -480,7 +480,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
                case FILEINFO_MODE_STREAM:
                {
                                php_stream *stream;
-                               off_t streampos;
+                               php_off_t streampos;
 
                                php_stream_from_zval_no_verify(stream, what);
                                if (!stream) {
index ce747378aa3aa33e0d5e7993d4d332e59d9456cb..b3b1f746d4b7b6004af6eae51d708d41f0b37e84 100644 (file)
@@ -1145,7 +1145,7 @@ apprentice_load(struct magic_set *ms, const char *fn, int action)
        uint32_t i, j;
        size_t files = 0, maxfiles = 0;
        char **filearr = NULL;
-       struct stat st;
+       php_stat_t st;
        struct magic_map *map;
        struct magic_entry_set mset[MAGIC_SETS];
        php_stream *dir;
@@ -1187,7 +1187,7 @@ apprentice_load(struct magic_set *ms, const char *fn, int action)
                                php_stream_closedir(dir);
                                goto out;
                        }
-                       if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
+                       if (zend_stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
                                continue;
                        }
                        if (files >= maxfiles) {
@@ -2698,7 +2698,7 @@ internal_loaded:
        if (NULL != fn) {
                nentries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
                entries = (uint32_t)(st.sb.st_size / sizeof(struct magic));
-               if ((off_t)(entries * sizeof(struct magic)) != st.sb.st_size) {
+               if ((php_off_t)(entries * sizeof(struct magic)) != st.sb.st_size) {
                        file_error(ms, 0, "Size of `%s' %llu is not a multiple of %zu",
                                dbname, (unsigned long long)st.sb.st_size,
                                sizeof(struct magic));
@@ -2788,7 +2788,7 @@ apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn)
 
        assert(nm + sizeof(ar) < m);
 
-       if (php_stream_seek(stream,(off_t)sizeof(struct magic), SEEK_SET) != sizeof(struct magic)) {
+       if (php_stream_seek(stream,(php_off_t)sizeof(struct magic), SEEK_SET) != sizeof(struct magic)) {
                file_error(ms, errno, "error seeking `%s'", dbname);
                goto out;
        }
index c86763faa0fdd11257ab585783c1252cc2bd90d0..93cc2318844f73e1626ae0070d7032e64c93523e 100644 (file)
@@ -291,11 +291,11 @@ cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
 }
 
 static ssize_t
-cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
+cdf_read(const cdf_info_t *info, php_off_t off, void *buf, size_t len)
 {
        size_t siz = (size_t)off + len;
 
-       if ((off_t)(off + len) != (off_t)siz) {
+       if ((php_off_t)(off + len) != (php_off_t)siz) {
                errno = EINVAL;
                return -1;
        }
@@ -308,7 +308,7 @@ cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
        if (info->i_fd == -1)
                return -1;
 
-       if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (off_t)-1)
+       if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (php_off_t)-1)
                return -1;
 
        if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len)
@@ -323,7 +323,7 @@ cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
        char buf[512];
 
        (void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
-       if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
+       if (cdf_read(info, (php_off_t)0, buf, sizeof(buf)) == -1)
                return -1;
        cdf_unpack_header(h, buf);
        cdf_swap_header(h);
@@ -357,7 +357,7 @@ cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
        size_t ss = CDF_SEC_SIZE(h);
        size_t pos = CDF_SEC_POS(h, id);
        assert(ss == len);
-       return cdf_read(info, (off_t)pos, ((char *)buf) + offs, len);
+       return cdf_read(info, (php_off_t)pos, ((char *)buf) + offs, len);
 }
 
 ssize_t
index fed7420a6a41f805bca9393b0bc53e70d340d98d..613fc6a3d20deba28056aec0ba6f2ad7ed8a065b 100644 (file)
@@ -300,7 +300,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
                return -1;
        }
        (void)close(tfd);
-       if (FINFO_LSEEK_FUNC(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
+       if (FINFO_LSEEK_FUNC(fd, (php_off_t)0, SEEK_SET) == (php_off_t)-1) {
                file_badseek(ms);
                return -1;
        }
@@ -406,7 +406,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method,
                (void) close(0);
                if (fd != -1) {
                    (void) dup(fd);
-                   (void) FINFO_LSEEK_FUNC(0, (off_t)0, SEEK_SET);
+                   (void) FINFO_LSEEK_FUNC(0, (php_off_t)0, SEEK_SET);
                } else {
                    (void) dup(fdin[0]);
                    (void) close(fdin[0]);
index 010958a4296fb8872a0203cc928617cb268fb6d8..889ff6263ec3fe632b0dc7ccd60308745339aceb 100644 (file)
@@ -37,7 +37,7 @@
        case ET_CORE:
                flags |= FLAGS_IS_CORE;
                if (dophn_core(ms, clazz, swap, fd,
-                   (off_t)elf_getu(swap, elfhdr.e_phoff),
+                   (php_off_t)elf_getu(swap, elfhdr.e_phoff),
                    elf_getu16(swap, elfhdr.e_phnum), 
                    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
                    fsize, &flags) == -1)
@@ -47,7 +47,7 @@
        case ET_EXEC:
        case ET_DYN:
                if (dophn_exec(ms, clazz, swap, fd,
-                   (off_t)elf_getu(swap, elfhdr.e_phoff),
+                   (php_off_t)elf_getu(swap, elfhdr.e_phoff),
                    elf_getu16(swap, elfhdr.e_phnum), 
                    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
                    fsize, &flags, elf_getu16(swap, elfhdr.e_shnum))
@@ -56,7 +56,7 @@
                /*FALLTHROUGH*/
        case ET_REL:
                if (doshn(ms, clazz, swap, fd,
-                   (off_t)elf_getu(swap, elfhdr.e_shoff),
+                   (php_off_t)elf_getu(swap, elfhdr.e_shoff),
                    elf_getu16(swap, elfhdr.e_shnum),
                    (size_t)elf_getu16(swap, elfhdr.e_shentsize),
                    fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
index 2010b26339ed7c3b791b9efc54c460bce74fbc92..f945d06a8ba6f90041d6dc1510f89b7ba7d56f21 100644 (file)
@@ -90,7 +90,7 @@ handle_mime(struct magic_set *ms, int mime, const char *str)
 }
 
 protected int
-file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream)
+file_fsmagic(struct magic_set *ms, const char *fn, php_stat_t *sb, php_stream *stream)
 {
        int ret, did = 0;
        int mime = ms->flags & MAGIC_MIME;
index 6f776721b03ba584ff87d69f2d10ee2cdc028356..ec316b6bbd2b9b60d0e4c02c66e85f5665e19e84 100644 (file)
@@ -42,13 +42,13 @@ FILE_RCSID("@(#)$File: readelf.c,v 1.99 2013/11/05 15:44:01 christos Exp $")
 #include "magic.h"
 
 #ifdef ELFCORE
-private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
-    off_t, int *);
+private int dophn_core(struct magic_set *, int, int, int, php_off_t, int, size_t,
+    php_off_t, int *);
 #endif
 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
-    off_t, int *, int);
+    php_off_t, int *, int);
 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
-    off_t, int *, int);
+    php_off_t, int *, int);
 private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
     int, size_t, int *);
 
@@ -144,7 +144,7 @@ getu64(int swap, uint64_t value)
 #define xsh_size       (size_t)(clazz == ELFCLASS32            \
                         ? elf_getu32(swap, sh32.sh_size)       \
                         : elf_getu64(swap, sh64.sh_size))
-#define xsh_offset     (off_t)(clazz == ELFCLASS32             \
+#define xsh_offset     (php_off_t)(clazz == ELFCLASS32         \
                         ? elf_getu32(swap, sh32.sh_offset)     \
                         : elf_getu64(swap, sh64.sh_offset))
 #define xsh_type       (clazz == ELFCLASS32                    \
@@ -162,13 +162,13 @@ getu64(int swap, uint64_t value)
 #define xph_type       (clazz == ELFCLASS32                    \
                         ? elf_getu32(swap, ph32.p_type)        \
                         : elf_getu32(swap, ph64.p_type))
-#define xph_offset     (off_t)(clazz == ELFCLASS32             \
+#define xph_offset     (php_off_t)(clazz == ELFCLASS32         \
                         ? elf_getu32(swap, ph32.p_offset)      \
                         : elf_getu64(swap, ph64.p_offset))
 #define xph_align      (size_t)((clazz == ELFCLASS32           \
-                        ? (off_t) (ph32.p_align ?              \
+                        ? (php_off_t) (ph32.p_align ?          \
                            elf_getu32(swap, ph32.p_align) : 4) \
-                        : (off_t) (ph64.p_align ?              \
+                        : (php_off_t) (ph64.p_align ?          \
                            elf_getu64(swap, ph64.p_align) : 4)))
 #define xph_filesz     (size_t)((clazz == ELFCLASS32           \
                         ? elf_getu32(swap, ph32.p_filesz)      \
@@ -293,8 +293,8 @@ private const char os_style_names[][8] = {
 #define FLAGS_IS_CORE          0x10
 
 private int
-dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
-    int num, size_t size, off_t fsize, int *flags)
+dophn_core(struct magic_set *ms, int clazz, int swap, int fd, php_off_t off,
+    int num, size_t size, php_off_t fsize, int *flags)
 {
        Elf32_Phdr ph32;
        Elf64_Phdr ph64;
@@ -312,7 +312,7 @@ dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
         * Loop through all the program headers.
         */
        for ( ; num; num--) {
-               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) {
+               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (php_off_t)-1) {
                        file_badseek(ms);
                        return -1;
                }
@@ -334,7 +334,7 @@ dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
                 * This is a PT_NOTE section; loop through all the notes
                 * in the section.
                 */
-               if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (off_t)-1) {
+               if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (php_off_t)-1) {
                        file_badseek(ms);
                        return -1;
                }
@@ -917,7 +917,7 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
        Elf64_Shdr sh64;
        int stripped = 1;
        void *nbuf;
-       off_t noff, coff, name_off;
+       php_off_t noff, coff, name_off;
        uint64_t cap_hw1 = 0;   /* SunOS 5.x hardware capabilites */
        uint64_t cap_sf1 = 0;   /* SunOS 5.x software capabilites */
        char name[50];
@@ -929,7 +929,7 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
        }
 
        for ( ; num; num--) {
-               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) {
+               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (php_off_t)-1) {
                        file_badseek(ms);
                        return -1;
                }
@@ -959,8 +959,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
                switch (xsh_type) {
                case SHT_NOTE:
                        nbuf = emalloc((size_t)xsh_size);
-                       if ((noff = FINFO_LSEEK_FUNC(fd, (off_t)xsh_offset, SEEK_SET)) ==
-                           (off_t)-1) {
+                       if ((noff = FINFO_LSEEK_FUNC(fd, (php_off_t)xsh_offset, SEEK_SET)) ==
+                           (php_off_t)-1) {
                                file_badread(ms);
                                efree(nbuf);
                                return -1;
@@ -974,7 +974,7 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
 
                        noff = 0;
                        for (;;) {
-                               if (noff >= (off_t)xsh_size)
+                               if (noff >= (php_off_t)xsh_size)
                                        break;
                                noff = donote(ms, nbuf, (size_t)noff,
                                    (size_t)xsh_size, clazz, swap, 4,
@@ -985,8 +985,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
                        efree(nbuf);
                        break;
                case SHT_SUNW_cap:
-                       if (FINFO_LSEEK_FUNC(fd, (off_t)xsh_offset, SEEK_SET) ==
-                           (off_t)-1) {
+                       if (FINFO_LSEEK_FUNC(fd, (php_off_t)xsh_offset, SEEK_SET) ==
+                           (php_off_t)-1) {
                                file_badseek(ms);
                                return -1;
                        }
@@ -996,7 +996,7 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
                                Elf64_Cap cap64;
                                char cbuf[/*CONSTCOND*/
                                    MAX(sizeof cap32, sizeof cap64)];
-                               if ((coff += xcap_sizeof) > (off_t)xsh_size)
+                               if ((coff += xcap_sizeof) > (php_off_t)xsh_size)
                                        break;
                                if (FINFO_READ_FUNC(fd, cbuf, (size_t)xcap_sizeof) !=
                                    (ssize_t)xcap_sizeof) {
@@ -1099,8 +1099,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  * otherwise it's statically linked.
  */
 private int
-dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
-    int num, size_t size, off_t fsize, int *flags, int sh_num)
+dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, php_off_t off,
+    int num, size_t size, php_off_t fsize, int *flags, int sh_num)
 {
        Elf32_Phdr ph32;
        Elf64_Phdr ph64;
@@ -1117,7 +1117,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
        }
 
        for ( ; num; num--) {
-               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) {
+               if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (php_off_t)-1) {
                        file_badseek(ms);
                        return -1;
                }
@@ -1161,7 +1161,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
                         * This is a PT_NOTE section; loop through all the notes
                         * in the section.
                         */
-                       if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (off_t)-1) {
+                       if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (php_off_t)-1) {
                                file_badseek(ms);
                                return -1;
                        }
@@ -1204,7 +1204,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
        int clazz;
        int swap;
        struct stat st;
-       off_t fsize;
+       php_off_t fsize;
        int flags = 0;
        Elf32_Ehdr elf32hdr;
        Elf64_Ehdr elf64hdr;
@@ -1227,7 +1227,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
        /*
         * If we cannot seek, it must be a pipe, socket or fifo.
         */
-       if((FINFO_LSEEK_FUNC(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
+       if((FINFO_LSEEK_FUNC(fd, (php_off_t)0, SEEK_SET) == (php_off_t)-1) && (errno == ESPIPE))
                fd = file_pipe2file(ms, fd, buf, nbytes);
 
        if (fstat(fd, &st) == -1) {