Skip to content

Commit a3dd514

Browse files
committed
fileinfo: Introduce php_libmagic.* to simplify patch
Signed-off-by: Anatol Belski <ab@php.net>
1 parent a24727a commit a3dd514

21 files changed

+220
-283
lines changed

ext/fileinfo/config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int main(void)
4949
libmagic_sources="$libmagic_sources libmagic/strcasestr.c"
5050
],[AC_MSG_RESULT([skipped, cross-compiling])])
5151

52-
PHP_NEW_EXTENSION(fileinfo, fileinfo.c $libmagic_sources, $ext_shared,,-I@ext_srcdir@/libmagic)
52+
PHP_NEW_EXTENSION(fileinfo, fileinfo.c php_libmagic.c $libmagic_sources, $ext_shared,,-I@ext_srcdir@/libmagic)
5353
PHP_ADD_BUILD_DIR($ext_builddir/libmagic)
5454

5555
AC_CHECK_FUNCS([utimes strndup])

ext/fileinfo/config.w32

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ if (PHP_FILEINFO != 'no') {
1010
readcdf.c softmagic.c der.c \
1111
strcasestr.c buffer.c is_csv.c";
1212

13-
EXTENSION('fileinfo', 'fileinfo.c', true, "/I" + configure_module_dirname + "/libmagic /I" + configure_module_dirname);
13+
EXTENSION('fileinfo', 'fileinfo.c php_libmagic.c', true, "/I" + configure_module_dirname + "/libmagic /I" + configure_module_dirname);
1414
ADD_SOURCES(configure_module_dirname + '\\libmagic', LIBMAGIC_SOURCES, "fileinfo");
1515
}

ext/fileinfo/fileinfo.c

-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17-
#ifdef HAVE_CONFIG_H
18-
#include "config.h"
19-
#endif
2017
#include "php.h"
2118

2219
#include "libmagic/magic.h"

ext/fileinfo/libmagic/apprentice.c

+34-95
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
* apprentice - make one pass through /etc/magic, learning its secrets.
3030
*/
3131

32-
#include "php.h"
33-
3432
#include "file.h"
3533

3634
#ifndef lint
@@ -39,19 +37,6 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.326 2022/09/13 18:46:07 christos Exp $")
3937

4038
#include "magic.h"
4139
#include <stdlib.h>
42-
43-
#if defined(__hpux) && !defined(HAVE_STRTOULL)
44-
#if SIZEOF_LONG == 8
45-
# define strtoull strtoul
46-
#else
47-
# define strtoull __strtoull
48-
#endif
49-
#endif
50-
51-
#ifdef PHP_WIN32
52-
#include "win32/unistd.h"
53-
#define strtoull _strtoui64
54-
#else
5540
#ifdef HAVE_UNISTD_H
5641
#include <unistd.h>
5742
#endif
@@ -87,10 +72,6 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.326 2022/09/13 18:46:07 christos Exp $")
8772
#endif
8873
#endif
8974

90-
#ifndef offsetof
91-
#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
92-
#endif
93-
9475
#ifndef MAP_FAILED
9576
#define MAP_FAILED (void *) -1
9677
#endif
@@ -207,6 +188,39 @@ private struct {
207188

208189
#include "../data_file.c"
209190

191+
#ifdef COMPILE_ONLY
192+
193+
int main(int, char *[]);
194+
195+
int
196+
main(int argc, char *argv[])
197+
{
198+
int ret;
199+
struct magic_set *ms;
200+
char *progname;
201+
202+
if ((progname = strrchr(argv[0], '/')) != NULL)
203+
progname++;
204+
else
205+
progname = argv[0];
206+
207+
if (argc != 2) {
208+
(void)fprintf(stderr, "Usage: %s file\n", progname);
209+
return 1;
210+
}
211+
212+
if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
213+
(void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
214+
return 1;
215+
}
216+
ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
217+
if (ret == 1)
218+
(void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
219+
magic_close(ms);
220+
return ret;
221+
}
222+
#endif /* COMPILE_ONLY */
223+
210224
struct type_tbl_s {
211225
const char name[16];
212226
const size_t len;
@@ -440,15 +454,7 @@ add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx)
440454
ml->map = idx == 0 ? map : NULL;
441455
ml->magic = map->magic[idx];
442456
ml->nmagic = map->nmagic[idx];
443-
if (ml->nmagic) {
444-
ml->magic_rxcomp = CAST(file_regex_t **,
445-
ecalloc(ml->nmagic, sizeof(*ml->magic_rxcomp)));
446-
if (ml->magic_rxcomp == NULL) {
447-
efree(ml);
448-
return -1;
449-
}
450-
} else
451-
ml->magic_rxcomp = NULL;
457+
452458
mlp->prev->next = ml;
453459
ml->prev = mlp->prev;
454460
ml->next = mlp;
@@ -656,51 +662,6 @@ mlist_free(struct mlist *mlist)
656662
mlist_free_one(mlist);
657663
}
658664

659-
#ifndef COMPILE_ONLY
660-
/* void **bufs: an array of compiled magic files */
661-
protected int
662-
buffer_apprentice(struct magic_set *ms, struct magic **bufs,
663-
size_t *sizes, size_t nbufs)
664-
{
665-
size_t i, j;
666-
struct mlist *ml;
667-
struct magic_map *map;
668-
669-
if (nbufs == 0)
670-
return -1;
671-
672-
(void)file_reset(ms, 0);
673-
674-
init_file_tables();
675-
676-
for (i = 0; i < MAGIC_SETS; i++) {
677-
mlist_free(ms->mlist[i]);
678-
if ((ms->mlist[i] = mlist_alloc()) == NULL) {
679-
file_oomem(ms, sizeof(*ms->mlist[i]));
680-
goto fail;
681-
}
682-
}
683-
684-
for (i = 0; i < nbufs; i++) {
685-
map = apprentice_buf(ms, bufs[i], sizes[i]);
686-
if (map == NULL)
687-
goto fail;
688-
689-
for (j = 0; j < MAGIC_SETS; j++) {
690-
if (add_mlist(ms->mlist[j], map, j) == -1) {
691-
file_oomem(ms, sizeof(*ml));
692-
goto fail;
693-
}
694-
}
695-
}
696-
697-
return 0;
698-
fail:
699-
mlist_free_all(ms);
700-
return -1;
701-
}
702-
#endif
703-
704665
/* const char *fn: list of magic files and directories */
705666
protected int
706667
file_apprentice(struct magic_set *ms, const char *fn, int action)
@@ -3223,28 +3184,6 @@ eatsize(const char **p)
32233184
*p = l;
32243185
}
32253186

3226-
/*
3227-
* handle a buffer containing a compiled file.
3228-
*/
3229-
private struct magic_map *
3230-
apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
3231-
{
3232-
struct magic_map *map;
3233-
3234-
if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
3235-
file_oomem(ms, sizeof(*map));
3236-
return NULL;
3237-
}
3238-
map->len = len;
3239-
map->p = buf;
3240-
map->type = MAP_TYPE_USER;
3241-
if (check_buffer(ms, map, "buffer") != 0) {
3242-
apprentice_unmap(map);
3243-
return NULL;
3244-
}
3245-
return map;
3246-
}
3247-
32483187
/*
32493188
* handle a compiled file.
32503189
*/

ext/fileinfo/libmagic/buffer.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ FILE_RCSID("@(#)$File: buffer.c,v 1.8 2020/02/16 15:52:49 christos Exp $")
3131
#endif /* lint */
3232

3333
#include "magic.h"
34-
#ifdef PHP_WIN32
35-
#include "win32/unistd.h"
36-
#else
34+
#ifdef HAVE_UNISTD_H
3735
#include <unistd.h>
3836
#endif
3937
#include <string.h>

ext/fileinfo/libmagic/cdf.c

+4-47
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,9 @@ FILE_RCSID("@(#)$File: cdf.c,v 1.121 2021/10/20 13:56:15 christos Exp $")
4343
#include <err.h>
4444
#endif
4545
#include <stdlib.h>
46-
47-
#ifdef PHP_WIN32
48-
#include "win32/unistd.h"
49-
#else
46+
#ifdef HAVE_UNISTD_H
5047
#include <unistd.h>
5148
#endif
52-
53-
#ifndef UINT32_MAX
54-
# define UINT32_MAX (0xffffffff)
55-
#endif
56-
5749
#include <string.h>
5850
#include <time.h>
5951
#include <ctype.h>
@@ -101,44 +93,9 @@ static union {
10193
CDF_TOLE8(CAST(uint64_t, x))))
10294
#define CDF_GETUINT32(x, y) cdf_getuint32(x, y)
10395

104-
#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
105-
#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
106-
#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
107-
108-
109-
/*ARGSUSED*/
110-
static void *
111-
cdf_malloc(const char *file __attribute__((__unused__)),
112-
size_t line __attribute__((__unused__)), size_t n)
113-
{
114-
DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
115-
file, line, __func__, n));
116-
if (n == 0)
117-
n++;
118-
return malloc(n);
119-
}
120-
121-
/*ARGSUSED*/
122-
static void *
123-
cdf_realloc(const char *file __attribute__((__unused__)),
124-
size_t line __attribute__((__unused__)), void *p, size_t n)
125-
{
126-
DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
127-
file, line, __func__, n));
128-
return realloc(p, n);
129-
}
130-
131-
/*ARGSUSED*/
132-
static void *
133-
cdf_calloc(const char *file __attribute__((__unused__)),
134-
size_t line __attribute__((__unused__)), size_t n, size_t u)
135-
{
136-
DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u %"
137-
SIZE_T_FORMAT "u\n", file, line, __func__, n, u));
138-
if (n == 0)
139-
n++;
140-
return calloc(n, u);
141-
}
96+
#define CDF_MALLOC(n) emalloc(n)
97+
#define CDF_REALLOC(p, n) erealloc(p, n)
98+
#define CDF_CALLOC(n, u) ecalloc(n, u)
14299

143100
#if defined(HAVE_BYTESWAP_H)
144101
# define _cdf_tole2(x) bswap_16(x)

ext/fileinfo/libmagic/cdf.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@
3535
#ifndef _H_CDF_
3636
#define _H_CDF_
3737

38-
#ifdef PHP_WIN32
38+
#ifdef WIN32
3939
#include <winsock2.h>
40-
#define asctime_r php_asctime_r
41-
#define ctime_r php_ctime_r
4240
#endif
4341
#ifdef __DJGPP__
4442
#define timespec timeval

ext/fileinfo/libmagic/cdf_time.c

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2424
* POSSIBILITY OF SUCH DAMAGE.
2525
*/
26-
#include "php.h"
2726

2827
#include "file.h"
2928

ext/fileinfo/libmagic/config.h

+9
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
#include "php.h"
2+
#include "php_libmagic.h"
3+
4+
#ifndef HAVE_STDINT_H
5+
#define HAVE_STDINT_H 1
6+
#endif
7+
8+
#ifndef HAVE_STDINT_H
9+
#define HAVE_STDINT_H 1
10+
#endif

ext/fileinfo/libmagic/der.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ FILE_RCSID("@(#)$File: der.c,v 1.24 2022/07/30 18:08:36 christos Exp $")
5454
#include "magic.h"
5555
#include "der.h"
5656
#else
57-
#ifndef PHP_WIN32
57+
#ifdef HAVE_SYS_MMAN_H
5858
#include <sys/mman.h>
5959
#endif
6060
#include <sys/stat.h>

ext/fileinfo/libmagic/encoding.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ file_encoding(struct magic_set *ms, const struct buffer *b,
175175

176176
done:
177177
if (ubuf == &udefbuf)
178-
free(udefbuf);
178+
efree(udefbuf);
179179

180180
return rv;
181181
}

0 commit comments

Comments
 (0)