Skip to content

Commit bf93f1d

Browse files
committed
fileinfo: Followup fixes
Signed-off-by: Anatol Belski <ab@php.net>
1 parent c3eeab0 commit bf93f1d

14 files changed

+104
-5417
lines changed

ext/fileinfo/data_file.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257716,7 +257716,7 @@ const unsigned char php_magic_database[6652944] = {
257716257716
0x6E, 0x74, 0x20, 0x62, 0x69, 0x74, 0x6D, 0x61, 0x70, 0x20, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69,
257717257717
0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257718257718
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257719-
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257719+
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D, 0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00,
257720257720
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257721257721
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257722257722
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -258021,8 +258021,8 @@ const unsigned char php_magic_database[6652944] = {
258021258021
0x70, 0x2C, 0x20, 0x4F, 0x53, 0x2F, 0x32, 0x20, 0x32, 0x2E, 0x78, 0x20, 0x66, 0x6F, 0x72, 0x6D,
258022258022
0x61, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258023258023
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258024-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D,
258025-
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258024+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D,
258025+
0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258026258026
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258027258027
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258028258028
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -258233,7 +258233,7 @@ const unsigned char php_magic_database[6652944] = {
258233258233
0x20, 0x50, 0x68, 0x6F, 0x74, 0x6F, 0x73, 0x68, 0x6F, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258234258234
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258235258235
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258236-
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258236+
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D, 0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00,
258237258237
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258238258238
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258239258239
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -258327,7 +258327,7 @@ const unsigned char php_magic_database[6652944] = {
258327258327
0x20, 0x50, 0x68, 0x6F, 0x74, 0x6F, 0x73, 0x68, 0x6F, 0x70, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20,
258328258328
0x61, 0x6C, 0x70, 0x68, 0x61, 0x20, 0x63, 0x68, 0x61, 0x6E, 0x6E, 0x65, 0x6C, 0x20, 0x6D, 0x61,
258329258329
0x73, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258330-
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258330+
0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D, 0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00,
258331258331
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258332258332
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258333258333
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -258444,8 +258444,8 @@ const unsigned char php_magic_database[6652944] = {
258444258444
0x70, 0x2C, 0x20, 0x57, 0x69, 0x6E, 0x64, 0x6F, 0x77, 0x73, 0x20, 0x33, 0x2E, 0x78, 0x20, 0x66,
258445258445
0x6F, 0x72, 0x6D, 0x61, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258446258446
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258447-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x62, 0x6D,
258448-
0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258447+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x78, 0x2D,
258448+
0x6D, 0x73, 0x2D, 0x62, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258449258449
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258450258450
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258451258451
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

ext/fileinfo/libmagic/apprentice.c

Lines changed: 9 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.297 2020/05/09 18:57:15 christos Exp $")
5353
#include "win32/unistd.h"
5454
#define strtoull _strtoui64
5555
#else
56+
#ifdef HAVE_UNISTD_H
5657
#include <unistd.h>
5758
#endif
59+
#endif
5860
#include <string.h>
5961
#include <assert.h>
6062
#include <ctype.h>
@@ -149,10 +151,7 @@ private uint16_t swap2(uint16_t);
149151
private uint32_t swap4(uint32_t);
150152
private uint64_t swap8(uint64_t);
151153
private char *mkdbname(struct magic_set *, const char *, int);
152-
private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
153-
size_t);
154154
private struct magic_map *apprentice_map(struct magic_set *, const char *);
155-
private int check_buffer(struct magic_set *, struct magic_map *, const char *);
156155
private void apprentice_unmap(struct magic_map *);
157156
private int apprentice_compile(struct magic_set *, struct magic_map *,
158157
const char *);
@@ -428,8 +427,10 @@ private int
428427
apprentice_1(struct magic_set *ms, const char *fn, int action)
429428
{
430429
struct magic_map *map;
430+
#ifndef COMPILE_ONLY
431431
struct mlist *ml;
432432
size_t i;
433+
#endif
433434

434435
if (magicsize != FILE_MAGICSIZE) {
435436
file_error(ms, 0, "magic element size %lu != %lu",
@@ -609,51 +610,6 @@ mlist_free(struct mlist *mlist)
609610
mlist_free_one(mlist);
610611
}
611612

612-
#ifndef COMPILE_ONLY
613-
/* void **bufs: an array of compiled magic files */
614-
protected int
615-
buffer_apprentice(struct magic_set *ms, struct magic **bufs,
616-
size_t *sizes, size_t nbufs)
617-
{
618-
size_t i, j;
619-
struct mlist *ml;
620-
struct magic_map *map;
621-
622-
if (nbufs == 0)
623-
return -1;
624-
625-
(void)file_reset(ms, 0);
626-
627-
init_file_tables();
628-
629-
for (i = 0; i < MAGIC_SETS; i++) {
630-
mlist_free(ms->mlist[i]);
631-
if ((ms->mlist[i] = mlist_alloc()) == NULL) {
632-
file_oomem(ms, sizeof(*ms->mlist[i]));
633-
goto fail;
634-
}
635-
}
636-
637-
for (i = 0; i < nbufs; i++) {
638-
map = apprentice_buf(ms, bufs[i], sizes[i]);
639-
if (map == NULL)
640-
goto fail;
641-
642-
for (j = 0; j < MAGIC_SETS; j++) {
643-
if (add_mlist(ms->mlist[j], map, j) == -1) {
644-
file_oomem(ms, sizeof(*ml));
645-
goto fail;
646-
}
647-
}
648-
}
649-
650-
return 0;
651-
fail:
652-
mlist_free_all(ms);
653-
return -1;
654-
}
655-
#endif
656-
657613
/* const char *fn: list of magic files and directories */
658614
protected int
659615
file_apprentice(struct magic_set *ms, const char *fn, int action)
@@ -2251,6 +2207,11 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line,
22512207
if (check_format(ms, m) == -1)
22522208
return -1;
22532209
}
2210+
#ifndef COMPILE_ONLY
2211+
if (action == FILE_CHECK) {
2212+
file_mdump(m);
2213+
}
2214+
#endif
22542215
m->mimetype[0] = '\0'; /* initialise MIME type to none */
22552216
return 0;
22562217
}
@@ -3055,28 +3016,6 @@ eatsize(const char **p)
30553016
*p = l;
30563017
}
30573018

3058-
/*
3059-
* handle a buffer containing a compiled file.
3060-
*/
3061-
private struct magic_map *
3062-
apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
3063-
{
3064-
struct magic_map *map;
3065-
3066-
if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
3067-
file_oomem(ms, sizeof(*map));
3068-
return NULL;
3069-
}
3070-
map->len = len;
3071-
map->p = buf;
3072-
map->type = MAP_TYPE_USER;
3073-
if (check_buffer(ms, map, "buffer") != 0) {
3074-
apprentice_unmap(map);
3075-
return NULL;
3076-
}
3077-
return map;
3078-
}
3079-
30803019
/*
30813020
* handle a compiled file.
30823021
*/
@@ -3224,62 +3163,6 @@ apprentice_map(struct magic_set *ms, const char *fn)
32243163
return NULL;
32253164
}
32263165

3227-
private int
3228-
check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
3229-
{
3230-
uint32_t *ptr;
3231-
uint32_t entries, nentries;
3232-
uint32_t version;
3233-
int i, needsbyteswap;
3234-
3235-
ptr = CAST(uint32_t *, map->p);
3236-
if (*ptr != MAGICNO) {
3237-
if (swap4(*ptr) != MAGICNO) {
3238-
file_error(ms, 0, "bad magic in `%s'", dbname);
3239-
return -1;
3240-
}
3241-
needsbyteswap = 1;
3242-
} else
3243-
needsbyteswap = 0;
3244-
if (needsbyteswap)
3245-
version = swap4(ptr[1]);
3246-
else
3247-
version = ptr[1];
3248-
if (version != VERSIONNO) {
3249-
file_error(ms, 0, "File %s supports only version %d magic "
3250-
"files. `%s' is version %d", FILE_VERSION_MAJOR,
3251-
VERSIONNO, dbname, version);
3252-
return -1;
3253-
}
3254-
entries = CAST(uint32_t, map->len / sizeof(struct magic));
3255-
if ((entries * sizeof(struct magic)) != map->len) {
3256-
file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
3257-
"a multiple of %" SIZE_T_FORMAT "u",
3258-
dbname, map->len, sizeof(struct magic));
3259-
return -1;
3260-
}
3261-
map->magic[0] = CAST(struct magic *, map->p) + 1;
3262-
nentries = 0;
3263-
for (i = 0; i < MAGIC_SETS; i++) {
3264-
if (needsbyteswap)
3265-
map->nmagic[i] = swap4(ptr[i + 2]);
3266-
else
3267-
map->nmagic[i] = ptr[i + 2];
3268-
if (i != MAGIC_SETS - 1)
3269-
map->magic[i + 1] = map->magic[i] + map->nmagic[i];
3270-
nentries += map->nmagic[i];
3271-
}
3272-
if (entries != nentries + 1) {
3273-
file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
3274-
dbname, entries, nentries + 1);
3275-
return -1;
3276-
}
3277-
if (needsbyteswap)
3278-
for (i = 0; i < MAGIC_SETS; i++)
3279-
byteswap(map->magic[i], map->nmagic[i]);
3280-
return 0;
3281-
}
3282-
32833166
/*
32843167
* handle an mmaped file.
32853168
*/

ext/fileinfo/libmagic/der.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ der_tag(char *buf, size_t len, uint32_t tag)
237237
static int
238238
der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
239239
{
240-
uint32_t i = 0;
241240
const uint8_t *d = CAST(const uint8_t *, q);
242241
switch (tag) {
243242
case DER_TAG_PRINTABLE_STRING:
@@ -255,7 +254,7 @@ der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
255254
break;
256255
}
257256

258-
for (; i < len; i++) {
257+
for (uint32_t i = 0; i < len; i++) {
259258
uint32_t z = i << 1;
260259
if (z < blen - 2)
261260
snprintf(buf + z, blen - z, "%.2x", d[i]);

ext/fileinfo/libmagic/file.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,6 @@ protected int file_is_tar(struct magic_set *, const struct buffer *);
493493
protected int file_softmagic(struct magic_set *, const struct buffer *,
494494
uint16_t *, uint16_t *, int, int);
495495
protected int file_apprentice(struct magic_set *, const char *, int);
496-
protected int buffer_apprentice(struct magic_set *, struct magic **,
497-
size_t *, size_t);
498496
protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
499497
protected uint64_t file_signextend(struct magic_set *, struct magic *,
500498
uint64_t);

ext/fileinfo/libmagic/funcs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ file_badread(struct magic_set *ms)
227227
file_error(ms, errno, "error reading");
228228
}
229229

230+
#ifndef COMPILE_ONLY
231+
230232
protected int
231233
file_separator(struct magic_set *ms)
232234
{
@@ -465,6 +467,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, zend_stat_t *st,
465467

466468
return m;
467469
}
470+
#endif
468471

469472
protected int
470473
file_reset(struct magic_set *ms, int checkloaded)

ext/fileinfo/libmagic/magic.c

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ FILE_RCSID("@(#)$File: magic.c,v 1.112 2020/06/08 19:44:10 christos Exp $")
7676
#endif
7777

7878
private int unreadable_info(struct magic_set *, mode_t, const char *);
79-
#if 0
80-
private const char* get_default_magic(void);
81-
#endif
8279
private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
8380

8481
#ifndef STDIN_FILENO
@@ -154,41 +151,6 @@ magic_list(struct magic_set *ms, const char *magicfile)
154151
return file_apprentice(ms, magicfile, FILE_LIST);
155152
}
156153

157-
#if 0
158-
private void
159-
close_and_restore(const struct magic_set *ms, const char *name, int fd,
160-
const zend_stat_t *sb)
161-
{
162-
if (fd == STDIN_FILENO || name == NULL)
163-
return;
164-
(void) close(fd);
165-
166-
if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
167-
/*
168-
* Try to restore access, modification times if read it.
169-
* This is really *bad* because it will modify the status
170-
* time of the file... And of course this will affect
171-
* backup programs
172-
*/
173-
#ifdef HAVE_UTIMES
174-
struct timeval utsbuf[2];
175-
(void)memset(utsbuf, 0, sizeof(utsbuf));
176-
utsbuf[0].tv_sec = sb->st_atime;
177-
utsbuf[1].tv_sec = sb->st_mtime;
178-
179-
(void) utimes(name, utsbuf); /* don't care if loses */
180-
#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
181-
struct utimbuf utbuf;
182-
183-
(void)memset(&utbuf, 0, sizeof(utbuf));
184-
utbuf.actime = sb->st_atime;
185-
utbuf.modtime = sb->st_mtime;
186-
(void) utime(name, &utbuf); /* don't care if loses */
187-
#endif
188-
}
189-
}
190-
#endif
191-
192154
#ifndef COMPILE_ONLY
193155

194156
/*

ext/fileinfo/libmagic/print.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ FILE_RCSID("@(#)$File: print.c,v 1.88 2020/05/09 18:57:15 christos Exp $")
4444
#endif
4545
#include <time.h>
4646

47-
#define SZOF(a) (sizeof(a) / sizeof(a[0]))
48-
4947
#include "cdf.h"
5048

5149
#ifndef COMPILE_ONLY

ext/fileinfo/libmagic/softmagic.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,10 +1934,13 @@ file_strncmp(const char *s1, const char *s2, size_t len, size_t maxlen,
19341934
}
19351935
else if ((flags & STRING_COMPACT_WHITESPACE) &&
19361936
isspace(*a)) {
1937+
/* XXX Dirty. The data and the pattern is what is causing this.
1938+
Revert _i for the next port and see if it still matters. */
1939+
uint32_t _i = 0;
19371940
a++;
19381941
if (isspace(*b++)) {
19391942
if (!isspace(*a))
1940-
while (b < eb && isspace(*b))
1943+
while (EXPECTED(_i++ < 2048) && b < eb && isspace(*b))
19411944
b++;
19421945
}
19431946
else {

0 commit comments

Comments
 (0)