Skip to content

Commit 25bb817

Browse files
committed
fileinfo: libmagic: Simplify libmagic patch
For the time being, the new args passing pre-compiled regex are cut out. On the PHP side, PCRE patterns are cached and thus a duplicated mechanism isn't necessary. There might be still advantages to carry pre-compiled patterns for libmagic separately, but reusing this mechanism needs a better consideration, as it might require to retain from the PHP internal pattern caching mechanism. Signed-off-by: Anatol Belski <ab@php.net>
1 parent a4280d2 commit 25bb817

File tree

5 files changed

+13
-24
lines changed

5 files changed

+13
-24
lines changed

ext/fileinfo/libmagic/apprentice.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ private uint64_t swap8(uint64_t);
147147
#endif
148148

149149
private char *mkdbname(struct magic_set *, const char *, int);
150-
private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
151-
size_t);
152150
private struct magic_map *apprentice_map(struct magic_set *, const char *);
153-
private int check_buffer(struct magic_set *, struct magic_map *, const char *);
154151
private void apprentice_unmap(struct magic_map *);
155152
private int apprentice_compile(struct magic_set *, struct magic_map *,
156153
const char *);
@@ -597,8 +594,6 @@ file_ms_alloc(int flags)
597594
private void
598595
apprentice_unmap(struct magic_map *map)
599596
{
600-
size_t i;
601-
char *p;
602597
if (map == NULL)
603598
return;
604599
if (map->p != php_magic_database) {
@@ -1316,7 +1311,7 @@ load_1(struct magic_set *ms, int action, const char *fn, int *errs,
13161311
continue;
13171312
}
13181313
if ((*bang[i].fun)(ms, &me,
1319-
line + bang[i].len + 2,
1314+
line + bang[i].len + 2,
13201315
len - bang[i].len - 2) != 0) {
13211316
(*errs)++;
13221317
continue;
@@ -3648,7 +3643,6 @@ file_magicfind(struct magic_set *ms, const char *name, struct mlist *v)
36483643
continue;
36493644
if (strcmp(ma[i].value.s, name) == 0) {
36503645
v->magic = &ma[i];
3651-
v->magic_rxcomp = &(ml->magic_rxcomp[i]);
36523646
for (j = i + 1; j < ml->nmagic; j++)
36533647
if (ma[j].cont_level == 0)
36543648
break;

ext/fileinfo/libmagic/file.h

-3
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@
164164
#define FILE_COMPILE 2
165165
#define FILE_LIST 3
166166

167-
typedef void* file_regex_t;
168-
169167
struct buffer {
170168
int fd;
171169
zend_stat_t st;
@@ -405,7 +403,6 @@ struct magic {
405403
/* list of magic entries */
406404
struct mlist {
407405
struct magic *magic; /* array of magic entries */
408-
file_regex_t **magic_rxcomp; /* array of compiled regexps */
409406
size_t nmagic; /* number of entries in array */
410407
void *map; /* internal resources used by entry */
411408
struct mlist *next, *prev;

ext/fileinfo/libmagic/funcs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ file_checkfmt(char *msg, size_t mlen, const char *fmt)
132132
protected int
133133
file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
134134
{
135-
int len;
135+
size_t len;
136136
char *buf, *newstr;
137137
char tbuf[1024];
138138

@@ -150,7 +150,7 @@ file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
150150
size_t blen = ms->o.blen;
151151
if (buf) efree(buf);
152152
file_clearbuf(ms);
153-
file_error(ms, 0, "Output buffer space exceeded %d+%"
153+
file_error(ms, 0, "Output buffer space exceeded %" SIZE_T_FORMAT "u+%"
154154
SIZE_T_FORMAT "u", len, blen);
155155
return -1;
156156
}

ext/fileinfo/libmagic/readcdf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
145145

146146
memset(&ts, 0, sizeof(ts));
147147

148-
if (!NOTMIME(ms) && root_storage)
148+
if (!NOTMIME(ms) && root_storage)
149149
str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
150150
clsid2mime);
151151

ext/fileinfo/libmagic/softmagic.c

+9-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.328 2022/09/13 18:46:07 christos Exp $")
4343
#include <time.h>
4444
#include "der.h"
4545

46-
private int match(struct magic_set *, struct magic *, file_regex_t **, size_t,
46+
private int match(struct magic_set *, struct magic *, size_t,
4747
const struct buffer *, size_t, int, int, int, uint16_t *,
4848
uint16_t *, int *, int *, int *, int *);
4949
private int mget(struct magic_set *, struct magic *, const struct buffer *,
@@ -52,7 +52,7 @@ private int mget(struct magic_set *, struct magic *, const struct buffer *,
5252
uint16_t *, int *, int *, int *, int *);
5353
private int msetoffset(struct magic_set *, struct magic *, struct buffer *,
5454
const struct buffer *, size_t, unsigned int);
55-
private int magiccheck(struct magic_set *, struct magic *, file_regex_t **);
55+
private int magiccheck(struct magic_set *, struct magic *);
5656
private int mprint(struct magic_set *, struct magic *);
5757
private int moffset(struct magic_set *, struct magic *, const struct buffer *,
5858
int32_t *);
@@ -131,7 +131,7 @@ file_softmagic(struct magic_set *ms, const struct buffer *b,
131131
}
132132

133133
for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next) {
134-
int ret = match(ms, ml->magic, ml->magic_rxcomp, ml->nmagic, b,
134+
int ret = match(ms, ml->magic, ml->nmagic, b,
135135
0, mode, text, 0, indir_count, name_count,
136136
&printed_something, &need_separator, NULL, NULL);
137137
switch (ret) {
@@ -206,7 +206,7 @@ file_fmtcheck(struct magic_set *ms, const char *desc, const char *def,
206206
* so that higher-level continuations are processed.
207207
*/
208208
private int
209-
match(struct magic_set *ms, struct magic *magic, file_regex_t **magic_rxcomp,
209+
match(struct magic_set *ms, struct magic *magic,
210210
size_t nmagic, const struct buffer *b, size_t offset, int mode, int text,
211211
int flip, uint16_t *indir_count, uint16_t *name_count,
212212
int *printed_something, int *need_separator, int *returnval,
@@ -236,7 +236,6 @@ match(struct magic_set *ms, struct magic *magic, file_regex_t **magic_rxcomp,
236236
for (magindex = 0; magindex < nmagic; magindex++) {
237237
int flush = 0;
238238
struct magic *m = &magic[magindex];
239-
file_regex_t **m_rxcomp = &magic_rxcomp[magindex];
240239

241240
if (m->type != FILE_NAME)
242241
if ((IS_LIBMAGIC_STRING(m->type) &&
@@ -274,7 +273,7 @@ match(struct magic_set *ms, struct magic *magic, file_regex_t **magic_rxcomp,
274273
*returnval = 1;
275274
}
276275

277-
switch (magiccheck(ms, m, m_rxcomp)) {
276+
switch (magiccheck(ms, m)) {
278277
case -1:
279278
return -1;
280279
case 0:
@@ -335,7 +334,6 @@ match(struct magic_set *ms, struct magic *magic, file_regex_t **magic_rxcomp,
335334
while (magindex + 1 < nmagic &&
336335
magic[magindex + 1].cont_level != 0) {
337336
m = &magic[++magindex];
338-
m_rxcomp = &magic_rxcomp[magindex];
339337
ms->line = m->lineno; /* for messages */
340338

341339
if (cont_level < m->cont_level)
@@ -389,7 +387,7 @@ match(struct magic_set *ms, struct magic *magic, file_regex_t **magic_rxcomp,
389387
break;
390388
}
391389

392-
switch (flush ? 1 : magiccheck(ms, m, m_rxcomp)) {
390+
switch (flush ? 1 : magiccheck(ms, m)) {
393391
case -1:
394392
return -1;
395393
case 0:
@@ -1870,7 +1868,7 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
18701868
for (mlp = ms->mlist[0]->next; mlp != ms->mlist[0];
18711869
mlp = mlp->next)
18721870
{
1873-
if ((rv = match(ms, mlp->magic, mlp->magic_rxcomp,
1871+
if ((rv = match(ms, mlp->magic,
18741872
mlp->nmagic, &bb, 0, BINTEST, text, 0, indir_count,
18751873
name_count, printed_something, need_separator,
18761874
NULL, NULL)) != 0)
@@ -1923,7 +1921,7 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
19231921
nfound_match = 0;
19241922
(*name_count)++;
19251923
eoffset = ms->eoffset;
1926-
rv = match(ms, ml.magic, ml.magic_rxcomp, ml.nmagic, b,
1924+
rv = match(ms, ml.magic, ml.nmagic, b,
19271925
offset + o, mode, text, flip, indir_count, name_count,
19281926
printed_something, need_separator, returnval,
19291927
&nfound_match);
@@ -2047,7 +2045,7 @@ file_strncmp16(const char *a, const char *b, size_t len, size_t maxlen,
20472045
}
20482046

20492047
private int
2050-
magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache)
2048+
magiccheck(struct magic_set *ms, struct magic *m)
20512049
{
20522050
uint64_t l = m->value.q;
20532051
uint64_t v;

0 commit comments

Comments
 (0)