Download src/php/php-src/ext/fileinfo/libmagic.patch from meddyrat/dis-bench: direct link, hf CLI and curl.
- Browser
- Download file 99.9 kB
-
https://huggingface.co/datasets/meddyrat/dis-bench/resolve/main/src/php/php-src/ext/fileinfo/libmagic.patch
- Command line
-
hf download hf://datasets/meddyrat/dis-bench/src/php/php-src/ext/fileinfo/libmagic.patch
-
curl -L -o libmagic.patch https://huggingface.co/datasets/meddyrat/dis-bench/resolve/main/src/php/php-src/ext/fileinfo/libmagic.patch
99.9 kB
| diff -u libmagic.orig/apprentice.c libmagic/apprentice.c | |
| --- libmagic.orig/apprentice.c 2024-11-27 16:37:00.000000000 +0100 | |
| +++ libmagic/apprentice.c 2025-02-09 02:25:02.364884555 +0100 | |
| #include "file.h" | |
| #ifndef lint | |
| -FILE_RCSID("@(#)$File: apprentice.c,v 1.356 2024/11/27 15:37:00 christos Exp $") | |
| +FILE_RCSID("@(#)$File: apprentice.c,v 1.355 2024/11/25 22:31:53 christos Exp $") | |
| #endif /* lint */ | |
| #include "magic.h" | |
| #ifdef QUICK | |
| #include <sys/mman.h> | |
| #endif | |
| +#ifdef HAVE_DIRENT_H | |
| #include <dirent.h> | |
| +#endif | |
| #include <limits.h> | |
| #ifdef HAVE_BYTESWAP_H | |
| #include <byteswap.h> | |
| #endif | |
| file_private char *mkdbname(struct magic_set *, const char *, int); | |
| -file_private struct magic_map *apprentice_buf(struct magic_set *, struct magic *, | |
| - size_t); | |
| file_private struct magic_map *apprentice_map(struct magic_set *, const char *); | |
| -file_private int check_buffer(struct magic_set *, struct magic_map *, const char *); | |
| file_private void apprentice_unmap(struct magic_map *); | |
| file_private int apprentice_compile(struct magic_set *, struct magic_map *, | |
| const char *); | |
| { NULL, 0, NULL } | |
| }; | |
| +#include "../data_file.c" | |
| + | |
| #ifdef COMPILE_ONLY | |
| int main(int, char *[]); | |
| { | |
| int type; | |
| - if (isalpha(CAST(unsigned char, l[1]))) { | |
| + if (l[0] == '\0') | |
| + return FILE_INVALID; | |
| + if (l[1] == '\0') | |
| + return FILE_INVALID; | |
| + else if (isalpha(CAST(unsigned char, l[1]))) { | |
| switch (l[1]) { | |
| case 'C': | |
| /* "dC" and "uC" */ | |
| struct mlist *ml; | |
| mlp->map = NULL; | |
| - if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL) | |
| + if ((ml = CAST(struct mlist *, emalloc(sizeof(*ml)))) == NULL) | |
| return -1; | |
| 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 **, | |
| - calloc(ml->nmagic, sizeof(*ml->magic_rxcomp))); | |
| - if (ml->magic_rxcomp == NULL) { | |
| - free(ml); | |
| - return -1; | |
| - } | |
| - } else | |
| - ml->magic_rxcomp = NULL; | |
| + | |
| mlp->prev->next = ml; | |
| ml->prev = mlp->prev; | |
| ml->next = mlp; | |
| return; | |
| for (i = 0; i < MAGIC_SETS; i++) | |
| mlist_free(ms->mlist[i]); | |
| - free(ms->o.pbuf); | |
| - free(ms->o.buf); | |
| - free(ms->c.li); | |
| - free(ms->fnamebuf); | |
| + efree(ms->o.pbuf); | |
| + efree(ms->o.buf); | |
| + efree(ms->c.li); | |
| #ifdef USE_C_LOCALE | |
| freelocale(ms->c_lc_ctype); | |
| #endif | |
| - free(ms); | |
| + efree(ms); | |
| } | |
| file_protected struct magic_set * | |
| struct magic_set *ms; | |
| size_t i, len; | |
| - if ((ms = CAST(struct magic_set *, calloc(CAST(size_t, 1u), | |
| + if ((ms = CAST(struct magic_set *, ecalloc(CAST(size_t, 1u), | |
| sizeof(*ms)))) == NULL) | |
| return NULL; | |
| ms->o.blen = 0; | |
| 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; | |
| ms->error = -1; | |
| for (i = 0; i < MAGIC_SETS; i++) | |
| ms->mlist[i] = NULL; | |
| - ms->fnamebuf = NULL; | |
| ms->file = "unknown"; | |
| ms->line = 0; | |
| ms->magwarn = 0; | |
| #endif | |
| return ms; | |
| free: | |
| - free(ms); | |
| + efree(ms); | |
| return NULL; | |
| } | |
| file_private void | |
| apprentice_unmap(struct magic_map *map) | |
| { | |
| - size_t i; | |
| - char *p; | |
| if (map == NULL) | |
| return; | |
| - | |
| - switch (map->type) { | |
| - case MAP_TYPE_USER: | |
| - break; | |
| - case MAP_TYPE_MALLOC: | |
| - p = CAST(char *, map->p); | |
| - for (i = 0; i < MAGIC_SETS; i++) { | |
| - char *b = RCAST(char *, map->magic[i]); | |
| - if (p != NULL && b >= p && b <= p + map->len) | |
| - continue; | |
| - free(b); | |
| + if (map->p != php_magic_database) { | |
| + if (map->p == NULL) { | |
| + int j; | |
| + for (j = 0; j < MAGIC_SETS; j++) { | |
| + if (map->magic[j]) { | |
| + efree(map->magic[j]); | |
| + } | |
| + } | |
| + } else { | |
| + efree(map->p); | |
| } | |
| - free(p); | |
| - break; | |
| -#ifdef QUICK | |
| - case MAP_TYPE_MMAP: | |
| - if (map->p && map->p != MAP_FAILED) | |
| - (void)munmap(map->p, map->len); | |
| - break; | |
| -#endif | |
| - default: | |
| - fprintf(stderr, "Bad map type %d", map->type); | |
| - abort(); | |
| } | |
| - free(map); | |
| + efree(map); | |
| } | |
| file_private struct mlist * | |
| mlist_alloc(void) | |
| { | |
| - struct mlist *mlist; | |
| - if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) { | |
| - return NULL; | |
| - } | |
| + struct mlist *mlist = CAST(struct mlist *, ecalloc(1, sizeof(*mlist))); | |
| mlist->next = mlist->prev = mlist; | |
| return mlist; | |
| } | |
| file_private void | |
| mlist_free_one(struct mlist *ml) | |
| { | |
| - size_t i; | |
| - | |
| if (ml->map) | |
| apprentice_unmap(CAST(struct magic_map *, ml->map)); | |
| - | |
| - for (i = 0; i < ml->nmagic; ++i) { | |
| - if (ml->magic_rxcomp[i]) { | |
| - file_regfree(ml->magic_rxcomp[i]); | |
| - free(ml->magic_rxcomp[i]); | |
| - ml->magic_rxcomp[i] = NULL; | |
| - } | |
| - } | |
| - free(ml->magic_rxcomp); | |
| - ml->magic_rxcomp = NULL; | |
| - free(ml); | |
| + efree(ml); | |
| } | |
| file_private void | |
| mlist_free_one(mlist); | |
| } | |
| -#ifndef COMPILE_ONLY | |
| -/* void **bufs: an array of compiled magic files */ | |
| -file_protected int | |
| -buffer_apprentice(struct magic_set *ms, struct magic **bufs, | |
| - size_t *sizes, size_t nbufs) | |
| -{ | |
| - size_t i, j; | |
| - 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[0])); | |
| - 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(*ms->mlist[0])); | |
| - goto fail; | |
| - } | |
| - } | |
| - } | |
| - | |
| - return 0; | |
| -fail: | |
| - mlist_free_all(ms); | |
| - return -1; | |
| -} | |
| -#endif | |
| - | |
| /* const char *fn: list of magic files and directories */ | |
| file_protected int | |
| file_apprentice(struct magic_set *ms, const char *fn, int action) | |
| { | |
| char *p; | |
| int fileerr, errs = -1; | |
| - size_t i, j; | |
| + size_t i; | |
| (void)file_reset(ms, 0); | |
| + /* XXX disabling default magic loading so the compiled in data is used */ | |
| +#if 0 | |
| if ((fn = magic_getpath(fn, action)) == NULL) | |
| return -1; | |
| +#endif | |
| init_file_tables(); | |
| - free(ms->fnamebuf); | |
| - if ((ms->fnamebuf = strdup(fn)) == NULL) { | |
| - file_oomem(ms, strlen(fn)); | |
| - return -1; | |
| - } | |
| - | |
| 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[0])); | |
| - for (j = 0; j < i; j++) { | |
| - mlist_free(ms->mlist[j]); | |
| - ms->mlist[j] = NULL; | |
| - } | |
| - return -1; | |
| - } | |
| + ms->mlist[i] = mlist_alloc(); | |
| } | |
| - fn = ms->fnamebuf; | |
| + | |
| + if (fn == NULL) | |
| + fn = getenv("MAGIC"); | |
| + if (fn == NULL) { | |
| + return apprentice_1(ms, fn, action); | |
| + } | |
| + | |
| + char *mfn = estrdup(fn); /* modified in loop */ | |
| + fn = mfn; | |
| while (fn) { | |
| p = CCAST(char *, strchr(fn, PATHSEP)); | |
| fn = p; | |
| } | |
| + efree(mfn); | |
| + | |
| if (errs == -1) { | |
| for (i = 0; i < MAGIC_SETS; i++) { | |
| mlist_free(ms->mlist[i]); | |
| mpa.lineno = mpb.lineno = 0; | |
| int x = memcmp(&mpa, &mpb, sizeof(mpa)); | |
| if (x == 0) { | |
| + /* For PHP BC: don't warn */ | |
| +#if 0 | |
| // Don't warn for DER | |
| if (mpa.type == FILE_DER) | |
| return 0; | |
| file_mdump(ma->mp); | |
| file_mdump(mb->mp); | |
| +#endif | |
| return 0; | |
| } | |
| return x > 0 ? -1 : 1; | |
| } | |
| size_t incr = mset[i].max + ALLOC_INCR; | |
| if ((mp = CAST(struct magic_entry *, | |
| - realloc(mset[i].me, sizeof(*mp) * incr))) == | |
| + erealloc(mset[i].me, sizeof(*mp) * incr))) == | |
| NULL) { | |
| file_oomem(ms, sizeof(*mp) * incr); | |
| return -1; | |
| load_1(struct magic_set *ms, int action, const char *fn, int *errs, | |
| struct magic_entry_set *mset) | |
| { | |
| - size_t lineno = 0, llen = 0; | |
| + char buffer[BUFSIZ + 1]; | |
| char *line = NULL; | |
| - ssize_t len; | |
| + size_t len; | |
| + size_t lineno = 0; | |
| struct magic_entry me; | |
| - FILE *f = fopen(ms->file = fn, "r"); | |
| - if (f == NULL) { | |
| + php_stream *stream; | |
| + | |
| + | |
| + ms->file = fn; | |
| + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL); | |
| + | |
| + if (stream == NULL) { | |
| if (errno != ENOENT) | |
| file_error(ms, errno, "cannot read magic file `%s'", | |
| fn); | |
| memset(&me, 0, sizeof(me)); | |
| /* read and parse this file */ | |
| - for (ms->line = 1; (len = getline(&line, &llen, f)) != -1; | |
| - ms->line++) { | |
| + for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &len)) != NULL; | |
| + ms->line++) { | |
| if (ms->magwarn >= ms->magwarn_max) | |
| break; | |
| if (len == 0) /* null line, garbage, etc */ | |
| } | |
| if (me.mp) | |
| (void)addentry(ms, &me, mset); | |
| - free(line); | |
| - (void)fclose(f); | |
| + efree(line); | |
| + php_stream_close(stream); | |
| } | |
| /* | |
| } | |
| slen = sizeof(**ma) * mentrycount; | |
| - if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) { | |
| + if ((*ma = CAST(struct magic *, emalloc(slen))) == NULL) { | |
| file_oomem(ms, slen); | |
| return -1; | |
| } | |
| if (me == NULL) | |
| return; | |
| for (i = 0; i < nme; i++) | |
| - free(me[i].mp); | |
| - free(me); | |
| + efree(me[i].mp); | |
| + efree(me); | |
| } | |
| file_private struct magic_map * | |
| int errs = 0; | |
| uint32_t i, j; | |
| size_t files = 0, maxfiles = 0; | |
| - char **filearr = NULL, *mfn; | |
| - struct stat st; | |
| + char **filearr = NULL; | |
| + zend_stat_t st = {0}; | |
| struct magic_map *map; | |
| struct magic_entry_set mset[MAGIC_SETS]; | |
| - DIR *dir; | |
| - struct dirent *d; | |
| + php_stream *dir; | |
| + php_stream_dirent d; | |
| + | |
| memset(mset, 0, sizeof(mset)); | |
| ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */ | |
| - if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) | |
| + if ((map = CAST(struct magic_map *, ecalloc(1, sizeof(*map)))) == NULL) | |
| { | |
| file_oomem(ms, sizeof(*map)); | |
| return NULL; | |
| (void)fprintf(stderr, "%s\n", usg_hdr); | |
| /* load directory or file */ | |
| - if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) { | |
| - dir = opendir(fn); | |
| + /* 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)) { | |
| + int mflen; | |
| + char mfn[MAXPATHLEN]; | |
| + | |
| + dir = php_stream_opendir((char *)fn, REPORT_ERRORS, NULL); | |
| if (!dir) { | |
| errs++; | |
| goto out; | |
| } | |
| - while ((d = readdir(dir)) != NULL) { | |
| - if (d->d_name[0] == '.') | |
| - continue; | |
| - if (asprintf(&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, | |
| - strlen(fn) + strlen(d->d_name) + 2); | |
| + strlen(fn) + strlen(d.d_name) + 2); | |
| errs++; | |
| - closedir(dir); | |
| + php_stream_closedir(dir); | |
| goto out; | |
| } | |
| - if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { | |
| - free(mfn); | |
| + if (zend_stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { | |
| continue; | |
| } | |
| if (files >= maxfiles) { | |
| size_t mlen; | |
| - char **nfilearr; | |
| maxfiles = (maxfiles + 1) * 2; | |
| mlen = maxfiles * sizeof(*filearr); | |
| - if ((nfilearr = CAST(char **, | |
| - realloc(filearr, mlen))) == NULL) { | |
| + if ((filearr = CAST(char **, | |
| + erealloc(filearr, mlen))) == NULL) { | |
| file_oomem(ms, mlen); | |
| - free(mfn); | |
| - closedir(dir); | |
| + php_stream_closedir(dir); | |
| errs++; | |
| goto out; | |
| } | |
| - filearr = nfilearr; | |
| } | |
| - filearr[files++] = mfn; | |
| + filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen); | |
| } | |
| - closedir(dir); | |
| + php_stream_closedir(dir); | |
| if (filearr) { | |
| qsort(filearr, files, sizeof(*filearr), cmpstrp); | |
| for (i = 0; i < files; i++) { | |
| load_1(ms, action, filearr[i], &errs, mset); | |
| - free(filearr[i]); | |
| + efree(filearr[i]); | |
| } | |
| - free(filearr); | |
| - filearr = NULL; | |
| + efree(filearr); | |
| } | |
| } else | |
| load_1(ms, action, fn, &errs, mset); | |
| } | |
| out: | |
| - free(filearr); | |
| for (j = 0; j < MAGIC_SETS; j++) | |
| magic_entry_free(mset[j].me, mset[j].count); | |
| if (me->cont_count == me->max_count) { | |
| struct magic *nm; | |
| size_t cnt = me->max_count + ALLOC_CHUNK; | |
| - if ((nm = CAST(struct magic *, realloc(me->mp, | |
| + if ((nm = CAST(struct magic *, erealloc(me->mp, | |
| sizeof(*nm) * cnt))) == NULL) { | |
| file_oomem(ms, sizeof(*nm) * cnt); | |
| return -1; | |
| static const size_t len = sizeof(*m) * ALLOC_CHUNK; | |
| 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; | |
| } | |
| } | |
| /* get offset, then skip over it */ | |
| - if (*l == '-' || *l == '+') { | |
| + if (*l == '-') { | |
| ++l; /* step over */ | |
| - m->flag |= l[-1] == '-' ? OFFNEGATIVE : OFFPOSITIVE; | |
| + m->flag |= OFFNEGATIVE; | |
| } | |
| m->offset = CAST(int32_t, strtol(l, &t, 0)); | |
| if (l == t) { | |
| m->mask_op = 0; | |
| if (*l == '~') { | |
| - if (!IS_STRING(m->type)) | |
| + if (!IS_LIBMAGIC_STRING(m->type)) | |
| m->mask_op |= FILE_OPINVERSE; | |
| else if (ms->flags & MAGIC_CHECK) | |
| file_magwarn(ms, "'~' invalid for string types"); | |
| m->str_range = 0; | |
| m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0; | |
| if ((op = get_op(*l)) != -1) { | |
| - if (IS_STRING(m->type)) { | |
| + if (IS_LIBMAGIC_STRING(m->type)) { | |
| int r; | |
| if (op != FILE_OPDIVIDE) { | |
| } | |
| break; | |
| } | |
| + | |
| /* | |
| * Grab the value part, except for an 'x' reln. | |
| */ | |
| ++l; | |
| m->flag |= NOSPACE; | |
| } | |
| - for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); ) | |
| + for (i = 0; i < sizeof(m->desc) && (m->desc[i++] = *l++) != '\0';) | |
| continue; | |
| if (m->desc[0] == '\0') { | |
| // Tuck in the filename for debugging. | |
| file_private int | |
| parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line, | |
| - size_t llen, off_t off, size_t len, const char *name, const char *extra, | |
| + size_t llen, zend_off_t off, size_t len, const char *name, const char *extra, | |
| int nt) | |
| { | |
| size_t i; | |
| return -1; | |
| } | |
| if (m->type == FILE_REGEX) { | |
| - file_regex_t rx; | |
| - int rc = | |
| - file_regcomp(ms, &rx, m->value.s, REG_EXTENDED); | |
| - if (rc == 0) { | |
| - file_regfree(&rx); | |
| + zend_string *pattern; | |
| + int options = 0; | |
| + pcre_cache_entry *pce; | |
| + | |
| + pattern = convert_libmagic_pattern(m->value.s, strlen(m->value.s), options); | |
| + | |
| + if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) { | |
| + zend_string_release(pattern); | |
| + return -1; | |
| } | |
| - return rc ? -1 : 0; | |
| + zend_string_release(pattern); | |
| + | |
| + return 0; | |
| } | |
| return 0; | |
| default: | |
| } | |
| /* | |
| - * handle a buffer containing a compiled file. | |
| - */ | |
| -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. | |
| */ | |
| file_private struct magic_map * | |
| apprentice_map(struct magic_set *ms, const char *fn) | |
| { | |
| - int fd; | |
| - struct stat st; | |
| + uint32_t *ptr; | |
| + uint32_t version, entries = 0, nentries; | |
| + int needsbyteswap; | |
| char *dbname = NULL; | |
| struct magic_map *map; | |
| - struct magic_map *rv = NULL; | |
| + size_t i; | |
| + php_stream *stream = NULL; | |
| + php_stream_statbuf st; | |
| - fd = -1; | |
| - if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) { | |
| - file_oomem(ms, sizeof(*map)); | |
| - goto error; | |
| - } | |
| - map->type = MAP_TYPE_USER; /* unspecified */ | |
| + map = CAST(struct magic_map *, ecalloc(1, sizeof(*map))); | |
| + | |
| + if (fn == NULL) { | |
| + map->p = (void *)&php_magic_database; | |
| + 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); | |
| if (dbname == NULL) | |
| goto error; | |
| - if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1) | |
| + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL); | |
| + | |
| + if (!stream) { | |
| goto error; | |
| + } | |
| - if (fstat(fd, &st) == -1) { | |
| +#ifndef PHP_WIN32 | |
| + if (php_stream_stat(stream, &st) < 0) { | |
| file_error(ms, errno, "cannot stat `%s'", dbname); | |
| goto error; | |
| } | |
| - if (st.st_size < 8 || st.st_size > maxoff_t()) { | |
| +#endif | |
| + if (st.sb.st_size < 8 || st.sb.st_size > maxoff_t()) { | |
| file_error(ms, 0, "file `%s' is too %s", dbname, | |
| - st.st_size < 8 ? "small" : "large"); | |
| + st.sb.st_size < 8 ? "small" : "large"); | |
| goto error; | |
| } | |
| - map->len = CAST(size_t, st.st_size); | |
| -#ifdef QUICK | |
| - map->type = MAP_TYPE_MMAP; | |
| - if ((map->p = mmap(0, CAST(size_t, st.st_size), PROT_READ|PROT_WRITE, | |
| - MAP_PRIVATE|MAP_FILE, fd, CAST(off_t, 0))) == MAP_FAILED) { | |
| - file_error(ms, errno, "cannot map `%s'", dbname); | |
| - goto error; | |
| - } | |
| -#else | |
| map->type = MAP_TYPE_MALLOC; | |
| - if ((map->p = CAST(void *, malloc(map->len))) == NULL) { | |
| - file_oomem(ms, map->len); | |
| - goto error; | |
| - } | |
| - if (read(fd, map->p, map->len) != (ssize_t)map->len) { | |
| - file_badread(ms); | |
| - goto error; | |
| - } | |
| -#endif | |
| - (void)close(fd); | |
| - fd = -1; | |
| + map->len = CAST(size_t, st.sb.st_size); | |
| + map->p = CAST(void *, emalloc(map->len)); | |
| - if (check_buffer(ms, map, dbname) != 0) { | |
| - goto error; | |
| - } | |
| -#ifdef QUICK | |
| - if (mprotect(map->p, CAST(size_t, st.st_size), PROT_READ) == -1) { | |
| - file_error(ms, errno, "cannot mprotect `%s'", dbname); | |
| + if (php_stream_read(stream, map->p, (size_t)st.sb.st_size) != (size_t)st.sb.st_size) { | |
| + file_badread(ms); | |
| goto error; | |
| } | |
| -#endif | |
| - | |
| - free(dbname); | |
| - return map; | |
| -error: | |
| - if (fd != -1) | |
| - (void)close(fd); | |
| - apprentice_unmap(map); | |
| - free(dbname); | |
| - return rv; | |
| -} | |
| + php_stream_close(stream); | |
| + stream = NULL; | |
| -file_private int | |
| -check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname) | |
| -{ | |
| - uint32_t *ptr; | |
| - uint32_t entries, nentries; | |
| - uint32_t version; | |
| - int i, needsbyteswap; | |
| - | |
| - entries = CAST(uint32_t, map->len / sizeof(struct magic)); | |
| - if (entries < MAGIC_SETS + 1) { | |
| - file_error(ms, 0, "Too few magic entries %u in `%s'", | |
| - entries, dbname); | |
| - return -1; | |
| - } | |
| - if ((entries * sizeof(struct magic)) != map->len) { | |
| - file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not " | |
| - "a multiple of %" SIZE_T_FORMAT "u", | |
| - dbname, map->len, sizeof(struct magic)); | |
| - return -1; | |
| - } | |
| - | |
| - ptr = CAST(uint32_t *, map->p); | |
| +internal_loaded: | |
| + ptr = (uint32_t *)(void *)map->p; | |
| if (*ptr != MAGICNO) { | |
| if (swap4(*ptr) != MAGICNO) { | |
| file_error(ms, 0, "bad magic in `%s'", dbname); | |
| - return -1; | |
| + goto error; | |
| } | |
| needsbyteswap = 1; | |
| } else | |
| else | |
| version = ptr[1]; | |
| if (version != VERSIONNO) { | |
| - file_error(ms, 0, "File %s supports only version %d magic " | |
| - "files. `%s' is version %d", VERSION, | |
| + file_error(ms, 0, "File %d supports only version %d magic " | |
| + "files. `%s' is version %d", MAGIC_VERSION, | |
| VERSIONNO, dbname, version); | |
| - return -1; | |
| + goto error; | |
| + } | |
| + | |
| + /* 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 | |
| + future. */ | |
| + if (needsbyteswap && fn == NULL) { | |
| + map->p = emalloc(sizeof(php_magic_database)); | |
| + map->p = memcpy(map->p, php_magic_database, sizeof(php_magic_database)); | |
| + } | |
| + | |
| + if (NULL != fn) { | |
| + nentries = (uint32_t)(st.sb.st_size / sizeof(struct magic)); | |
| + entries = (uint32_t)(st.sb.st_size / sizeof(struct magic)); | |
| + if ((zend_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)); | |
| + goto error; | |
| + } | |
| } | |
| map->magic[0] = CAST(struct magic *, map->p) + 1; | |
| nentries = 0; | |
| 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); | |
| - return -1; | |
| + goto error; | |
| } | |
| if (needsbyteswap) | |
| for (i = 0; i < MAGIC_SETS; i++) | |
| byteswap(map->magic[i], map->nmagic[i]); | |
| - return 0; | |
| + | |
| + if (dbname) { | |
| + efree(dbname); | |
| + } | |
| + return map; | |
| + | |
| +error: | |
| + if (stream) { | |
| + php_stream_close(stream); | |
| + } | |
| + apprentice_unmap(map); | |
| + if (dbname) { | |
| + efree(dbname); | |
| + } | |
| + return NULL; | |
| } | |
| /* | |
| { | |
| static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS; | |
| static const size_t m = sizeof(**map->magic); | |
| - int fd = -1; | |
| + php_stream *stream; | |
| size_t len; | |
| char *dbname; | |
| int rv = -1; | |
| if (dbname == NULL) | |
| goto out; | |
| - if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) | |
| - { | |
| + /* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */ | |
| + stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS, NULL); | |
| + | |
| + if (!stream) { | |
| file_error(ms, errno, "cannot open `%s'", dbname); | |
| goto out; | |
| } | |
| hdr.h[1] = VERSIONNO; | |
| memcpy(hdr.h + 2, map->nmagic, nm); | |
| - if (write(fd, &hdr, sizeof(hdr)) != CAST(ssize_t, sizeof(hdr))) { | |
| + if (php_stream_write(stream,(const char *)&hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) { | |
| file_error(ms, errno, "error writing `%s'", dbname); | |
| - goto out2; | |
| + goto out; | |
| } | |
| for (i = 0; i < MAGIC_SETS; i++) { | |
| len = m * map->nmagic[i]; | |
| - if (write(fd, map->magic[i], len) != CAST(ssize_t, len)) { | |
| + if (php_stream_write(stream, (const char *)map->magic[i], len) != (ssize_t)len) { | |
| file_error(ms, errno, "error writing `%s'", dbname); | |
| - goto out2; | |
| + goto out; | |
| } | |
| } | |
| rv = 0; | |
| -out2: | |
| - if (fd != -1) | |
| - (void)close(fd); | |
| + if (stream) { | |
| + php_stream_close(stream); | |
| + } | |
| out: | |
| - apprentice_unmap(map); | |
| - free(dbname); | |
| + efree(dbname); | |
| return rv; | |
| } | |
| q++; | |
| /* Compatibility with old code that looked in .mime */ | |
| if (ms->flags & MAGIC_MIME) { | |
| - if (asprintf(&buf, "%.*s.mime%s", CAST(int, q - fn), fn, ext) | |
| - < 0) | |
| - return NULL; | |
| - if (access(buf, R_OK) != -1) { | |
| + spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", CAST(int, q - fn), fn, ext); | |
| +#ifdef PHP_WIN32 | |
| + if (VCWD_ACCESS(buf, R_OK) == 0) { | |
| +#else | |
| + if (VCWD_ACCESS(buf, R_OK) != -1) { | |
| +#endif | |
| ms->flags &= MAGIC_MIME_TYPE; | |
| return buf; | |
| } | |
| - free(buf); | |
| + efree(buf); | |
| } | |
| - if (asprintf(&buf, "%.*s%s", CAST(int, q - fn), fn, ext) < 0) | |
| - return NULL; | |
| + spprintf(&buf, MAXPATHLEN, "%.*s%s", CAST(int, q - fn), fn, ext); | |
| /* Compatibility with old code that looked in .mime */ | |
| if (strstr(fn, ".mime") != NULL) | |
| m->offset = swap4(CAST(uint32_t, m->offset)); | |
| m->in_offset = swap4(CAST(uint32_t, m->in_offset)); | |
| m->lineno = swap4(CAST(uint32_t, m->lineno)); | |
| - if (IS_STRING(m->type)) { | |
| + if (IS_LIBMAGIC_STRING(m->type)) { | |
| m->str_range = swap4(m->str_range); | |
| m->str_flags = swap4(m->str_flags); | |
| } | |
| continue; | |
| if (strcmp(ma[i].value.s, name) == 0) { | |
| v->magic = &ma[i]; | |
| - v->magic_rxcomp = &(ml->magic_rxcomp[i]); | |
| for (j = i + 1; j < ml->nmagic; j++) | |
| if (ma[j].cont_level == 0) | |
| break; | |
| diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c | |
| --- libmagic.orig/ascmagic.c 2024-06-19 18:18:53.000000000 +0200 | |
| +++ libmagic/ascmagic.c 2025-02-09 01:20:19.757840211 +0100 | |
| rv = file_ascmagic_with_encoding(ms, &bb, | |
| ubuf, ulen, code, type, text); | |
| - free(ubuf); | |
| + efree(ubuf); | |
| return rv; | |
| } | |
| /* malloc size is a conservative overestimate; could be | |
| improved, or at least realloced after conversion. */ | |
| mlen = ulen * 6; | |
| - if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) { | |
| + if ((utf8_buf = CAST(unsigned char *, emalloc(mlen))) == NULL) { | |
| file_oomem(ms, mlen); | |
| goto done; | |
| } | |
| goto done; | |
| if (has_long_lines) | |
| - if (file_printf(ms, ", with very long lines (%" | |
| - SIZE_T_FORMAT "u)", has_long_lines) == -1) | |
| + if (file_printf(ms, ", with very long lines (%zu)", | |
| + has_long_lines) == -1) | |
| goto done; | |
| /* | |
| } | |
| rv = 1; | |
| done: | |
| - free(utf8_buf); | |
| + if (utf8_buf) | |
| + efree(utf8_buf); | |
| return rv; | |
| } | |
| diff -u libmagic.orig/buffer.c libmagic/buffer.c | |
| --- libmagic.orig/buffer.c 2024-06-19 18:18:53.000000000 +0200 | |
| +++ libmagic/buffer.c 2025-02-09 01:20:19.757910844 +0100 | |
| #endif /* lint */ | |
| #include "magic.h" | |
| +#ifdef HAVE_UNISTD_H | |
| #include <unistd.h> | |
| +#endif | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include <sys/stat.h> | |
| void | |
| -buffer_init(struct buffer *b, int fd, const struct stat *st, const void *data, | |
| +buffer_init(struct buffer *b, int fd, const zend_stat_t *st, const void *data, | |
| size_t len) | |
| { | |
| b->fd = fd; | |
| if (st) | |
| memcpy(&b->st, st, sizeof(b->st)); | |
| - else if (b->fd == -1 || fstat(b->fd, &b->st) == -1) | |
| + else if (b->fd == -1 || zend_fstat(b->fd, &b->st) == -1) | |
| memset(&b->st, 0, sizeof(b->st)); | |
| b->fbuf = data; | |
| b->flen = len; | |
| void | |
| buffer_fini(struct buffer *b) | |
| { | |
| - free(b->ebuf); | |
| + efree(b->ebuf); | |
| b->ebuf = NULL; | |
| b->elen = 0; | |
| } | |
| b->elen = CAST(size_t, b->st.st_size) < b->flen ? | |
| CAST(size_t, b->st.st_size) : b->flen; | |
| if (b->elen == 0) { | |
| - free(b->ebuf); | |
| + efree(b->ebuf); | |
| b->ebuf = NULL; | |
| return 0; | |
| } | |
| - if ((b->ebuf = malloc(b->elen)) == NULL) | |
| + if ((b->ebuf = emalloc(b->elen)) == NULL) | |
| goto out; | |
| b->eoff = b->st.st_size - b->elen; | |
| - if (pread(b->fd, b->ebuf, b->elen, b->eoff) == -1) { | |
| - free(b->ebuf); | |
| + if (FINFO_LSEEK_FUNC(b->fd, b->eoff, SEEK_SET) == (zend_off_t)-1 || | |
| + FINFO_READ_FUNC(b->fd, b->ebuf, b->elen) != (ssize_t)b->elen) | |
| + { | |
| + efree(b->ebuf); | |
| b->ebuf = NULL; | |
| goto out; | |
| } | |
| diff -u libmagic.orig/cdf.c libmagic/cdf.c | |
| --- libmagic.orig/cdf.c 2024-11-25 22:24:59.000000000 +0100 | |
| +++ libmagic/cdf.c 2025-02-09 01:25:00.187641434 +0100 | |
| #include <err.h> | |
| #endif | |
| #include <stdlib.h> | |
| +#ifdef HAVE_UNISTD_H | |
| #include <unistd.h> | |
| +#endif | |
| #include <string.h> | |
| #include <time.h> | |
| #include <ctype.h> | |
| 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*/ | |
| -file_private 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*/ | |
| -file_private 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*/ | |
| -file_private 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) | |
| scn->sst_len = 0; | |
| scn->sst_dirlen = 0; | |
| scn->sst_ss = 0; | |
| - free(scn->sst_tab); | |
| + efree(scn->sst_tab); | |
| scn->sst_tab = NULL; | |
| return -1; | |
| } | |
| file_private size_t | |
| cdf_check_stream(const cdf_stream_t *sst, const cdf_header_t *h) | |
| { | |
| +#ifndef NDEBUG | |
| size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ? | |
| CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h); | |
| assert(ss == sst->sst_ss); | |
| +#endif | |
| return sst->sst_ss; | |
| } | |
| } | |
| file_private ssize_t | |
| -cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len) | |
| +cdf_read(const cdf_info_t *info, zend_off_t off, void *buf, size_t len) | |
| { | |
| size_t siz = CAST(size_t, off + len); | |
| - if (CAST(off_t, off + len) != CAST(off_t, siz)) | |
| + if (CAST(zend_off_t, off + len) != CAST(zend_off_t, siz)) | |
| goto out; | |
| if (info->i_buf != NULL && info->i_len >= siz) { | |
| if (info->i_fd == -1) | |
| goto out; | |
| - if (pread(info->i_fd, buf, len, off) != CAST(ssize_t, len)) | |
| + if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (zend_off_t)-1) | |
| + return -1; | |
| + | |
| + if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len) | |
| return -1; | |
| return CAST(ssize_t, len); | |
| char buf[512]; | |
| (void)memcpy(cdf_bo.s, "\01\02\03\04", 4); | |
| - if (cdf_read(info, CAST(off_t, 0), buf, sizeof(buf)) == -1) | |
| + if (cdf_read(info, CAST(zend_off_t, 0), buf, sizeof(buf)) == -1) | |
| return -1; | |
| cdf_unpack_header(h, buf); | |
| cdf_swap_header(h); | |
| } | |
| out: | |
| sat->sat_len = i; | |
| - free(msa); | |
| + efree(msa); | |
| return 0; | |
| out3: | |
| errno = EFTYPE; | |
| out2: | |
| - free(msa); | |
| + efree(msa); | |
| out1: | |
| - free(sat->sat_tab); | |
| + efree(sat->sat_tab); | |
| return -1; | |
| } | |
| return -1; | |
| if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) { | |
| - free(dir->dir_tab); | |
| + efree(dir->dir_tab); | |
| return -1; | |
| } | |
| if (NEED_SWAP) | |
| for (i = 0; i < dir->dir_len; i++) | |
| cdf_swap_dir(&dir->dir_tab[i]); | |
| - free(buf); | |
| + efree(buf); | |
| return 0; | |
| out: | |
| - free(dir->dir_tab); | |
| - free(buf); | |
| + efree(dir->dir_tab); | |
| + efree(buf); | |
| errno = EFTYPE; | |
| return -1; | |
| } | |
| out: | |
| errno = EFTYPE; | |
| out1: | |
| - free(ssat->sat_tab); | |
| + efree(ssat->sat_tab); | |
| return -1; | |
| } | |
| *maxcount = newcount; | |
| return inp; | |
| out: | |
| - free(*info); | |
| + efree(*info); | |
| *maxcount = 0; | |
| *info = NULL; | |
| return NULL; | |
| } | |
| return 0; | |
| out: | |
| - free(*info); | |
| + efree(*info); | |
| *info = NULL; | |
| *count = 0; | |
| *maxcount = 0; | |
| cdf_directory_t *d; | |
| char name[__arraycount(d->d_name)]; | |
| cdf_stream_t scn; | |
| - struct timespec ts; | |
| + struct timeval ts; | |
| static const char *types[] = { "empty", "user storage", | |
| "user stream", "lockbytes", "property", "root storage" }; | |
| break; | |
| } | |
| cdf_dump_stream(&scn); | |
| - free(scn.sst_tab); | |
| + efree(scn.sst_tab); | |
| break; | |
| default: | |
| break; | |
| cdf_dump_property_info(const cdf_property_info_t *info, size_t count) | |
| { | |
| cdf_timestamp_t tp; | |
| - struct timespec ts; | |
| + struct timeval ts; | |
| char buf[64]; | |
| size_t i, j; | |
| (void)fprintf(stderr, "Class %s\n", buf); | |
| (void)fprintf(stderr, "Count %d\n", ssi.si_count); | |
| cdf_dump_property_info(info, count); | |
| - free(info); | |
| + efree(info); | |
| } | |
| cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name), | |
| cdf_ctime(&ts.tv_sec, tbuf)); | |
| } | |
| - free(cat); | |
| + efree(cat); | |
| } | |
| #endif | |
| diff -u libmagic.orig/cdf.h libmagic/cdf.h | |
| --- libmagic.orig/cdf.h 2024-11-25 22:18:55.000000000 +0100 | |
| +++ libmagic/cdf.h 2025-02-09 01:23:35.871635744 +0100 | |
| #ifdef WIN32 | |
| #include <winsock2.h> | |
| -#define timespec timeval | |
| -#define tv_nsec tv_usec | |
| #endif | |
| #ifdef __DJGPP__ | |
| #define timespec timeval | |
| } cdf_catalog_t; | |
| struct timespec; | |
| -file_protected int cdf_timestamp_to_timespec(struct timespec *, | |
| - cdf_timestamp_t); | |
| -file_protected int cdf_timespec_to_timestamp(cdf_timestamp_t *, | |
| - const struct timespec *); | |
| -file_protected int cdf_read_header(const cdf_info_t *, cdf_header_t *); | |
| -file_protected void cdf_swap_header(cdf_header_t *); | |
| -file_protected void cdf_unpack_header(cdf_header_t *, char *); | |
| -file_protected void cdf_swap_dir(cdf_directory_t *); | |
| -file_protected void cdf_unpack_dir(cdf_directory_t *, char *); | |
| -file_protected void cdf_swap_class(cdf_classid_t *); | |
| -file_protected ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, | |
| - size_t, const cdf_header_t *, cdf_secid_t); | |
| -file_protected ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, | |
| - size_t, size_t, const cdf_header_t *, cdf_secid_t); | |
| -file_protected int cdf_read_sat(const cdf_info_t *, cdf_header_t *, | |
| - cdf_sat_t *); | |
| -file_protected size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t); | |
| -file_protected int cdf_read_long_sector_chain(const cdf_info_t *, | |
| - const cdf_header_t *, const cdf_sat_t *, cdf_secid_t, size_t, | |
| - cdf_stream_t *); | |
| -file_protected int cdf_read_short_sector_chain(const cdf_header_t *, | |
| - const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t, size_t, | |
| - cdf_stream_t *); | |
| -file_protected int cdf_read_sector_chain(const cdf_info_t *, | |
| - const cdf_header_t *, const cdf_sat_t *, const cdf_sat_t *, | |
| +int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t); | |
| +int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *); | |
| +int cdf_read_header(const cdf_info_t *, cdf_header_t *); | |
| +void cdf_swap_header(cdf_header_t *); | |
| +void cdf_unpack_header(cdf_header_t *, char *); | |
| +void cdf_swap_dir(cdf_directory_t *); | |
| +void cdf_unpack_dir(cdf_directory_t *, char *); | |
| +void cdf_swap_class(cdf_classid_t *); | |
| +ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t, | |
| + const cdf_header_t *, cdf_secid_t); | |
| +ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t, | |
| + const cdf_header_t *, cdf_secid_t); | |
| +int cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *); | |
| +size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t); | |
| +int cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *, | |
| + const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *); | |
| +int cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *, | |
| const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *); | |
| -file_protected int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, | |
| - const cdf_sat_t *, cdf_dir_t *); | |
| -file_protected int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, | |
| - const cdf_sat_t *, cdf_sat_t *); | |
| -file_protected int cdf_read_short_stream(const cdf_info_t *, | |
| - const cdf_header_t *, const cdf_sat_t *, const cdf_dir_t *, | |
| - cdf_stream_t *, const cdf_directory_t **); | |
| -file_protected int cdf_read_property_info(const cdf_stream_t *, | |
| - const cdf_header_t *, uint32_t, cdf_property_info_t **, size_t *, size_t *); | |
| -file_protected int cdf_read_user_stream(const cdf_info_t *, | |
| - const cdf_header_t *, const cdf_sat_t *, const cdf_sat_t *, | |
| - const cdf_stream_t *, const cdf_dir_t *, const char *, cdf_stream_t *); | |
| -file_protected int cdf_find_stream(const cdf_dir_t *, const char *, int); | |
| -file_protected int cdf_zero_stream(cdf_stream_t *); | |
| -file_protected int cdf_read_doc_summary_info(const cdf_info_t *, | |
| - const cdf_header_t *, const cdf_sat_t *, const cdf_sat_t *, | |
| - const cdf_stream_t *, const cdf_dir_t *, cdf_stream_t *); | |
| -file_protected int cdf_read_summary_info(const cdf_info_t *, | |
| - const cdf_header_t *, const cdf_sat_t *, const cdf_sat_t *, | |
| - const cdf_stream_t *, const cdf_dir_t *, cdf_stream_t *); | |
| -file_protected int cdf_unpack_summary_info(const cdf_stream_t *, | |
| - const cdf_header_t *, cdf_summary_info_header_t *, cdf_property_info_t **, | |
| - size_t *); | |
| -file_protected int cdf_unpack_catalog(const cdf_header_t *, | |
| - const cdf_stream_t *, cdf_catalog_t **); | |
| -file_protected int cdf_print_classid(char *, size_t, const cdf_classid_t *); | |
| -file_protected int cdf_print_property_name(char *, size_t, uint32_t); | |
| -file_protected int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t); | |
| -file_protected uint16_t cdf_tole2(uint16_t); | |
| -file_protected uint32_t cdf_tole4(uint32_t); | |
| -file_protected uint64_t cdf_tole8(uint64_t); | |
| -file_protected char *cdf_ctime(const time_t *, char *); | |
| -file_protected char *cdf_u16tos8(char *, size_t, const uint16_t *); | |
| +int cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *, | |
| + const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t, | |
| + size_t, cdf_stream_t *); | |
| +int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, | |
| + cdf_dir_t *); | |
| +int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, | |
| + cdf_sat_t *); | |
| +int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *, | |
| + const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *, | |
| + const cdf_directory_t **); | |
| +int cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t, | |
| + cdf_property_info_t **, size_t *, size_t *); | |
| +int cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *, | |
| + const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, | |
| + const cdf_dir_t *, const char *, cdf_stream_t *); | |
| +int cdf_find_stream(const cdf_dir_t *, const char *, int); | |
| +int cdf_zero_stream(cdf_stream_t *); | |
| +int cdf_read_doc_summary_info(const cdf_info_t *, const cdf_header_t *, | |
| + const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, | |
| + const cdf_dir_t *, cdf_stream_t *); | |
| +int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *, | |
| + const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, | |
| + const cdf_dir_t *, cdf_stream_t *); | |
| +int cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *, | |
| + cdf_summary_info_header_t *, cdf_property_info_t **, size_t *); | |
| +int cdf_unpack_catalog(const cdf_header_t *, const cdf_stream_t *, | |
| + cdf_catalog_t **); | |
| +int cdf_print_classid(char *, size_t, const cdf_classid_t *); | |
| +int cdf_print_property_name(char *, size_t, uint32_t); | |
| +int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t); | |
| +uint16_t cdf_tole2(uint16_t); | |
| +uint32_t cdf_tole4(uint32_t); | |
| +uint64_t cdf_tole8(uint64_t); | |
| +char *cdf_ctime(const time_t *, char *); | |
| +char *cdf_u16tos8(char *, size_t, const uint16_t *); | |
| #ifdef CDF_DEBUG | |
| -file_protected void cdf_dump_header(const cdf_header_t *); | |
| -file_protected void cdf_dump_sat(const char *, const cdf_sat_t *, size_t); | |
| -file_protected void cdf_dump(const void *, size_t); | |
| -file_protected void cdf_dump_stream(const cdf_stream_t *); | |
| -file_protected void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, | |
| - const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, | |
| - const cdf_dir_t *); | |
| -file_protected void cdf_dump_property_info(const cdf_property_info_t *, size_t); | |
| -file_protected void cdf_dump_summary_info(const cdf_header_t *, | |
| - const cdf_stream_t *); | |
| -file_protected void cdf_dump_catalog(const cdf_header_t *, | |
| - const cdf_stream_t *); | |
| +void cdf_dump_header(const cdf_header_t *); | |
| +void cdf_dump_sat(const char *, const cdf_sat_t *, size_t); | |
| +void cdf_dump(const void *, size_t); | |
| +void cdf_dump_stream(const cdf_stream_t *); | |
| +void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, | |
| + const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *); | |
| +void cdf_dump_property_info(const cdf_property_info_t *, size_t); | |
| +void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *); | |
| +void cdf_dump_catalog(const cdf_header_t *, const cdf_stream_t *); | |
| #endif | |
| diff -u libmagic.orig/compress.c libmagic/compress.c | |
| --- libmagic.orig/compress.c 2024-11-10 17:52:27.000000000 +0100 | |
| +++ libmagic/compress.c 2025-02-09 01:59:42.978538071 +0100 | |
| #if defined(HAVE_SYS_TIME_H) | |
| #include <sys/time.h> | |
| #endif | |
| - | |
| -#if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT) | |
| +#if defined(HAVE_ZLIB_H) && defined(PHP_FILEINFO_UNCOMPRESS) | |
| #define BUILTIN_DECOMPRESS | |
| #include <zlib.h> | |
| #endif | |
| -#if defined(HAVE_BZLIB_H) && defined(BZLIBSUPPORT) | |
| +#undef FIONREAD | |
| + | |
| +#if defined(PHP_FILEINFO_UNCOMPRESS) | |
| #define BUILTIN_BZLIB | |
| #include <bzlib.h> | |
| #endif | |
| #include <lzlib.h> | |
| #endif | |
| -#ifdef notyet | |
| -#if defined(HAVE_LRZIP_H) && defined(LRZIPLIBSUPPORT) | |
| -#define BUILTIN_LRZIP | |
| -#include <Lrzip.h> | |
| -#endif | |
| -#endif | |
| - | |
| #ifdef DEBUG | |
| int tty = -1; | |
| #define DPRINTF(...) do { \ | |
| } | |
| #endif | |
| +#ifdef PHP_FILEINFO_UNCOMPRESS | |
| + | |
| static int | |
| lzmacmp(const unsigned char *buf) | |
| { | |
| file_private int uncompresslzlib(const unsigned char *, unsigned char **, size_t, | |
| size_t *, int); | |
| #endif | |
| -#ifdef BUILTIN_LRZIP | |
| -file_private int uncompresslrzip(const unsigned char *, unsigned char **, size_t, | |
| - size_t *, int); | |
| -#endif | |
| static int makeerror(unsigned char **, size_t *, const char *, ...) | |
| } | |
| nsz = nbytes; | |
| - free(newbuf); | |
| + efree(newbuf); | |
| urv = uncompressbuf(fd, ms->bytes_max, i, | |
| (ms->flags & MAGIC_NO_COMPRESS_FORK), buf, &newbuf, &nsz); | |
| DPRINTF("uncompressbuf = %d, %s, %" SIZE_T_FORMAT "u\n", urv, | |
| if (urv == ERRDATA) | |
| prv = format_decompression_error(ms, i, newbuf); | |
| else | |
| - prv = file_buffer(ms, -1, NULL, name, newbuf, | |
| + prv = file_buffer(ms, NULL, NULL, name, newbuf, | |
| nsz); | |
| if (prv == -1) | |
| goto error; | |
| * XXX: If file_buffer fails here, we overwrite | |
| * the compressed text. FIXME. | |
| */ | |
| - if (file_buffer(ms, -1, NULL, NULL, buf, nbytes) == -1) | |
| + if (file_buffer(ms, NULL, NULL, NULL, buf, nbytes) == -1) | |
| { | |
| if (file_pop_buffer(ms, pb) != NULL) | |
| abort(); | |
| } | |
| if ((rbuf = file_pop_buffer(ms, pb)) != NULL) { | |
| if (file_printf(ms, "%s", rbuf) == -1) { | |
| - free(rbuf); | |
| + efree(rbuf); | |
| goto error; | |
| } | |
| - free(rbuf); | |
| + efree(rbuf); | |
| } | |
| if (!mime && file_printf(ms, ")") == -1) | |
| goto error; | |
| if (sa_saved && sig_act.sa_handler != SIG_IGN) | |
| (void)sigaction(SIGPIPE, &sig_act, NULL); | |
| - free(newbuf); | |
| + if (newbuf) | |
| + efree(newbuf); | |
| ms->flags |= MAGIC_COMPRESS; | |
| DPRINTF("Zmagic returns %d\n", rv); | |
| return rv; | |
| nocheck: | |
| do | |
| - switch ((rv = read(fd, buf, n))) { | |
| + switch ((rv = FINFO_READ_FUNC(fd, buf, n))) { | |
| case -1: | |
| if (errno == EINTR) | |
| continue; | |
| return -1; | |
| } | |
| (void)close(tfd); | |
| - if (lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1)) { | |
| + if (FINFO_LSEEK_FUNC(fd, (zend_off_t)0, SEEK_SET) == (zend_off_t)-1) { | |
| file_badseek(ms); | |
| return -1; | |
| } | |
| return fd; | |
| } | |
| -#if HAVE_FORK | |
| +#ifdef PHP_FILEINFO_UNCOMPRESS | |
| #ifdef BUILTIN_DECOMPRESS | |
| #define FHCRC (1 << 1) | |
| } | |
| #endif | |
| -#ifdef BUILTIN_LRZIP | |
| -file_private int | |
| -uncompresslrzip(const unsigned char *old, unsigned char **newch, | |
| - size_t bytes_max, size_t *n, int extra __attribute__((__unused__))) | |
| -{ | |
| - Lrzip *lr; | |
| - FILE *in, *out; | |
| - int res = OKDATA; | |
| - | |
| - DPRINTF("builtin rlzip decompression\n"); | |
| - lr = lrzip_new(LRZIP_MODE_DECOMPRESS); | |
| - if (lr == NULL) { | |
| - res = makeerror(newch, n, "unable to create an lrzip decoder"); | |
| - goto out0; | |
| - } | |
| - lrzip_config_env(lr); | |
| - in = fmemopen(RCAST(void *, old), bytes_max, "r"); | |
| - if (in == NULL) { | |
| - res = makeerror(newch, n, "unable to construct input file"); | |
| - goto out1; | |
| - } | |
| - if (!lrzip_file_add(lr, in)) { | |
| - res = makeerror(newch, n, "unable to add input file"); | |
| - goto out2; | |
| - } | |
| - *newch = calloc(*n = 2 * bytes_max, 1); | |
| - if (*newch == NULL) { | |
| - res = makeerror(newch, n, "unable to allocate output buffer"); | |
| - goto out2; | |
| - } | |
| - out = fmemopen(*newch, *n, "w"); | |
| - if (out == NULL) { | |
| - free(*newch); | |
| - res = makeerror(newch, n, "unable to allocate output file"); | |
| - goto out2; | |
| - } | |
| - lrzip_outfile_set(lr, out); | |
| - if (lrzip_run(lr)) { | |
| - free(*newch); | |
| - res = makeerror(newch, n, "unable to decompress file"); | |
| - goto out3; | |
| - } | |
| - *n = (size_t)ftell(out); | |
| -out3: | |
| - fclose(out); | |
| -out2: | |
| - fclose(in); | |
| -out1: | |
| - lrzip_free(lr); | |
| -out0: | |
| - return res; | |
| -} | |
| -#endif | |
| - | |
| static int | |
| makeerror(unsigned char **buf, size_t *len, const char *fmt, ...) | |
| { | |
| case METH_LZIP: | |
| return "lzlib"; | |
| #endif | |
| -#ifdef BUILTIN_LRZIP | |
| - case METH_LRZIP: | |
| - return "lrzip"; | |
| -#endif | |
| default: | |
| return compr[method].argv[0]; | |
| } | |
| case METH_LZIP: | |
| return uncompresslzlib; | |
| #endif | |
| -#ifdef BUILTIN_LRZIP | |
| - case METH_LRZIP: | |
| - return uncompresslrzip; | |
| -#endif | |
| default: | |
| return NULL; | |
| } | |
| int (*decompress)(const unsigned char *, unsigned char **, | |
| size_t, size_t *, int) = getdecompressor(method); | |
| - *newch = CAST(unsigned char *, malloc(bytes_max + 1)); | |
| + *newch = CAST(unsigned char *, emalloc(bytes_max + 1)); | |
| if (*newch == NULL) | |
| return makeerror(newch, n, "No buffer, %s", strerror(errno)); | |
| return rv; | |
| } | |
| #endif | |
| +#endif | |
| diff -u libmagic.orig/der.c libmagic/der.c | |
| --- libmagic.orig/der.c 2024-11-25 23:31:53.000000000 +0100 | |
| +++ libmagic/der.c 2025-02-09 01:20:19.770853011 +0100 | |
| #include "magic.h" | |
| #include "der.h" | |
| #else | |
| +#ifdef HAVE_SYS_MMAN_H | |
| #include <sys/mman.h> | |
| +#endif | |
| #include <sys/stat.h> | |
| #include <err.h> | |
| #endif | |
| diff -u libmagic.orig/der.h libmagic/der.h | |
| --- libmagic.orig/der.h 2024-11-25 22:26:18.000000000 +0100 | |
| +++ libmagic/der.h 2023-04-09 22:21:58.195018580 +0200 | |
| * POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| -file_protected int der_offs(struct magic_set *, struct magic *, size_t); | |
| -file_protected int der_cmp(struct magic_set *, struct magic *); | |
| +extern int der_offs(struct magic_set *, struct magic *, size_t); | |
| +extern int der_cmp(struct magic_set *, struct magic *); | |
| diff -u libmagic.orig/encoding.c libmagic/encoding.c | |
| --- libmagic.orig/encoding.c 2024-10-29 21:56:48.000000000 +0100 | |
| +++ libmagic/encoding.c 2025-02-09 01:20:19.770879123 +0100 | |
| nbytes = ms->encoding_max; | |
| mlen = (nbytes + 1) * sizeof((*ubuf)[0]); | |
| - *ubuf = CAST(file_unichar_t *, calloc(CAST(size_t, 1), mlen)); | |
| + *ubuf = CAST(file_unichar_t *, ecalloc(CAST(size_t, 1), mlen)); | |
| if (*ubuf == NULL) { | |
| file_oomem(ms, mlen); | |
| goto done; | |
| unsigned char *nbuf; | |
| mlen = (nbytes + 1) * sizeof(nbuf[0]); | |
| - if ((nbuf = CAST(unsigned char *, malloc(mlen))) == NULL) { | |
| + if ((nbuf = CAST(unsigned char *, emalloc(mlen))) == NULL) { | |
| file_oomem(ms, mlen); | |
| goto done; | |
| } | |
| rv = 0; | |
| *type = "binary"; | |
| } | |
| - free(nbuf); | |
| + efree(nbuf); | |
| } | |
| done: | |
| if (ubuf == &udefbuf) | |
| - free(udefbuf); | |
| + efree(udefbuf); | |
| return rv; | |
| } | |
| diff -u libmagic.orig/file.h libmagic/file.h | |
| --- libmagic.orig/file.h 2024-11-27 16:37:00.000000000 +0100 | |
| +++ libmagic/file.h 2025-02-09 01:47:36.242811911 +0100 | |
| */ | |
| /* | |
| * file.h - definitions for file(1) program | |
| - * @(#)$File: file.h,v 1.258 2024/11/27 15:37:00 christos Exp $ | |
| + * @(#)$File: file.h,v 1.257 2024/11/25 22:31:53 christos Exp $ | |
| */ | |
| #ifndef __file_h__ | |
| #define __file_h__ | |
| -#ifdef HAVE_CONFIG_H | |
| -#include <config.h> | |
| -#endif | |
| +#include "config.h" | |
| #ifdef HAVE_STDINT_H | |
| #include <stdint.h> | |
| #include <stdio.h> /* Include that here, to make sure __P gets defined */ | |
| #include <errno.h> | |
| #include <fcntl.h> /* For open and flags */ | |
| -#include <regex.h> | |
| -#include <time.h> | |
| + | |
| #include <sys/types.h> | |
| #ifndef WIN32 | |
| #include <sys/param.h> | |
| /* Do this here and now, because struct stat gets re-defined on solaris */ | |
| #include <sys/stat.h> | |
| #include <stdarg.h> | |
| -#include <locale.h> | |
| -#if defined(HAVE_XLOCALE_H) | |
| -#include <xlocale.h> | |
| -#endif | |
| #define ENABLE_CONDITIONALS | |
| #define MAXstring 128 /* max len of "string" types */ | |
| #define MAGICNO 0xF11E041C | |
| -#define VERSIONNO 20 | |
| +#define VERSIONNO 19 | |
| #define FILE_MAGICSIZE 432 | |
| #define FILE_GUID_SIZE sizeof("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX") | |
| #define FILE_COMPILE 2 | |
| #define FILE_LIST 3 | |
| -typedef regex_t file_regex_t; | |
| - | |
| struct buffer { | |
| int fd; | |
| - struct stat st; | |
| + zend_stat_t st; | |
| const void *fbuf; | |
| size_t flen; | |
| - off_t eoff; | |
| + zend_off_t eoff; | |
| void *ebuf; | |
| size_t elen; | |
| }; | |
| struct magic { | |
| /* Word 1 */ | |
| - uint16_t flag; | |
| + uint16_t cont_level; /* level of ">" */ | |
| + uint8_t flag; | |
| #define INDIR 0x01 /* if '(...)' appears */ | |
| #define OFFADD 0x02 /* if '>&' or '>...(&' appears */ | |
| #define INDIROFFADD 0x04 /* if '>&(' appears */ | |
| for top-level tests) */ | |
| #define TEXTTEST 0x40 /* for passing to file_softmagic */ | |
| #define OFFNEGATIVE 0x80 /* relative to the end of file */ | |
| -#define OFFPOSITIVE 0x100 /* relative to the beginning of file */ | |
| - uint8_t cont_level; /* level of ">" */ | |
| uint8_t factor; | |
| /* Word 2 */ | |
| #define FILE_OCTAL 59 | |
| #define FILE_NAMES_SIZE 60 /* size of array to contain all names */ | |
| -#define IS_STRING(t) \ | |
| +#define IS_LIBMAGIC_STRING(t) \ | |
| ((t) == FILE_STRING || \ | |
| (t) == FILE_PSTRING || \ | |
| (t) == FILE_BESTRING16 || \ | |
| /* list of magic entries */ | |
| struct mlist { | |
| struct magic *magic; /* array of magic entries */ | |
| - file_regex_t **magic_rxcomp; /* array of compiled regexps */ | |
| size_t nmagic; /* number of entries in array */ | |
| void *map; /* internal resources used by entry */ | |
| struct mlist *next, *prev; | |
| int flags; /* Control magic tests. */ | |
| int event_flags; /* Note things that happened. */ | |
| #define EVENT_HAD_ERR 0x01 | |
| - char *fnamebuf; /* holding the full path/buffer */ | |
| const char *file; | |
| size_t line; /* current magic line number */ | |
| mode_t mode; /* copy of current stat mode */ | |
| file_protected const char *file_fmtnum(char *, size_t, const char *, int); | |
| file_protected struct magic_set *file_ms_alloc(int); | |
| file_protected void file_ms_free(struct magic_set *); | |
| -file_protected int file_default(struct magic_set *, size_t); | |
| -file_protected int file_buffer(struct magic_set *, int, struct stat *, | |
| - const char *, const void *, size_t); | |
| -file_protected int file_fsmagic(struct magic_set *, const char *, | |
| - struct stat *); | |
| +file_protected int file_buffer(struct magic_set *, php_stream *, zend_stat_t *, const char *, const void *, | |
| + size_t); | |
| +file_protected int file_fsmagic(struct magic_set *, const char *, zend_stat_t *); | |
| file_protected int file_pipe2file(struct magic_set *, int, const void *, | |
| size_t); | |
| file_protected int file_vprintf(struct magic_set *, const char *, va_list) | |
| file_protected int file_reset(struct magic_set *, int); | |
| file_protected int file_tryelf(struct magic_set *, const struct buffer *); | |
| file_protected int file_trycdf(struct magic_set *, const struct buffer *); | |
| -#if HAVE_FORK | |
| +#ifdef PHP_FILEINFO_UNCOMPRESS | |
| file_protected int file_zmagic(struct magic_set *, const struct buffer *, | |
| const char *); | |
| #endif | |
| file_protected int file_clear_closexec(int); | |
| file_protected char *file_strtrim(char *); | |
| -file_protected void buffer_init(struct buffer *, int, const struct stat *, | |
| +file_protected void buffer_init(struct buffer *, int, const zend_stat_t *, | |
| const void *, size_t); | |
| file_protected void buffer_fini(struct buffer *); | |
| file_protected int buffer_fill(const struct buffer *); | |
| -file_protected int file_regcomp(struct magic_set *, file_regex_t *, | |
| - const char *, int); | |
| -file_protected int file_regexec(struct magic_set *, file_regex_t *, | |
| - const char *, size_t, regmatch_t *, int); | |
| -file_protected void file_regfree(file_regex_t *); | |
| - | |
| typedef struct { | |
| char *buf; | |
| size_t blen; | |
| extern file_protected const size_t file_nnames; | |
| #endif | |
| -#ifndef HAVE_PREAD | |
| -ssize_t pread(int, void *, size_t, off_t); | |
| -#endif | |
| -#ifndef HAVE_VASPRINTF | |
| -int vasprintf(char **, const char *, va_list); | |
| -#endif | |
| -#ifndef HAVE_ASPRINTF | |
| -int asprintf(char **, const char *, ...); | |
| -#endif | |
| -#ifndef HAVE_DPRINTF | |
| -int dprintf(int, const char *, ...); | |
| -#endif | |
| - | |
| #ifndef HAVE_STRLCPY | |
| size_t strlcpy(char *, const char *, size_t); | |
| #endif | |
| #ifndef HAVE_ASCTIME_R | |
| char *asctime_r(const struct tm *, char *); | |
| #endif | |
| -#ifndef HAVE_GMTIME_R | |
| -struct tm *gmtime_r(const time_t *, struct tm *); | |
| -#endif | |
| -#ifndef HAVE_LOCALTIME_R | |
| -struct tm *localtime_r(const time_t *, struct tm *); | |
| -#endif | |
| -#ifndef HAVE_FMTCHECK | |
| -const char *fmtcheck(const char *, const char *) | |
| - __attribute__((__format_arg__(2))); | |
| -#endif | |
| - | |
| -#ifdef HAVE_LIBSECCOMP | |
| -int enable_sandbox(void); | |
| -#endif | |
| - | |
| -file_protected const char *file_getprogname(void); | |
| -file_protected void file_setprogname(const char *); | |
| -file_protected void file_err(int, const char *, ...) | |
| - __attribute__((__format__(__printf__, 2, 3), __noreturn__)); | |
| -file_protected void file_errx(int, const char *, ...) | |
| - __attribute__((__format__(__printf__, 2, 3), __noreturn__)); | |
| -file_protected void file_warn(const char *, ...) | |
| - __attribute__((__format__(__printf__, 1, 2))); | |
| -file_protected void file_warnx(const char *, ...) | |
| - __attribute__((__format__(__printf__, 1, 2))); | |
| #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) | |
| #define QUICK | |
| diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c | |
| --- libmagic.orig/fsmagic.c 2024-06-19 18:18:53.000000000 +0200 | |
| +++ libmagic/fsmagic.c 2025-02-09 01:20:19.770987982 +0100 | |
| # define minor(dev) ((dev) & 0xff) | |
| #endif | |
| #undef HAVE_MAJOR | |
| -#ifdef S_IFLNK | |
| -file_private int | |
| -bad_link(struct magic_set *ms, int err, char *buf) | |
| -{ | |
| - int mime = ms->flags & MAGIC_MIME; | |
| - if ((mime & MAGIC_MIME_TYPE) && | |
| - file_printf(ms, "inode/symlink") | |
| - == -1) | |
| - return -1; | |
| - else if (!mime) { | |
| - if (ms->flags & MAGIC_ERROR) { | |
| - file_error(ms, err, | |
| - "broken symbolic link to %s", buf); | |
| - return -1; | |
| - } | |
| - if (file_printf(ms, "broken symbolic link to %s", buf) == -1) | |
| - return -1; | |
| - } | |
| - return 1; | |
| -} | |
| + | |
| +#ifdef PHP_WIN32 | |
| + | |
| +# undef S_IFIFO | |
| #endif | |
| file_private int | |
| handle_mime(struct magic_set *ms, int mime, const char *str) | |
| } | |
| file_protected int | |
| -file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) | |
| +file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb) | |
| { | |
| int ret, did = 0; | |
| int mime = ms->flags & MAGIC_MIME; | |
| int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION); | |
| -#ifdef S_IFLNK | |
| - char buf[BUFSIZ+4]; | |
| - ssize_t nch; | |
| - struct stat tstatbuf; | |
| -#endif | |
| if (fn == NULL) | |
| return 0; | |
| #define COMMA (did++ ? ", " : "") | |
| - /* | |
| - * 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. | |
| - */ | |
| -#ifdef S_IFLNK | |
| - if ((ms->flags & MAGIC_SYMLINK) == 0) | |
| - ret = lstat(fn, sb); | |
| - else | |
| -#endif | |
| - ret = stat(fn, sb); /* don't merge into if; see "ret =" above */ | |
| - | |
| -#ifdef WIN32 | |
| - { | |
| - HANDLE hFile = CreateFile((LPCSTR)fn, 0, FILE_SHARE_DELETE | | |
| - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, | |
| - NULL); | |
| - if (hFile != INVALID_HANDLE_VALUE) { | |
| - /* | |
| - * Stat failed, but we can still open it - assume it's | |
| - * a block device, if nothing else. | |
| - */ | |
| - if (ret) { | |
| - sb->st_mode = S_IFBLK; | |
| - ret = 0; | |
| - } | |
| - switch (GetFileType(hFile)) { | |
| - case FILE_TYPE_CHAR: | |
| - sb->st_mode |= S_IFCHR; | |
| - sb->st_mode &= ~S_IFREG; | |
| - break; | |
| - case FILE_TYPE_PIPE: | |
| - sb->st_mode |= S_IFIFO; | |
| - sb->st_mode &= ~S_IFREG; | |
| - break; | |
| - } | |
| - CloseHandle(hFile); | |
| - } | |
| - } | |
| -#endif | |
| + ret = php_sys_stat(fn, sb); | |
| if (ret) { | |
| if (ms->flags & MAGIC_ERROR) { | |
| } | |
| switch (sb->st_mode & S_IFMT) { | |
| - case S_IFDIR: | |
| - if (mime) { | |
| - if (handle_mime(ms, mime, "directory") == -1) | |
| - return -1; | |
| - } else if (silent) { | |
| - } else if (file_printf(ms, "%sdirectory", COMMA) == -1) | |
| - return -1; | |
| - break; | |
| -#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, "chardevice") == -1) | |
| - return -1; | |
| - } else if (silent) { | |
| - } else { | |
| -#ifdef HAVE_STRUCT_STAT_ST_RDEV | |
| -# ifdef dv_unit | |
| +#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) { | |
| + ret = 0; | |
| + break; | |
| + } | |
| + if (mime) { | |
| + if (handle_mime(ms, mime, "chardevice") == -1) | |
| + return -1; | |
| + } else { | |
| +# ifdef HAVE_STAT_ST_RDEV | |
| +# ifdef dv_unit | |
| if (file_printf(ms, "%scharacter special (%d/%d/%d)", | |
| COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev), | |
| dv_subunit(sb->st_rdev)) == -1) | |
| if (file_printf(ms, "%scharacter special", COMMA) == -1) | |
| return -1; | |
| #endif | |
| - } | |
| - break; | |
| -#endif | |
| -#ifdef S_IFBLK | |
| - case S_IFBLK: | |
| - /* | |
| - * If -s has been specified, treat block 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, "blockdevice") == -1) | |
| - return -1; | |
| - } else if (silent) { | |
| - } else { | |
| -#ifdef HAVE_STRUCT_STAT_ST_RDEV | |
| -# ifdef dv_unit | |
| - if (file_printf(ms, "%sblock special (%d/%d/%d)", | |
| - COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev), | |
| - dv_subunit(sb->st_rdev)) == -1) | |
| - return -1; | |
| -# else | |
| - if (file_printf(ms, "%sblock special (%ld/%ld)", | |
| - COMMA, (long)major(sb->st_rdev), | |
| - (long)minor(sb->st_rdev)) == -1) | |
| - return -1; | |
| + } | |
| + return 1; | |
| # endif | |
| -#else | |
| - if (file_printf(ms, "%sblock special", COMMA) == -1) | |
| - return -1; | |
| #endif | |
| - } | |
| - break; | |
| -#endif | |
| - /* TODO add code to handle V7 MUX and Blit MUX files */ | |
| + | |
| #ifdef S_IFIFO | |
| case S_IFIFO: | |
| if((ms->flags & MAGIC_DEVICES) != 0) | |
| #endif | |
| #ifdef S_IFLNK | |
| case S_IFLNK: | |
| - if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) { | |
| + /* stat is used, if it made here then the link is broken */ | |
| if (ms->flags & MAGIC_ERROR) { | |
| - file_error(ms, errno, "unreadable symlink `%s'", | |
| - fn); | |
| + file_error(ms, errno, "unreadable symlink `%s'", fn); | |
| return -1; | |
| } | |
| - if (mime) { | |
| - if (handle_mime(ms, mime, "symlink") == -1) | |
| - return -1; | |
| - } else if (silent) { | |
| - } else if (file_printf(ms, | |
| - "%sunreadable symlink `%s' (%s)", COMMA, fn, | |
| - strerror(errno)) == -1) | |
| - return -1; | |
| - break; | |
| - } | |
| - buf[nch] = '\0'; /* readlink(2) does not do this */ | |
| - | |
| - /* If broken symlink, say so and quit early. */ | |
| -#ifdef __linux__ | |
| - /* | |
| - * linux procfs/devfs makes symlinks like pipe:[3515864880] | |
| - * that we can't stat their readlink output, so stat the | |
| - * original filename instead. | |
| - */ | |
| - if (stat(fn, &tstatbuf) < 0) | |
| - return bad_link(ms, errno, buf); | |
| -#else | |
| - if (*buf == '/') { | |
| - if (stat(buf, &tstatbuf) < 0) | |
| - return bad_link(ms, errno, buf); | |
| - } else { | |
| - char *tmp; | |
| - char buf2[BUFSIZ+BUFSIZ+4]; | |
| - | |
| - if ((tmp = CCAST(char *, strrchr(fn, '/'))) == NULL) { | |
| - tmp = buf; /* in current directory anyway */ | |
| - } else { | |
| - if (tmp - fn + 1 > BUFSIZ) { | |
| - if (ms->flags & MAGIC_ERROR) { | |
| - file_error(ms, 0, | |
| - "path too long: `%s'", buf); | |
| - return -1; | |
| - } | |
| - if (mime) { | |
| - if (handle_mime(ms, mime, | |
| - "x-path-too-long") == -1) | |
| - return -1; | |
| - } else if (silent) { | |
| - } else if (file_printf(ms, | |
| - "%spath too long: `%s'", COMMA, | |
| - fn) == -1) | |
| - return -1; | |
| - break; | |
| - } | |
| - /* take dir part */ | |
| - (void)strlcpy(buf2, fn, sizeof buf2); | |
| - buf2[tmp - fn + 1] = '\0'; | |
| - /* plus (rel) link */ | |
| - (void)strlcat(buf2, buf, sizeof buf2); | |
| - tmp = buf2; | |
| - } | |
| - if (stat(tmp, &tstatbuf) < 0) | |
| - return bad_link(ms, errno, buf); | |
| - } | |
| + return 1; | |
| #endif | |
| - /* Otherwise, handle it. */ | |
| - if ((ms->flags & MAGIC_SYMLINK) != 0) { | |
| - const char *p; | |
| - ms->flags &= MAGIC_SYMLINK; | |
| - p = magic_file(ms, buf); | |
| - ms->flags |= MAGIC_SYMLINK; | |
| - if (p == NULL) | |
| - return -1; | |
| - } else { /* just print what it points to */ | |
| - if (mime) { | |
| - if (handle_mime(ms, mime, "symlink") == -1) | |
| - return -1; | |
| - } else if (silent) { | |
| - } else if (file_printf(ms, "%ssymbolic link to %s", | |
| - COMMA, buf) == -1) | |
| - return -1; | |
| - } | |
| - break; | |
| -#endif | |
| #ifdef S_IFSOCK | |
| #ifndef __COHERENT__ | |
| case S_IFSOCK: | |
| diff -u libmagic.orig/funcs.c libmagic/funcs.c | |
| --- libmagic.orig/funcs.c 2024-06-19 18:18:53.000000000 +0200 | |
| +++ libmagic/funcs.c 2025-02-09 01:29:25.403659334 +0100 | |
| file_private void | |
| file_clearbuf(struct magic_set *ms) | |
| { | |
| - free(ms->o.buf); | |
| + efree(ms->o.buf); | |
| ms->o.buf = NULL; | |
| ms->o.blen = 0; | |
| } | |
| file_protected int | |
| file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) | |
| { | |
| - int len; | |
| + size_t len; | |
| char *buf, *newstr; | |
| char tbuf[1024]; | |
| return -1; | |
| } | |
| - len = vasprintf(&buf, fmt, ap); | |
| - if (len < 0 || (size_t)len > 1024 || len + ms->o.blen > 1024 * 1024) { | |
| + len = vspprintf(&buf, 0, fmt, ap); | |
| + if (len > 1024 || len + ms->o.blen > 1024 * 1024) { | |
| size_t blen = ms->o.blen; | |
| - free(buf); | |
| + if (buf) efree(buf); | |
| file_clearbuf(ms); | |
| - file_error(ms, 0, "Output buffer space exceeded %d+%" | |
| + file_error(ms, 0, "Output buffer space exceeded %" SIZE_T_FORMAT "u+%" | |
| SIZE_T_FORMAT "u", len, blen); | |
| return -1; | |
| } | |
| if (ms->o.buf != NULL) { | |
| - len = asprintf(&newstr, "%s%s", ms->o.buf, buf); | |
| - free(buf); | |
| - if (len < 0) | |
| - goto out; | |
| - free(ms->o.buf); | |
| + len = spprintf(&newstr, 0, "%s%s", ms->o.buf, buf); | |
| + efree(buf); | |
| + efree(ms->o.buf); | |
| buf = newstr; | |
| } | |
| ms->o.buf = buf; | |
| ms->o.blen = len; | |
| return 0; | |
| -out: | |
| - file_clearbuf(ms); | |
| - file_error(ms, errno, "vasprintf failed"); | |
| - return -1; | |
| } | |
| file_protected int | |
| */ | |
| /*ARGSUSED*/ | |
| file_protected int | |
| -file_buffer(struct magic_set *ms, int fd, struct stat *st, | |
| +file_buffer(struct magic_set *ms, php_stream *stream, zend_stat_t *st, | |
| const char *inname __attribute__ ((__unused__)), | |
| const void *buf, size_t nb) | |
| { | |
| const char *ftype = NULL; | |
| char *rbuf = NULL; | |
| struct buffer b; | |
| + int fd = -1; | |
| + | |
| + if (stream) { | |
| +#ifdef _WIN64 | |
| + php_socket_t _fd = fd; | |
| +#else | |
| + int _fd; | |
| +#endif | |
| + int _ret = php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&_fd, 0); | |
| + if (SUCCESS == _ret) { | |
| + fd = (int)_fd; | |
| + } | |
| + } | |
| buffer_init(&b, fd, st, buf, nb); | |
| ms->mode = b.st.st_mode; | |
| } | |
| } | |
| #endif | |
| -#if HAVE_FORK | |
| + | |
| +#ifdef PHP_FILEINFO_UNCOMPRESS | |
| /* try compression stuff */ | |
| if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) { | |
| m = file_zmagic(ms, &b, inname); | |
| rv = file_tryelf(ms, &b); | |
| rbuf = file_pop_buffer(ms, pb); | |
| if (rv == -1) { | |
| - free(rbuf); | |
| + efree(rbuf); | |
| rbuf = NULL; | |
| } | |
| if ((ms->flags & MAGIC_DEBUG) != 0) | |
| if (file_printf(ms, "%s", code_mime) == -1) | |
| rv = -1; | |
| } | |
| -#if HAVE_FORK | |
| +#ifdef PHP_FILEINFO_UNCOMPRESS | |
| done_encoding: | |
| #endif | |
| - free(rbuf); | |
| + efree(rbuf); | |
| buffer_fini(&b); | |
| if (rv) | |
| return rv; | |
| } | |
| file_clearbuf(ms); | |
| if (ms->o.pbuf) { | |
| - free(ms->o.pbuf); | |
| + efree(ms->o.pbuf); | |
| ms->o.pbuf = NULL; | |
| } | |
| ms->event_flags &= ~EVENT_HAD_ERR; | |
| return NULL; | |
| } | |
| psize = len * 4 + 1; | |
| - if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { | |
| + if ((pbuf = CAST(char *, erealloc(ms->o.pbuf, psize))) == NULL) { | |
| file_oomem(ms, psize); | |
| return NULL; | |
| } | |
| if (level >= ms->c.len) { | |
| len = (ms->c.len = 20 + level) * sizeof(*ms->c.li); | |
| ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? | |
| - malloc(len) : | |
| - realloc(ms->c.li, len)); | |
| + emalloc(len) : | |
| + erealloc(ms->c.li, len)); | |
| if (ms->c.li == NULL) { | |
| file_oomem(ms, len); | |
| return -1; | |
| file_protected int | |
| file_replace(struct magic_set *ms, const char *pat, const char *rep) | |
| { | |
| - file_regex_t rx; | |
| - int rc, rv = -1; | |
| - | |
| - rc = file_regcomp(ms, &rx, pat, REG_EXTENDED); | |
| - if (rc == 0) { | |
| - regmatch_t rm; | |
| - int nm = 0; | |
| - while (file_regexec(ms, &rx, ms->o.buf, 1, &rm, 0) == 0) { | |
| - ms->o.buf[rm.rm_so] = '\0'; | |
| - if (file_printf(ms, "%s%s", rep, | |
| - rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) | |
| - goto out; | |
| - nm++; | |
| - } | |
| - rv = nm; | |
| + zend_string *pattern; | |
| + uint32_t opts = 0; | |
| + pcre_cache_entry *pce; | |
| + zend_string *res; | |
| + zend_string *repl; | |
| + size_t rep_cnt = 0; | |
| + | |
| + opts |= PCRE2_MULTILINE; | |
| + pattern = convert_libmagic_pattern(pat, strlen(pat), opts); | |
| + pce = pcre_get_compiled_regex_cache_ex(pattern, 0); | |
| + zend_string_release_ex(pattern, 0); | |
| + if (pce == NULL) { | |
| + rep_cnt = -1; | |
| + goto out; | |
| + } | |
| + | |
| + repl = zend_string_init(rep, strlen(rep), 0); | |
| + res = php_pcre_replace_impl(pce, NULL, ms->o.buf, strlen(ms->o.buf), repl, -1, &rep_cnt); | |
| + | |
| + zend_string_release_ex(repl, 0); | |
| + if (NULL == res) { | |
| + rep_cnt = -1; | |
| + goto out; | |
| } | |
| -out: | |
| - file_regfree(&rx); | |
| - return rv; | |
| -} | |
| -file_private int | |
| -check_regex(struct magic_set *ms, const char *pat) | |
| -{ | |
| - char sbuf[512]; | |
| - unsigned char oc = '\0'; | |
| - const char *p; | |
| - unsigned long l; | |
| + memcpy(ms->o.buf, ZSTR_VAL(res), ZSTR_LEN(res)); | |
| + ms->o.buf[ZSTR_LEN(res)] = '\0'; | |
| - for (p = pat; *p; p++) { | |
| - unsigned char c = *p; | |
| - // Avoid repetition | |
| - if (c == oc && strchr("?*+{", c) != NULL) { | |
| - size_t len = strlen(pat); | |
| - file_magwarn(ms, | |
| - "repetition-operator operand `%c' " | |
| - "invalid in regex `%s'", c, | |
| - file_printable(ms, sbuf, sizeof(sbuf), pat, len)); | |
| - return -1; | |
| - } | |
| - if (c == '{') { | |
| - char *ep, *eep; | |
| - errno = 0; | |
| - l = strtoul(p + 1, &ep, 10); | |
| - if (ep != p + 1 && l > 1000) | |
| - goto bounds; | |
| - | |
| - if (*ep == ',') { | |
| - l = strtoul(ep + 1, &eep, 10); | |
| - if (eep != ep + 1 && l > 1000) | |
| - goto bounds; | |
| - } | |
| - } | |
| - oc = c; | |
| - if (isprint(c) || isspace(c) || c == '\b' | |
| - || c == 0x8a) // XXX: apple magic fixme | |
| - continue; | |
| - size_t len = strlen(pat); | |
| - file_magwarn(ms, | |
| - "non-ascii characters in regex \\%#o `%s'", | |
| - c, file_printable(ms, sbuf, sizeof(sbuf), pat, len)); | |
| - return -1; | |
| - } | |
| - return 0; | |
| -bounds: | |
| - file_magwarn(ms, "bounds too large %ld in regex `%s'", l, pat); | |
| - return -1; | |
| -} | |
| + zend_string_release_ex(res, 0); | |
| -file_protected int | |
| -file_regcomp(struct magic_set *ms file_locale_used, file_regex_t *rx, | |
| - const char *pat, int flags) | |
| -{ | |
| - if (check_regex(ms, pat) == -1) | |
| - return -1; | |
| - | |
| -#ifdef USE_C_LOCALE | |
| - locale_t old = uselocale(ms->c_lc_ctype); | |
| - assert(old != NULL); | |
| -#else | |
| - char old[1024]; | |
| - strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old)); | |
| - (void)setlocale(LC_CTYPE, "C"); | |
| -#endif | |
| - int rc; | |
| - rc = regcomp(rx, pat, flags); | |
| - | |
| -#ifdef USE_C_LOCALE | |
| - uselocale(old); | |
| -#else | |
| - (void)setlocale(LC_CTYPE, old); | |
| -#endif | |
| - if (rc > 0 && (ms->flags & MAGIC_CHECK)) { | |
| - char errmsg[512], buf[512]; | |
| - | |
| - (void)regerror(rc, rx, errmsg, sizeof(errmsg)); | |
| - file_magerror(ms, "regex error %d for `%s', (%s)", rc, | |
| - file_printable(ms, buf, sizeof(buf), pat, strlen(pat)), | |
| - errmsg); | |
| - } | |
| - return rc; | |
| -} | |
| - | |
| -/*ARGSUSED*/ | |
| -file_protected int | |
| -file_regexec(struct magic_set *ms file_locale_used, file_regex_t *rx, | |
| - const char *str, size_t nmatch, regmatch_t* pmatch, int eflags) | |
| -{ | |
| -#ifdef USE_C_LOCALE | |
| - locale_t old = uselocale(ms->c_lc_ctype); | |
| - assert(old != NULL); | |
| -#else | |
| - char old[1024]; | |
| - strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old)); | |
| - (void)setlocale(LC_CTYPE, "C"); | |
| -#endif | |
| - int rc; | |
| - /* XXX: force initialization because glibc does not always do this */ | |
| - if (nmatch != 0) | |
| - memset(pmatch, 0, nmatch * sizeof(*pmatch)); | |
| - rc = regexec(rx, str, nmatch, pmatch, eflags); | |
| -#ifdef USE_C_LOCALE | |
| - uselocale(old); | |
| -#else | |
| - (void)setlocale(LC_CTYPE, old); | |
| -#endif | |
| - return rc; | |
| -} | |
| - | |
| -file_protected void | |
| -file_regfree(file_regex_t *rx) | |
| -{ | |
| - regfree(rx); | |
| +out: | |
| + return rep_cnt; | |
| } | |
| file_protected file_pushbuf_t * | |
| if (ms->event_flags & EVENT_HAD_ERR) | |
| return NULL; | |
| - if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL) | |
| + if ((pb = (CAST(file_pushbuf_t *, emalloc(sizeof(*pb))))) == NULL) | |
| return NULL; | |
| pb->buf = ms->o.buf; | |
| char *rbuf; | |
| if (ms->event_flags & EVENT_HAD_ERR) { | |
| - free(pb->buf); | |
| - free(pb); | |
| + efree(pb->buf); | |
| + efree(pb); | |
| return NULL; | |
| } | |
| ms->o.blen = pb->blen; | |
| ms->offset = pb->offset; | |
| - free(pb); | |
| + efree(pb); | |
| return rbuf; | |
| } | |
| #endif | |
| } | |
| +#if 0 | |
| file_protected int | |
| file_pipe_closexec(int *fds) | |
| { | |
| return 0; | |
| #endif | |
| } | |
| +#endif | |
| file_protected int | |
| file_clear_closexec(int fd) { | |
| diff -u libmagic.orig/magic.c libmagic/magic.c | |
| --- libmagic.orig/magic.c 2024-06-19 18:18:53.000000000 +0200 | |
| +++ libmagic/magic.c 2025-02-09 01:20:19.771155033 +0100 | |
| * SUCH DAMAGE. | |
| */ | |
| -#ifdef WIN32 | |
| -#include <windows.h> | |
| -#include <shlwapi.h> | |
| -#endif | |
| - | |
| #include "file.h" | |
| #ifndef lint | |
| #include "magic.h" | |
| #include <stdlib.h> | |
| +#ifdef HAVE_UNISTD_H | |
| #include <unistd.h> | |
| +#endif | |
| #include <string.h> | |
| #ifdef QUICK | |
| #include <sys/mman.h> | |
| #endif | |
| #endif | |
| -file_private void close_and_restore(const struct magic_set *, const char *, int, | |
| - const struct stat *); | |
| -file_private int unreadable_info(struct magic_set *, mode_t, const char *); | |
| -file_private const char* get_default_magic(void); | |
| -#ifndef COMPILE_ONLY | |
| -file_private const char *file_or_fd(struct magic_set *, const char *, int); | |
| +#ifdef PHP_WIN32 | |
| +# undef S_IFLNK | |
| +# undef S_IFIFO | |
| #endif | |
| +file_private int unreadable_info(struct magic_set *, mode_t, const char *); | |
| +file_private const char *file_or_stream(struct magic_set *, const char *, php_stream *); | |
| + | |
| #ifndef STDIN_FILENO | |
| #define STDIN_FILENO 0 | |
| #endif | |
| -#ifdef WIN32 | |
| -/* HINSTANCE of this shared library. Needed for get_default_magic() */ | |
| -static HINSTANCE _w32_dll_instance = NULL; | |
| - | |
| -static void | |
| -_w32_append_path(char **hmagicpath, const char *fmt, ...) | |
| -{ | |
| - char *tmppath; | |
| - char *newpath; | |
| - va_list ap; | |
| - | |
| - va_start(ap, fmt); | |
| - if (vasprintf(&tmppath, fmt, ap) < 0) { | |
| - va_end(ap); | |
| - return; | |
| - } | |
| - va_end(ap); | |
| - | |
| - if (access(tmppath, R_OK) == -1) | |
| - goto out; | |
| - | |
| - if (*hmagicpath == NULL) { | |
| - *hmagicpath = tmppath; | |
| - return; | |
| - } | |
| - | |
| - if (asprintf(&newpath, "%s%c%s", *hmagicpath, PATHSEP, tmppath) < 0) | |
| - goto out; | |
| - | |
| - free(*hmagicpath); | |
| - free(tmppath); | |
| - *hmagicpath = newpath; | |
| - return; | |
| -out: | |
| - free(tmppath); | |
| -} | |
| - | |
| -static void | |
| -_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module) | |
| -{ | |
| - static const char *trypaths[] = { | |
| - "%s/share/misc/magic.mgc", | |
| - "%s/magic.mgc", | |
| - }; | |
| - LPSTR dllpath; | |
| - size_t sp; | |
| - | |
| - dllpath = calloc(MAX_PATH + 1, sizeof(*dllpath)); | |
| - | |
| - if (!GetModuleFileNameA(module, dllpath, MAX_PATH)) | |
| - goto out; | |
| - | |
| - PathRemoveFileSpecA(dllpath); | |
| - | |
| - if (module) { | |
| - char exepath[MAX_PATH]; | |
| - GetModuleFileNameA(NULL, exepath, MAX_PATH); | |
| - PathRemoveFileSpecA(exepath); | |
| - if (stricmp(exepath, dllpath) == 0) | |
| - goto out; | |
| - } | |
| - | |
| - sp = strlen(dllpath); | |
| - if (sp > 3 && stricmp(&dllpath[sp - 3], "bin") == 0) { | |
| - _w32_append_path(hmagicpath, | |
| - "%s/../share/misc/magic.mgc", dllpath); | |
| - goto out; | |
| - } | |
| - | |
| - for (sp = 0; sp < __arraycount(trypaths); sp++) | |
| - _w32_append_path(hmagicpath, trypaths[sp], dllpath); | |
| -out: | |
| - free(dllpath); | |
| -} | |
| - | |
| -#ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY | |
| -/* Placate GCC by offering a sacrificial previous prototype */ | |
| -BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); | |
| - | |
| -BOOL WINAPI | |
| -DllMain(HINSTANCE hinstDLL, DWORD fdwReason, | |
| - LPVOID lpvReserved __attribute__((__unused__))) | |
| -{ | |
| - if (fdwReason == DLL_PROCESS_ATTACH) | |
| - _w32_dll_instance = hinstDLL; | |
| - return 1; | |
| -} | |
| -#endif | |
| -#endif | |
| - | |
| -file_private const char * | |
| -get_default_magic(void) | |
| -{ | |
| - static const char hmagic[] = "/.magic/magic.mgc"; | |
| - static char *default_magic; | |
| - char *home, *hmagicpath; | |
| - | |
| -#ifndef WIN32 | |
| - struct stat st; | |
| - | |
| - if (default_magic) { | |
| - free(default_magic); | |
| - default_magic = NULL; | |
| - } | |
| - if ((home = getenv("HOME")) == NULL) | |
| - return MAGIC; | |
| - | |
| - if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0) | |
| - return MAGIC; | |
| - if (stat(hmagicpath, &st) == -1) { | |
| - free(hmagicpath); | |
| - 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 (access(hmagicpath, R_OK) == -1) | |
| - goto out; | |
| - } | |
| - } | |
| - | |
| - if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0) | |
| - goto out; | |
| - free(hmagicpath); | |
| - return default_magic; | |
| -out: | |
| - default_magic = NULL; | |
| - free(hmagicpath); | |
| - return MAGIC; | |
| -#else | |
| - hmagicpath = NULL; | |
| - | |
| - if (default_magic) { | |
| - free(default_magic); | |
| - default_magic = NULL; | |
| - } | |
| - | |
| - /* Before anything else, try to get a magic file from user HOME */ | |
| - if ((home = getenv("HOME")) != NULL) | |
| - _w32_append_path(&hmagicpath, "%s%s", home, hmagic); | |
| - | |
| - /* First, try to get a magic file from user-application data */ | |
| - if ((home = getenv("LOCALAPPDATA")) != NULL) | |
| - _w32_append_path(&hmagicpath, "%s%s", home, hmagic); | |
| - | |
| - /* Second, try to get a magic file from the user profile data */ | |
| - if ((home = getenv("USERPROFILE")) != NULL) | |
| - _w32_append_path(&hmagicpath, | |
| - "%s/Local Settings/Application Data%s", home, hmagic); | |
| - | |
| - /* Third, try to get a magic file from Common Files */ | |
| - if ((home = getenv("COMMONPROGRAMFILES")) != NULL) | |
| - _w32_append_path(&hmagicpath, "%s%s", home, hmagic); | |
| - | |
| - /* Fourth, try to get magic file relative to exe location */ | |
| - _w32_get_magic_relative_to(&hmagicpath, NULL); | |
| - | |
| - /* Fifth, try to get magic file relative to dll location */ | |
| - _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance); | |
| - | |
| - /* Avoid MAGIC constant - it likely points to a file within MSys tree */ | |
| - default_magic = hmagicpath; | |
| - return default_magic; | |
| -#endif | |
| -} | |
| - | |
| -file_public const char * | |
| -magic_getpath(const char *magicfile, int action) | |
| -{ | |
| - if (magicfile != NULL) | |
| - return magicfile; | |
| - | |
| - magicfile = getenv("MAGIC"); | |
| - if (magicfile != NULL) | |
| - return magicfile; | |
| - | |
| - return action == FILE_LOAD ? get_default_magic() : MAGIC; | |
| -} | |
| - | |
| file_public struct magic_set * | |
| magic_open(int flags) | |
| { | |
| return file_apprentice(ms, magicfile, FILE_LOAD); | |
| } | |
| -#ifndef COMPILE_ONLY | |
| -/* | |
| - * Install a set of compiled magic buffers. | |
| - */ | |
| -file_public int | |
| -magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes, | |
| - size_t nbufs) | |
| -{ | |
| - if (ms == NULL) | |
| - return -1; | |
| - return buffer_apprentice(ms, RCAST(struct magic **, bufs), | |
| - sizes, nbufs); | |
| -} | |
| -#endif | |
| - | |
| file_public int | |
| magic_compile(struct magic_set *ms, const char *magicfile) | |
| { | |
| return file_apprentice(ms, magicfile, FILE_LIST); | |
| } | |
| -file_private void | |
| -close_and_restore(const struct magic_set *ms, const char *name, int fd, | |
| - const struct stat *sb) | |
| -{ | |
| - if (fd == STDIN_FILENO || name == NULL) | |
| - return; | |
| - (void) close(fd); | |
| - | |
| - if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) { | |
| - /* | |
| - * Try to restore access, modification times if read it. | |
| - * This is really *bad* because it will modify the status | |
| - * time of the file... And of course this will affect | |
| - * backup programs | |
| - */ | |
| -#ifdef HAVE_UTIMES | |
| - struct timeval utsbuf[2]; | |
| - (void)memset(utsbuf, 0, sizeof(utsbuf)); | |
| - utsbuf[0].tv_sec = sb->st_atime; | |
| - utsbuf[1].tv_sec = sb->st_mtime; | |
| - | |
| - (void) utimes(name, utsbuf); /* don't care if loses */ | |
| -#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H) | |
| - struct utimbuf utbuf; | |
| - | |
| - (void)memset(&utbuf, 0, sizeof(utbuf)); | |
| - utbuf.actime = sb->st_atime; | |
| - utbuf.modtime = sb->st_mtime; | |
| - (void) utime(name, &utbuf); /* don't care if loses */ | |
| -#endif | |
| - } | |
| -} | |
| - | |
| #ifndef COMPILE_ONLY | |
| /* | |
| { | |
| if (ms == NULL) | |
| return NULL; | |
| - return file_or_fd(ms, NULL, fd); | |
| + return file_or_stream(ms, NULL, NULL); | |
| } | |
| /* | |
| { | |
| if (ms == NULL) | |
| return NULL; | |
| - return file_or_fd(ms, inname, STDIN_FILENO); | |
| + return file_or_stream(ms, inname, NULL); | |
| +} | |
| + | |
| +file_public const char * | |
| +magic_stream(struct magic_set *ms, php_stream *stream) | |
| +{ | |
| + if (ms == NULL) | |
| + return NULL; | |
| + return file_or_stream(ms, NULL, stream); | |
| } | |
| file_private const char * | |
| -file_or_fd(struct magic_set *ms, const char *inname, int fd) | |
| +file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream) | |
| { | |
| int rv = -1; | |
| unsigned char *buf; | |
| - struct stat sb; | |
| + zend_stat_t sb = {0}; | |
| ssize_t nbytes = 0; /* number of bytes read from a datafile */ | |
| - int ispipe = 0; | |
| - int okstat = 0; | |
| - off_t pos = CAST(off_t, -1); | |
| + int no_in_stream = 0; | |
| if (file_reset(ms, 1) == -1) | |
| goto out; | |
| * some overlapping space for matches near EOF | |
| */ | |
| #define SLOP (1 + sizeof(union VALUETYPE)) | |
| - if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL) | |
| + if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL) | |
| return NULL; | |
| switch (file_fsmagic(ms, inname, &sb)) { | |
| goto done; | |
| } | |
| -#ifdef WIN32 | |
| - /* Place stdin in binary mode, so EOF (Ctrl+Z) doesn't stop early. */ | |
| - if (fd == STDIN_FILENO) | |
| - _setmode(STDIN_FILENO, O_BINARY); | |
| -#endif | |
| - if (inname != NULL) { | |
| - int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC; | |
| - errno = 0; | |
| - if ((fd = open(inname, flags)) < 0) { | |
| - okstat = stat(inname, &sb) == 0; | |
| -#ifdef WIN32 | |
| - /* | |
| - * Can't stat, can't open. It may have been opened in | |
| - * fsmagic, so if the user doesn't have read permission, | |
| - * allow it to say so; otherwise an error was probably | |
| - * displayed in fsmagic. | |
| - */ | |
| - if (!okstat && errno == EACCES) { | |
| - sb.st_mode = S_IFBLK; | |
| - okstat = 1; | |
| - } | |
| -#endif | |
| - if (okstat && | |
| - unreadable_info(ms, sb.st_mode, inname) == -1) | |
| + errno = 0; | |
| + | |
| + if (inname && !stream) { | |
| + no_in_stream = 1; | |
| + stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL); | |
| + if (!stream) { | |
| + if (unreadable_info(ms, sb.st_mode, inname) == -1) | |
| goto done; | |
| - rv = 0; | |
| + rv = -1; | |
| goto done; | |
| } | |
| -#if O_CLOEXEC == 0 && defined(F_SETFD) | |
| - (void)fcntl(fd, F_SETFD, FD_CLOEXEC); | |
| -#endif | |
| } | |
| - if (fd != -1) { | |
| - okstat = fstat(fd, &sb) == 0; | |
| - if (okstat && S_ISFIFO(sb.st_mode)) | |
| - ispipe = 1; | |
| - if (inname == NULL) | |
| - pos = lseek(fd, CAST(off_t, 0), SEEK_CUR); | |
| + php_stream_statbuf ssb; | |
| + if (php_stream_stat(stream, &ssb) < 0) { | |
| + if (ms->flags & MAGIC_ERROR) { | |
| + file_error(ms, errno, "cannot stat `%s'", inname); | |
| + rv = -1; | |
| + goto done; | |
| + } | |
| } | |
| + memcpy(&sb, &ssb.sb, sizeof(zend_stat_t)); | |
| /* | |
| * try looking at the first ms->bytes_max bytes | |
| */ | |
| - if (ispipe) { | |
| - if (fd != -1) { | |
| - ssize_t r = 0; | |
| - | |
| - while ((r = sread(fd, RCAST(void *, &buf[nbytes]), | |
| - CAST(size_t, ms->bytes_max - nbytes), 1)) > 0) { | |
| - nbytes += r; | |
| - if (r < PIPE_BUF) break; | |
| - } | |
| - } | |
| - | |
| - if (nbytes == 0 && inname) { | |
| - /* We can not read it, but we were able to stat it. */ | |
| - if (unreadable_info(ms, sb.st_mode, inname) == -1) | |
| - goto done; | |
| - rv = 0; | |
| - goto done; | |
| - } | |
| - | |
| - } else if (fd != -1) { | |
| - /* Windows refuses to read from a big console buffer. */ | |
| - size_t howmany = | |
| -#ifdef WIN32 | |
| - _isatty(fd) ? 8 * 1024 : | |
| -#endif | |
| - ms->bytes_max; | |
| - if ((nbytes = read(fd, RCAST(void *, buf), howmany)) == -1) { | |
| - if (inname == NULL && fd != STDIN_FILENO) | |
| - file_error(ms, errno, "cannot read fd %d", fd); | |
| - else | |
| - file_error(ms, errno, "cannot read `%s'", | |
| - inname == NULL ? "/dev/stdin" : inname); | |
| - goto done; | |
| - } | |
| + if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) { | |
| + file_error(ms, errno, "cannot read `%s'", inname); | |
| + goto done; | |
| } | |
| (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ | |
| - if (file_buffer(ms, fd, okstat ? &sb : NULL, inname, buf, CAST(size_t, nbytes)) == -1) | |
| + if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1) | |
| goto done; | |
| rv = 0; | |
| done: | |
| - free(buf); | |
| - if (fd != -1) { | |
| - if (pos != CAST(off_t, -1)) | |
| - (void)lseek(fd, pos, SEEK_SET); | |
| - close_and_restore(ms, inname, fd, &sb); | |
| + efree(buf); | |
| + | |
| + if (no_in_stream && stream) { | |
| + php_stream_close(stream); | |
| } | |
| out: | |
| return rv == 0 ? file_getbuffer(ms) : NULL; | |
| * The main work is done here! | |
| * We have the file name and/or the data buffer to be identified. | |
| */ | |
| - if (file_buffer(ms, -1, NULL, NULL, buf, nb) == -1) { | |
| + if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) { | |
| return NULL; | |
| } | |
| return file_getbuffer(ms); | |
| diff -u libmagic.orig/magic.h libmagic/magic.h | |
| --- libmagic.orig/magic.h 2025-02-09 12:43:22.903059789 +0100 | |
| +++ libmagic/magic.h 2025-02-09 01:39:57.110146603 +0100 | |
| * extensions */ | |
| #define MAGIC_COMPRESS_TRANSP 0x2000000 /* Check inside compressed files | |
| * but not report compression */ | |
| -#define MAGIC_NO_COMPRESS_FORK 0x4000000 /* Don't allow decompression that | |
| - * needs to fork */ | |
| #define MAGIC_NODESC (MAGIC_EXTENSION|MAGIC_MIME|MAGIC_APPLE) | |
| #define MAGIC_NO_CHECK_COMPRESS 0x0001000 /* Don't check for compressed files */ | |
| MAGIC_NO_CHECK_TOKENS | \ | |
| MAGIC_NO_CHECK_ENCODING | \ | |
| MAGIC_NO_CHECK_JSON | \ | |
| - MAGIC_NO_CHECK_SIMH | \ | |
| + MAGIC_NO_CHECK_SIMH | \ | |
| 0 \ | |
| ) | |
| b\20no_check_elf\0\ | |
| b\21no_check_text\0\ | |
| b\22no_check_cdf\0\ | |
| -b\23no_check_csv\0\ | |
| +b\23no_check_reserved0\0\ | |
| b\24no_check_tokens\0\ | |
| b\25no_check_encoding\0\ | |
| b\26no_check_json\0\ | |
| -b\27no_check_simh\0\ | |
| +b\27no_check_reserved2\0\ | |
| b\30extension\0\ | |
| b\31transp_compression\0\ | |
| " | |
| const char *magic_getpath(const char *, int); | |
| const char *magic_file(magic_t, const char *); | |
| +const char *magic_stream(magic_t, php_stream *); | |
| const char *magic_descriptor(magic_t, int); | |
| const char *magic_buffer(magic_t, const void *, size_t); | |
| diff -u libmagic.orig/print.c libmagic/print.c | |
| --- libmagic.orig/print.c 2024-10-06 19:04:42.000000000 +0200 | |
| +++ libmagic/print.c 2025-02-09 01:36:41.713156291 +0100 | |
| if (m->mask_op & FILE_OPINVERSE) | |
| (void) fputc('~', stderr); | |
| - if (IS_STRING(m->type)) { | |
| + if (IS_LIBMAGIC_STRING(m->type)) { | |
| if (m->str_flags) { | |
| (void) fputc('/', stderr); | |
| if (m->str_flags & STRING_COMPACT_WHITESPACE) | |
| static void __attribute__((__format__(__printf__, 1, 0))) | |
| file_vmagwarn(const char *f, va_list va) | |
| { | |
| - /* cuz we use stdout for most, stderr here */ | |
| - (void) fflush(stdout); | |
| + char *expanded_format = NULL; | |
| + int expanded_len = vasprintf(&expanded_format, f, va); | |
| - (void) fprintf(stderr, "Warning: "); | |
| - (void) vfprintf(stderr, f, va); | |
| - (void) fputc('\n', stderr); | |
| + if (expanded_len >= 0 && expanded_format) { | |
| + php_error_docref(NULL, E_WARNING, "%s", expanded_format); | |
| + | |
| + free(expanded_format); | |
| + } | |
| } | |
| /*VARARGS*/ | |
| file_protected void | |
| file_magwarn(struct magic_set *ms, const char *f, ...) | |
| { | |
| + /* Upstream has a check here: ++ms->magwarn == ms->magwarn_max, | |
| + * but for PHP BC we keep emitting all warnings and | |
| + * letting the user control the output behaviour. */ | |
| va_list va; | |
| - if (++ms->magwarn == ms->magwarn_max) { | |
| - (void) fprintf(stderr, | |
| - "%s, %lu: Maximum number of warnings (%u) exceeded.\n", | |
| - ms->file, CAST(unsigned long, ms->line), | |
| - ms->magwarn_max); | |
| - (void) fprintf(stderr, | |
| - "%s, %lu: Additional warnings are suppressed.\n", | |
| - ms->file, CAST(unsigned long, ms->line)); | |
| - } | |
| - if (ms->magwarn >= ms->magwarn_max) { | |
| - return; | |
| - } | |
| - | |
| - if (ms->file) | |
| - (void) fprintf(stderr, "%s, %lu: ", ms->file, | |
| - CAST(unsigned long, ms->line)); | |
| - | |
| va_start(va, f); | |
| file_vmagwarn(f, va); | |
| va_end(va); | |
| if (flags & FILE_T_LOCAL) { | |
| tzset(); | |
| - tm = localtime_r(&t, &tmz); | |
| + tm = php_localtime_r(&t, &tmz); | |
| } else { | |
| - tm = gmtime_r(&t, &tmz); | |
| + tm = php_gmtime_r(&t, &tmz); | |
| } | |
| if (tm == NULL) | |
| goto out; | |
| - pp = asctime_r(tm, buf); | |
| + pp = php_asctime_r(tm, buf); | |
| if (pp == NULL) | |
| goto out; | |
| diff -u libmagic.orig/readcdf.c libmagic/readcdf.c | |
| --- libmagic.orig/readcdf.c 2024-11-25 22:07:46.000000000 +0100 | |
| +++ libmagic/readcdf.c 2025-02-09 01:20:19.771337672 +0100 | |
| #include <assert.h> | |
| #include <stdlib.h> | |
| +#ifdef HAVE_UNISTD_H | |
| #include <unistd.h> | |
| +#endif | |
| #include <string.h> | |
| #include <time.h> | |
| #include <ctype.h> | |
| if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1]) | |
| return cv[i].mime; | |
| } | |
| -#ifdef CDF_DEBUG | |
| - fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0], | |
| - clsid[1]); | |
| -#endif | |
| return NULL; | |
| } | |
| { | |
| size_t i; | |
| const char *rv = NULL; | |
| -#ifdef USE_C_LOCALE | |
| - locale_t old_lc_ctype, c_lc_ctype; | |
| + char *vbuf_lower; | |
| - c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0); | |
| - assert(c_lc_ctype != NULL); | |
| - old_lc_ctype = uselocale(c_lc_ctype); | |
| - assert(old_lc_ctype != NULL); | |
| -#else | |
| - char *old_lc_ctype = setlocale(LC_CTYPE, NULL); | |
| - assert(old_lc_ctype != NULL); | |
| - old_lc_ctype = strdup(old_lc_ctype); | |
| - assert(old_lc_ctype != NULL); | |
| - (void)setlocale(LC_CTYPE, "C"); | |
| -#endif | |
| - for (i = 0; nv[i].pattern != NULL; i++) | |
| - if (strcasestr(vbuf, nv[i].pattern) != NULL) { | |
| + vbuf_lower = zend_str_tolower_dup(vbuf, strlen(vbuf)); | |
| + for (i = 0; nv[i].pattern != NULL; i++) { | |
| + char *pattern_lower; | |
| + int found; | |
| + | |
| + pattern_lower = zend_str_tolower_dup(nv[i].pattern, strlen(nv[i].pattern)); | |
| + found = (strstr(vbuf_lower, pattern_lower) != NULL); | |
| + efree(pattern_lower); | |
| + | |
| + if (found) { | |
| rv = nv[i].mime; | |
| break; | |
| } | |
| -#ifdef CDF_DEBUG | |
| - fprintf(stderr, "unknown app %s\n", vbuf); | |
| -#endif | |
| -#ifdef USE_C_LOCALE | |
| - (void)uselocale(old_lc_ctype); | |
| - freelocale(c_lc_ctype); | |
| -#else | |
| - (void)setlocale(LC_CTYPE, old_lc_ctype); | |
| - free(old_lc_ctype); | |
| -#endif | |
| + } | |
| + | |
| + efree(vbuf_lower); | |
| return rv; | |
| } | |
| const char *s, *e; | |
| int len; | |
| + memset(&ts, 0, sizeof(ts)); | |
| + | |
| if (!NOTMIME(ms) && root_storage) | |
| str = cdf_clsid_to_mime(root_storage->d_storage_uuid, | |
| clsid2mime); | |
| if (file_printf(ms, "%s%s", | |
| cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name), | |
| i == cat->cat_num - 1 ? "]" : ", ") == -1) { | |
| - free(cat); | |
| + efree(cat); | |
| return -1; | |
| } | |
| - free(cat); | |
| + efree(cat); | |
| } else if (ms->flags & MAGIC_MIME_TYPE) { | |
| if (file_printf(ms, "application/CDFV2") == -1) | |
| return -1; | |
| } | |
| m = cdf_file_property_info(ms, info, count, root_storage); | |
| - free(info); | |
| + efree(info); | |
| return m == -1 ? -2 : m; | |
| } | |
| cdf_zero_stream(&scn); | |
| cdf_zero_stream(&sst); | |
| out3: | |
| - free(dir.dir_tab); | |
| + efree(dir.dir_tab); | |
| out2: | |
| - free(ssat.sat_tab); | |
| + efree(ssat.sat_tab); | |
| out1: | |
| - free(sat.sat_tab); | |
| + efree(sat.sat_tab); | |
| out0: | |
| /* If we handled it already, return */ | |
| if (i != -1) | |
| diff -u libmagic.orig/softmagic.c libmagic/softmagic.c | |
| --- libmagic.orig/softmagic.c 2024-11-27 16:37:00.000000000 +0100 | |
| +++ libmagic/softmagic.c 2025-02-09 01:21:46.845689318 +0100 | |
| #include "file.h" | |
| #ifndef lint | |
| -FILE_RCSID("@(#)$File: softmagic.c,v 1.350 2024/11/27 15:37:00 christos Exp $") | |
| +FILE_RCSID("@(#)$File: softmagic.c,v 1.349 2024/11/10 18:33:17 christos Exp $") | |
| #endif /* lint */ | |
| #include "magic.h" | |
| #include <time.h> | |
| #include "der.h" | |
| -file_private int match(struct magic_set *, struct magic *, file_regex_t **, size_t, | |
| +file_private int match(struct magic_set *, struct magic *, size_t, | |
| const struct buffer *, size_t, int, int, int, uint16_t *, | |
| uint16_t *, int *, int *, int *, int *, int *); | |
| file_private int mget(struct magic_set *, struct magic *, const struct buffer *, | |
| uint16_t *, int *, int *, int *, int *, int *); | |
| file_private int msetoffset(struct magic_set *, struct magic *, struct buffer *, | |
| const struct buffer *, size_t, unsigned int); | |
| -file_private int magiccheck(struct magic_set *, struct magic *, file_regex_t **); | |
| +file_private int magiccheck(struct magic_set *, struct magic *); | |
| file_private int mprint(struct magic_set *, struct magic *); | |
| file_private int moffset(struct magic_set *, struct magic *, | |
| const struct buffer *, size_t, int32_t *); | |
| } | |
| for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next) { | |
| - int ret = match(ms, ml->magic, ml->magic_rxcomp, ml->nmagic, b, | |
| + int ret = match(ms, ml->magic, ml->nmagic, b, | |
| 0, mode, text, 0, indir_count, name_count, | |
| &printed_something, &need_separator, &firstline, | |
| NULL, NULL); | |
| return rv; | |
| } | |
| -#define FILE_FMTDEBUG | |
| -#ifdef FILE_FMTDEBUG | |
| + | |
| +#if defined(FILE_FMTDEBUG) && defined(HAVE_FMTCHECK) | |
| #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__) | |
| file_private const char * __attribute__((__format_arg__(3))) | |
| " with `%s'", file, line, desc, def); | |
| return ptr; | |
| } | |
| -#else | |
| +#elif defined(HAVE_FMTCHECK) | |
| #define F(a, b, c) fmtcheck((b), (c)) | |
| +#else | |
| +#define F(a, b, c) ((b)) | |
| #endif | |
| +/* NOTE this function has been kept an the state of 5.39 for BC. Observe | |
| + * further as the upgrade to 5.41 or above goes. */ | |
| /* | |
| * Go through the whole list, stopping if you find a match. Process all | |
| * the continuations of that match before returning. | |
| * so that higher-level continuations are processed. | |
| */ | |
| file_private int | |
| -match(struct magic_set *ms, struct magic *magic, file_regex_t **magic_rxcomp, | |
| +match(struct magic_set *ms, struct magic *magic, | |
| size_t nmagic, const struct buffer *b, size_t offset, int mode, int text, | |
| int flip, uint16_t *indir_count, uint16_t *name_count, | |
| int *printed_something, int *need_separator, int *firstline, | |
| for (magindex = 0; magindex < nmagic; magindex++) { | |
| int flush = 0; | |
| struct magic *m = &magic[magindex]; | |
| - file_regex_t **m_rxcomp = &magic_rxcomp[magindex]; | |
| if (m->type != FILE_NAME) | |
| - if ((IS_STRING(m->type) && | |
| + if ((IS_LIBMAGIC_STRING(m->type) && | |
| #define FLT (STRING_BINTEST | STRING_TEXTTEST) | |
| ((text && (m->str_flags & FLT) == STRING_BINTEST) || | |
| (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) || | |
| *returnval = 1; | |
| } | |
| - switch (magiccheck(ms, m, m_rxcomp)) { | |
| + switch (magiccheck(ms, m)) { | |
| case -1: | |
| return -1; | |
| case 0: | |
| while (magindex + 1 < nmagic && | |
| magic[magindex + 1].cont_level != 0) { | |
| m = &magic[++magindex]; | |
| - m_rxcomp = &magic_rxcomp[magindex]; | |
| ms->line = m->lineno; /* for messages */ | |
| if (cont_level < m->cont_level) | |
| break; | |
| } | |
| - switch (flush ? 1 : magiccheck(ms, m, m_rxcomp)) { | |
| + switch (flush ? 1 : magiccheck(ms, m)) { | |
| case -1: | |
| return -1; | |
| case 0: | |
| file_private int | |
| check_fmt(struct magic_set *ms, const char *fmt) | |
| { | |
| - file_regex_t rx; | |
| - int rc, rv = -1; | |
| - const char* pat = "%[-0-9\\.]*s"; | |
| + pcre_cache_entry *pce; | |
| + int rv = -1; | |
| + zend_string *pattern; | |
| if (strchr(fmt, '%') == NULL) | |
| return 0; | |
| - rc = file_regcomp(ms, &rx, pat, REG_EXTENDED|REG_NOSUB); | |
| - if (rc == 0) { | |
| - rc = file_regexec(ms, &rx, fmt, 0, 0, 0); | |
| - rv = !rc; | |
| + pattern = ZSTR_INIT_LITERAL("~%[-0-9\\.]*s~", 0); | |
| + if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) { | |
| + rv = -1; | |
| + } else { | |
| + pcre2_code *re = php_pcre_pce_re(pce); | |
| + pcre2_match_data *match_data = php_pcre_create_match_data(0, re); | |
| + if (match_data) { | |
| + rv = pcre2_match(re, (PCRE2_SPTR)fmt, strlen(fmt), 0, 0, match_data, php_pcre_mctx()) > 0; | |
| + php_pcre_free_match_data(match_data); | |
| + } | |
| } | |
| - file_regfree(&rx); | |
| + zend_string_release_ex(pattern, 0); | |
| return rv; | |
| } | |
| for (len = 0; len < n && str[len]; len++) | |
| continue; | |
| - if ((copy = CAST(char *, malloc(len + 1))) == NULL) | |
| + if ((copy = CAST(char *, emalloc(len + 1))) == NULL) | |
| return NULL; | |
| (void)memcpy(copy, str, len); | |
| copy[len] = '\0'; | |
| char *cp, *scp; | |
| int rval; | |
| - cp = strndup(RCAST(const char *, ms->search.s), | |
| + cp = estrndup(RCAST(const char *, ms->search.s), | |
| ms->search.rm_len); | |
| if (cp == NULL) { | |
| file_oomem(ms, ms->search.rm_len); | |
| rval = file_printf(ms, F(ms, desc, "%s"), file_printable(ms, | |
| sbuf, sizeof(sbuf), scp, ms->search.rm_len)); | |
| - free(cp); | |
| + efree(cp); | |
| if (rval == -1) | |
| return -1; | |
| ms->eoffset = ms->offset = CAST(int32_t, b->elen - m->offset); | |
| } else { | |
| offset = m->offset; | |
| - if ((m->flag & OFFPOSITIVE) || cont_level == 0) { | |
| + if (cont_level == 0) { | |
| normal: | |
| // XXX: Pass real fd, then who frees bb? | |
| buffer_init(bb, -1, NULL, b->fbuf, b->flen); | |
| size_t len; | |
| *c = ms->c; | |
| len = c->len * sizeof(*c->li); | |
| - ms->c.li = CAST(struct level_info *, malloc(len)); | |
| + ms->c.li = CAST(struct level_info *, emalloc(len)); | |
| if (ms->c.li == NULL) { | |
| ms->c = *c; | |
| return -1; | |
| file_private void | |
| restore_cont(struct magic_set *ms, struct cont *c) | |
| { | |
| - free(ms->c.li); | |
| + efree(ms->c.li); | |
| ms->c = *c; | |
| } | |
| for (mlp = ms->mlist[0]->next; mlp != ms->mlist[0]; | |
| mlp = mlp->next) | |
| { | |
| - if ((rv = match(ms, mlp->magic, mlp->magic_rxcomp, | |
| + if ((rv = match(ms, mlp->magic, | |
| mlp->nmagic, &bb, 0, BINTEST, text, 0, indir_count, | |
| name_count, printed_something, need_separator, | |
| firstline, NULL, NULL)) != 0) | |
| if ((ms->flags & MAGIC_NODESC) == 0 && | |
| file_printf(ms, F(ms, m->desc, "%u"), offset) == -1) | |
| { | |
| - free(rbuf); | |
| + if (rbuf) efree(rbuf); | |
| return -1; | |
| } | |
| if (file_printf(ms, "%s", rbuf) == -1) { | |
| - free(rbuf); | |
| + if (rbuf) efree(rbuf); | |
| return -1; | |
| } | |
| } | |
| - free(rbuf); | |
| + if (rbuf) efree(rbuf); | |
| return rv; | |
| case FILE_USE: | |
| nfound_match = 0; | |
| (*name_count)++; | |
| eoffset = ms->eoffset; | |
| - rv = match(ms, ml.magic, ml.magic_rxcomp, ml.nmagic, b, | |
| + rv = match(ms, ml.magic, ml.nmagic, b, | |
| offset + o, mode, text, flip, indir_count, name_count, | |
| printed_something, need_separator, firstline, returnval, | |
| &nfound_match); | |
| } | |
| else if ((flags & STRING_COMPACT_WHITESPACE) && | |
| isspace(*a)) { | |
| + /* XXX Dirty. The data and the pattern is what is causing this. | |
| + Revert _i for the next port and see if it still matters. */ | |
| + uint32_t _i = 0; | |
| a++; | |
| - if (isspace(*b)) { | |
| - b++; | |
| + if (isspace(*b++)) { | |
| if (!isspace(*a)) | |
| - while (b < eb && isspace(*b)) | |
| + while (EXPECTED(_i++ < 2048) && b < eb && isspace(*b)) | |
| b++; | |
| } | |
| else { | |
| return file_strncmp(a, b, len, maxlen, flags); | |
| } | |
| -file_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; | |
| -} | |
| - | |
| file_private int | |
| -magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache) | |
| +magiccheck(struct magic_set *ms, struct magic *m) | |
| { | |
| uint64_t l = m->value.q; | |
| uint64_t v; | |
| slen = MIN(m->vallen, sizeof(m->value.s)); | |
| l = 0; | |
| v = 0; | |
| - if ((ms->flags & MAGIC_DEBUG) != 0) { | |
| - size_t xlen = ms->search.s_len > 100 ? 100 | |
| - : ms->search.s_len; | |
| - | |
| - fprintf(stderr, "search: ["); | |
| - file_showstr(stderr, ms->search.s, xlen); | |
| - fprintf(stderr, "%s] for [", ms->search.s_len == xlen | |
| - ? "" : "..."); | |
| - file_showstr(stderr, m->value.s, slen); | |
| - } | |
| #ifdef HAVE_MEMMEM | |
| if (slen > 0 && m->str_flags == 0) { | |
| const char *found; | |
| idx = m->str_range + slen; | |
| if (m->str_range == 0 || ms->search.s_len < idx) | |
| idx = ms->search.s_len; | |
| - found = CAST(const char *, memmem(ms->search.s, idx, | |
| - m->value.s, slen)); | |
| - if ((ms->flags & MAGIC_DEBUG) != 0) { | |
| - fprintf(stderr, "] %sfound\n", | |
| - found ? "" : "not "); | |
| - } | |
| + found = CAST(const char *, php_memnstr(ms->search.s, | |
| + m->value.s, slen, ms->search.s + idx)); | |
| if (!found) { | |
| v = 1; | |
| break; | |
| break; | |
| } | |
| } | |
| - if ((ms->flags & MAGIC_DEBUG) != 0) { | |
| - fprintf(stderr, "] %sfound\n", v == 0 ? "" : "not "); | |
| - } | |
| break; | |
| } | |
| case FILE_REGEX: { | |
| - int rc; | |
| - file_regex_t *rx = *m_cache; | |
| - const char *search; | |
| - regmatch_t pmatch; | |
| - size_t slen = ms->search.s_len; | |
| - char *copy; | |
| + zend_string *pattern; | |
| + uint32_t options = 0; | |
| + pcre_cache_entry *pce; | |
| - if (ms->search.s == NULL) | |
| - return 0; | |
| + options |= PCRE2_MULTILINE; | |
| - if (rx == NULL) { | |
| - rx = *m_cache = alloc_regex(ms, m); | |
| - if (rx == NULL) | |
| - return -1; | |
| + if (m->str_flags & STRING_IGNORE_CASE) { | |
| + options |= PCRE2_CASELESS; | |
| } | |
| - l = 0; | |
| - if (slen != 0) { | |
| - copy = CAST(char *, malloc(slen)); | |
| - if (copy == NULL) { | |
| - file_error(ms, errno, | |
| - "can't allocate %" SIZE_T_FORMAT "u bytes", | |
| - slen); | |
| - return -1; | |
| - } | |
| - memcpy(copy, ms->search.s, slen); | |
| - copy[--slen] = '\0'; | |
| - search = copy; | |
| - } else { | |
| - search = CCAST(char *, ""); | |
| - copy = NULL; | |
| - } | |
| - rc = file_regexec(ms, rx, RCAST(const char *, search), | |
| - 1, &pmatch, 0); | |
| - free(copy); | |
| - switch (rc) { | |
| - case 0: | |
| - ms->search.s += CAST(int, pmatch.rm_so); | |
| - ms->search.offset += CAST(size_t, pmatch.rm_so); | |
| - ms->search.rm_len = CAST(size_t, | |
| - pmatch.rm_eo - pmatch.rm_so); | |
| - v = 0; | |
| - break; | |
| - case REG_NOMATCH: | |
| - v = 1; | |
| - break; | |
| + pattern = convert_libmagic_pattern((char *)m->value.s, m->vallen, options); | |
| - default: | |
| + l = v = 0; | |
| + if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) { | |
| + zend_string_release(pattern); | |
| return -1; | |
| + } else { | |
| + /* pce now contains the compiled regex */ | |
| + zval retval; | |
| + zval subpats; | |
| + zend_string *haystack; | |
| + | |
| + ZVAL_NULL(&retval); | |
| + ZVAL_NULL(&subpats); | |
| + | |
| + /* Cut the search len from haystack, equals to REG_STARTEND */ | |
| + haystack = zend_string_init(ms->search.s, ms->search.s_len, 0); | |
| + | |
| + /* match v = 0, no match v = 1 */ | |
| + php_pcre_match_impl(pce, haystack, &retval, &subpats, 0, PREG_OFFSET_CAPTURE, 0); | |
| + /* Free haystack */ | |
| + zend_string_release(haystack); | |
| + | |
| + if (Z_LVAL(retval) < 0) { | |
| + zval_ptr_dtor(&subpats); | |
| + zend_string_release(pattern); | |
| + return -1; | |
| + } else if ((Z_LVAL(retval) > 0) && (Z_TYPE(subpats) == IS_ARRAY)) { | |
| + /* Need to fetch global match which equals pmatch[0] */ | |
| + zval *pzval; | |
| + HashTable *ht = Z_ARRVAL(subpats); | |
| + if ((pzval = zend_hash_index_find(ht, 0)) != NULL && Z_TYPE_P(pzval) == IS_ARRAY) { | |
| + /* If everything goes according to the master plan | |
| + tmpcopy now contains two elements: | |
| + 0 = the match | |
| + 1 = starting position of the match */ | |
| + zval *match, *offset; | |
| + if ((match = zend_hash_index_find(Z_ARRVAL_P(pzval), 0)) && | |
| + (offset = zend_hash_index_find(Z_ARRVAL_P(pzval), 1))) { | |
| + if (Z_TYPE_P(match) != IS_STRING && Z_TYPE_P(offset) != IS_LONG) { | |
| + goto error_out; | |
| + } | |
| + ms->search.s += Z_LVAL_P(offset); /* this is where the match starts */ | |
| + ms->search.offset += Z_LVAL_P(offset); /* this is where the match starts as size_t */ | |
| + ms->search.rm_len = Z_STRLEN_P(match) /* This is the length of the matched pattern */; | |
| + v = 0; | |
| + } else { | |
| + goto error_out; | |
| + } | |
| + } else { | |
| +error_out: | |
| + zval_ptr_dtor(&subpats); | |
| + zend_string_release(pattern); | |
| + return -1; | |
| + } | |
| + } else { | |
| + v = 1; | |
| + } | |
| + zval_ptr_dtor(&subpats); | |
| + zend_string_release(pattern); | |
| } | |
| break; | |
| } | |