Skip to content

Commit 52ff389

Browse files
committed
move right to 5.29, yet some bugs present
1 parent ce62b72 commit 52ff389

File tree

16 files changed

+93168
-58927
lines changed

16 files changed

+93168
-58927
lines changed

ext/fileinfo/data_file.c

Lines changed: 91119 additions & 58029 deletions
Large diffs are not rendered by default.

ext/fileinfo/libmagic/apprentice.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "file.h"
3535

3636
#ifndef lint
37-
FILE_RCSID("@(#)$File: apprentice.c,v 1.249 2016/05/17 21:43:07 christos Exp $")
37+
FILE_RCSID("@(#)$File: apprentice.c,v 1.255 2016/10/24 18:02:17 christos Exp $")
3838
#endif /* lint */
3939

4040
#include "magic.h"
@@ -91,9 +91,9 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.249 2016/05/17 21:43:07 christos Exp $")
9191
#define ALLOC_CHUNK (size_t)10
9292
#define ALLOC_INCR (size_t)200
9393

94-
#define MAP_TYPE_MMAP 0
94+
#define MAP_TYPE_USER 0
9595
#define MAP_TYPE_MALLOC 1
96-
#define MAP_TYPE_USER 2
96+
#define MAP_TYPE_MMAP 2
9797

9898
struct magic_entry {
9999
struct magic *mp;
@@ -386,11 +386,11 @@ add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx)
386386
{
387387
struct mlist *ml;
388388

389-
mlp->map = idx == 0 ? map : NULL;
389+
mlp->map = NULL;
390390
if ((ml = CAST(struct mlist *, emalloc(sizeof(*ml)))) == NULL)
391391
return -1;
392392

393-
ml->map = NULL;
393+
ml->map = idx == 0 ? map : NULL;
394394
ml->magic = map->magic[idx];
395395
ml->nmagic = map->nmagic[idx];
396396

@@ -426,6 +426,8 @@ apprentice_1(struct magic_set *ms, const char *fn, int action)
426426
}
427427

428428
map = apprentice_map(ms, fn);
429+
if (map == (struct magic_map *)-1)
430+
return -1;
429431
if (map == NULL) {
430432
if (fn) {
431433
if (ms->flags & MAGIC_CHECK)
@@ -439,7 +441,6 @@ apprentice_1(struct magic_set *ms, const char *fn, int action)
439441
for (i = 0; i < MAGIC_SETS; i++) {
440442
if (add_mlist(ms->mlist[i], map, i) == -1) {
441443
file_oomem(ms, sizeof(*ml));
442-
apprentice_unmap(map);
443444
return -1;
444445
}
445446
}
@@ -508,6 +509,8 @@ file_ms_alloc(int flags)
508509
ms->elf_shnum_max = FILE_ELF_SHNUM_MAX;
509510
ms->elf_phnum_max = FILE_ELF_PHNUM_MAX;
510511
ms->elf_notes_max = FILE_ELF_NOTES_MAX;
512+
ms->regex_max = FILE_REGEX_MAX;
513+
ms->bytes_max = FILE_BYTES_MAX;
511514
return ms;
512515
free:
513516
efree(ms);
@@ -556,7 +559,7 @@ mlist_free(struct mlist *mlist)
556559
ml = mlist->next;
557560
for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
558561
if (ml->map)
559-
apprentice_unmap(ml->map);
562+
apprentice_unmap(CAST(struct magic_map *, ml->map));
560563
efree(ml);
561564
if (ml == mlist)
562565
break;
@@ -1817,10 +1820,13 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line,
18171820
if (m->flag & INDIR) {
18181821
m->in_type = FILE_LONG;
18191822
m->in_offset = 0;
1823+
m->in_op = 0;
18201824
/*
1821-
* read [.lbs][+-]nnnnn)
1825+
* read [.,lbs][+-]nnnnn)
18221826
*/
1823-
if (*l == '.') {
1827+
if (*l == '.' || *l == ',') {
1828+
if (*l == ',')
1829+
m->in_op |= FILE_OPSIGNED;
18241830
l++;
18251831
switch (*l) {
18261832
case 'l':
@@ -1872,7 +1878,6 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line,
18721878
l++;
18731879
}
18741880

1875-
m->in_op = 0;
18761881
if (*l == '~') {
18771882
m->in_op |= FILE_OPINVERSE;
18781883
l++;
@@ -1951,7 +1956,7 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line,
19511956
m->type = get_standard_integer_type(l, &l);
19521957
else if (*l == 's' && !isalpha((unsigned char)l[1])) {
19531958
m->type = FILE_STRING;
1954-
++l;
1959+
++l;
19551960
}
19561961
}
19571962
}
@@ -3078,10 +3083,9 @@ apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn)
30783083
} hdr;
30793084
php_stream *stream;
30803085

3086+
dbname = mkdbname(ms, fn, 1);
30813087

3082-
dbname = mkdbname(ms, fn, 0);
3083-
3084-
if (dbname == NULL)
3088+
if (dbname == NULL)
30853089
goto out;
30863090

30873091
/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */
@@ -3162,7 +3166,7 @@ mkdbname(struct magic_set *ms, const char *fn, int strip)
31623166
spprintf(&buf, MAXPATHLEN, "%.*s%s", (int)(q - fn), fn, ext);
31633167

31643168
/* Compatibility with old code that looked in .mime */
3165-
if (strstr(p, ".mime") != NULL)
3169+
if (strstr(fn, ".mime") != NULL)
31663170
ms->flags &= MAGIC_MIME_TYPE;
31673171
return buf;
31683172
}

ext/fileinfo/libmagic/ascmagic.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "file.h"
3636

3737
#ifndef lint
38-
FILE_RCSID("@(#)$File: ascmagic.c,v 1.95 2016/05/03 16:10:37 christos Exp $")
38+
FILE_RCSID("@(#)$File: ascmagic.c,v 1.97 2016/06/27 20:56:25 christos Exp $")
3939
#endif /* lint */
4040

4141
#include "magic.h"
@@ -144,8 +144,10 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
144144
(size_t)(utf8_end - utf8_buf), NULL, NULL,
145145
TEXTTEST, text)) == 0)
146146
rv = -1;
147-
if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
148-
return rv == -1 ? 0 : 1;
147+
if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) {
148+
rv = rv == -1 ? 0 : 1;
149+
goto done;
150+
}
149151
}
150152
if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)))
151153
return 0;

ext/fileinfo/libmagic/cdf.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "file.h"
3636

3737
#ifndef lint
38-
FILE_RCSID("@(#)$File: cdf.c,v 1.82 2016/06/01 22:25:25 christos Exp $")
38+
FILE_RCSID("@(#)$File: cdf.c,v 1.85 2016/10/24 18:02:17 christos Exp $")
3939
#endif
4040

4141
#include <assert.h>
@@ -276,7 +276,7 @@ cdf_unpack_dir(cdf_directory_t *d, char *buf)
276276
CDF_UNPACK(d->d_unused0);
277277
}
278278

279-
static int
279+
int
280280
cdf_zero_stream(cdf_stream_t *scn)
281281
{
282282
scn->sst_len = 0;
@@ -745,22 +745,25 @@ cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
745745
break;
746746

747747
/* If the it is not there, just fake it; some docs don't have it */
748-
if (i == dir->dir_len)
748+
if (i == dir->dir_len) {
749+
DPRINTF(("Cannot find root storage dir\n"));
749750
goto out;
751+
}
750752
d = &dir->dir_tab[i];
751753
*root = d;
752754

753755
/* If the it is not there, just fake it; some docs don't have it */
754-
if (d->d_stream_first_sector < 0)
756+
if (d->d_stream_first_sector < 0) {
757+
DPRINTF(("No first secror in dir\n"));
755758
goto out;
759+
}
756760

757761
return cdf_read_long_sector_chain(info, h, sat,
758762
d->d_stream_first_sector, d->d_size, scn);
759763
out:
760764
scn->sst_tab = NULL;
761765
(void)cdf_zero_stream(scn);
762-
errno = EFTYPE;
763-
return -1;
766+
return 0;
764767
}
765768

766769
static int
@@ -772,6 +775,15 @@ cdf_namecmp(const char *d, const uint16_t *s, size_t l)
772775
return 0;
773776
}
774777

778+
int
779+
cdf_read_doc_summary_info(const cdf_info_t *info, const cdf_header_t *h,
780+
const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
781+
const cdf_dir_t *dir, cdf_stream_t *scn)
782+
{
783+
return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
784+
"\05DocumentSummaryInformation", scn);
785+
}
786+
775787
int
776788
cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
777789
const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
@@ -1112,7 +1124,7 @@ cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst,
11121124
cep->ce_namlen = rlen;
11131125

11141126
np = CAST(const uint16_t *, CAST(const void *, (b + 16)));
1115-
if (CAST(const char *, np + cep->ce_namlen) > eb) {
1127+
if (RCAST(const char *, np + cep->ce_namlen) > eb) {
11161128
cep->ce_namlen = 0;
11171129
break;
11181130
}
@@ -1289,7 +1301,7 @@ cdf_dump(const void *v, size_t len)
12891301
}
12901302

12911303
void
1292-
cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst)
1304+
cdf_dump_stream(const cdf_stream_t *sst)
12931305
{
12941306
size_t ss = sst->sst_ss;
12951307
cdf_dump(sst->sst_tab, ss * sst->sst_len);
@@ -1345,7 +1357,7 @@ cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
13451357
name, d->d_stream_first_sector, d->d_size);
13461358
break;
13471359
}
1348-
cdf_dump_stream(h, &scn);
1360+
cdf_dump_stream(&scn);
13491361
free(scn.sst_tab);
13501362
break;
13511363
default:
@@ -1533,8 +1545,8 @@ main(int argc, char *argv[])
15331545
else
15341546
cdf_dump_summary_info(&h, &scn);
15351547
#endif
1536-
if (cdf_read_catalog(&info, &h, &sat, &ssat, &sst, &dir,
1537-
&scn) == -1)
1548+
if (cdf_read_user_stream(&info, &h, &sat, &ssat, &sst,
1549+
&dir, "Catalog", &scn) == -1)
15381550
warn("Cannot read catalog");
15391551
#ifdef CDF_DEBUG
15401552
else

ext/fileinfo/libmagic/cdf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ int cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *,
318318
const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
319319
const cdf_dir_t *, const char *, cdf_stream_t *);
320320
int cdf_find_stream(const cdf_dir_t *, const char *, int);
321+
int cdf_zero_stream(cdf_stream_t *);
322+
int cdf_read_doc_summary_info(const cdf_info_t *, const cdf_header_t *,
323+
const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
324+
const cdf_dir_t *, cdf_stream_t *);
321325
int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
322326
const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
323327
const cdf_dir_t *, cdf_stream_t *);
@@ -338,7 +342,7 @@ char *cdf_u16tos8(char *, size_t, const uint16_t *);
338342
void cdf_dump_header(const cdf_header_t *);
339343
void cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
340344
void cdf_dump(const void *, size_t);
341-
void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *);
345+
void cdf_dump_stream(const cdf_stream_t *);
342346
void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
343347
const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
344348
void cdf_dump_property_info(const cdf_property_info_t *, size_t);

ext/fileinfo/libmagic/compress.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
* uncompress(method, old, n, newch) - uncompress old into new,
3333
* using method, return sizeof new
3434
*/
35-
#include "config.h"
3635
#include "file.h"
3736

3837
#ifndef lint
39-
FILE_RCSID("@(#)$File: compress.c,v 1.97 2016/05/13 23:02:28 christos Exp $")
38+
FILE_RCSID("@(#)$File: compress.c,v 1.100 2016/10/24 18:02:17 christos Exp $")
4039
#endif
4140

4241
#include "magic.h"
@@ -93,6 +92,9 @@ static const char *lrzip_args[] = {
9392
static const char *lz4_args[] = {
9493
"lz4", "-cd", NULL
9594
};
95+
static const char *zstd_args[] = {
96+
"zstd", "-cd", NULL
97+
};
9698

9799
private const struct {
98100
const void *magic;
@@ -115,8 +117,9 @@ private const struct {
115117
{ "\3757zXZ\0", 6, xz_args }, /* XZ Utils */
116118
{ "LRZI", 4, lrzip_args }, /* LRZIP */
117119
{ "\004\"M\030",4, lz4_args }, /* LZ4 */
120+
{ "\x28\xB5\x2F\xFD", 4, zstd_args }, /* zstd */
118121
#ifdef ZLIBSUPPORT
119-
{ zlibcmp, 0, zlib_args }, /* zlib */
122+
{ RCAST(const void *, zlibcmp), 0, zlib_args }, /* zlib */
120123
#endif
121124
};
122125

@@ -164,7 +167,7 @@ file_zmagic(struct magic_set *ms, int fd, const char *name,
164167
continue;
165168
#ifdef ZLIBSUPPORT
166169
if (compr[i].maglen == 0)
167-
zm = (CAST(int (*)(const unsigned char *),
170+
zm = (RCAST(int (*)(const unsigned char *),
168171
CCAST(void *, compr[i].magic)))(buf);
169172
else
170173
#endif
@@ -323,7 +326,7 @@ sread(int fd, void *buf, size_t n, int canbepipe)
323326
return rn - n;
324327
default:
325328
n -= rv;
326-
buf = ((char *)buf) + rv;
329+
buf = CAST(char *, CCAST(void *, buf)) + rv;
327330
break;
328331
}
329332
while (n > 0);
@@ -479,7 +482,7 @@ uncompresszlib(const unsigned char *old, unsigned char **newch,
479482

480483
return OKDATA;
481484
err:
482-
strlcpy((char *)*newch, z.msg, bytes_max);
485+
strlcpy((char *)*newch, z.msg ? z.msg : zError(rc), bytes_max);
483486
*n = strlen((char *)*newch);
484487
return ERRDATA;
485488
}

ext/fileinfo/libmagic/der.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,12 @@
3535
#include "file.h"
3636

3737
#ifndef lint
38-
FILE_RCSID("@(#)$File: der.c,v 1.7 2016/06/01 22:01:15 christos Exp $")
38+
FILE_RCSID("@(#)$File: der.c,v 1.10 2016/10/24 18:02:17 christos Exp $")
3939
#endif
4040
#endif
4141

4242
#include <sys/types.h>
43-
#include <sys/stat.h>
44-
#ifndef PHP_WIN32
45-
#include <sys/mman.h>
46-
#endif
43+
4744
#include <stdio.h>
4845
#include <fcntl.h>
4946
#include <stdlib.h>
@@ -54,6 +51,10 @@ FILE_RCSID("@(#)$File: der.c,v 1.7 2016/06/01 22:01:15 christos Exp $")
5451
#include "magic.h"
5552
#include "der.h"
5653
#else
54+
#ifndef PHP_WIN32
55+
#include <sys/mman.h>
56+
#endif
57+
#include <sys/stat.h>
5758
#include <err.h>
5859
#endif
5960

@@ -182,6 +183,8 @@ getlength(const uint8_t *c, size_t *p, size_t l)
182183

183184
for (i = 0; i < digits; i++)
184185
len = (len << 8) | c[(*p)++];
186+
if (*p + len >= l)
187+
return DER_BAD;
185188
return len;
186189
}
187190

@@ -199,7 +202,7 @@ der_tag(char *buf, size_t len, uint32_t tag)
199202
static int
200203
der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
201204
{
202-
const uint8_t *d = q;
205+
const uint8_t *d = CAST(const uint8_t *, q);
203206
uint32_t i;
204207
switch (tag) {
205208
case DER_TAG_PRINTABLE_STRING:
@@ -222,7 +225,7 @@ der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
222225
int32_t
223226
der_offs(struct magic_set *ms, struct magic *m, size_t nbytes)
224227
{
225-
const uint8_t *b = CAST(const void *, ms->search.s);
228+
const uint8_t *b = RCAST(const uint8_t *, ms->search.s);
226229
size_t offs = 0, len = ms->search.s_len ? ms->search.s_len : nbytes;
227230

228231
if (gettag(b, &offs, len) == DER_BAD)
@@ -253,7 +256,7 @@ der_offs(struct magic_set *ms, struct magic *m, size_t nbytes)
253256
int
254257
der_cmp(struct magic_set *ms, struct magic *m)
255258
{
256-
const uint8_t *b = CAST(const void *, ms->search.s);
259+
const uint8_t *b = RCAST(const uint8_t *, ms->search.s);
257260
const char *s = m->value.s;
258261
size_t offs = 0, len = ms->search.s_len;
259262
uint32_t tag, tlen;

0 commit comments

Comments
 (0)