From a3dd514d4dc430cbe498817ffdd23171eb52a474 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Thu, 15 Sep 2022 02:23:39 +0200 Subject: [PATCH] fileinfo: Introduce php_libmagic.* to simplify patch Signed-off-by: Anatol Belski --- ext/fileinfo/config.m4 | 2 +- ext/fileinfo/config.w32 | 2 +- ext/fileinfo/fileinfo.c | 3 - ext/fileinfo/libmagic/apprentice.c | 129 ++++++++--------------------- ext/fileinfo/libmagic/buffer.c | 4 +- ext/fileinfo/libmagic/cdf.c | 51 +----------- ext/fileinfo/libmagic/cdf.h | 4 +- ext/fileinfo/libmagic/cdf_time.c | 1 - ext/fileinfo/libmagic/config.h | 9 ++ ext/fileinfo/libmagic/der.c | 2 +- ext/fileinfo/libmagic/encoding.c | 2 +- ext/fileinfo/libmagic/file.h | 31 ++----- ext/fileinfo/libmagic/funcs.c | 9 +- ext/fileinfo/libmagic/magic.c | 15 ++-- ext/fileinfo/libmagic/print.c | 1 - ext/fileinfo/libmagic/readcdf.c | 4 +- ext/fileinfo/libmagic/softmagic.c | 84 +------------------ ext/fileinfo/libmagic/strcasestr.c | 2 - ext/fileinfo/php_fileinfo.h | 2 + ext/fileinfo/php_libmagic.c | 75 +++++++++++++++++ ext/fileinfo/php_libmagic.h | 71 ++++++++++++++++ 21 files changed, 220 insertions(+), 283 deletions(-) create mode 100644 ext/fileinfo/php_libmagic.c create mode 100644 ext/fileinfo/php_libmagic.h diff --git a/ext/fileinfo/config.m4 b/ext/fileinfo/config.m4 index 1f62d464aae..fd7ba376d97 100644 --- a/ext/fileinfo/config.m4 +++ b/ext/fileinfo/config.m4 @@ -49,7 +49,7 @@ int main(void) libmagic_sources="$libmagic_sources libmagic/strcasestr.c" ],[AC_MSG_RESULT([skipped, cross-compiling])]) - PHP_NEW_EXTENSION(fileinfo, fileinfo.c $libmagic_sources, $ext_shared,,-I@ext_srcdir@/libmagic) + PHP_NEW_EXTENSION(fileinfo, fileinfo.c php_libmagic.c $libmagic_sources, $ext_shared,,-I@ext_srcdir@/libmagic) PHP_ADD_BUILD_DIR($ext_builddir/libmagic) AC_CHECK_FUNCS([utimes strndup]) diff --git a/ext/fileinfo/config.w32 b/ext/fileinfo/config.w32 index eefccb3d72b..e42f1ce3f79 100644 --- a/ext/fileinfo/config.w32 +++ b/ext/fileinfo/config.w32 @@ -10,6 +10,6 @@ if (PHP_FILEINFO != 'no') { readcdf.c softmagic.c der.c \ strcasestr.c buffer.c is_csv.c"; - EXTENSION('fileinfo', 'fileinfo.c', true, "/I" + configure_module_dirname + "/libmagic /I" + configure_module_dirname); + EXTENSION('fileinfo', 'fileinfo.c php_libmagic.c', true, "/I" + configure_module_dirname + "/libmagic /I" + configure_module_dirname); ADD_SOURCES(configure_module_dirname + '\\libmagic', LIBMAGIC_SOURCES, "fileinfo"); } diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index 90af9baa3d0..f0be4c934b1 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -14,9 +14,6 @@ +----------------------------------------------------------------------+ */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif #include "php.h" #include "libmagic/magic.h" diff --git a/ext/fileinfo/libmagic/apprentice.c b/ext/fileinfo/libmagic/apprentice.c index c8062c06182..094c5e924ce 100644 --- a/ext/fileinfo/libmagic/apprentice.c +++ b/ext/fileinfo/libmagic/apprentice.c @@ -29,8 +29,6 @@ * apprentice - make one pass through /etc/magic, learning its secrets. */ -#include "php.h" - #include "file.h" #ifndef lint @@ -39,19 +37,6 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.326 2022/09/13 18:46:07 christos Exp $") #include "magic.h" #include - -#if defined(__hpux) && !defined(HAVE_STRTOULL) -#if SIZEOF_LONG == 8 -# define strtoull strtoul -#else -# define strtoull __strtoull -#endif -#endif - -#ifdef PHP_WIN32 -#include "win32/unistd.h" -#define strtoull _strtoui64 -#else #ifdef HAVE_UNISTD_H #include #endif @@ -87,10 +72,6 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.326 2022/09/13 18:46:07 christos Exp $") #endif #endif -#ifndef offsetof -#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) -#endif - #ifndef MAP_FAILED #define MAP_FAILED (void *) -1 #endif @@ -207,6 +188,39 @@ private struct { #include "../data_file.c" +#ifdef COMPILE_ONLY + +int main(int, char *[]); + +int +main(int argc, char *argv[]) +{ + int ret; + struct magic_set *ms; + char *progname; + + if ((progname = strrchr(argv[0], '/')) != NULL) + progname++; + else + progname = argv[0]; + + if (argc != 2) { + (void)fprintf(stderr, "Usage: %s file\n", progname); + return 1; + } + + if ((ms = magic_open(MAGIC_CHECK)) == NULL) { + (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + return 1; + } + ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0; + if (ret == 1) + (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms)); + magic_close(ms); + return ret; +} +#endif /* COMPILE_ONLY */ + struct type_tbl_s { const char name[16]; const size_t len; @@ -440,15 +454,7 @@ add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx) ml->map = idx == 0 ? map : NULL; ml->magic = map->magic[idx]; ml->nmagic = map->nmagic[idx]; - if (ml->nmagic) { - ml->magic_rxcomp = CAST(file_regex_t **, - ecalloc(ml->nmagic, sizeof(*ml->magic_rxcomp))); - if (ml->magic_rxcomp == NULL) { - efree(ml); - return -1; - } - } else - ml->magic_rxcomp = NULL; + mlp->prev->next = ml; ml->prev = mlp->prev; ml->next = mlp; @@ -656,51 +662,6 @@ mlist_free(struct mlist *mlist) mlist_free_one(mlist); } -#ifndef COMPILE_ONLY -/* void **bufs: an array of compiled magic files */ -protected int -buffer_apprentice(struct magic_set *ms, struct magic **bufs, - size_t *sizes, size_t nbufs) -{ - size_t i, j; - struct mlist *ml; - struct magic_map *map; - - if (nbufs == 0) - return -1; - - (void)file_reset(ms, 0); - - init_file_tables(); - - for (i = 0; i < MAGIC_SETS; i++) { - mlist_free(ms->mlist[i]); - if ((ms->mlist[i] = mlist_alloc()) == NULL) { - file_oomem(ms, sizeof(*ms->mlist[i])); - goto fail; - } - } - - for (i = 0; i < nbufs; i++) { - map = apprentice_buf(ms, bufs[i], sizes[i]); - if (map == NULL) - goto fail; - - for (j = 0; j < MAGIC_SETS; j++) { - if (add_mlist(ms->mlist[j], map, j) == -1) { - file_oomem(ms, sizeof(*ml)); - goto fail; - } - } - } - - return 0; -fail: - mlist_free_all(ms); - return -1; -} -#endif - /* const char *fn: list of magic files and directories */ protected int file_apprentice(struct magic_set *ms, const char *fn, int action) @@ -3223,28 +3184,6 @@ eatsize(const char **p) *p = l; } -/* - * handle a buffer containing a compiled file. - */ -private struct magic_map * -apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len) -{ - struct magic_map *map; - - if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { - file_oomem(ms, sizeof(*map)); - return NULL; - } - map->len = len; - map->p = buf; - map->type = MAP_TYPE_USER; - if (check_buffer(ms, map, "buffer") != 0) { - apprentice_unmap(map); - return NULL; - } - return map; -} - /* * handle a compiled file. */ diff --git a/ext/fileinfo/libmagic/buffer.c b/ext/fileinfo/libmagic/buffer.c index 3a02308220e..b1b9e66a6d5 100644 --- a/ext/fileinfo/libmagic/buffer.c +++ b/ext/fileinfo/libmagic/buffer.c @@ -31,9 +31,7 @@ FILE_RCSID("@(#)$File: buffer.c,v 1.8 2020/02/16 15:52:49 christos Exp $") #endif /* lint */ #include "magic.h" -#ifdef PHP_WIN32 -#include "win32/unistd.h" -#else +#ifdef HAVE_UNISTD_H #include #endif #include diff --git a/ext/fileinfo/libmagic/cdf.c b/ext/fileinfo/libmagic/cdf.c index 6313db7ded7..71baaad6a8e 100644 --- a/ext/fileinfo/libmagic/cdf.c +++ b/ext/fileinfo/libmagic/cdf.c @@ -43,17 +43,9 @@ FILE_RCSID("@(#)$File: cdf.c,v 1.121 2021/10/20 13:56:15 christos Exp $") #include #endif #include - -#ifdef PHP_WIN32 -#include "win32/unistd.h" -#else +#ifdef HAVE_UNISTD_H #include #endif - -#ifndef UINT32_MAX -# define UINT32_MAX (0xffffffff) -#endif - #include #include #include @@ -101,44 +93,9 @@ static union { CDF_TOLE8(CAST(uint64_t, x)))) #define CDF_GETUINT32(x, y) cdf_getuint32(x, y) -#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n)) -#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n)) -#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u)) - - -/*ARGSUSED*/ -static void * -cdf_malloc(const char *file __attribute__((__unused__)), - size_t line __attribute__((__unused__)), size_t n) -{ - DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n", - file, line, __func__, n)); - if (n == 0) - n++; - return malloc(n); -} - -/*ARGSUSED*/ -static void * -cdf_realloc(const char *file __attribute__((__unused__)), - size_t line __attribute__((__unused__)), void *p, size_t n) -{ - DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n", - file, line, __func__, n)); - return realloc(p, n); -} - -/*ARGSUSED*/ -static void * -cdf_calloc(const char *file __attribute__((__unused__)), - size_t line __attribute__((__unused__)), size_t n, size_t u) -{ - DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u %" - SIZE_T_FORMAT "u\n", file, line, __func__, n, u)); - if (n == 0) - n++; - return calloc(n, u); -} +#define CDF_MALLOC(n) emalloc(n) +#define CDF_REALLOC(p, n) erealloc(p, n) +#define CDF_CALLOC(n, u) ecalloc(n, u) #if defined(HAVE_BYTESWAP_H) # define _cdf_tole2(x) bswap_16(x) diff --git a/ext/fileinfo/libmagic/cdf.h b/ext/fileinfo/libmagic/cdf.h index 07f1dd8c157..587bed84d91 100644 --- a/ext/fileinfo/libmagic/cdf.h +++ b/ext/fileinfo/libmagic/cdf.h @@ -35,10 +35,8 @@ #ifndef _H_CDF_ #define _H_CDF_ -#ifdef PHP_WIN32 +#ifdef WIN32 #include -#define asctime_r php_asctime_r -#define ctime_r php_ctime_r #endif #ifdef __DJGPP__ #define timespec timeval diff --git a/ext/fileinfo/libmagic/cdf_time.c b/ext/fileinfo/libmagic/cdf_time.c index ca80300cd4d..4d72e908451 100644 --- a/ext/fileinfo/libmagic/cdf_time.c +++ b/ext/fileinfo/libmagic/cdf_time.c @@ -23,7 +23,6 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -#include "php.h" #include "file.h" diff --git a/ext/fileinfo/libmagic/config.h b/ext/fileinfo/libmagic/config.h index 22ce7f63974..5acdf168730 100644 --- a/ext/fileinfo/libmagic/config.h +++ b/ext/fileinfo/libmagic/config.h @@ -1 +1,10 @@ #include "php.h" +#include "php_libmagic.h" + +#ifndef HAVE_STDINT_H +#define HAVE_STDINT_H 1 +#endif + +#ifndef HAVE_STDINT_H +#define HAVE_STDINT_H 1 +#endif diff --git a/ext/fileinfo/libmagic/der.c b/ext/fileinfo/libmagic/der.c index 617d73e599a..4b6b48829d8 100644 --- a/ext/fileinfo/libmagic/der.c +++ b/ext/fileinfo/libmagic/der.c @@ -54,7 +54,7 @@ FILE_RCSID("@(#)$File: der.c,v 1.24 2022/07/30 18:08:36 christos Exp $") #include "magic.h" #include "der.h" #else -#ifndef PHP_WIN32 +#ifdef HAVE_SYS_MMAN_H #include #endif #include diff --git a/ext/fileinfo/libmagic/encoding.c b/ext/fileinfo/libmagic/encoding.c index 32415eaaf50..db94607812a 100644 --- a/ext/fileinfo/libmagic/encoding.c +++ b/ext/fileinfo/libmagic/encoding.c @@ -175,7 +175,7 @@ file_encoding(struct magic_set *ms, const struct buffer *b, done: if (ubuf == &udefbuf) - free(udefbuf); + efree(udefbuf); return rv; } diff --git a/ext/fileinfo/libmagic/file.h b/ext/fileinfo/libmagic/file.h index 44e4924b720..45c59a2fd2b 100644 --- a/ext/fileinfo/libmagic/file.h +++ b/ext/fileinfo/libmagic/file.h @@ -35,12 +35,13 @@ #include "config.h" -#include "php.h" -#include "ext/standard/php_string.h" -#include "ext/pcre/php_pcre.h" - +#ifdef HAVE_STDINT_H #include +#endif + +#ifdef HAVE_INTTYPES_H #include +#endif #ifndef __STDC_LIMIT_MACROS #define __STDC_LIMIT_MACROS @@ -78,17 +79,13 @@ #include /* For open and flags */ #include -#ifdef PHP_WIN32 -#include "win32/param.h" -#else +#ifndef WIN32 #include #endif /* Do this here and now, because struct stat gets re-defined on solaris */ #include #include -#define abort() zend_error_noreturn(E_ERROR, "fatal libmagic error") - #define ENABLE_CONDITIONALS #ifndef MAGIC @@ -167,6 +164,8 @@ #define FILE_COMPILE 2 #define FILE_LIST 3 +typedef void* file_regex_t; + struct buffer { int fd; zend_stat_t st; @@ -586,8 +585,6 @@ protected void buffer_init(struct buffer *, int, const zend_stat_t *, protected void buffer_fini(struct buffer *); protected int buffer_fill(const struct buffer *); -public zend_string* convert_libmagic_pattern(const char *val, size_t len, uint32_t options); - typedef struct { char *buf; size_t blen; @@ -650,16 +647,4 @@ static const char *rcsid(const char *p) { \ #define __RCSID(a) #endif -#ifdef PHP_WIN32 -#ifdef _WIN64 -#define FINFO_LSEEK_FUNC _lseeki64 -#else -#define FINFO_LSEEK_FUNC _lseek -#endif -#define FINFO_READ_FUNC _read -#else -#define FINFO_LSEEK_FUNC lseek -#define FINFO_READ_FUNC read -#endif - #endif /* __file_h__ */ diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c index ed6f4b43ec3..35c93eb2197 100644 --- a/ext/fileinfo/libmagic/funcs.c +++ b/ext/fileinfo/libmagic/funcs.c @@ -51,13 +51,6 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.131 2022/09/13 18:46:07 christos Exp $") #define SIZE_MAX ((size_t)~0) #endif -#include "php.h" -#include "main/php_network.h" - -#ifndef PREG_OFFSET_CAPTURE -# define PREG_OFFSET_CAPTURE (1<<8) -#endif - protected char * file_copystr(char *buf, size_t blen, size_t width, const char *str) { @@ -139,7 +132,7 @@ file_checkfmt(char *msg, size_t mlen, const char *fmt) protected int file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) { - size_t len; + int len; char *buf, *newstr; char tbuf[1024]; diff --git a/ext/fileinfo/libmagic/magic.c b/ext/fileinfo/libmagic/magic.c index 72d824ac04c..e7be9d6af5d 100644 --- a/ext/fileinfo/libmagic/magic.c +++ b/ext/fileinfo/libmagic/magic.c @@ -25,6 +25,11 @@ * SUCH DAMAGE. */ +#ifdef WIN32 +#include +#include +#endif + #include "file.h" #ifndef lint @@ -34,16 +39,12 @@ FILE_RCSID("@(#)$File: magic.c,v 1.117 2021/12/06 15:33:00 christos Exp $") #include "magic.h" #include -#ifdef PHP_WIN32 -#include "win32/unistd.h" -#else +#ifdef HAVE_UNISTD_H #include #endif #include -#include "config.h" - -#ifdef PHP_WIN32 -#include +#ifdef QUICK +#include #endif #include /* for PIPE_BUF */ diff --git a/ext/fileinfo/libmagic/print.c b/ext/fileinfo/libmagic/print.c index 482d123266a..f3da80ac1fe 100644 --- a/ext/fileinfo/libmagic/print.c +++ b/ext/fileinfo/libmagic/print.c @@ -28,7 +28,6 @@ /* * print.c - debugging printout routines */ -#include "php.h" #include "file.h" diff --git a/ext/fileinfo/libmagic/readcdf.c b/ext/fileinfo/libmagic/readcdf.c index ad106830093..6edbc58ccd6 100644 --- a/ext/fileinfo/libmagic/readcdf.c +++ b/ext/fileinfo/libmagic/readcdf.c @@ -31,9 +31,7 @@ FILE_RCSID("@(#)$File: readcdf.c,v 1.76 2022/01/17 16:59:01 christos Exp $") #include #include -#ifdef PHP_WIN32 -#include "win32/unistd.h" -#else +#ifdef HAVE_UNISTD_H #include #endif #include diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c index cf8e313d872..2ef86d9ef0d 100644 --- a/ext/fileinfo/libmagic/softmagic.c +++ b/ext/fileinfo/libmagic/softmagic.c @@ -43,10 +43,6 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.328 2022/09/13 18:46:07 christos Exp $") #include #include "der.h" -#ifndef PREG_OFFSET_CAPTURE -# define PREG_OFFSET_CAPTURE (1<<8) -#endif - private int match(struct magic_set *, struct magic *, file_regex_t **, size_t, const struct buffer *, size_t, int, int, int, uint16_t *, uint16_t *, int *, int *, int *, int *); @@ -2050,81 +2046,6 @@ file_strncmp16(const char *a, const char *b, size_t len, size_t maxlen, return file_strncmp(a, b, len, maxlen, flags); } -private file_regex_t * -alloc_regex(struct magic_set *ms, struct magic *m) -{ - int rc; - file_regex_t *rx = CAST(file_regex_t *, malloc(sizeof(*rx))); - - if (rx == NULL) { - file_error(ms, errno, "can't allocate %" SIZE_T_FORMAT - "u bytes", sizeof(*rx)); - return NULL; - } - - rc = file_regcomp(ms, rx, m->value.s, REG_EXTENDED | REG_NEWLINE | - ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0)); - if (rc == 0) - return rx; - - free(rx); - return NULL; -} - -public zend_string* convert_libmagic_pattern(const char *val, size_t len, uint32_t options) -{ - int i, j; - zend_string *t; - - for (i = j = 0; i < len; i++) { - switch (val[i]) { - case '~': - j += 2; - break; - case '\0': - j += 4; - break; - default: - j++; - break; - } - } - t = zend_string_alloc(j + 4, 0); - - j = 0; - ZSTR_VAL(t)[j++] = '~'; - - for (i = 0; i < len; i++, j++) { - switch (val[i]) { - case '~': - ZSTR_VAL(t)[j++] = '\\'; - ZSTR_VAL(t)[j] = '~'; - break; - case '\0': - ZSTR_VAL(t)[j++] = '\\'; - ZSTR_VAL(t)[j++] = 'x'; - ZSTR_VAL(t)[j++] = '0'; - ZSTR_VAL(t)[j] = '0'; - break; - default: - ZSTR_VAL(t)[j] = val[i]; - break; - } - } - ZSTR_VAL(t)[j++] = '~'; - - if (options & PCRE2_CASELESS) - ZSTR_VAL(t)[j++] = 'i'; - - if (options & PCRE2_MULTILINE) - ZSTR_VAL(t)[j++] = 'm'; - - ZSTR_VAL(t)[j]='\0'; - ZSTR_LEN(t) = j; - - return t; -} - private int magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache) { @@ -2297,6 +2218,7 @@ magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache) ms->search.rm_len = ms->search.s_len - idx; break; } +#endif for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) { if (slen + idx > ms->search.s_len) { @@ -2331,10 +2253,6 @@ magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache) if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) { zend_string_release(pattern); return -1; - } - memcpy(copy, ms->search.s, slen); - copy[--slen] = '\0'; - search = copy; } else { /* pce now contains the compiled regex */ zval retval; diff --git a/ext/fileinfo/libmagic/strcasestr.c b/ext/fileinfo/libmagic/strcasestr.c index 4d62787c70d..3db407f3d8b 100644 --- a/ext/fileinfo/libmagic/strcasestr.c +++ b/ext/fileinfo/libmagic/strcasestr.c @@ -39,8 +39,6 @@ __RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); #include "file.h" -#include -#include #include #include #include diff --git a/ext/fileinfo/php_fileinfo.h b/ext/fileinfo/php_fileinfo.h index bd96ba3d93f..a0536294429 100644 --- a/ext/fileinfo/php_fileinfo.h +++ b/ext/fileinfo/php_fileinfo.h @@ -24,6 +24,8 @@ extern zend_module_entry fileinfo_module_entry; #ifdef PHP_WIN32 #define PHP_FILEINFO_API __declspec(dllexport) +#elif defined(__GNUC__) && __GNUC__ >= 4 +#define PHP_FILEINFO_API __attribute__((visibility("default"))) #else #define PHP_FILEINFO_API #endif diff --git a/ext/fileinfo/php_libmagic.c b/ext/fileinfo/php_libmagic.c new file mode 100644 index 00000000000..eaa7d3e94e6 --- /dev/null +++ b/ext/fileinfo/php_libmagic.c @@ -0,0 +1,75 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Anatol Belski | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "php_libmagic.h" + +zend_string* convert_libmagic_pattern(const char *val, size_t len, uint32_t options) +{ + int i, j; + zend_string *t; + + for (i = j = 0; i < len; i++) { + switch (val[i]) { + case '~': + j += 2; + break; + case '\0': + j += 4; + break; + default: + j++; + break; + } + } + t = zend_string_alloc(j + 4, 0); + + j = 0; + ZSTR_VAL(t)[j++] = '~'; + + for (i = 0; i < len; i++, j++) { + switch (val[i]) { + case '~': + ZSTR_VAL(t)[j++] = '\\'; + ZSTR_VAL(t)[j] = '~'; + break; + case '\0': + ZSTR_VAL(t)[j++] = '\\'; + ZSTR_VAL(t)[j++] = 'x'; + ZSTR_VAL(t)[j++] = '0'; + ZSTR_VAL(t)[j] = '0'; + break; + default: + ZSTR_VAL(t)[j] = val[i]; + break; + } + } + ZSTR_VAL(t)[j++] = '~'; + + if (options & PCRE2_CASELESS) + ZSTR_VAL(t)[j++] = 'i'; + + if (options & PCRE2_MULTILINE) + ZSTR_VAL(t)[j++] = 'm'; + + ZSTR_VAL(t)[j]='\0'; + ZSTR_LEN(t) = j; + + return t; +} + + + diff --git a/ext/fileinfo/php_libmagic.h b/ext/fileinfo/php_libmagic.h new file mode 100644 index 00000000000..b02fa782f25 --- /dev/null +++ b/ext/fileinfo/php_libmagic.h @@ -0,0 +1,71 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Anatol Belski | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_LIBMAGIC_H +#define PHP_LIBMAGIC_H + +#include "php_fileinfo.h" + +#include "main/php_network.h" +#include "ext/standard/php_string.h" +#include "ext/pcre/php_pcre.h" + +#ifdef PHP_WIN32 +#include "win32/param.h" +#include "win32/unistd.h" + +#ifdef _WIN64 +#define FINFO_LSEEK_FUNC _lseeki64 +#else +#define FINFO_LSEEK_FUNC _lseek +#endif +#define FINFO_READ_FUNC _read + +#define strtoull _strtoui64 + +#define asctime_r php_asctime_r +#define ctime_r php_ctime_r + +#else +#define FINFO_LSEEK_FUNC lseek +#define FINFO_READ_FUNC read +#endif + +#if defined(__hpux) && !defined(HAVE_STRTOULL) +#if SIZEOF_LONG == 8 +# define strtoull strtoul +#else +# define strtoull __strtoull +#endif +#endif + +#ifndef offsetof +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) +#endif + +#ifndef UINT32_MAX +# define UINT32_MAX (0xffffffff) +#endif + +#ifndef PREG_OFFSET_CAPTURE +# define PREG_OFFSET_CAPTURE (1<<8) +#endif + +#define abort() zend_error_noreturn(E_ERROR, "fatal libmagic error") + +zend_string* convert_libmagic_pattern(const char *val, size_t len, uint32_t options); + +#endif /* PHP_LIBMAGIC_H */